blob: 9c9b7da24d5d8400d14f580117c41881efaf9664 [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
7lib.rtdep('lib.colors', 'lib.PreferenceManager',
8 'hterm.msg',
9 'hterm.Keyboard', 'hterm.Options', 'hterm.Screen',
10 'hterm.ScrollPort', 'hterm.Size', 'hterm.VT');
11
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
29 * @param {string} opt_profileName Optional preference profile name. If not
30 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
rginda9f5222b2012-03-05 11:53:28 -080032hterm.Terminal = function(opt_profileName) {
33 this.profileName_ = null;
34 this.setProfile(opt_profileName || 'default');
35
rginda8ba33642011-12-14 12:31:31 -080036 // Two screen instances.
37 this.primaryScreen_ = new hterm.Screen();
38 this.alternateScreen_ = new hterm.Screen();
39
40 // The "current" screen.
41 this.screen_ = this.primaryScreen_;
42
rginda8ba33642011-12-14 12:31:31 -080043 // The local notion of the screen size. ScreenBuffers also have a size which
44 // indicates their present size. During size changes, the two may disagree.
45 // Also, the inactive screen's size is not altered until it is made the active
46 // screen.
47 this.screenSize = new hterm.Size(0, 0);
48
rginda8ba33642011-12-14 12:31:31 -080049 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080050 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080051 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
52 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080053 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rginda8ba33642011-12-14 12:31:31 -080054
rginda87b86462011-12-14 13:48:03 -080055 // The div that contains this terminal.
56 this.div_ = null;
57
rgindac9bc5502012-01-18 11:48:44 -080058 // The document that contains the scrollPort. Defaulted to the global
59 // document here so that the terminal is functional even if it hasn't been
60 // inserted into a document yet, but re-set in decorate().
61 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080062
rginda8ba33642011-12-14 12:31:31 -080063 // The rows that have scrolled off screen and are no longer addressable.
64 this.scrollbackRows_ = [];
65
rgindac9bc5502012-01-18 11:48:44 -080066 // Saved tab stops.
67 this.tabStops_ = [];
68
David Benjamin66e954d2012-05-05 21:08:12 -040069 // Keep track of whether default tab stops have been erased; after a TBC
70 // clears all tab stops, defaults aren't restored on resize until a reset.
71 this.defaultTabStops = true;
72
rginda8ba33642011-12-14 12:31:31 -080073 // The VT's notion of the top and bottom rows. Used during some VT
74 // cursor positioning and scrolling commands.
75 this.vtScrollTop_ = null;
76 this.vtScrollBottom_ = null;
77
78 // The DIV element for the visible cursor.
79 this.cursorNode_ = null;
80
rginda9f5222b2012-03-05 11:53:28 -080081 // These prefs are cached so we don't have to read from local storage with
82 // each output and keystroke.
83 this.scrollOnOutput_ = this.prefs_.get('scroll-on-output');
84 this.scrollOnKeystroke_ = this.prefs_.get('scroll-on-keystroke');
rginda8e92a692012-05-20 19:37:20 -070085 this.foregroundColor_ = this.prefs_.get('foreground-color');
86 this.backgroundColor_ = this.prefs_.get('background-color');
rginda9f5222b2012-03-05 11:53:28 -080087
rgindaf0090c92012-02-10 14:58:52 -080088 // Terminal bell sound.
89 this.bellAudio_ = this.document_.createElement('audio');
rginda9f5222b2012-03-05 11:53:28 -080090 this.bellAudio_.setAttribute('src', this.prefs_.get('audible-bell-sound'));
rgindaf0090c92012-02-10 14:58:52 -080091 this.bellAudio_.setAttribute('preload', 'auto');
92
rginda6d397402012-01-17 10:58:29 -080093 // Cursor position and attributes saved with DECSC.
94 this.savedOptions_ = {};
95
rginda8ba33642011-12-14 12:31:31 -080096 // The current mode bits for the terminal.
97 this.options_ = new hterm.Options();
98
99 // Timeouts we might need to clear.
100 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800101
102 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800103 this.vt = new hterm.VT(this);
rginda11057d52012-04-25 12:29:56 -0700104 this.vt.enable8BitControl = this.prefs_.get('enable-8-bit-control');
105 this.vt.maxStringSequence = this.prefs_.get('max-string-sequence');
rginda87b86462011-12-14 13:48:03 -0800106
rgindafeaf3142012-01-31 15:14:20 -0800107 // The keyboard hander.
108 this.keyboard = new hterm.Keyboard(this);
109
rginda87b86462011-12-14 13:48:03 -0800110 // General IO interface that can be given to third parties without exposing
111 // the entire terminal object.
112 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800113
rgindad5613292012-06-19 15:40:37 -0700114 // True if mouse-click-drag should scroll the terminal.
115 this.enableMouseDragScroll = true;
116
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400117 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800118 this.setDefaultTabStops();
rginda87b86462011-12-14 13:48:03 -0800119};
120
121/**
rginda35c456b2012-02-09 17:29:05 -0800122 * Default tab with of 8 to match xterm.
123 */
124hterm.Terminal.prototype.tabWidth = 8;
125
126/**
rginda35c456b2012-02-09 17:29:05 -0800127 * The assumed width of a scrollbar.
128 */
129hterm.Terminal.prototype.scrollbarWidthPx = 16;
130
131/**
rginda9f5222b2012-03-05 11:53:28 -0800132 * Select a preference profile.
133 *
134 * This will load the terminal preferences for the given profile name and
135 * associate subsequent preference changes with the new preference profile.
136 *
137 * @param {string} newName The name of the preference profile. Forward slash
138 * characters will be removed from the name.
139 */
140hterm.Terminal.prototype.setProfile = function(profileName) {
141 // If we already have a profile selected, we're going to need to re-sync
142 // with the new profile.
143 var needSync = !!this.profileName_;
144
145 this.profileName_ = profileName.replace(/\//g, '');
146
rgindacbbd7482012-06-13 15:06:16 -0700147 this.prefs_ = new lib.PreferenceManager(
rginda9f5222b2012-03-05 11:53:28 -0800148 '/hterm/prefs/profiles/' + this.profileName_);
149
150 var self = this;
151 this.prefs_.definePreferences
rginda30f20f62012-04-05 16:36:19 -0700152 ([
153 /**
154 * Set whether the alt key acts as a meta key or as a distinct alt key.
rginda9f5222b2012-03-05 11:53:28 -0800155 */
rginda30f20f62012-04-05 16:36:19 -0700156 ['alt-is-meta', false, function(v) {
rgindaf9c36852012-05-09 11:08:39 -0700157 self.keyboard.altIsMeta = v;
rginda9f5222b2012-03-05 11:53:28 -0800158 }
159 ],
160
rginda30f20f62012-04-05 16:36:19 -0700161 /**
rginda39bdf6f2012-04-10 16:50:55 -0700162 * Controls how the alt key is handled.
163 *
164 * escape....... Send an ESC prefix.
165 * 8-bit........ Add 128 to the unshifted character as in xterm.
166 * browser-key.. Wait for the keypress event and see what the browser says.
167 * (This won't work well on platforms where the browser
168 * performs a default action for some alt sequences.)
rginda30f20f62012-04-05 16:36:19 -0700169 */
rginda39bdf6f2012-04-10 16:50:55 -0700170 ['alt-sends-what', 'escape', function(v) {
171 if (!/^(escape|8-bit|browser-key)$/.test(v))
172 v = 'escape';
173
rgindaf9c36852012-05-09 11:08:39 -0700174 self.keyboard.altSendsWhat = v;
rginda30f20f62012-04-05 16:36:19 -0700175 }
176 ],
177
178 /**
179 * Terminal bell sound. Empty string for no audible bell.
180 */
181 ['audible-bell-sound', '../audio/bell.ogg', function(v) {
182 self.bellAudio_.setAttribute('src', v);
183 }
184 ],
185
186 /**
187 * The background color for text with no other color attributes.
188 */
189 ['background-color', 'rgb(16, 16, 16)', function(v) {
rginda8e92a692012-05-20 19:37:20 -0700190 self.setBackgroundColor(v);
rginda9f5222b2012-03-05 11:53:28 -0800191 }
192 ],
193
194 /**
rginda30f20f62012-04-05 16:36:19 -0700195 * The background image.
rginda30f20f62012-04-05 16:36:19 -0700196 */
rginda8e92a692012-05-20 19:37:20 -0700197 ['background-image', '',
rginda30f20f62012-04-05 16:36:19 -0700198 function(v) {
199 self.scrollPort_.setBackgroundImage(v);
200 }
201 ],
202
203 /**
Philip Douglass959b49d2012-05-30 13:29:29 -0400204 * The background image size,
205 *
206 * Defaults to none.
207 */
208 ['background-size', '', function(v) {
209 self.scrollPort_.setBackgroundSize(v);
210 }
211 ],
212
213 /**
214 * The background image position,
215 *
216 * Defaults to none.
217 */
218 ['background-position', '', function(v) {
219 self.scrollPort_.setBackgroundPosition(v);
220 }
221 ],
222
223 /**
rginda30f20f62012-04-05 16:36:19 -0700224 * If true, the backspace should send BS ('\x08', aka ^H). Otherwise
225 * the backspace key should send '\x7f'.
226 */
227 ['backspace-sends-backspace', false, function(v) {
228 self.keyboard.backspaceSendsBackspace = v;
229 }
230 ],
231
232 /**
rgindade84e382012-04-20 15:39:31 -0700233 * Whether or not to blink the cursor by default.
234 */
235 ['cursor-blink', false, function(v) {
236 self.setCursorBlink(!!v);
237 }
238 ],
239
240 /**
rginda30f20f62012-04-05 16:36:19 -0700241 * The color of the visible cursor.
242 */
243 ['cursor-color', 'rgba(255,0,0,0.5)', function(v) {
rginda8e92a692012-05-20 19:37:20 -0700244 self.setCursorColor(v);
rginda30f20f62012-04-05 16:36:19 -0700245 }
246 ],
247
248 /**
rginda11057d52012-04-25 12:29:56 -0700249 * True to enable 8-bit control characters, false to ignore them.
250 *
251 * We'll respect the two-byte versions of these control characters
252 * regardless of this setting.
253 */
254 ['enable-8-bit-control', false, function(v) {
255 self.vt.enable8BitControl = !!v;
256 }
257 ],
258
259 /**
rginda30f20f62012-04-05 16:36:19 -0700260 * True if we should use bold weight font for text with the bold/bright
261 * attribute. False to use bright colors only. Null to autodetect.
262 */
263 ['enable-bold', null, function(v) {
264 self.syncBoldSafeState();
265 }
266 ],
267
268 /**
rginda9f5222b2012-03-05 11:53:28 -0800269 * Default font family for the terminal text.
270 */
271 ['font-family', ('"DejaVu Sans Mono", "Everson Mono", ' +
272 'FreeMono, "Menlo", "Lucida Console", ' +
273 'monospace'),
274 function(v) { self.syncFontFamily() }
275 ],
276
277 /**
rginda30f20f62012-04-05 16:36:19 -0700278 * The default font size in pixels.
279 */
280 ['font-size', 15, function(v) {
281 self.setFontSize(v);
282 }
283 ],
284
285 /**
rginda9f5222b2012-03-05 11:53:28 -0800286 * Anti-aliasing.
287 */
288 ['font-smoothing', 'antialiased',
289 function(v) { self.syncFontFamily() }
290 ],
291
292 /**
rginda30f20f62012-04-05 16:36:19 -0700293 * The foreground color for text with no other color attributes.
rginda9f5222b2012-03-05 11:53:28 -0800294 */
rginda30f20f62012-04-05 16:36:19 -0700295 ['foreground-color', 'rgb(240, 240, 240)', function(v) {
rginda8e92a692012-05-20 19:37:20 -0700296 self.setForegroundColor(v);
rginda9f5222b2012-03-05 11:53:28 -0800297 }
298 ],
299
300 /**
rginda30f20f62012-04-05 16:36:19 -0700301 * If true, home/end will control the terminal scrollbar and shift home/end
302 * will send the VT keycodes. If false then home/end sends VT codes and
303 * shift home/end scrolls.
rginda9f5222b2012-03-05 11:53:28 -0800304 */
rginda30f20f62012-04-05 16:36:19 -0700305 ['home-keys-scroll', false, function(v) {
306 self.keyboard.homeKeysScroll = v;
307 }
308 ],
309
310 /**
rginda11057d52012-04-25 12:29:56 -0700311 * Max length of a DCS, OSC, PM, or APS sequence before we give up and
312 * ignore the code.
313 */
314 ['max-string-sequence', 1024, function(v) {
315 self.vt.maxStringSequence = v;
316 }
317 ],
318
319 /**
rginda30f20f62012-04-05 16:36:19 -0700320 * Set whether the meta key sends a leading escape or not.
321 */
322 ['meta-sends-escape', true, function(v) {
323 self.keyboard.metaSendsEscape = v;
rginda9f5222b2012-03-05 11:53:28 -0800324 }
325 ],
326
327 /**
rgindad5613292012-06-19 15:40:37 -0700328 * Set whether we should treat DEC mode 1002 (mouse cell motion tracking)
329 * as if it were 1000 (mouse click tracking).
330 *
331 * This makes it possible to use vi's ":set mouse=a" mode without losing
332 * access to the system text selection mechanism.
333 */
334 ['mouse-cell-motion-trick', false, function(v) {
335 self.vt.setMouseCellMotionTrick(v);
336 }
337 ],
338
339 /**
rginda9f5222b2012-03-05 11:53:28 -0800340 * If true, scroll to the bottom on any keystroke.
341 */
342 ['scroll-on-keystroke', true, function(v) {
343 self.scrollOnKeystroke_ = v;
344 }
345 ],
346
347 /**
348 * If true, scroll to the bottom on terminal output.
349 */
350 ['scroll-on-output', false, function(v) {
351 self.scrollOnOutput_ = v;
352 }
353 ],
354
355 /**
David Reveman8f552492012-03-28 12:18:41 -0400356 * The vertical scrollbar mode.
357 */
358 ['scrollbar-visible', true, function(v) {
359 self.setScrollbarVisible(v);
360 }
361 ],
rginda30f20f62012-04-05 16:36:19 -0700362
363 /**
rgindaf522ce02012-04-17 17:49:17 -0700364 * The default environment variables.
365 */
366 ['environment', {TERM: 'xterm-256color'}, null],
367
368 /**
rginda30f20f62012-04-05 16:36:19 -0700369 * If true, page up/down will control the terminal scrollbar and shift
370 * page up/down will send the VT keycodes. If false then page up/down
371 * sends VT codes and shift page up/down scrolls.
372 */
373 ['page-keys-scroll', false, function(v) {
374 self.keyboard.pageKeysScroll = v;
375 }
376 ],
377
rginda9f5222b2012-03-05 11:53:28 -0800378 ]);
379
380 if (needSync)
381 this.prefs_.notifyAll();
382};
383
rginda8e92a692012-05-20 19:37:20 -0700384
385/**
386 * Set the color for the cursor.
387 *
388 * If you want this setting to persist, set it through prefs_, rather than
389 * with this method.
390 */
391hterm.Terminal.prototype.setCursorColor = function(color) {
392 this.cursorNode_.style.backgroundColor = color;
393 this.cursorNode_.style.borderColor = color;
394};
395
396/**
397 * Return the current cursor color as a string.
398 */
399hterm.Terminal.prototype.getCursorColor = function() {
400 return this.cursorNode_.style.backgroundColor;
401};
402
403/**
rgindad5613292012-06-19 15:40:37 -0700404 * Enable or disable mouse based text selection in the terminal.
405 */
406hterm.Terminal.prototype.setSelectionEnabled = function(state) {
407 this.enableMouseDragScroll = state;
408 this.scrollPort_.setSelectionEnabled(state);
409};
410
411/**
rginda8e92a692012-05-20 19:37:20 -0700412 * Set the background color.
413 *
414 * If you want this setting to persist, set it through prefs_, rather than
415 * with this method.
416 */
417hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700418 this.backgroundColor_ = lib.colors.normalizeCSS(color);
rginda8e92a692012-05-20 19:37:20 -0700419 this.scrollPort_.setBackgroundColor(color);
420};
421
rginda9f5222b2012-03-05 11:53:28 -0800422/**
423 * Return the current terminal background color.
424 *
425 * Intended for use by other classes, so we don't have to expose the entire
426 * prefs_ object.
427 */
428hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700429 return this.backgroundColor_;
430};
431
432/**
433 * Set the foreground color.
434 *
435 * If you want this setting to persist, set it through prefs_, rather than
436 * with this method.
437 */
438hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700439 this.foregroundColor_ = lib.colors.normalizeCSS(color);
rginda8e92a692012-05-20 19:37:20 -0700440 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800441};
442
443/**
444 * Return the current terminal foreground color.
445 *
446 * Intended for use by other classes, so we don't have to expose the entire
447 * prefs_ object.
448 */
449hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700450 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800451};
452
453/**
rginda87b86462011-12-14 13:48:03 -0800454 * Create a new instance of a terminal command and run it with a given
455 * argument string.
456 *
457 * @param {function} commandClass The constructor for a terminal command.
458 * @param {string} argString The argument string to pass to the command.
459 */
460hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700461 var environment = this.prefs_.get('environment');
462 if (typeof environment != 'object' || environment == null)
463 environment = {};
464
rginda87b86462011-12-14 13:48:03 -0800465 var self = this;
466 this.command = new commandClass(
467 { argString: argString || '',
468 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700469 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800470 onExit: function(code) {
471 self.io.pop();
472 self.io.println(hterm.msg('COMMAND_COMPLETE',
473 [self.command.commandName, code]));
rgindafeaf3142012-01-31 15:14:20 -0800474 self.uninstallKeyboard();
rginda87b86462011-12-14 13:48:03 -0800475 }
476 });
477
rgindafeaf3142012-01-31 15:14:20 -0800478 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800479 this.command.run();
480};
481
482/**
rgindafeaf3142012-01-31 15:14:20 -0800483 * Returns true if the current screen is the primary screen, false otherwise.
484 */
485hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700486 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800487};
488
489/**
490 * Install the keyboard handler for this terminal.
491 *
492 * This will prevent the browser from seeing any keystrokes sent to the
493 * terminal.
494 */
495hterm.Terminal.prototype.installKeyboard = function() {
496 this.keyboard.installKeyboard(this.document_.body.firstChild);
497}
498
499/**
500 * Uninstall the keyboard handler for this terminal.
501 */
502hterm.Terminal.prototype.uninstallKeyboard = function() {
503 this.keyboard.installKeyboard(null);
504}
505
506/**
rginda35c456b2012-02-09 17:29:05 -0800507 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800508 *
509 * Call setFontSize(0) to reset to the default font size.
510 *
511 * This function does not modify the font-size preference.
512 *
513 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800514 */
515hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800516 if (px === 0)
517 px = this.prefs_.get('font-size');
518
rginda35c456b2012-02-09 17:29:05 -0800519 this.scrollPort_.setFontSize(px);
520};
521
522/**
523 * Get the current font size.
524 */
525hterm.Terminal.prototype.getFontSize = function() {
526 return this.scrollPort_.getFontSize();
527};
528
529/**
rginda8e92a692012-05-20 19:37:20 -0700530 * Get the current font family.
531 */
532hterm.Terminal.prototype.getFontFamily = function() {
533 return this.scrollPort_.getFontFamily();
534};
535
536/**
rginda35c456b2012-02-09 17:29:05 -0800537 * Set the CSS "font-family" for this terminal.
538 */
rginda9f5222b2012-03-05 11:53:28 -0800539hterm.Terminal.prototype.syncFontFamily = function() {
540 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
541 this.prefs_.get('font-smoothing'));
542 this.syncBoldSafeState();
543};
544
545hterm.Terminal.prototype.syncBoldSafeState = function() {
546 var enableBold = this.prefs_.get('enable-bold');
547 if (enableBold !== null) {
548 this.screen_.textAttributes.enableBold = enableBold;
549 return;
550 }
551
rgindaf7521392012-02-28 17:20:34 -0800552 var normalSize = this.scrollPort_.measureCharacterSize();
553 var boldSize = this.scrollPort_.measureCharacterSize('bold');
554
555 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800556 if (!isBoldSafe) {
557 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700558 'from normal. Font family is: ' +
559 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800560 }
rginda9f5222b2012-03-05 11:53:28 -0800561
562 this.screen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800563};
564
565/**
rginda87b86462011-12-14 13:48:03 -0800566 * Return a copy of the current cursor position.
567 *
568 * @return {hterm.RowCol} The RowCol object representing the current position.
569 */
570hterm.Terminal.prototype.saveCursor = function() {
571 return this.screen_.cursorPosition.clone();
572};
573
rgindaa19afe22012-01-25 15:40:22 -0800574hterm.Terminal.prototype.getTextAttributes = function() {
575 return this.screen_.textAttributes;
576};
577
rginda1a09aa02012-06-18 21:11:25 -0700578hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
579 this.screen_.textAttributes = textAttributes;
580};
581
rginda87b86462011-12-14 13:48:03 -0800582/**
rgindaf522ce02012-04-17 17:49:17 -0700583 * Return the current browser zoom factor applied to the terminal.
584 *
585 * @return {number} The current browser zoom factor.
586 */
587hterm.Terminal.prototype.getZoomFactor = function() {
588 return this.scrollPort_.characterSize.zoomFactor;
589};
590
591/**
rginda9846e2f2012-01-27 13:53:33 -0800592 * Change the title of this terminal's window.
593 */
594hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800595 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800596};
597
598/**
rginda87b86462011-12-14 13:48:03 -0800599 * Restore a previously saved cursor position.
600 *
601 * @param {hterm.RowCol} cursor The position to restore.
602 */
603hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700604 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
605 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800606 this.screen_.setCursorPosition(row, column);
607 if (cursor.column > column ||
608 cursor.column == column && cursor.overflow) {
609 this.screen_.cursorPosition.overflow = true;
610 }
rginda87b86462011-12-14 13:48:03 -0800611};
612
613/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400614 * Clear the cursor's overflow flag.
615 */
616hterm.Terminal.prototype.clearCursorOverflow = function() {
617 this.screen_.cursorPosition.overflow = false;
618};
619
620/**
rginda87b86462011-12-14 13:48:03 -0800621 * Set the width of the terminal, resizing the UI to match.
622 */
623hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800624 if (columnCount == null) {
625 this.div_.style.width = '100%';
626 return;
627 }
628
rginda35c456b2012-02-09 17:29:05 -0800629 this.div_.style.width = this.scrollPort_.characterSize.width *
630 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400631 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800632 this.scheduleSyncCursorPosition_();
633};
rginda87b86462011-12-14 13:48:03 -0800634
rgindac9bc5502012-01-18 11:48:44 -0800635/**
rginda35c456b2012-02-09 17:29:05 -0800636 * Set the height of the terminal, resizing the UI to match.
637 */
638hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800639 if (rowCount == null) {
640 this.div_.style.height = '100%';
641 return;
642 }
643
rginda35c456b2012-02-09 17:29:05 -0800644 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700645 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800646 this.realizeSize_(this.screenSize.width, rowCount);
647 this.scheduleSyncCursorPosition_();
648};
649
650/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400651 * Deal with terminal size changes.
652 *
653 */
654hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
655 if (columnCount != this.screenSize.width)
656 this.realizeWidth_(columnCount);
657
658 if (rowCount != this.screenSize.height)
659 this.realizeHeight_(rowCount);
660
661 // Send new terminal size to plugin.
662 this.io.onTerminalResize(columnCount, rowCount);
663};
664
665/**
rgindac9bc5502012-01-18 11:48:44 -0800666 * Deal with terminal width changes.
667 *
668 * This function does what needs to be done when the terminal width changes
669 * out from under us. It happens here rather than in onResize_() because this
670 * code may need to run synchronously to handle programmatic changes of
671 * terminal width.
672 *
673 * Relying on the browser to send us an async resize event means we may not be
674 * in the correct state yet when the next escape sequence hits.
675 */
676hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
677 var deltaColumns = columnCount - this.screen_.getWidth();
678
rginda87b86462011-12-14 13:48:03 -0800679 this.screenSize.width = columnCount;
680 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800681
682 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400683 if (this.defaultTabStops)
684 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800685 } else {
686 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400687 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800688 break;
689
690 this.tabStops_.pop();
691 }
692 }
693
694 this.screen_.setColumnCount(this.screenSize.width);
695};
696
697/**
698 * Deal with terminal height changes.
699 *
700 * This function does what needs to be done when the terminal height changes
701 * out from under us. It happens here rather than in onResize_() because this
702 * code may need to run synchronously to handle programmatic changes of
703 * terminal height.
704 *
705 * Relying on the browser to send us an async resize event means we may not be
706 * in the correct state yet when the next escape sequence hits.
707 */
708hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
709 var deltaRows = rowCount - this.screen_.getHeight();
710
711 this.screenSize.height = rowCount;
712
713 var cursor = this.saveCursor();
714
715 if (deltaRows < 0) {
716 // Screen got smaller.
717 deltaRows *= -1;
718 while (deltaRows) {
719 var lastRow = this.getRowCount() - 1;
720 if (lastRow - this.scrollbackRows_.length == cursor.row)
721 break;
722
723 if (this.getRowText(lastRow))
724 break;
725
726 this.screen_.popRow();
727 deltaRows--;
728 }
729
730 var ary = this.screen_.shiftRows(deltaRows);
731 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
732
733 // We just removed rows from the top of the screen, we need to update
734 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800735 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800736 } else if (deltaRows > 0) {
737 // Screen got larger.
738
739 if (deltaRows <= this.scrollbackRows_.length) {
740 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
741 var rows = this.scrollbackRows_.splice(
742 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
743 this.screen_.unshiftRows(rows);
744 deltaRows -= scrollbackCount;
745 cursor.row += scrollbackCount;
746 }
747
748 if (deltaRows)
749 this.appendRows_(deltaRows);
750 }
751
rginda35c456b2012-02-09 17:29:05 -0800752 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800753 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800754};
755
756/**
757 * Scroll the terminal to the top of the scrollback buffer.
758 */
759hterm.Terminal.prototype.scrollHome = function() {
760 this.scrollPort_.scrollRowToTop(0);
761};
762
763/**
764 * Scroll the terminal to the end.
765 */
766hterm.Terminal.prototype.scrollEnd = function() {
767 this.scrollPort_.scrollRowToBottom(this.getRowCount());
768};
769
770/**
771 * Scroll the terminal one page up (minus one line) relative to the current
772 * position.
773 */
774hterm.Terminal.prototype.scrollPageUp = function() {
775 var i = this.scrollPort_.getTopRowIndex();
776 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
777};
778
779/**
780 * Scroll the terminal one page down (minus one line) relative to the current
781 * position.
782 */
783hterm.Terminal.prototype.scrollPageDown = function() {
784 var i = this.scrollPort_.getTopRowIndex();
785 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800786};
787
rgindac9bc5502012-01-18 11:48:44 -0800788/**
789 * Full terminal reset.
790 */
rginda87b86462011-12-14 13:48:03 -0800791hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800792 this.clearAllTabStops();
793 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700794
795 this.clearHome(this.primaryScreen_);
796 this.primaryScreen_.textAttributes.reset();
797
798 this.clearHome(this.alternateScreen_);
799 this.alternateScreen_.textAttributes.reset();
800
rgindab8bc8932012-04-27 12:45:03 -0700801 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
802
rgindac9bc5502012-01-18 11:48:44 -0800803 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800804};
805
rgindac9bc5502012-01-18 11:48:44 -0800806/**
807 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700808 *
809 * Perform a soft reset to the default values listed in
810 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800811 */
rginda0f5c0292012-01-13 11:00:13 -0800812hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700813 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800814 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700815
rgindab8bc8932012-04-27 12:45:03 -0700816 // Xterm also resets the color palette on soft reset, even though it doesn't
817 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700818 this.primaryScreen_.textAttributes.resetColorPalette();
819 this.alternateScreen_.textAttributes.resetColorPalette();
820
rgindab8bc8932012-04-27 12:45:03 -0700821 // The xterm man page explicitly says this will happen on soft reset.
822 this.setVTScrollRegion(null, null);
823
824 // Xterm also shows the cursor on soft reset, but does not alter the blink
825 // state.
rgindaa19afe22012-01-25 15:40:22 -0800826 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800827};
828
rgindac9bc5502012-01-18 11:48:44 -0800829/**
830 * Move the cursor forward to the next tab stop, or to the last column
831 * if no more tab stops are set.
832 */
833hterm.Terminal.prototype.forwardTabStop = function() {
834 var column = this.screen_.cursorPosition.column;
835
836 for (var i = 0; i < this.tabStops_.length; i++) {
837 if (this.tabStops_[i] > column) {
838 this.setCursorColumn(this.tabStops_[i]);
839 return;
840 }
841 }
842
David Benjamin66e954d2012-05-05 21:08:12 -0400843 // xterm does not clear the overflow flag on HT or CHT.
844 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800845 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400846 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800847};
848
rgindac9bc5502012-01-18 11:48:44 -0800849/**
850 * Move the cursor backward to the previous tab stop, or to the first column
851 * if no previous tab stops are set.
852 */
853hterm.Terminal.prototype.backwardTabStop = function() {
854 var column = this.screen_.cursorPosition.column;
855
856 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
857 if (this.tabStops_[i] < column) {
858 this.setCursorColumn(this.tabStops_[i]);
859 return;
860 }
861 }
862
863 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800864};
865
rgindac9bc5502012-01-18 11:48:44 -0800866/**
867 * Set a tab stop at the given column.
868 *
869 * @param {int} column Zero based column.
870 */
871hterm.Terminal.prototype.setTabStop = function(column) {
872 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
873 if (this.tabStops_[i] == column)
874 return;
875
876 if (this.tabStops_[i] < column) {
877 this.tabStops_.splice(i + 1, 0, column);
878 return;
879 }
880 }
881
882 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800883};
884
rgindac9bc5502012-01-18 11:48:44 -0800885/**
886 * Clear the tab stop at the current cursor position.
887 *
888 * No effect if there is no tab stop at the current cursor position.
889 */
890hterm.Terminal.prototype.clearTabStopAtCursor = function() {
891 var column = this.screen_.cursorPosition.column;
892
893 var i = this.tabStops_.indexOf(column);
894 if (i == -1)
895 return;
896
897 this.tabStops_.splice(i, 1);
898};
899
900/**
901 * Clear all tab stops.
902 */
903hterm.Terminal.prototype.clearAllTabStops = function() {
904 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -0400905 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -0800906};
907
908/**
909 * Set up the default tab stops, starting from a given column.
910 *
911 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -0400912 * from the specified column, or 0 if no column is provided. It also flags
913 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -0800914 *
915 * This does not clear the existing tab stops first, use clearAllTabStops
916 * for that.
917 *
918 * @param {int} opt_start Optional starting zero based starting column, useful
919 * for filling out missing tab stops when the terminal is resized.
920 */
921hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
922 var start = opt_start || 0;
923 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -0400924 // Round start up to a default tab stop.
925 start = start - 1 - ((start - 1) % w) + w;
926 for (var i = start; i < this.screenSize.width; i += w) {
927 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -0800928 }
David Benjamin66e954d2012-05-05 21:08:12 -0400929
930 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -0800931};
932
rginda6d397402012-01-17 10:58:29 -0800933/**
rginda8ba33642011-12-14 12:31:31 -0800934 * Interpret a sequence of characters.
935 *
936 * Incomplete escape sequences are buffered until the next call.
937 *
938 * @param {string} str Sequence of characters to interpret or pass through.
939 */
940hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800941 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800942 this.scheduleSyncCursorPosition_();
943};
944
945/**
946 * Take over the given DIV for use as the terminal display.
947 *
948 * @param {HTMLDivElement} div The div to use as the terminal display.
949 */
950hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800951 this.div_ = div;
952
rginda8ba33642011-12-14 12:31:31 -0800953 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -0700954 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -0400955 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
956 this.scrollPort_.setBackgroundPosition(
957 this.prefs_.get('background-position'));
rginda30f20f62012-04-05 16:36:19 -0700958
rginda0918b652012-04-04 11:26:24 -0700959 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -0800960
rginda9f5222b2012-03-05 11:53:28 -0800961 this.setFontSize(this.prefs_.get('font-size'));
962 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -0800963
David Reveman8f552492012-03-28 12:18:41 -0400964 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
965
rginda8ba33642011-12-14 12:31:31 -0800966 this.document_ = this.scrollPort_.getDocument();
967
rginda8e92a692012-05-20 19:37:20 -0700968 this.document_.body.firstChild.addEventListener(
969 'focus', this.onFocusChange_.bind(this, true));
970 this.document_.body.firstChild.addEventListener(
971 'blur', this.onFocusChange_.bind(this, false));
972
973 var style = this.document_.createElement('style');
974 style.textContent =
975 ('.cursor-node[focus="false"] {' +
976 ' box-sizing: border-box;' +
977 ' background-color: transparent !important;' +
978 ' border-width: 2px;' +
979 ' border-style: solid;' +
980 '}');
981 this.document_.head.appendChild(style);
982
rginda8ba33642011-12-14 12:31:31 -0800983 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -0700984 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -0800985 this.cursorNode_.style.cssText =
986 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800987 'top: -99px;' +
988 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -0800989 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
990 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda8e92a692012-05-20 19:37:20 -0700991 '-webkit-transition: opacity, background-color 100ms linear;');
992 this.setCursorColor(this.prefs_.get('cursor-color'));
rgindad5613292012-06-19 15:40:37 -0700993
rginda8ba33642011-12-14 12:31:31 -0800994 this.document_.body.appendChild(this.cursorNode_);
995
rgindad5613292012-06-19 15:40:37 -0700996 // When 'enableMouseDragScroll' is off we reposition this element directly
997 // under the mouse cursor after a click. This makes Chrome associate
998 // subsequent mousemove events with the scroll-blocker. Since the
999 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1000 // events do not cause the scrollport to scroll.
1001 //
1002 // It's a hack, but it's the cleanest way I could find.
1003 this.scrollBlockerNode_ = this.document_.createElement('div');
1004 this.scrollBlockerNode_.style.cssText =
1005 ('position: absolute;' +
1006 'top: -99px;' +
1007 'display: block;' +
1008 'width: 10px;' +
1009 'height: 10px;');
1010 this.document_.body.appendChild(this.scrollBlockerNode_);
1011
1012 var onMouse = this.onMouse_.bind(this);
1013 this.scrollPort_.onScrollWheel = onMouse;
1014 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1015 ].forEach(function(event) {
1016 this.scrollBlockerNode_.addEventListener(event, onMouse);
1017 this.cursorNode_.addEventListener(event, onMouse);
1018 this.document_.addEventListener(event, onMouse);
1019 }.bind(this));
1020
1021 this.cursorNode_.addEventListener('mousedown', function() {
1022 setTimeout(this.focus.bind(this));
1023 }.bind(this));
1024
rgindade84e382012-04-20 15:39:31 -07001025 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda8ba33642011-12-14 12:31:31 -08001026 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001027
rginda87b86462011-12-14 13:48:03 -08001028 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001029 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001030};
1031
rginda0918b652012-04-04 11:26:24 -07001032/**
1033 * Return the HTML document that contains the terminal DOM nodes.
1034 */
rginda87b86462011-12-14 13:48:03 -08001035hterm.Terminal.prototype.getDocument = function() {
1036 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001037};
1038
1039/**
rginda0918b652012-04-04 11:26:24 -07001040 * Focus the terminal.
1041 */
1042hterm.Terminal.prototype.focus = function() {
1043 this.scrollPort_.focus();
1044};
1045
1046/**
rginda8ba33642011-12-14 12:31:31 -08001047 * Return the HTML Element for a given row index.
1048 *
1049 * This is a method from the RowProvider interface. The ScrollPort uses
1050 * it to fetch rows on demand as they are scrolled into view.
1051 *
1052 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1053 * pairs to conserve memory.
1054 *
1055 * @param {integer} index The zero-based row index, measured relative to the
1056 * start of the scrollback buffer. On-screen rows will always have the
1057 * largest indicies.
1058 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1059 */
1060hterm.Terminal.prototype.getRowNode = function(index) {
1061 if (index < this.scrollbackRows_.length)
1062 return this.scrollbackRows_[index];
1063
1064 var screenIndex = index - this.scrollbackRows_.length;
1065 return this.screen_.rowsArray[screenIndex];
1066};
1067
1068/**
1069 * Return the text content for a given range of rows.
1070 *
1071 * This is a method from the RowProvider interface. The ScrollPort uses
1072 * it to fetch text content on demand when the user attempts to copy their
1073 * selection to the clipboard.
1074 *
1075 * @param {integer} start The zero-based row index to start from, measured
1076 * relative to the start of the scrollback buffer. On-screen rows will
1077 * always have the largest indicies.
1078 * @param {integer} end The zero-based row index to end on, measured
1079 * relative to the start of the scrollback buffer.
1080 * @return {string} A single string containing the text value of the range of
1081 * rows. Lines will be newline delimited, with no trailing newline.
1082 */
1083hterm.Terminal.prototype.getRowsText = function(start, end) {
1084 var ary = [];
1085 for (var i = start; i < end; i++) {
1086 var node = this.getRowNode(i);
1087 ary.push(node.textContent);
1088 }
1089
1090 return ary.join('\n');
1091};
1092
1093/**
1094 * Return the text content for a given row.
1095 *
1096 * This is a method from the RowProvider interface. The ScrollPort uses
1097 * it to fetch text content on demand when the user attempts to copy their
1098 * selection to the clipboard.
1099 *
1100 * @param {integer} index The zero-based row index to return, measured
1101 * relative to the start of the scrollback buffer. On-screen rows will
1102 * always have the largest indicies.
1103 * @return {string} A string containing the text value of the selected row.
1104 */
1105hterm.Terminal.prototype.getRowText = function(index) {
1106 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001107 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001108};
1109
1110/**
1111 * Return the total number of rows in the addressable screen and in the
1112 * scrollback buffer of this terminal.
1113 *
1114 * This is a method from the RowProvider interface. The ScrollPort uses
1115 * it to compute the size of the scrollbar.
1116 *
1117 * @return {integer} The number of rows in this terminal.
1118 */
1119hterm.Terminal.prototype.getRowCount = function() {
1120 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1121};
1122
1123/**
1124 * Create DOM nodes for new rows and append them to the end of the terminal.
1125 *
1126 * This is the only correct way to add a new DOM node for a row. Notice that
1127 * the new row is appended to the bottom of the list of rows, and does not
1128 * require renumbering (of the rowIndex property) of previous rows.
1129 *
1130 * If you think you want a new blank row somewhere in the middle of the
1131 * terminal, look into moveRows_().
1132 *
1133 * This method does not pay attention to vtScrollTop/Bottom, since you should
1134 * be using moveRows() in cases where they would matter.
1135 *
1136 * The cursor will be positioned at column 0 of the first inserted line.
1137 */
1138hterm.Terminal.prototype.appendRows_ = function(count) {
1139 var cursorRow = this.screen_.rowsArray.length;
1140 var offset = this.scrollbackRows_.length + cursorRow;
1141 for (var i = 0; i < count; i++) {
1142 var row = this.document_.createElement('x-row');
1143 row.appendChild(this.document_.createTextNode(''));
1144 row.rowIndex = offset + i;
1145 this.screen_.pushRow(row);
1146 }
1147
1148 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1149 if (extraRows > 0) {
1150 var ary = this.screen_.shiftRows(extraRows);
1151 Array.prototype.push.apply(this.scrollbackRows_, ary);
1152 this.scheduleScrollDown_();
1153 }
1154
1155 if (cursorRow >= this.screen_.rowsArray.length)
1156 cursorRow = this.screen_.rowsArray.length - 1;
1157
rginda87b86462011-12-14 13:48:03 -08001158 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001159};
1160
1161/**
1162 * Relocate rows from one part of the addressable screen to another.
1163 *
1164 * This is used to recycle rows during VT scrolls (those which are driven
1165 * by VT commands, rather than by the user manipulating the scrollbar.)
1166 *
1167 * In this case, the blank lines scrolled into the scroll region are made of
1168 * the nodes we scrolled off. These have their rowIndex properties carefully
1169 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001170 */
1171hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1172 var ary = this.screen_.removeRows(fromIndex, count);
1173 this.screen_.insertRows(toIndex, ary);
1174
1175 var start, end;
1176 if (fromIndex < toIndex) {
1177 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001178 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001179 } else {
1180 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001181 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001182 }
1183
1184 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001185 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001186};
1187
1188/**
1189 * Renumber the rowIndex property of the given range of rows.
1190 *
1191 * The start and end indicies are relative to the screen, not the scrollback.
1192 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001193 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001194 * no need to renumber scrollback rows.
1195 */
1196hterm.Terminal.prototype.renumberRows_ = function(start, end) {
1197 var offset = this.scrollbackRows_.length;
1198 for (var i = start; i < end; i++) {
1199 this.screen_.rowsArray[i].rowIndex = offset + i;
1200 }
1201};
1202
1203/**
1204 * Print a string to the terminal.
1205 *
1206 * This respects the current insert and wraparound modes. It will add new lines
1207 * to the end of the terminal, scrolling off the top into the scrollback buffer
1208 * if necessary.
1209 *
1210 * The string is *not* parsed for escape codes. Use the interpret() method if
1211 * that's what you're after.
1212 *
1213 * @param{string} str The string to print.
1214 */
1215hterm.Terminal.prototype.print = function(str) {
rgindaa19afe22012-01-25 15:40:22 -08001216 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
1217 this.newLine();
rginda2312fff2012-01-05 16:20:52 -08001218
rgindaa19afe22012-01-25 15:40:22 -08001219 if (this.options_.insertMode) {
1220 this.screen_.insertString(str);
1221 } else {
1222 this.screen_.overwriteString(str);
1223 }
1224
1225 var overflow = this.screen_.maybeClipCurrentRow();
1226
1227 if (this.options_.wraparound && overflow) {
1228 var lastColumn;
1229
1230 do {
rginda35c456b2012-02-09 17:29:05 -08001231 this.newLine();
1232 lastColumn = overflow.characterLength;
rgindaa19afe22012-01-25 15:40:22 -08001233
1234 if (!this.options_.insertMode)
1235 this.screen_.deleteChars(overflow.characterLength);
1236
1237 this.screen_.prependNodes(overflow);
rgindaa19afe22012-01-25 15:40:22 -08001238
1239 overflow = this.screen_.maybeClipCurrentRow();
1240 } while (overflow);
1241
1242 this.setCursorColumn(lastColumn);
1243 }
rginda8ba33642011-12-14 12:31:31 -08001244
1245 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001246
rginda9f5222b2012-03-05 11:53:28 -08001247 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001248 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001249};
1250
1251/**
rginda87b86462011-12-14 13:48:03 -08001252 * Set the VT scroll region.
1253 *
rginda87b86462011-12-14 13:48:03 -08001254 * This also resets the cursor position to the absolute (0, 0) position, since
1255 * that's what xterm appears to do.
1256 *
1257 * @param {integer} scrollTop The zero-based top of the scroll region.
1258 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1259 * inclusive.
1260 */
1261hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1262 this.vtScrollTop_ = scrollTop;
1263 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001264};
1265
1266/**
rginda8ba33642011-12-14 12:31:31 -08001267 * Return the top row index according to the VT.
1268 *
1269 * This will return 0 unless the terminal has been told to restrict scrolling
1270 * to some lower row. It is used for some VT cursor positioning and scrolling
1271 * commands.
1272 *
1273 * @return {integer} The topmost row in the terminal's scroll region.
1274 */
1275hterm.Terminal.prototype.getVTScrollTop = function() {
1276 if (this.vtScrollTop_ != null)
1277 return this.vtScrollTop_;
1278
1279 return 0;
rginda87b86462011-12-14 13:48:03 -08001280};
rginda8ba33642011-12-14 12:31:31 -08001281
1282/**
1283 * Return the bottom row index according to the VT.
1284 *
1285 * This will return the height of the terminal unless the it has been told to
1286 * restrict scrolling to some higher row. It is used for some VT cursor
1287 * positioning and scrolling commands.
1288 *
1289 * @return {integer} The bottommost row in the terminal's scroll region.
1290 */
1291hterm.Terminal.prototype.getVTScrollBottom = function() {
1292 if (this.vtScrollBottom_ != null)
1293 return this.vtScrollBottom_;
1294
rginda87b86462011-12-14 13:48:03 -08001295 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001296}
1297
1298/**
1299 * Process a '\n' character.
1300 *
1301 * If the cursor is on the final row of the terminal this will append a new
1302 * blank row to the screen and scroll the topmost row into the scrollback
1303 * buffer.
1304 *
1305 * Otherwise, this moves the cursor to column zero of the next row.
1306 */
1307hterm.Terminal.prototype.newLine = function() {
1308 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001309 // If we're at the end of the screen we need to append a new line and
1310 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001311 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001312 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1313 // End of the scroll region does not affect the scrollback buffer.
1314 this.vtScrollUp(1);
1315 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001316 } else {
rginda87b86462011-12-14 13:48:03 -08001317 // Anywhere else in the screen just moves the cursor.
1318 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001319 }
1320};
1321
1322/**
1323 * Like newLine(), except maintain the cursor column.
1324 */
1325hterm.Terminal.prototype.lineFeed = function() {
1326 var column = this.screen_.cursorPosition.column;
1327 this.newLine();
1328 this.setCursorColumn(column);
1329};
1330
1331/**
rginda87b86462011-12-14 13:48:03 -08001332 * If autoCarriageReturn is set then newLine(), else lineFeed().
1333 */
1334hterm.Terminal.prototype.formFeed = function() {
1335 if (this.options_.autoCarriageReturn) {
1336 this.newLine();
1337 } else {
1338 this.lineFeed();
1339 }
1340};
1341
1342/**
1343 * Move the cursor up one row, possibly inserting a blank line.
1344 *
1345 * The cursor column is not changed.
1346 */
1347hterm.Terminal.prototype.reverseLineFeed = function() {
1348 var scrollTop = this.getVTScrollTop();
1349 var currentRow = this.screen_.cursorPosition.row;
1350
1351 if (currentRow == scrollTop) {
1352 this.insertLines(1);
1353 } else {
1354 this.setAbsoluteCursorRow(currentRow - 1);
1355 }
1356};
1357
1358/**
rginda8ba33642011-12-14 12:31:31 -08001359 * Replace all characters to the left of the current cursor with the space
1360 * character.
1361 *
1362 * TODO(rginda): This should probably *remove* the characters (not just replace
1363 * with a space) if there are no characters at or beyond the current cursor
1364 * position. Once it does that, it'll have the same text-attribute related
1365 * issues as hterm.Screen.prototype.clearCursorRow :/
1366 */
1367hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001368 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001369 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001370 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001371 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001372};
1373
1374/**
David Benjamin684a9b72012-05-01 17:19:58 -04001375 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001376 *
1377 * The cursor position is unchanged.
1378 *
1379 * TODO(rginda): Test that this works even when the cursor is positioned beyond
1380 * the end of the text.
1381 *
1382 * TODO(rginda): This likely has text-attribute related troubles similar to the
1383 * todo on hterm.Screen.prototype.clearCursorRow.
David Benjamin684a9b72012-05-01 17:19:58 -04001384 *
1385 * TODO(davidben): Probably better to not add the whitespace to the clipboard
1386 * if erasing to the end of the drawn portion of the line. That said, xterm
1387 * behaves the same here.
rginda8ba33642011-12-14 12:31:31 -08001388 */
1389hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001390 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001391
rginda87b86462011-12-14 13:48:03 -08001392 var maxCount = this.screenSize.width - cursor.column;
David Benjamin684a9b72012-05-01 17:19:58 -04001393 if (opt_count === undefined || opt_count >= maxCount) {
1394 this.screen_.deleteChars(maxCount);
1395 } else {
rgindacbbd7482012-06-13 15:06:16 -07001396 this.screen_.overwriteString(lib.f.getWhitespace(opt_count));
David Benjamin684a9b72012-05-01 17:19:58 -04001397 }
rginda87b86462011-12-14 13:48:03 -08001398 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001399 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001400};
1401
1402/**
1403 * Erase the current line.
1404 *
1405 * The cursor position is unchanged.
1406 *
1407 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1408 * has a text-attribute related TODO.
1409 */
1410hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001411 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001412 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001413 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001414 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001415};
1416
1417/**
David Benjamina08d78f2012-05-05 00:28:49 -04001418 * Erase all characters from the start of the screen to the current cursor
1419 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001420 *
1421 * The cursor position is unchanged.
1422 *
1423 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1424 * has a text-attribute related TODO.
1425 */
1426hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001427 var cursor = this.saveCursor();
1428
1429 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001430
David Benjamina08d78f2012-05-05 00:28:49 -04001431 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001432 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001433 this.screen_.clearCursorRow();
1434 }
1435
rginda87b86462011-12-14 13:48:03 -08001436 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001437 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001438};
1439
1440/**
1441 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001442 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001443 *
1444 * The cursor position is unchanged.
1445 *
1446 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1447 * has a text-attribute related TODO.
1448 */
1449hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001450 var cursor = this.saveCursor();
1451
1452 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001453
David Benjamina08d78f2012-05-05 00:28:49 -04001454 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001455 for (var i = cursor.row + 1; i <= bottom; i++) {
1456 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001457 this.screen_.clearCursorRow();
1458 }
1459
rginda87b86462011-12-14 13:48:03 -08001460 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001461 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001462};
1463
1464/**
1465 * Fill the terminal with a given character.
1466 *
1467 * This methods does not respect the VT scroll region.
1468 *
1469 * @param {string} ch The character to use for the fill.
1470 */
1471hterm.Terminal.prototype.fill = function(ch) {
1472 var cursor = this.saveCursor();
1473
1474 this.setAbsoluteCursorPosition(0, 0);
1475 for (var row = 0; row < this.screenSize.height; row++) {
1476 for (var col = 0; col < this.screenSize.width; col++) {
1477 this.setAbsoluteCursorPosition(row, col);
1478 this.screen_.overwriteString(ch);
1479 }
1480 }
1481
1482 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001483};
1484
1485/**
rginda9ea433c2012-03-16 11:57:00 -07001486 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001487 *
rginda9ea433c2012-03-16 11:57:00 -07001488 * This does not respect the scroll region.
1489 *
1490 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1491 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001492 *
1493 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1494 * has a text-attribute related TODO.
1495 */
rginda9ea433c2012-03-16 11:57:00 -07001496hterm.Terminal.prototype.clearHome = function(opt_screen) {
1497 var screen = opt_screen || this.screen_;
1498 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001499
rginda11057d52012-04-25 12:29:56 -07001500 if (bottom == 0) {
1501 // Empty screen, nothing to do.
1502 return;
1503 }
1504
rgindae4d29232012-01-19 10:47:13 -08001505 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001506 screen.setCursorPosition(i, 0);
1507 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001508 }
1509
rginda9ea433c2012-03-16 11:57:00 -07001510 screen.setCursorPosition(0, 0);
1511};
1512
1513/**
1514 * Erase the entire display without changing the cursor position.
1515 *
1516 * The cursor position is unchanged. This does not respect the scroll
1517 * region.
1518 *
1519 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1520 * to the current screen.
1521 *
1522 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1523 * has a text-attribute related TODO.
1524 */
1525hterm.Terminal.prototype.clear = function(opt_screen) {
1526 var screen = opt_screen || this.screen_;
1527 var cursor = screen.cursorPosition.clone();
1528 this.clearHome(screen);
1529 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001530};
1531
1532/**
1533 * VT command to insert lines at the current cursor row.
1534 *
1535 * This respects the current scroll region. Rows pushed off the bottom are
1536 * lost (they won't show up in the scrollback buffer).
1537 *
1538 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1539 * has a text-attribute related TODO.
1540 *
1541 * @param {integer} count The number of lines to insert.
1542 */
1543hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001544 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001545
1546 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001547 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001548
rgindae4d29232012-01-19 10:47:13 -08001549 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001550 if (start != cursor.row)
1551 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001552
1553 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001554 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001555 this.screen_.clearCursorRow();
1556 }
1557
rginda87b86462011-12-14 13:48:03 -08001558 cursor.column = 0;
1559 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001560};
1561
1562/**
1563 * VT command to delete lines at the current cursor row.
1564 *
1565 * New rows are added to the bottom of scroll region to take their place. New
1566 * rows are strictly there to take up space and have no content or style.
1567 */
1568hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001569 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001570
rginda87b86462011-12-14 13:48:03 -08001571 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001572 var bottom = this.getVTScrollBottom();
1573
rginda87b86462011-12-14 13:48:03 -08001574 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001575 count = Math.min(count, maxCount);
1576
rginda87b86462011-12-14 13:48:03 -08001577 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001578 if (count != maxCount)
1579 this.moveRows_(top, count, moveStart);
1580
1581 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001582 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001583 this.screen_.clearCursorRow();
1584 }
1585
rginda87b86462011-12-14 13:48:03 -08001586 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001587 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001588};
1589
1590/**
1591 * Inserts the given number of spaces at the current cursor position.
1592 *
rginda87b86462011-12-14 13:48:03 -08001593 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001594 */
1595hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001596 var cursor = this.saveCursor();
1597
rgindacbbd7482012-06-13 15:06:16 -07001598 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001599 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001600 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001601
1602 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001603 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001604};
1605
1606/**
1607 * Forward-delete the specified number of characters starting at the cursor
1608 * position.
1609 *
1610 * @param {integer} count The number of characters to delete.
1611 */
1612hterm.Terminal.prototype.deleteChars = function(count) {
1613 this.screen_.deleteChars(count);
David Benjamin54e8bf62012-06-01 22:31:40 -04001614 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001615};
1616
1617/**
1618 * Shift rows in the scroll region upwards by a given number of lines.
1619 *
1620 * New rows are inserted at the bottom of the scroll region to fill the
1621 * vacated rows. The new rows not filled out with the current text attributes.
1622 *
1623 * This function does not affect the scrollback rows at all. Rows shifted
1624 * off the top are lost.
1625 *
rginda87b86462011-12-14 13:48:03 -08001626 * The cursor position is not altered.
1627 *
rginda8ba33642011-12-14 12:31:31 -08001628 * @param {integer} count The number of rows to scroll.
1629 */
1630hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001631 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001632
rginda87b86462011-12-14 13:48:03 -08001633 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001634 this.deleteLines(count);
1635
rginda87b86462011-12-14 13:48:03 -08001636 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001637};
1638
1639/**
1640 * Shift rows below the cursor down by a given number of lines.
1641 *
1642 * This function respects the current scroll region.
1643 *
1644 * New rows are inserted at the top of the scroll region to fill the
1645 * vacated rows. The new rows not filled out with the current text attributes.
1646 *
1647 * This function does not affect the scrollback rows at all. Rows shifted
1648 * off the bottom are lost.
1649 *
1650 * @param {integer} count The number of rows to scroll.
1651 */
1652hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001653 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001654
rginda87b86462011-12-14 13:48:03 -08001655 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001656 this.insertLines(opt_count);
1657
rginda87b86462011-12-14 13:48:03 -08001658 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001659};
1660
rginda87b86462011-12-14 13:48:03 -08001661
rginda8ba33642011-12-14 12:31:31 -08001662/**
1663 * Set the cursor position.
1664 *
1665 * The cursor row is relative to the scroll region if the terminal has
1666 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1667 *
1668 * @param {integer} row The new zero-based cursor row.
1669 * @param {integer} row The new zero-based cursor column.
1670 */
1671hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1672 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001673 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001674 } else {
rginda87b86462011-12-14 13:48:03 -08001675 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001676 }
rginda87b86462011-12-14 13:48:03 -08001677};
rginda8ba33642011-12-14 12:31:31 -08001678
rginda87b86462011-12-14 13:48:03 -08001679hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1680 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001681 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1682 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001683 this.screen_.setCursorPosition(row, column);
1684};
1685
1686hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001687 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1688 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001689 this.screen_.setCursorPosition(row, column);
1690};
1691
1692/**
1693 * Set the cursor column.
1694 *
1695 * @param {integer} column The new zero-based cursor column.
1696 */
1697hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001698 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001699};
1700
1701/**
1702 * Return the cursor column.
1703 *
1704 * @return {integer} The zero-based cursor column.
1705 */
1706hterm.Terminal.prototype.getCursorColumn = function() {
1707 return this.screen_.cursorPosition.column;
1708};
1709
1710/**
1711 * Set the cursor row.
1712 *
1713 * The cursor row is relative to the scroll region if the terminal has
1714 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1715 *
1716 * @param {integer} row The new cursor row.
1717 */
rginda87b86462011-12-14 13:48:03 -08001718hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1719 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001720};
1721
1722/**
1723 * Return the cursor row.
1724 *
1725 * @return {integer} The zero-based cursor row.
1726 */
1727hterm.Terminal.prototype.getCursorRow = function(row) {
1728 return this.screen_.cursorPosition.row;
1729};
1730
1731/**
1732 * Request that the ScrollPort redraw itself soon.
1733 *
1734 * The redraw will happen asynchronously, soon after the call stack winds down.
1735 * Multiple calls will be coalesced into a single redraw.
1736 */
1737hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001738 if (this.timeouts_.redraw)
1739 return;
rginda8ba33642011-12-14 12:31:31 -08001740
1741 var self = this;
rginda87b86462011-12-14 13:48:03 -08001742 this.timeouts_.redraw = setTimeout(function() {
1743 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001744 self.scrollPort_.redraw_();
1745 }, 0);
1746};
1747
1748/**
1749 * Request that the ScrollPort be scrolled to the bottom.
1750 *
1751 * The scroll will happen asynchronously, soon after the call stack winds down.
1752 * Multiple calls will be coalesced into a single scroll.
1753 *
1754 * This affects the scrollbar position of the ScrollPort, and has nothing to
1755 * do with the VT scroll commands.
1756 */
1757hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1758 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001759 return;
rginda8ba33642011-12-14 12:31:31 -08001760
1761 var self = this;
1762 this.timeouts_.scrollDown = setTimeout(function() {
1763 delete self.timeouts_.scrollDown;
1764 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1765 }, 10);
1766};
1767
1768/**
1769 * Move the cursor up a specified number of rows.
1770 *
1771 * @param {integer} count The number of rows to move the cursor.
1772 */
1773hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001774 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001775};
1776
1777/**
1778 * Move the cursor down a specified number of rows.
1779 *
1780 * @param {integer} count The number of rows to move the cursor.
1781 */
1782hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001783 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001784 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1785 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1786 this.screenSize.height - 1);
1787
rgindacbbd7482012-06-13 15:06:16 -07001788 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001789 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001790 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001791};
1792
1793/**
1794 * Move the cursor left a specified number of columns.
1795 *
1796 * @param {integer} count The number of columns to move the cursor.
1797 */
1798hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001799 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001800};
1801
1802/**
1803 * Move the cursor right a specified number of columns.
1804 *
1805 * @param {integer} count The number of columns to move the cursor.
1806 */
1807hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001808 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001809 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001810 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001811 this.setCursorColumn(column);
1812};
1813
1814/**
1815 * Reverse the foreground and background colors of the terminal.
1816 *
1817 * This only affects text that was drawn with no attributes.
1818 *
1819 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1820 * been drawn with attributes that happen to coincide with the default
1821 * 'no-attribute' colors. My guess is probably not.
1822 */
1823hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001824 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001825 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001826 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1827 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001828 } else {
rginda9f5222b2012-03-05 11:53:28 -08001829 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1830 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001831 }
1832};
1833
1834/**
rginda87b86462011-12-14 13:48:03 -08001835 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001836 */
1837hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001838 if (this.bellAudio_.getAttribute('src'))
1839 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001840
rginda6d397402012-01-17 10:58:29 -08001841 this.cursorNode_.style.backgroundColor =
1842 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001843
1844 var self = this;
1845 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001846 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001847 }, 200);
rginda87b86462011-12-14 13:48:03 -08001848};
1849
1850/**
rginda8ba33642011-12-14 12:31:31 -08001851 * Set the origin mode bit.
1852 *
1853 * If origin mode is on, certain VT cursor and scrolling commands measure their
1854 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1855 * to the top of the addressable screen.
1856 *
1857 * Defaults to off.
1858 *
1859 * @param {boolean} state True to set origin mode, false to unset.
1860 */
1861hterm.Terminal.prototype.setOriginMode = function(state) {
1862 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001863 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001864};
1865
1866/**
1867 * Set the insert mode bit.
1868 *
1869 * If insert mode is on, existing text beyond the cursor position will be
1870 * shifted right to make room for new text. Otherwise, new text overwrites
1871 * any existing text.
1872 *
1873 * Defaults to off.
1874 *
1875 * @param {boolean} state True to set insert mode, false to unset.
1876 */
1877hterm.Terminal.prototype.setInsertMode = function(state) {
1878 this.options_.insertMode = state;
1879};
1880
1881/**
rginda87b86462011-12-14 13:48:03 -08001882 * Set the auto carriage return bit.
1883 *
1884 * If auto carriage return is on then a formfeed character is interpreted
1885 * as a newline, otherwise it's the same as a linefeed. The difference boils
1886 * down to whether or not the cursor column is reset.
1887 */
1888hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1889 this.options_.autoCarriageReturn = state;
1890};
1891
1892/**
rginda8ba33642011-12-14 12:31:31 -08001893 * Set the wraparound mode bit.
1894 *
1895 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1896 * to the start of the following row. Otherwise, the cursor is clamped to the
1897 * end of the screen and attempts to write past it are ignored.
1898 *
1899 * Defaults to on.
1900 *
1901 * @param {boolean} state True to set wraparound mode, false to unset.
1902 */
1903hterm.Terminal.prototype.setWraparound = function(state) {
1904 this.options_.wraparound = state;
1905};
1906
1907/**
1908 * Set the reverse-wraparound mode bit.
1909 *
1910 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1911 * to the end of the previous row. Otherwise, the cursor is clamped to column
1912 * 0.
1913 *
1914 * Defaults to off.
1915 *
1916 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1917 */
1918hterm.Terminal.prototype.setReverseWraparound = function(state) {
1919 this.options_.reverseWraparound = state;
1920};
1921
1922/**
1923 * Selects between the primary and alternate screens.
1924 *
1925 * If alternate mode is on, the alternate screen is active. Otherwise the
1926 * primary screen is active.
1927 *
1928 * Swapping screens has no effect on the scrollback buffer.
1929 *
1930 * Each screen maintains its own cursor position.
1931 *
1932 * Defaults to off.
1933 *
1934 * @param {boolean} state True to set alternate mode, false to unset.
1935 */
1936hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001937 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001938 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1939
rginda35c456b2012-02-09 17:29:05 -08001940 if (this.screen_.rowsArray.length &&
1941 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
1942 // If the screen changed sizes while we were away, our rowIndexes may
1943 // be incorrect.
1944 var offset = this.scrollbackRows_.length;
1945 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07001946 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08001947 ary[i].rowIndex = offset + i;
1948 }
1949 }
rginda8ba33642011-12-14 12:31:31 -08001950
rginda35c456b2012-02-09 17:29:05 -08001951 this.realizeWidth_(this.screenSize.width);
1952 this.realizeHeight_(this.screenSize.height);
1953 this.scrollPort_.syncScrollHeight();
1954 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001955
rginda6d397402012-01-17 10:58:29 -08001956 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08001957 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08001958};
1959
1960/**
1961 * Set the cursor-blink mode bit.
1962 *
1963 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1964 * a visible cursor does not blink.
1965 *
1966 * You should make sure to turn blinking off if you're going to dispose of a
1967 * terminal, otherwise you'll leak a timeout.
1968 *
1969 * Defaults to on.
1970 *
1971 * @param {boolean} state True to set cursor-blink mode, false to unset.
1972 */
1973hterm.Terminal.prototype.setCursorBlink = function(state) {
1974 this.options_.cursorBlink = state;
1975
1976 if (!state && this.timeouts_.cursorBlink) {
1977 clearTimeout(this.timeouts_.cursorBlink);
1978 delete this.timeouts_.cursorBlink;
1979 }
1980
1981 if (this.options_.cursorVisible)
1982 this.setCursorVisible(true);
1983};
1984
1985/**
1986 * Set the cursor-visible mode bit.
1987 *
1988 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1989 *
1990 * Defaults to on.
1991 *
1992 * @param {boolean} state True to set cursor-visible mode, false to unset.
1993 */
1994hterm.Terminal.prototype.setCursorVisible = function(state) {
1995 this.options_.cursorVisible = state;
1996
1997 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001998 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001999 return;
2000 }
2001
rginda87b86462011-12-14 13:48:03 -08002002 this.syncCursorPosition_();
2003
2004 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002005
2006 if (this.options_.cursorBlink) {
2007 if (this.timeouts_.cursorBlink)
2008 return;
2009
2010 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2011 500);
2012 } else {
2013 if (this.timeouts_.cursorBlink) {
2014 clearTimeout(this.timeouts_.cursorBlink);
2015 delete this.timeouts_.cursorBlink;
2016 }
2017 }
2018};
2019
2020/**
rginda87b86462011-12-14 13:48:03 -08002021 * Synchronizes the visible cursor and document selection with the current
2022 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002023 */
2024hterm.Terminal.prototype.syncCursorPosition_ = function() {
2025 var topRowIndex = this.scrollPort_.getTopRowIndex();
2026 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2027 var cursorRowIndex = this.scrollbackRows_.length +
2028 this.screen_.cursorPosition.row;
2029
2030 if (cursorRowIndex > bottomRowIndex) {
2031 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002032 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002033 return;
2034 }
2035
rginda35c456b2012-02-09 17:29:05 -08002036 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
2037 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
2038
rginda8ba33642011-12-14 12:31:31 -08002039 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002040 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2041 'px';
2042 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2043 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002044
2045 this.cursorNode_.setAttribute('title',
2046 '(' + this.screen_.cursorPosition.row +
2047 ', ' + this.screen_.cursorPosition.column +
2048 ')');
2049
2050 // Update the caret for a11y purposes.
2051 var selection = this.document_.getSelection();
2052 if (selection && selection.isCollapsed)
2053 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002054};
2055
2056/**
2057 * Synchronizes the visible cursor with the current cursor coordinates.
2058 *
2059 * The sync will happen asynchronously, soon after the call stack winds down.
2060 * Multiple calls will be coalesced into a single sync.
2061 */
2062hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2063 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002064 return;
rginda8ba33642011-12-14 12:31:31 -08002065
2066 var self = this;
2067 this.timeouts_.syncCursor = setTimeout(function() {
2068 self.syncCursorPosition_();
2069 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002070 }, 0);
2071};
2072
rgindacc2996c2012-02-24 14:59:31 -08002073/**
rgindaf522ce02012-04-17 17:49:17 -07002074 * Show or hide the zoom warning.
2075 *
2076 * The zoom warning is a message warning the user that their browser zoom must
2077 * be set to 100% in order for hterm to function properly.
2078 *
2079 * @param {boolean} state True to show the message, false to hide it.
2080 */
2081hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2082 if (!this.zoomWarningNode_) {
2083 if (!state)
2084 return;
2085
2086 this.zoomWarningNode_ = this.document_.createElement('div');
2087 this.zoomWarningNode_.style.cssText = (
2088 'color: black;' +
2089 'background-color: #ff2222;' +
2090 'font-size: large;' +
2091 'border-radius: 8px;' +
2092 'opacity: 0.75;' +
2093 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2094 'top: 0.5em;' +
2095 'right: 1.2em;' +
2096 'position: absolute;' +
2097 '-webkit-text-size-adjust: none;' +
2098 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002099 }
2100
rgindade84e382012-04-20 15:39:31 -07002101 this.zoomWarningNode_.textContent = hterm.msg('ZOOM_WARNING') ||
2102 ('!! ' + parseInt(this.scrollPort_.characterSize.zoomFactor * 100) +
2103 '% !!');
rgindaf522ce02012-04-17 17:49:17 -07002104 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2105
2106 if (state) {
2107 if (!this.zoomWarningNode_.parentNode)
2108 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2109 } else if (this.zoomWarningNode_.parentNode) {
2110 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2111 }
2112};
2113
2114/**
rgindacc2996c2012-02-24 14:59:31 -08002115 * Show the terminal overlay for a given amount of time.
2116 *
2117 * The terminal overlay appears in inverse video in a large font, centered
2118 * over the terminal. You should probably keep the overlay message brief,
2119 * since it's in a large font and you probably aren't going to check the size
2120 * of the terminal first.
2121 *
2122 * @param {string} msg The text (not HTML) message to display in the overlay.
2123 * @param {number} opt_timeout The amount of time to wait before fading out
2124 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2125 * stay up forever (or until the next overlay).
2126 */
2127hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002128 if (!this.overlayNode_) {
2129 if (!this.div_)
2130 return;
2131
2132 this.overlayNode_ = this.document_.createElement('div');
2133 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002134 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002135 'font-size: xx-large;' +
2136 'opacity: 0.75;' +
2137 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2138 'position: absolute;' +
2139 '-webkit-user-select: none;' +
2140 '-webkit-transition: opacity 180ms ease-in;');
2141 }
2142
rginda9f5222b2012-03-05 11:53:28 -08002143 this.overlayNode_.style.color = this.prefs_.get('background-color');
2144 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2145 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2146
rgindaf0090c92012-02-10 14:58:52 -08002147 this.overlayNode_.textContent = msg;
2148 this.overlayNode_.style.opacity = '0.75';
2149
2150 if (!this.overlayNode_.parentNode)
2151 this.div_.appendChild(this.overlayNode_);
2152
2153 this.overlayNode_.style.top = (
2154 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
2155 this.overlayNode_.style.left = (
2156 this.div_.clientWidth - this.overlayNode_.clientWidth -
2157 this.scrollbarWidthPx) / 2;
2158
2159 var self = this;
2160
2161 if (this.overlayTimeout_)
2162 clearTimeout(this.overlayTimeout_);
2163
rgindacc2996c2012-02-24 14:59:31 -08002164 if (opt_timeout === null)
2165 return;
2166
rgindaf0090c92012-02-10 14:58:52 -08002167 this.overlayTimeout_ = setTimeout(function() {
2168 self.overlayNode_.style.opacity = '0';
2169 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002170 if (self.overlayNode_.parentNode)
2171 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002172 self.overlayTimeout_ = null;
2173 self.overlayNode_.style.opacity = '0.75';
2174 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002175 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002176};
2177
2178hterm.Terminal.prototype.overlaySize = function() {
2179 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2180};
2181
rginda87b86462011-12-14 13:48:03 -08002182/**
2183 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2184 *
2185 * @param {string} string The VT string representing the keystroke.
2186 */
2187hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002188 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002189 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2190
2191 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08002192};
2193
2194/**
rgindad5613292012-06-19 15:40:37 -07002195 * Add the terminalRow and terminalColumn properties to mouse events and
2196 * then forward on to onMouse().
2197 *
2198 * The terminalRow and terminalColumn properties contain the (row, column)
2199 * coordinates for the mouse event.
2200 */
2201hterm.Terminal.prototype.onMouse_ = function(e) {
2202 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2203 this.scrollPort_.characterSize.height) + 1;
2204 e.terminalColumn = parseInt(e.clientX /
2205 this.scrollPort_.characterSize.width) + 1;
2206
2207 if (e.type == 'mousedown') {
2208 if (e.terminalColumn > this.screenSize.width) {
2209 // Mousedown in the scrollbar area.
2210 return;
2211 }
2212
2213 if (!this.enableMouseDragScroll) {
2214 // Move the scroll-blocker into place if we want to keep the scrollport
2215 // from scrolling.
2216 this.scrollBlockerNode_.engaged = true;
2217 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2218 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2219 }
2220 } else if (this.scrollBlockerNode_.engaged &&
2221 (e.type == 'mousemove' || e.type == 'mouseup')) {
2222 // Disengage the scroll-blocker after one of these events.
2223 this.scrollBlockerNode_.engaged = false;
2224 this.scrollBlockerNode_.style.top = '-99px';
2225 }
2226
2227 if (!e.processedByTerminalHandler_) {
2228 // We register our event handlers on the document, as well as the cursor
2229 // and the scroll blocker. Mouse events that occur on the cursor or
2230 // scroll blocker will also appear on the document, but we don't want to
2231 // process them twice.
2232 //
2233 // We can't just prevent bubbling because that has other side effects, so
2234 // we decorate the event object with this property instead.
2235 e.processedByTerminalHandler_ = true;
2236
2237 this.onMouse(e);
2238 }
2239};
2240
2241/**
2242 * Clients should override this if they care to know about mouse events.
2243 *
2244 * The event parameter will be a normal DOM mouse click event with additional
2245 * 'terminalRow' and 'terminalColumn' properties.
2246 */
2247hterm.Terminal.prototype.onMouse = function(e) { };
2248
2249/**
rginda8e92a692012-05-20 19:37:20 -07002250 * React when focus changes.
2251 */
2252hterm.Terminal.prototype.onFocusChange_ = function(state) {
2253 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
2254};
2255
2256/**
rginda8ba33642011-12-14 12:31:31 -08002257 * React when the ScrollPort is scrolled.
2258 */
2259hterm.Terminal.prototype.onScroll_ = function() {
2260 this.scheduleSyncCursorPosition_();
2261};
2262
2263/**
rginda9846e2f2012-01-27 13:53:33 -08002264 * React when text is pasted into the scrollPort.
2265 */
2266hterm.Terminal.prototype.onPaste_ = function(e) {
David Benjamin8f962172012-07-17 07:38:43 -04002267 this.io.onVTKeystroke(this.vt.encodeUTF8(e.text));
rginda9846e2f2012-01-27 13:53:33 -08002268};
2269
2270/**
rginda8ba33642011-12-14 12:31:31 -08002271 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002272 *
2273 * Note: This function should not directly contain code that alters the internal
2274 * state of the terminal. That kind of code belongs in realizeWidth or
2275 * realizeHeight, so that it can be executed synchronously in the case of a
2276 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002277 */
2278hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002279 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002280 this.scrollPort_.characterSize.width);
2281 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2282 this.scrollPort_.characterSize.height);
2283
2284 if (!(columnCount || rowCount)) {
2285 // We avoid these situations since they happen sometimes when the terminal
2286 // gets removed from the document, and we can't deal with that.
2287 return;
2288 }
2289
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002290 this.realizeSize_(columnCount, rowCount);
rgindac9bc5502012-01-18 11:48:44 -08002291 this.scheduleSyncCursorPosition_();
rgindaf522ce02012-04-17 17:49:17 -07002292 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaf0090c92012-02-10 14:58:52 -08002293 this.overlaySize();
rginda8ba33642011-12-14 12:31:31 -08002294};
2295
2296/**
2297 * Service the cursor blink timeout.
2298 */
2299hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002300 if (this.cursorNode_.style.opacity == '0') {
2301 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002302 } else {
rginda87b86462011-12-14 13:48:03 -08002303 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002304 }
2305};
David Reveman8f552492012-03-28 12:18:41 -04002306
2307/**
2308 * Set the scrollbar-visible mode bit.
2309 *
2310 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2311 * Otherwise it will not.
2312 *
2313 * Defaults to on.
2314 *
2315 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2316 */
2317hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2318 this.scrollPort_.setScrollbarVisible(state);
2319};