blob: 16590f4a64fafd29949a2ce80d2e9dead01f281c [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
5/**
6 * Constructor for the Terminal class.
7 *
8 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
9 * classes to provide the complete terminal functionality.
10 *
11 * There are a number of lower-level Terminal methods that can be called
12 * directly to manipulate the cursor, text, scroll region, and other terminal
13 * attributes. However, the primary method is interpret(), which parses VT
14 * escape sequences and invokes the appropriate Terminal methods.
15 *
16 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
17 *
18 * TODO(rginda): Eventually we're going to need to support characters which are
19 * displayed twice as wide as standard latin characters. This is to support
20 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080021 *
22 * @param {string} opt_profileName Optional preference profile name. If not
23 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080024 */
rginda9f5222b2012-03-05 11:53:28 -080025hterm.Terminal = function(opt_profileName) {
26 this.profileName_ = null;
27 this.setProfile(opt_profileName || 'default');
28
rginda8ba33642011-12-14 12:31:31 -080029 // Two screen instances.
30 this.primaryScreen_ = new hterm.Screen();
31 this.alternateScreen_ = new hterm.Screen();
32
33 // The "current" screen.
34 this.screen_ = this.primaryScreen_;
35
rginda8ba33642011-12-14 12:31:31 -080036 // The local notion of the screen size. ScreenBuffers also have a size which
37 // indicates their present size. During size changes, the two may disagree.
38 // Also, the inactive screen's size is not altered until it is made the active
39 // screen.
40 this.screenSize = new hterm.Size(0, 0);
41
rginda8ba33642011-12-14 12:31:31 -080042 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080043 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080044 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
45 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080046 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rginda8ba33642011-12-14 12:31:31 -080047
rginda87b86462011-12-14 13:48:03 -080048 // The div that contains this terminal.
49 this.div_ = null;
50
rgindac9bc5502012-01-18 11:48:44 -080051 // The document that contains the scrollPort. Defaulted to the global
52 // document here so that the terminal is functional even if it hasn't been
53 // inserted into a document yet, but re-set in decorate().
54 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080055
rginda8ba33642011-12-14 12:31:31 -080056 // The rows that have scrolled off screen and are no longer addressable.
57 this.scrollbackRows_ = [];
58
rgindac9bc5502012-01-18 11:48:44 -080059 // Saved tab stops.
60 this.tabStops_ = [];
61
David Benjamin66e954d2012-05-05 21:08:12 -040062 // Keep track of whether default tab stops have been erased; after a TBC
63 // clears all tab stops, defaults aren't restored on resize until a reset.
64 this.defaultTabStops = true;
65
rginda8ba33642011-12-14 12:31:31 -080066 // The VT's notion of the top and bottom rows. Used during some VT
67 // cursor positioning and scrolling commands.
68 this.vtScrollTop_ = null;
69 this.vtScrollBottom_ = null;
70
71 // The DIV element for the visible cursor.
72 this.cursorNode_ = null;
73
rginda9f5222b2012-03-05 11:53:28 -080074 // These prefs are cached so we don't have to read from local storage with
75 // each output and keystroke.
76 this.scrollOnOutput_ = this.prefs_.get('scroll-on-output');
77 this.scrollOnKeystroke_ = this.prefs_.get('scroll-on-keystroke');
rginda8e92a692012-05-20 19:37:20 -070078 this.foregroundColor_ = this.prefs_.get('foreground-color');
79 this.backgroundColor_ = this.prefs_.get('background-color');
rginda9f5222b2012-03-05 11:53:28 -080080
rgindaf0090c92012-02-10 14:58:52 -080081 // Terminal bell sound.
82 this.bellAudio_ = this.document_.createElement('audio');
rginda9f5222b2012-03-05 11:53:28 -080083 this.bellAudio_.setAttribute('src', this.prefs_.get('audible-bell-sound'));
rgindaf0090c92012-02-10 14:58:52 -080084 this.bellAudio_.setAttribute('preload', 'auto');
85
rginda6d397402012-01-17 10:58:29 -080086 // Cursor position and attributes saved with DECSC.
87 this.savedOptions_ = {};
88
rginda8ba33642011-12-14 12:31:31 -080089 // The current mode bits for the terminal.
90 this.options_ = new hterm.Options();
91
92 // Timeouts we might need to clear.
93 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -080094
95 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -080096 this.vt = new hterm.VT(this);
rginda11057d52012-04-25 12:29:56 -070097 this.vt.enable8BitControl = this.prefs_.get('enable-8-bit-control');
98 this.vt.maxStringSequence = this.prefs_.get('max-string-sequence');
rginda87b86462011-12-14 13:48:03 -080099
rgindafeaf3142012-01-31 15:14:20 -0800100 // The keyboard hander.
101 this.keyboard = new hterm.Keyboard(this);
102
rginda87b86462011-12-14 13:48:03 -0800103 // General IO interface that can be given to third parties without exposing
104 // the entire terminal object.
105 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800106
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400107 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800108 this.setDefaultTabStops();
rginda87b86462011-12-14 13:48:03 -0800109};
110
111/**
rginda35c456b2012-02-09 17:29:05 -0800112 * Default tab with of 8 to match xterm.
113 */
114hterm.Terminal.prototype.tabWidth = 8;
115
116/**
rginda35c456b2012-02-09 17:29:05 -0800117 * The assumed width of a scrollbar.
118 */
119hterm.Terminal.prototype.scrollbarWidthPx = 16;
120
121/**
rginda9f5222b2012-03-05 11:53:28 -0800122 * Select a preference profile.
123 *
124 * This will load the terminal preferences for the given profile name and
125 * associate subsequent preference changes with the new preference profile.
126 *
127 * @param {string} newName The name of the preference profile. Forward slash
128 * characters will be removed from the name.
129 */
130hterm.Terminal.prototype.setProfile = function(profileName) {
131 // If we already have a profile selected, we're going to need to re-sync
132 // with the new profile.
133 var needSync = !!this.profileName_;
134
135 this.profileName_ = profileName.replace(/\//g, '');
136
rginda8e92a692012-05-20 19:37:20 -0700137 this.prefs_ = new PreferenceManager(
rginda9f5222b2012-03-05 11:53:28 -0800138 '/hterm/prefs/profiles/' + this.profileName_);
139
140 var self = this;
141 this.prefs_.definePreferences
rginda30f20f62012-04-05 16:36:19 -0700142 ([
143 /**
144 * Set whether the alt key acts as a meta key or as a distinct alt key.
rginda9f5222b2012-03-05 11:53:28 -0800145 */
rginda30f20f62012-04-05 16:36:19 -0700146 ['alt-is-meta', false, function(v) {
rgindaf9c36852012-05-09 11:08:39 -0700147 self.keyboard.altIsMeta = v;
rginda9f5222b2012-03-05 11:53:28 -0800148 }
149 ],
150
rginda30f20f62012-04-05 16:36:19 -0700151 /**
rginda39bdf6f2012-04-10 16:50:55 -0700152 * Controls how the alt key is handled.
153 *
154 * escape....... Send an ESC prefix.
155 * 8-bit........ Add 128 to the unshifted character as in xterm.
156 * browser-key.. Wait for the keypress event and see what the browser says.
157 * (This won't work well on platforms where the browser
158 * performs a default action for some alt sequences.)
rginda30f20f62012-04-05 16:36:19 -0700159 */
rginda39bdf6f2012-04-10 16:50:55 -0700160 ['alt-sends-what', 'escape', function(v) {
161 if (!/^(escape|8-bit|browser-key)$/.test(v))
162 v = 'escape';
163
rgindaf9c36852012-05-09 11:08:39 -0700164 self.keyboard.altSendsWhat = v;
rginda30f20f62012-04-05 16:36:19 -0700165 }
166 ],
167
168 /**
169 * Terminal bell sound. Empty string for no audible bell.
170 */
171 ['audible-bell-sound', '../audio/bell.ogg', function(v) {
172 self.bellAudio_.setAttribute('src', v);
173 }
174 ],
175
176 /**
177 * The background color for text with no other color attributes.
178 */
179 ['background-color', 'rgb(16, 16, 16)', function(v) {
rginda8e92a692012-05-20 19:37:20 -0700180 self.setBackgroundColor(v);
rginda9f5222b2012-03-05 11:53:28 -0800181 }
182 ],
183
184 /**
rginda30f20f62012-04-05 16:36:19 -0700185 * The background image.
rginda30f20f62012-04-05 16:36:19 -0700186 */
rginda8e92a692012-05-20 19:37:20 -0700187 ['background-image', '',
rginda30f20f62012-04-05 16:36:19 -0700188 function(v) {
189 self.scrollPort_.setBackgroundImage(v);
190 }
191 ],
192
193 /**
Philip Douglass959b49d2012-05-30 13:29:29 -0400194 * The background image size,
195 *
196 * Defaults to none.
197 */
198 ['background-size', '', function(v) {
199 self.scrollPort_.setBackgroundSize(v);
200 }
201 ],
202
203 /**
204 * The background image position,
205 *
206 * Defaults to none.
207 */
208 ['background-position', '', function(v) {
209 self.scrollPort_.setBackgroundPosition(v);
210 }
211 ],
212
213 /**
rginda30f20f62012-04-05 16:36:19 -0700214 * If true, the backspace should send BS ('\x08', aka ^H). Otherwise
215 * the backspace key should send '\x7f'.
216 */
217 ['backspace-sends-backspace', false, function(v) {
218 self.keyboard.backspaceSendsBackspace = v;
219 }
220 ],
221
222 /**
rgindade84e382012-04-20 15:39:31 -0700223 * Whether or not to blink the cursor by default.
224 */
225 ['cursor-blink', false, function(v) {
226 self.setCursorBlink(!!v);
227 }
228 ],
229
230 /**
rginda30f20f62012-04-05 16:36:19 -0700231 * The color of the visible cursor.
232 */
233 ['cursor-color', 'rgba(255,0,0,0.5)', function(v) {
rginda8e92a692012-05-20 19:37:20 -0700234 self.setCursorColor(v);
rginda30f20f62012-04-05 16:36:19 -0700235 }
236 ],
237
238 /**
rginda11057d52012-04-25 12:29:56 -0700239 * True to enable 8-bit control characters, false to ignore them.
240 *
241 * We'll respect the two-byte versions of these control characters
242 * regardless of this setting.
243 */
244 ['enable-8-bit-control', false, function(v) {
245 self.vt.enable8BitControl = !!v;
246 }
247 ],
248
249 /**
rginda30f20f62012-04-05 16:36:19 -0700250 * True if we should use bold weight font for text with the bold/bright
251 * attribute. False to use bright colors only. Null to autodetect.
252 */
253 ['enable-bold', null, function(v) {
254 self.syncBoldSafeState();
255 }
256 ],
257
258 /**
rginda9f5222b2012-03-05 11:53:28 -0800259 * Default font family for the terminal text.
260 */
261 ['font-family', ('"DejaVu Sans Mono", "Everson Mono", ' +
262 'FreeMono, "Menlo", "Lucida Console", ' +
263 'monospace'),
264 function(v) { self.syncFontFamily() }
265 ],
266
267 /**
rginda30f20f62012-04-05 16:36:19 -0700268 * The default font size in pixels.
269 */
270 ['font-size', 15, function(v) {
271 self.setFontSize(v);
272 }
273 ],
274
275 /**
rginda9f5222b2012-03-05 11:53:28 -0800276 * Anti-aliasing.
277 */
278 ['font-smoothing', 'antialiased',
279 function(v) { self.syncFontFamily() }
280 ],
281
282 /**
rginda30f20f62012-04-05 16:36:19 -0700283 * The foreground color for text with no other color attributes.
rginda9f5222b2012-03-05 11:53:28 -0800284 */
rginda30f20f62012-04-05 16:36:19 -0700285 ['foreground-color', 'rgb(240, 240, 240)', function(v) {
rginda8e92a692012-05-20 19:37:20 -0700286 self.setForegroundColor(v);
rginda9f5222b2012-03-05 11:53:28 -0800287 }
288 ],
289
290 /**
rginda30f20f62012-04-05 16:36:19 -0700291 * If true, home/end will control the terminal scrollbar and shift home/end
292 * will send the VT keycodes. If false then home/end sends VT codes and
293 * shift home/end scrolls.
rginda9f5222b2012-03-05 11:53:28 -0800294 */
rginda30f20f62012-04-05 16:36:19 -0700295 ['home-keys-scroll', false, function(v) {
296 self.keyboard.homeKeysScroll = v;
297 }
298 ],
299
300 /**
rginda11057d52012-04-25 12:29:56 -0700301 * Max length of a DCS, OSC, PM, or APS sequence before we give up and
302 * ignore the code.
303 */
304 ['max-string-sequence', 1024, function(v) {
305 self.vt.maxStringSequence = v;
306 }
307 ],
308
309 /**
rginda30f20f62012-04-05 16:36:19 -0700310 * Set whether the meta key sends a leading escape or not.
311 */
312 ['meta-sends-escape', true, function(v) {
313 self.keyboard.metaSendsEscape = v;
rginda9f5222b2012-03-05 11:53:28 -0800314 }
315 ],
316
317 /**
318 * If true, scroll to the bottom on any keystroke.
319 */
320 ['scroll-on-keystroke', true, function(v) {
321 self.scrollOnKeystroke_ = v;
322 }
323 ],
324
325 /**
326 * If true, scroll to the bottom on terminal output.
327 */
328 ['scroll-on-output', false, function(v) {
329 self.scrollOnOutput_ = v;
330 }
331 ],
332
333 /**
David Reveman8f552492012-03-28 12:18:41 -0400334 * The vertical scrollbar mode.
335 */
336 ['scrollbar-visible', true, function(v) {
337 self.setScrollbarVisible(v);
338 }
339 ],
rginda30f20f62012-04-05 16:36:19 -0700340
341 /**
rgindaf522ce02012-04-17 17:49:17 -0700342 * The default environment variables.
343 */
344 ['environment', {TERM: 'xterm-256color'}, null],
345
346 /**
rginda30f20f62012-04-05 16:36:19 -0700347 * If true, page up/down will control the terminal scrollbar and shift
348 * page up/down will send the VT keycodes. If false then page up/down
349 * sends VT codes and shift page up/down scrolls.
350 */
351 ['page-keys-scroll', false, function(v) {
352 self.keyboard.pageKeysScroll = v;
353 }
354 ],
355
rginda9f5222b2012-03-05 11:53:28 -0800356 ]);
357
358 if (needSync)
359 this.prefs_.notifyAll();
360};
361
rginda8e92a692012-05-20 19:37:20 -0700362
363/**
364 * Set the color for the cursor.
365 *
366 * If you want this setting to persist, set it through prefs_, rather than
367 * with this method.
368 */
369hterm.Terminal.prototype.setCursorColor = function(color) {
370 this.cursorNode_.style.backgroundColor = color;
371 this.cursorNode_.style.borderColor = color;
372};
373
374/**
375 * Return the current cursor color as a string.
376 */
377hterm.Terminal.prototype.getCursorColor = function() {
378 return this.cursorNode_.style.backgroundColor;
379};
380
381/**
382 * Set the background color.
383 *
384 * If you want this setting to persist, set it through prefs_, rather than
385 * with this method.
386 */
387hterm.Terminal.prototype.setBackgroundColor = function(color) {
388 this.backgroundColor_ = hterm.colors.normalizeCSS(color);
389 this.scrollPort_.setBackgroundColor(color);
390};
391
rginda9f5222b2012-03-05 11:53:28 -0800392/**
393 * Return the current terminal background color.
394 *
395 * Intended for use by other classes, so we don't have to expose the entire
396 * prefs_ object.
397 */
398hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700399 return this.backgroundColor_;
400};
401
402/**
403 * Set the foreground color.
404 *
405 * If you want this setting to persist, set it through prefs_, rather than
406 * with this method.
407 */
408hterm.Terminal.prototype.setForegroundColor = function(color) {
409 this.foregroundColor_ = hterm.colors.normalizeCSS(color);
410 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800411};
412
413/**
414 * Return the current terminal foreground color.
415 *
416 * Intended for use by other classes, so we don't have to expose the entire
417 * prefs_ object.
418 */
419hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700420 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800421};
422
423/**
rginda87b86462011-12-14 13:48:03 -0800424 * Create a new instance of a terminal command and run it with a given
425 * argument string.
426 *
427 * @param {function} commandClass The constructor for a terminal command.
428 * @param {string} argString The argument string to pass to the command.
429 */
430hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700431 var environment = this.prefs_.get('environment');
432 if (typeof environment != 'object' || environment == null)
433 environment = {};
434
rginda87b86462011-12-14 13:48:03 -0800435 var self = this;
436 this.command = new commandClass(
437 { argString: argString || '',
438 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700439 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800440 onExit: function(code) {
441 self.io.pop();
442 self.io.println(hterm.msg('COMMAND_COMPLETE',
443 [self.command.commandName, code]));
rgindafeaf3142012-01-31 15:14:20 -0800444 self.uninstallKeyboard();
rginda87b86462011-12-14 13:48:03 -0800445 }
446 });
447
rgindafeaf3142012-01-31 15:14:20 -0800448 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800449 this.command.run();
450};
451
452/**
rgindafeaf3142012-01-31 15:14:20 -0800453 * Returns true if the current screen is the primary screen, false otherwise.
454 */
455hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700456 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800457};
458
459/**
460 * Install the keyboard handler for this terminal.
461 *
462 * This will prevent the browser from seeing any keystrokes sent to the
463 * terminal.
464 */
465hterm.Terminal.prototype.installKeyboard = function() {
466 this.keyboard.installKeyboard(this.document_.body.firstChild);
467}
468
469/**
470 * Uninstall the keyboard handler for this terminal.
471 */
472hterm.Terminal.prototype.uninstallKeyboard = function() {
473 this.keyboard.installKeyboard(null);
474}
475
476/**
rginda35c456b2012-02-09 17:29:05 -0800477 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800478 *
479 * Call setFontSize(0) to reset to the default font size.
480 *
481 * This function does not modify the font-size preference.
482 *
483 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800484 */
485hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800486 if (px === 0)
487 px = this.prefs_.get('font-size');
488
rginda35c456b2012-02-09 17:29:05 -0800489 this.scrollPort_.setFontSize(px);
490};
491
492/**
493 * Get the current font size.
494 */
495hterm.Terminal.prototype.getFontSize = function() {
496 return this.scrollPort_.getFontSize();
497};
498
499/**
rginda8e92a692012-05-20 19:37:20 -0700500 * Get the current font family.
501 */
502hterm.Terminal.prototype.getFontFamily = function() {
503 return this.scrollPort_.getFontFamily();
504};
505
506/**
rginda35c456b2012-02-09 17:29:05 -0800507 * Set the CSS "font-family" for this terminal.
508 */
rginda9f5222b2012-03-05 11:53:28 -0800509hterm.Terminal.prototype.syncFontFamily = function() {
510 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
511 this.prefs_.get('font-smoothing'));
512 this.syncBoldSafeState();
513};
514
515hterm.Terminal.prototype.syncBoldSafeState = function() {
516 var enableBold = this.prefs_.get('enable-bold');
517 if (enableBold !== null) {
518 this.screen_.textAttributes.enableBold = enableBold;
519 return;
520 }
521
rgindaf7521392012-02-28 17:20:34 -0800522 var normalSize = this.scrollPort_.measureCharacterSize();
523 var boldSize = this.scrollPort_.measureCharacterSize('bold');
524
525 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800526 if (!isBoldSafe) {
527 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700528 'from normal. Font family is: ' +
529 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800530 }
rginda9f5222b2012-03-05 11:53:28 -0800531
532 this.screen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800533};
534
535/**
rginda87b86462011-12-14 13:48:03 -0800536 * Return a copy of the current cursor position.
537 *
538 * @return {hterm.RowCol} The RowCol object representing the current position.
539 */
540hterm.Terminal.prototype.saveCursor = function() {
541 return this.screen_.cursorPosition.clone();
542};
543
rgindaa19afe22012-01-25 15:40:22 -0800544hterm.Terminal.prototype.getTextAttributes = function() {
545 return this.screen_.textAttributes;
546};
547
rginda87b86462011-12-14 13:48:03 -0800548/**
rgindaf522ce02012-04-17 17:49:17 -0700549 * Return the current browser zoom factor applied to the terminal.
550 *
551 * @return {number} The current browser zoom factor.
552 */
553hterm.Terminal.prototype.getZoomFactor = function() {
554 return this.scrollPort_.characterSize.zoomFactor;
555};
556
557/**
rginda9846e2f2012-01-27 13:53:33 -0800558 * Change the title of this terminal's window.
559 */
560hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800561 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800562};
563
564/**
rginda87b86462011-12-14 13:48:03 -0800565 * Restore a previously saved cursor position.
566 *
567 * @param {hterm.RowCol} cursor The position to restore.
568 */
569hterm.Terminal.prototype.restoreCursor = function(cursor) {
rginda35c456b2012-02-09 17:29:05 -0800570 var row = hterm.clamp(cursor.row, 0, this.screenSize.height - 1);
571 var column = hterm.clamp(cursor.column, 0, this.screenSize.width - 1);
572 this.screen_.setCursorPosition(row, column);
573 if (cursor.column > column ||
574 cursor.column == column && cursor.overflow) {
575 this.screen_.cursorPosition.overflow = true;
576 }
rginda87b86462011-12-14 13:48:03 -0800577};
578
579/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400580 * Clear the cursor's overflow flag.
581 */
582hterm.Terminal.prototype.clearCursorOverflow = function() {
583 this.screen_.cursorPosition.overflow = false;
584};
585
586/**
rginda87b86462011-12-14 13:48:03 -0800587 * Set the width of the terminal, resizing the UI to match.
588 */
589hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800590 if (columnCount == null) {
591 this.div_.style.width = '100%';
592 return;
593 }
594
rginda35c456b2012-02-09 17:29:05 -0800595 this.div_.style.width = this.scrollPort_.characterSize.width *
596 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400597 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800598 this.scheduleSyncCursorPosition_();
599};
rginda87b86462011-12-14 13:48:03 -0800600
rgindac9bc5502012-01-18 11:48:44 -0800601/**
rginda35c456b2012-02-09 17:29:05 -0800602 * Set the height of the terminal, resizing the UI to match.
603 */
604hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800605 if (rowCount == null) {
606 this.div_.style.height = '100%';
607 return;
608 }
609
rginda35c456b2012-02-09 17:29:05 -0800610 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700611 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800612 this.realizeSize_(this.screenSize.width, rowCount);
613 this.scheduleSyncCursorPosition_();
614};
615
616/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400617 * Deal with terminal size changes.
618 *
619 */
620hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
621 if (columnCount != this.screenSize.width)
622 this.realizeWidth_(columnCount);
623
624 if (rowCount != this.screenSize.height)
625 this.realizeHeight_(rowCount);
626
627 // Send new terminal size to plugin.
628 this.io.onTerminalResize(columnCount, rowCount);
629};
630
631/**
rgindac9bc5502012-01-18 11:48:44 -0800632 * Deal with terminal width changes.
633 *
634 * This function does what needs to be done when the terminal width changes
635 * out from under us. It happens here rather than in onResize_() because this
636 * code may need to run synchronously to handle programmatic changes of
637 * terminal width.
638 *
639 * Relying on the browser to send us an async resize event means we may not be
640 * in the correct state yet when the next escape sequence hits.
641 */
642hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
643 var deltaColumns = columnCount - this.screen_.getWidth();
644
rginda87b86462011-12-14 13:48:03 -0800645 this.screenSize.width = columnCount;
646 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800647
648 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400649 if (this.defaultTabStops)
650 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800651 } else {
652 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400653 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800654 break;
655
656 this.tabStops_.pop();
657 }
658 }
659
660 this.screen_.setColumnCount(this.screenSize.width);
661};
662
663/**
664 * Deal with terminal height changes.
665 *
666 * This function does what needs to be done when the terminal height changes
667 * out from under us. It happens here rather than in onResize_() because this
668 * code may need to run synchronously to handle programmatic changes of
669 * terminal height.
670 *
671 * Relying on the browser to send us an async resize event means we may not be
672 * in the correct state yet when the next escape sequence hits.
673 */
674hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
675 var deltaRows = rowCount - this.screen_.getHeight();
676
677 this.screenSize.height = rowCount;
678
679 var cursor = this.saveCursor();
680
681 if (deltaRows < 0) {
682 // Screen got smaller.
683 deltaRows *= -1;
684 while (deltaRows) {
685 var lastRow = this.getRowCount() - 1;
686 if (lastRow - this.scrollbackRows_.length == cursor.row)
687 break;
688
689 if (this.getRowText(lastRow))
690 break;
691
692 this.screen_.popRow();
693 deltaRows--;
694 }
695
696 var ary = this.screen_.shiftRows(deltaRows);
697 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
698
699 // We just removed rows from the top of the screen, we need to update
700 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800701 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800702 } else if (deltaRows > 0) {
703 // Screen got larger.
704
705 if (deltaRows <= this.scrollbackRows_.length) {
706 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
707 var rows = this.scrollbackRows_.splice(
708 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
709 this.screen_.unshiftRows(rows);
710 deltaRows -= scrollbackCount;
711 cursor.row += scrollbackCount;
712 }
713
714 if (deltaRows)
715 this.appendRows_(deltaRows);
716 }
717
rginda35c456b2012-02-09 17:29:05 -0800718 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800719 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800720};
721
722/**
723 * Scroll the terminal to the top of the scrollback buffer.
724 */
725hterm.Terminal.prototype.scrollHome = function() {
726 this.scrollPort_.scrollRowToTop(0);
727};
728
729/**
730 * Scroll the terminal to the end.
731 */
732hterm.Terminal.prototype.scrollEnd = function() {
733 this.scrollPort_.scrollRowToBottom(this.getRowCount());
734};
735
736/**
737 * Scroll the terminal one page up (minus one line) relative to the current
738 * position.
739 */
740hterm.Terminal.prototype.scrollPageUp = function() {
741 var i = this.scrollPort_.getTopRowIndex();
742 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
743};
744
745/**
746 * Scroll the terminal one page down (minus one line) relative to the current
747 * position.
748 */
749hterm.Terminal.prototype.scrollPageDown = function() {
750 var i = this.scrollPort_.getTopRowIndex();
751 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800752};
753
rgindac9bc5502012-01-18 11:48:44 -0800754/**
755 * Full terminal reset.
756 */
rginda87b86462011-12-14 13:48:03 -0800757hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800758 this.clearAllTabStops();
759 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700760
761 this.clearHome(this.primaryScreen_);
762 this.primaryScreen_.textAttributes.reset();
763
764 this.clearHome(this.alternateScreen_);
765 this.alternateScreen_.textAttributes.reset();
766
rgindab8bc8932012-04-27 12:45:03 -0700767 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
768
rgindac9bc5502012-01-18 11:48:44 -0800769 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800770};
771
rgindac9bc5502012-01-18 11:48:44 -0800772/**
773 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700774 *
775 * Perform a soft reset to the default values listed in
776 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800777 */
rginda0f5c0292012-01-13 11:00:13 -0800778hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700779 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800780 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700781
rgindab8bc8932012-04-27 12:45:03 -0700782 // Xterm also resets the color palette on soft reset, even though it doesn't
783 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700784 this.primaryScreen_.textAttributes.resetColorPalette();
785 this.alternateScreen_.textAttributes.resetColorPalette();
786
rgindab8bc8932012-04-27 12:45:03 -0700787 // The xterm man page explicitly says this will happen on soft reset.
788 this.setVTScrollRegion(null, null);
789
790 // Xterm also shows the cursor on soft reset, but does not alter the blink
791 // state.
rgindaa19afe22012-01-25 15:40:22 -0800792 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800793};
794
rgindac9bc5502012-01-18 11:48:44 -0800795/**
796 * Move the cursor forward to the next tab stop, or to the last column
797 * if no more tab stops are set.
798 */
799hterm.Terminal.prototype.forwardTabStop = function() {
800 var column = this.screen_.cursorPosition.column;
801
802 for (var i = 0; i < this.tabStops_.length; i++) {
803 if (this.tabStops_[i] > column) {
804 this.setCursorColumn(this.tabStops_[i]);
805 return;
806 }
807 }
808
David Benjamin66e954d2012-05-05 21:08:12 -0400809 // xterm does not clear the overflow flag on HT or CHT.
810 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800811 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400812 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800813};
814
rgindac9bc5502012-01-18 11:48:44 -0800815/**
816 * Move the cursor backward to the previous tab stop, or to the first column
817 * if no previous tab stops are set.
818 */
819hterm.Terminal.prototype.backwardTabStop = function() {
820 var column = this.screen_.cursorPosition.column;
821
822 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
823 if (this.tabStops_[i] < column) {
824 this.setCursorColumn(this.tabStops_[i]);
825 return;
826 }
827 }
828
829 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800830};
831
rgindac9bc5502012-01-18 11:48:44 -0800832/**
833 * Set a tab stop at the given column.
834 *
835 * @param {int} column Zero based column.
836 */
837hterm.Terminal.prototype.setTabStop = function(column) {
838 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
839 if (this.tabStops_[i] == column)
840 return;
841
842 if (this.tabStops_[i] < column) {
843 this.tabStops_.splice(i + 1, 0, column);
844 return;
845 }
846 }
847
848 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800849};
850
rgindac9bc5502012-01-18 11:48:44 -0800851/**
852 * Clear the tab stop at the current cursor position.
853 *
854 * No effect if there is no tab stop at the current cursor position.
855 */
856hterm.Terminal.prototype.clearTabStopAtCursor = function() {
857 var column = this.screen_.cursorPosition.column;
858
859 var i = this.tabStops_.indexOf(column);
860 if (i == -1)
861 return;
862
863 this.tabStops_.splice(i, 1);
864};
865
866/**
867 * Clear all tab stops.
868 */
869hterm.Terminal.prototype.clearAllTabStops = function() {
870 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -0400871 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -0800872};
873
874/**
875 * Set up the default tab stops, starting from a given column.
876 *
877 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -0400878 * from the specified column, or 0 if no column is provided. It also flags
879 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -0800880 *
881 * This does not clear the existing tab stops first, use clearAllTabStops
882 * for that.
883 *
884 * @param {int} opt_start Optional starting zero based starting column, useful
885 * for filling out missing tab stops when the terminal is resized.
886 */
887hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
888 var start = opt_start || 0;
889 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -0400890 // Round start up to a default tab stop.
891 start = start - 1 - ((start - 1) % w) + w;
892 for (var i = start; i < this.screenSize.width; i += w) {
893 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -0800894 }
David Benjamin66e954d2012-05-05 21:08:12 -0400895
896 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -0800897};
898
rginda6d397402012-01-17 10:58:29 -0800899/**
900 * Save cursor position and attributes.
901 *
902 * TODO(rginda): Save attributes once we support them.
903 */
rginda87b86462011-12-14 13:48:03 -0800904hterm.Terminal.prototype.saveOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800905 this.savedOptions_.cursor = this.saveCursor();
rgindaa19afe22012-01-25 15:40:22 -0800906 this.savedOptions_.textAttributes = this.screen_.textAttributes.clone();
rginda87b86462011-12-14 13:48:03 -0800907};
908
rginda6d397402012-01-17 10:58:29 -0800909/**
910 * Restore cursor position and attributes.
911 *
912 * TODO(rginda): Restore attributes once we support them.
913 */
rginda87b86462011-12-14 13:48:03 -0800914hterm.Terminal.prototype.restoreOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800915 if (this.savedOptions_.cursor)
916 this.restoreCursor(this.savedOptions_.cursor);
rgindaa19afe22012-01-25 15:40:22 -0800917 if (this.savedOptions_.textAttributes)
918 this.screen_.textAttributes = this.savedOptions_.textAttributes;
rginda8ba33642011-12-14 12:31:31 -0800919};
920
921/**
922 * Interpret a sequence of characters.
923 *
924 * Incomplete escape sequences are buffered until the next call.
925 *
926 * @param {string} str Sequence of characters to interpret or pass through.
927 */
928hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800929 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800930 this.scheduleSyncCursorPosition_();
931};
932
933/**
934 * Take over the given DIV for use as the terminal display.
935 *
936 * @param {HTMLDivElement} div The div to use as the terminal display.
937 */
938hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800939 this.div_ = div;
940
rginda8ba33642011-12-14 12:31:31 -0800941 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -0700942 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -0400943 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
944 this.scrollPort_.setBackgroundPosition(
945 this.prefs_.get('background-position'));
rginda30f20f62012-04-05 16:36:19 -0700946
rginda0918b652012-04-04 11:26:24 -0700947 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -0800948
rginda9f5222b2012-03-05 11:53:28 -0800949 this.setFontSize(this.prefs_.get('font-size'));
950 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -0800951
David Reveman8f552492012-03-28 12:18:41 -0400952 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
953
rginda8ba33642011-12-14 12:31:31 -0800954 this.document_ = this.scrollPort_.getDocument();
955
rginda8e92a692012-05-20 19:37:20 -0700956 this.document_.body.firstChild.addEventListener(
957 'focus', this.onFocusChange_.bind(this, true));
958 this.document_.body.firstChild.addEventListener(
959 'blur', this.onFocusChange_.bind(this, false));
960
961 var style = this.document_.createElement('style');
962 style.textContent =
963 ('.cursor-node[focus="false"] {' +
964 ' box-sizing: border-box;' +
965 ' background-color: transparent !important;' +
966 ' border-width: 2px;' +
967 ' border-style: solid;' +
968 '}');
969 this.document_.head.appendChild(style);
970
rginda8ba33642011-12-14 12:31:31 -0800971 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -0700972 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -0800973 this.cursorNode_.style.cssText =
974 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800975 'top: -99px;' +
976 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -0800977 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
978 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda8e92a692012-05-20 19:37:20 -0700979 '-webkit-transition: opacity, background-color 100ms linear;');
980 this.setCursorColor(this.prefs_.get('cursor-color'));
rginda8ba33642011-12-14 12:31:31 -0800981 this.document_.body.appendChild(this.cursorNode_);
982
rgindade84e382012-04-20 15:39:31 -0700983 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda8ba33642011-12-14 12:31:31 -0800984 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -0800985
rginda87b86462011-12-14 13:48:03 -0800986 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -0800987 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -0800988};
989
rginda0918b652012-04-04 11:26:24 -0700990/**
991 * Return the HTML document that contains the terminal DOM nodes.
992 */
rginda87b86462011-12-14 13:48:03 -0800993hterm.Terminal.prototype.getDocument = function() {
994 return this.document_;
rginda8ba33642011-12-14 12:31:31 -0800995};
996
997/**
rginda0918b652012-04-04 11:26:24 -0700998 * Focus the terminal.
999 */
1000hterm.Terminal.prototype.focus = function() {
1001 this.scrollPort_.focus();
1002};
1003
1004/**
rginda8ba33642011-12-14 12:31:31 -08001005 * Return the HTML Element for a given row index.
1006 *
1007 * This is a method from the RowProvider interface. The ScrollPort uses
1008 * it to fetch rows on demand as they are scrolled into view.
1009 *
1010 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1011 * pairs to conserve memory.
1012 *
1013 * @param {integer} index The zero-based row index, measured relative to the
1014 * start of the scrollback buffer. On-screen rows will always have the
1015 * largest indicies.
1016 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1017 */
1018hterm.Terminal.prototype.getRowNode = function(index) {
1019 if (index < this.scrollbackRows_.length)
1020 return this.scrollbackRows_[index];
1021
1022 var screenIndex = index - this.scrollbackRows_.length;
1023 return this.screen_.rowsArray[screenIndex];
1024};
1025
1026/**
1027 * Return the text content for a given range of rows.
1028 *
1029 * This is a method from the RowProvider interface. The ScrollPort uses
1030 * it to fetch text content on demand when the user attempts to copy their
1031 * selection to the clipboard.
1032 *
1033 * @param {integer} start The zero-based row index to start from, measured
1034 * relative to the start of the scrollback buffer. On-screen rows will
1035 * always have the largest indicies.
1036 * @param {integer} end The zero-based row index to end on, measured
1037 * relative to the start of the scrollback buffer.
1038 * @return {string} A single string containing the text value of the range of
1039 * rows. Lines will be newline delimited, with no trailing newline.
1040 */
1041hterm.Terminal.prototype.getRowsText = function(start, end) {
1042 var ary = [];
1043 for (var i = start; i < end; i++) {
1044 var node = this.getRowNode(i);
1045 ary.push(node.textContent);
1046 }
1047
1048 return ary.join('\n');
1049};
1050
1051/**
1052 * Return the text content for a given row.
1053 *
1054 * This is a method from the RowProvider interface. The ScrollPort uses
1055 * it to fetch text content on demand when the user attempts to copy their
1056 * selection to the clipboard.
1057 *
1058 * @param {integer} index The zero-based row index to return, measured
1059 * relative to the start of the scrollback buffer. On-screen rows will
1060 * always have the largest indicies.
1061 * @return {string} A string containing the text value of the selected row.
1062 */
1063hterm.Terminal.prototype.getRowText = function(index) {
1064 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001065 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001066};
1067
1068/**
1069 * Return the total number of rows in the addressable screen and in the
1070 * scrollback buffer of this terminal.
1071 *
1072 * This is a method from the RowProvider interface. The ScrollPort uses
1073 * it to compute the size of the scrollbar.
1074 *
1075 * @return {integer} The number of rows in this terminal.
1076 */
1077hterm.Terminal.prototype.getRowCount = function() {
1078 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1079};
1080
1081/**
1082 * Create DOM nodes for new rows and append them to the end of the terminal.
1083 *
1084 * This is the only correct way to add a new DOM node for a row. Notice that
1085 * the new row is appended to the bottom of the list of rows, and does not
1086 * require renumbering (of the rowIndex property) of previous rows.
1087 *
1088 * If you think you want a new blank row somewhere in the middle of the
1089 * terminal, look into moveRows_().
1090 *
1091 * This method does not pay attention to vtScrollTop/Bottom, since you should
1092 * be using moveRows() in cases where they would matter.
1093 *
1094 * The cursor will be positioned at column 0 of the first inserted line.
1095 */
1096hterm.Terminal.prototype.appendRows_ = function(count) {
1097 var cursorRow = this.screen_.rowsArray.length;
1098 var offset = this.scrollbackRows_.length + cursorRow;
1099 for (var i = 0; i < count; i++) {
1100 var row = this.document_.createElement('x-row');
1101 row.appendChild(this.document_.createTextNode(''));
1102 row.rowIndex = offset + i;
1103 this.screen_.pushRow(row);
1104 }
1105
1106 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1107 if (extraRows > 0) {
1108 var ary = this.screen_.shiftRows(extraRows);
1109 Array.prototype.push.apply(this.scrollbackRows_, ary);
1110 this.scheduleScrollDown_();
1111 }
1112
1113 if (cursorRow >= this.screen_.rowsArray.length)
1114 cursorRow = this.screen_.rowsArray.length - 1;
1115
rginda87b86462011-12-14 13:48:03 -08001116 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001117};
1118
1119/**
1120 * Relocate rows from one part of the addressable screen to another.
1121 *
1122 * This is used to recycle rows during VT scrolls (those which are driven
1123 * by VT commands, rather than by the user manipulating the scrollbar.)
1124 *
1125 * In this case, the blank lines scrolled into the scroll region are made of
1126 * the nodes we scrolled off. These have their rowIndex properties carefully
1127 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001128 */
1129hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1130 var ary = this.screen_.removeRows(fromIndex, count);
1131 this.screen_.insertRows(toIndex, ary);
1132
1133 var start, end;
1134 if (fromIndex < toIndex) {
1135 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001136 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001137 } else {
1138 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001139 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001140 }
1141
1142 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001143 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001144};
1145
1146/**
1147 * Renumber the rowIndex property of the given range of rows.
1148 *
1149 * The start and end indicies are relative to the screen, not the scrollback.
1150 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001151 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001152 * no need to renumber scrollback rows.
1153 */
1154hterm.Terminal.prototype.renumberRows_ = function(start, end) {
1155 var offset = this.scrollbackRows_.length;
1156 for (var i = start; i < end; i++) {
1157 this.screen_.rowsArray[i].rowIndex = offset + i;
1158 }
1159};
1160
1161/**
1162 * Print a string to the terminal.
1163 *
1164 * This respects the current insert and wraparound modes. It will add new lines
1165 * to the end of the terminal, scrolling off the top into the scrollback buffer
1166 * if necessary.
1167 *
1168 * The string is *not* parsed for escape codes. Use the interpret() method if
1169 * that's what you're after.
1170 *
1171 * @param{string} str The string to print.
1172 */
1173hterm.Terminal.prototype.print = function(str) {
rgindaa19afe22012-01-25 15:40:22 -08001174 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
1175 this.newLine();
rginda2312fff2012-01-05 16:20:52 -08001176
rgindaa19afe22012-01-25 15:40:22 -08001177 if (this.options_.insertMode) {
1178 this.screen_.insertString(str);
1179 } else {
1180 this.screen_.overwriteString(str);
1181 }
1182
1183 var overflow = this.screen_.maybeClipCurrentRow();
1184
1185 if (this.options_.wraparound && overflow) {
1186 var lastColumn;
1187
1188 do {
rginda35c456b2012-02-09 17:29:05 -08001189 this.newLine();
1190 lastColumn = overflow.characterLength;
rgindaa19afe22012-01-25 15:40:22 -08001191
1192 if (!this.options_.insertMode)
1193 this.screen_.deleteChars(overflow.characterLength);
1194
1195 this.screen_.prependNodes(overflow);
rgindaa19afe22012-01-25 15:40:22 -08001196
1197 overflow = this.screen_.maybeClipCurrentRow();
1198 } while (overflow);
1199
1200 this.setCursorColumn(lastColumn);
1201 }
rginda8ba33642011-12-14 12:31:31 -08001202
1203 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001204
rginda9f5222b2012-03-05 11:53:28 -08001205 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001206 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001207};
1208
1209/**
rginda87b86462011-12-14 13:48:03 -08001210 * Set the VT scroll region.
1211 *
rginda87b86462011-12-14 13:48:03 -08001212 * This also resets the cursor position to the absolute (0, 0) position, since
1213 * that's what xterm appears to do.
1214 *
1215 * @param {integer} scrollTop The zero-based top of the scroll region.
1216 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1217 * inclusive.
1218 */
1219hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1220 this.vtScrollTop_ = scrollTop;
1221 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001222};
1223
1224/**
rginda8ba33642011-12-14 12:31:31 -08001225 * Return the top row index according to the VT.
1226 *
1227 * This will return 0 unless the terminal has been told to restrict scrolling
1228 * to some lower row. It is used for some VT cursor positioning and scrolling
1229 * commands.
1230 *
1231 * @return {integer} The topmost row in the terminal's scroll region.
1232 */
1233hterm.Terminal.prototype.getVTScrollTop = function() {
1234 if (this.vtScrollTop_ != null)
1235 return this.vtScrollTop_;
1236
1237 return 0;
rginda87b86462011-12-14 13:48:03 -08001238};
rginda8ba33642011-12-14 12:31:31 -08001239
1240/**
1241 * Return the bottom row index according to the VT.
1242 *
1243 * This will return the height of the terminal unless the it has been told to
1244 * restrict scrolling to some higher row. It is used for some VT cursor
1245 * positioning and scrolling commands.
1246 *
1247 * @return {integer} The bottommost row in the terminal's scroll region.
1248 */
1249hterm.Terminal.prototype.getVTScrollBottom = function() {
1250 if (this.vtScrollBottom_ != null)
1251 return this.vtScrollBottom_;
1252
rginda87b86462011-12-14 13:48:03 -08001253 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001254}
1255
1256/**
1257 * Process a '\n' character.
1258 *
1259 * If the cursor is on the final row of the terminal this will append a new
1260 * blank row to the screen and scroll the topmost row into the scrollback
1261 * buffer.
1262 *
1263 * Otherwise, this moves the cursor to column zero of the next row.
1264 */
1265hterm.Terminal.prototype.newLine = function() {
1266 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001267 // If we're at the end of the screen we need to append a new line and
1268 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001269 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001270 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1271 // End of the scroll region does not affect the scrollback buffer.
1272 this.vtScrollUp(1);
1273 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001274 } else {
rginda87b86462011-12-14 13:48:03 -08001275 // Anywhere else in the screen just moves the cursor.
1276 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001277 }
1278};
1279
1280/**
1281 * Like newLine(), except maintain the cursor column.
1282 */
1283hterm.Terminal.prototype.lineFeed = function() {
1284 var column = this.screen_.cursorPosition.column;
1285 this.newLine();
1286 this.setCursorColumn(column);
1287};
1288
1289/**
rginda87b86462011-12-14 13:48:03 -08001290 * If autoCarriageReturn is set then newLine(), else lineFeed().
1291 */
1292hterm.Terminal.prototype.formFeed = function() {
1293 if (this.options_.autoCarriageReturn) {
1294 this.newLine();
1295 } else {
1296 this.lineFeed();
1297 }
1298};
1299
1300/**
1301 * Move the cursor up one row, possibly inserting a blank line.
1302 *
1303 * The cursor column is not changed.
1304 */
1305hterm.Terminal.prototype.reverseLineFeed = function() {
1306 var scrollTop = this.getVTScrollTop();
1307 var currentRow = this.screen_.cursorPosition.row;
1308
1309 if (currentRow == scrollTop) {
1310 this.insertLines(1);
1311 } else {
1312 this.setAbsoluteCursorRow(currentRow - 1);
1313 }
1314};
1315
1316/**
rginda8ba33642011-12-14 12:31:31 -08001317 * Replace all characters to the left of the current cursor with the space
1318 * character.
1319 *
1320 * TODO(rginda): This should probably *remove* the characters (not just replace
1321 * with a space) if there are no characters at or beyond the current cursor
1322 * position. Once it does that, it'll have the same text-attribute related
1323 * issues as hterm.Screen.prototype.clearCursorRow :/
1324 */
1325hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001326 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001327 this.setCursorColumn(0);
rginda87b86462011-12-14 13:48:03 -08001328 this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1));
1329 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001330};
1331
1332/**
David Benjamin684a9b72012-05-01 17:19:58 -04001333 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001334 *
1335 * The cursor position is unchanged.
1336 *
1337 * TODO(rginda): Test that this works even when the cursor is positioned beyond
1338 * the end of the text.
1339 *
1340 * TODO(rginda): This likely has text-attribute related troubles similar to the
1341 * todo on hterm.Screen.prototype.clearCursorRow.
David Benjamin684a9b72012-05-01 17:19:58 -04001342 *
1343 * TODO(davidben): Probably better to not add the whitespace to the clipboard
1344 * if erasing to the end of the drawn portion of the line. That said, xterm
1345 * behaves the same here.
rginda8ba33642011-12-14 12:31:31 -08001346 */
1347hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001348 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001349
rginda87b86462011-12-14 13:48:03 -08001350 var maxCount = this.screenSize.width - cursor.column;
David Benjamin684a9b72012-05-01 17:19:58 -04001351 if (opt_count === undefined || opt_count >= maxCount) {
1352 this.screen_.deleteChars(maxCount);
1353 } else {
1354 this.screen_.overwriteString(hterm.getWhitespace(opt_count));
1355 }
rginda87b86462011-12-14 13:48:03 -08001356 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001357 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001358};
1359
1360/**
1361 * Erase the current line.
1362 *
1363 * The cursor position is unchanged.
1364 *
1365 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1366 * has a text-attribute related TODO.
1367 */
1368hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001369 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001370 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001371 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001372 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001373};
1374
1375/**
David Benjamina08d78f2012-05-05 00:28:49 -04001376 * Erase all characters from the start of the screen to the current cursor
1377 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001378 *
1379 * The cursor position is unchanged.
1380 *
1381 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1382 * has a text-attribute related TODO.
1383 */
1384hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001385 var cursor = this.saveCursor();
1386
1387 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001388
David Benjamina08d78f2012-05-05 00:28:49 -04001389 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001390 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001391 this.screen_.clearCursorRow();
1392 }
1393
rginda87b86462011-12-14 13:48:03 -08001394 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001395 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001396};
1397
1398/**
1399 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001400 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001401 *
1402 * The cursor position is unchanged.
1403 *
1404 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1405 * has a text-attribute related TODO.
1406 */
1407hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001408 var cursor = this.saveCursor();
1409
1410 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001411
David Benjamina08d78f2012-05-05 00:28:49 -04001412 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001413 for (var i = cursor.row + 1; i <= bottom; i++) {
1414 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001415 this.screen_.clearCursorRow();
1416 }
1417
rginda87b86462011-12-14 13:48:03 -08001418 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001419 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001420};
1421
1422/**
1423 * Fill the terminal with a given character.
1424 *
1425 * This methods does not respect the VT scroll region.
1426 *
1427 * @param {string} ch The character to use for the fill.
1428 */
1429hterm.Terminal.prototype.fill = function(ch) {
1430 var cursor = this.saveCursor();
1431
1432 this.setAbsoluteCursorPosition(0, 0);
1433 for (var row = 0; row < this.screenSize.height; row++) {
1434 for (var col = 0; col < this.screenSize.width; col++) {
1435 this.setAbsoluteCursorPosition(row, col);
1436 this.screen_.overwriteString(ch);
1437 }
1438 }
1439
1440 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001441};
1442
1443/**
rginda9ea433c2012-03-16 11:57:00 -07001444 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001445 *
rginda9ea433c2012-03-16 11:57:00 -07001446 * This does not respect the scroll region.
1447 *
1448 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1449 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001450 *
1451 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1452 * has a text-attribute related TODO.
1453 */
rginda9ea433c2012-03-16 11:57:00 -07001454hterm.Terminal.prototype.clearHome = function(opt_screen) {
1455 var screen = opt_screen || this.screen_;
1456 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001457
rginda11057d52012-04-25 12:29:56 -07001458 if (bottom == 0) {
1459 // Empty screen, nothing to do.
1460 return;
1461 }
1462
rgindae4d29232012-01-19 10:47:13 -08001463 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001464 screen.setCursorPosition(i, 0);
1465 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001466 }
1467
rginda9ea433c2012-03-16 11:57:00 -07001468 screen.setCursorPosition(0, 0);
1469};
1470
1471/**
1472 * Erase the entire display without changing the cursor position.
1473 *
1474 * The cursor position is unchanged. This does not respect the scroll
1475 * region.
1476 *
1477 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1478 * to the current screen.
1479 *
1480 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1481 * has a text-attribute related TODO.
1482 */
1483hterm.Terminal.prototype.clear = function(opt_screen) {
1484 var screen = opt_screen || this.screen_;
1485 var cursor = screen.cursorPosition.clone();
1486 this.clearHome(screen);
1487 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001488};
1489
1490/**
1491 * VT command to insert lines at the current cursor row.
1492 *
1493 * This respects the current scroll region. Rows pushed off the bottom are
1494 * lost (they won't show up in the scrollback buffer).
1495 *
1496 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1497 * has a text-attribute related TODO.
1498 *
1499 * @param {integer} count The number of lines to insert.
1500 */
1501hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001502 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001503
1504 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001505 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001506
rgindae4d29232012-01-19 10:47:13 -08001507 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001508 if (start != cursor.row)
1509 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001510
1511 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001512 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001513 this.screen_.clearCursorRow();
1514 }
1515
rginda87b86462011-12-14 13:48:03 -08001516 cursor.column = 0;
1517 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001518};
1519
1520/**
1521 * VT command to delete lines at the current cursor row.
1522 *
1523 * New rows are added to the bottom of scroll region to take their place. New
1524 * rows are strictly there to take up space and have no content or style.
1525 */
1526hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001527 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001528
rginda87b86462011-12-14 13:48:03 -08001529 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001530 var bottom = this.getVTScrollBottom();
1531
rginda87b86462011-12-14 13:48:03 -08001532 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001533 count = Math.min(count, maxCount);
1534
rginda87b86462011-12-14 13:48:03 -08001535 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001536 if (count != maxCount)
1537 this.moveRows_(top, count, moveStart);
1538
1539 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001540 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001541 this.screen_.clearCursorRow();
1542 }
1543
rginda87b86462011-12-14 13:48:03 -08001544 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001545 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001546};
1547
1548/**
1549 * Inserts the given number of spaces at the current cursor position.
1550 *
rginda87b86462011-12-14 13:48:03 -08001551 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001552 */
1553hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001554 var cursor = this.saveCursor();
1555
rginda0f5c0292012-01-13 11:00:13 -08001556 var ws = hterm.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001557 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001558 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001559
1560 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001561 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001562};
1563
1564/**
1565 * Forward-delete the specified number of characters starting at the cursor
1566 * position.
1567 *
1568 * @param {integer} count The number of characters to delete.
1569 */
1570hterm.Terminal.prototype.deleteChars = function(count) {
1571 this.screen_.deleteChars(count);
David Benjamin54e8bf62012-06-01 22:31:40 -04001572 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001573};
1574
1575/**
1576 * Shift rows in the scroll region upwards by a given number of lines.
1577 *
1578 * New rows are inserted at the bottom of the scroll region to fill the
1579 * vacated rows. The new rows not filled out with the current text attributes.
1580 *
1581 * This function does not affect the scrollback rows at all. Rows shifted
1582 * off the top are lost.
1583 *
rginda87b86462011-12-14 13:48:03 -08001584 * The cursor position is not altered.
1585 *
rginda8ba33642011-12-14 12:31:31 -08001586 * @param {integer} count The number of rows to scroll.
1587 */
1588hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001589 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001590
rginda87b86462011-12-14 13:48:03 -08001591 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001592 this.deleteLines(count);
1593
rginda87b86462011-12-14 13:48:03 -08001594 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001595};
1596
1597/**
1598 * Shift rows below the cursor down by a given number of lines.
1599 *
1600 * This function respects the current scroll region.
1601 *
1602 * New rows are inserted at the top of the scroll region to fill the
1603 * vacated rows. The new rows not filled out with the current text attributes.
1604 *
1605 * This function does not affect the scrollback rows at all. Rows shifted
1606 * off the bottom are lost.
1607 *
1608 * @param {integer} count The number of rows to scroll.
1609 */
1610hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001611 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001612
rginda87b86462011-12-14 13:48:03 -08001613 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001614 this.insertLines(opt_count);
1615
rginda87b86462011-12-14 13:48:03 -08001616 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001617};
1618
rginda87b86462011-12-14 13:48:03 -08001619
rginda8ba33642011-12-14 12:31:31 -08001620/**
1621 * Set the cursor position.
1622 *
1623 * The cursor row is relative to the scroll region if the terminal has
1624 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1625 *
1626 * @param {integer} row The new zero-based cursor row.
1627 * @param {integer} row The new zero-based cursor column.
1628 */
1629hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1630 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001631 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001632 } else {
rginda87b86462011-12-14 13:48:03 -08001633 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001634 }
rginda87b86462011-12-14 13:48:03 -08001635};
rginda8ba33642011-12-14 12:31:31 -08001636
rginda87b86462011-12-14 13:48:03 -08001637hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1638 var scrollTop = this.getVTScrollTop();
1639 row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
rginda2312fff2012-01-05 16:20:52 -08001640 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001641 this.screen_.setCursorPosition(row, column);
1642};
1643
1644hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rginda2312fff2012-01-05 16:20:52 -08001645 row = hterm.clamp(row, 0, this.screenSize.height - 1);
1646 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001647 this.screen_.setCursorPosition(row, column);
1648};
1649
1650/**
1651 * Set the cursor column.
1652 *
1653 * @param {integer} column The new zero-based cursor column.
1654 */
1655hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001656 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001657};
1658
1659/**
1660 * Return the cursor column.
1661 *
1662 * @return {integer} The zero-based cursor column.
1663 */
1664hterm.Terminal.prototype.getCursorColumn = function() {
1665 return this.screen_.cursorPosition.column;
1666};
1667
1668/**
1669 * Set the cursor row.
1670 *
1671 * The cursor row is relative to the scroll region if the terminal has
1672 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1673 *
1674 * @param {integer} row The new cursor row.
1675 */
rginda87b86462011-12-14 13:48:03 -08001676hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1677 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001678};
1679
1680/**
1681 * Return the cursor row.
1682 *
1683 * @return {integer} The zero-based cursor row.
1684 */
1685hterm.Terminal.prototype.getCursorRow = function(row) {
1686 return this.screen_.cursorPosition.row;
1687};
1688
1689/**
1690 * Request that the ScrollPort redraw itself soon.
1691 *
1692 * The redraw will happen asynchronously, soon after the call stack winds down.
1693 * Multiple calls will be coalesced into a single redraw.
1694 */
1695hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001696 if (this.timeouts_.redraw)
1697 return;
rginda8ba33642011-12-14 12:31:31 -08001698
1699 var self = this;
rginda87b86462011-12-14 13:48:03 -08001700 this.timeouts_.redraw = setTimeout(function() {
1701 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001702 self.scrollPort_.redraw_();
1703 }, 0);
1704};
1705
1706/**
1707 * Request that the ScrollPort be scrolled to the bottom.
1708 *
1709 * The scroll will happen asynchronously, soon after the call stack winds down.
1710 * Multiple calls will be coalesced into a single scroll.
1711 *
1712 * This affects the scrollbar position of the ScrollPort, and has nothing to
1713 * do with the VT scroll commands.
1714 */
1715hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1716 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001717 return;
rginda8ba33642011-12-14 12:31:31 -08001718
1719 var self = this;
1720 this.timeouts_.scrollDown = setTimeout(function() {
1721 delete self.timeouts_.scrollDown;
1722 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1723 }, 10);
1724};
1725
1726/**
1727 * Move the cursor up a specified number of rows.
1728 *
1729 * @param {integer} count The number of rows to move the cursor.
1730 */
1731hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001732 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001733};
1734
1735/**
1736 * Move the cursor down a specified number of rows.
1737 *
1738 * @param {integer} count The number of rows to move the cursor.
1739 */
1740hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001741 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001742 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1743 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1744 this.screenSize.height - 1);
1745
1746 var row = hterm.clamp(this.screen_.cursorPosition.row + count,
1747 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001748 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001749};
1750
1751/**
1752 * Move the cursor left a specified number of columns.
1753 *
1754 * @param {integer} count The number of columns to move the cursor.
1755 */
1756hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001757 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001758};
1759
1760/**
1761 * Move the cursor right a specified number of columns.
1762 *
1763 * @param {integer} count The number of columns to move the cursor.
1764 */
1765hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001766 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001767 var column = hterm.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001768 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001769 this.setCursorColumn(column);
1770};
1771
1772/**
1773 * Reverse the foreground and background colors of the terminal.
1774 *
1775 * This only affects text that was drawn with no attributes.
1776 *
1777 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1778 * been drawn with attributes that happen to coincide with the default
1779 * 'no-attribute' colors. My guess is probably not.
1780 */
1781hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001782 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001783 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001784 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1785 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001786 } else {
rginda9f5222b2012-03-05 11:53:28 -08001787 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1788 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001789 }
1790};
1791
1792/**
rginda87b86462011-12-14 13:48:03 -08001793 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001794 */
1795hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001796 if (this.bellAudio_.getAttribute('src'))
1797 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001798
rginda6d397402012-01-17 10:58:29 -08001799 this.cursorNode_.style.backgroundColor =
1800 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001801
1802 var self = this;
1803 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001804 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001805 }, 200);
rginda87b86462011-12-14 13:48:03 -08001806};
1807
1808/**
rginda8ba33642011-12-14 12:31:31 -08001809 * Set the origin mode bit.
1810 *
1811 * If origin mode is on, certain VT cursor and scrolling commands measure their
1812 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1813 * to the top of the addressable screen.
1814 *
1815 * Defaults to off.
1816 *
1817 * @param {boolean} state True to set origin mode, false to unset.
1818 */
1819hterm.Terminal.prototype.setOriginMode = function(state) {
1820 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001821 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001822};
1823
1824/**
1825 * Set the insert mode bit.
1826 *
1827 * If insert mode is on, existing text beyond the cursor position will be
1828 * shifted right to make room for new text. Otherwise, new text overwrites
1829 * any existing text.
1830 *
1831 * Defaults to off.
1832 *
1833 * @param {boolean} state True to set insert mode, false to unset.
1834 */
1835hterm.Terminal.prototype.setInsertMode = function(state) {
1836 this.options_.insertMode = state;
1837};
1838
1839/**
rginda87b86462011-12-14 13:48:03 -08001840 * Set the auto carriage return bit.
1841 *
1842 * If auto carriage return is on then a formfeed character is interpreted
1843 * as a newline, otherwise it's the same as a linefeed. The difference boils
1844 * down to whether or not the cursor column is reset.
1845 */
1846hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1847 this.options_.autoCarriageReturn = state;
1848};
1849
1850/**
rginda8ba33642011-12-14 12:31:31 -08001851 * Set the wraparound mode bit.
1852 *
1853 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1854 * to the start of the following row. Otherwise, the cursor is clamped to the
1855 * end of the screen and attempts to write past it are ignored.
1856 *
1857 * Defaults to on.
1858 *
1859 * @param {boolean} state True to set wraparound mode, false to unset.
1860 */
1861hterm.Terminal.prototype.setWraparound = function(state) {
1862 this.options_.wraparound = state;
1863};
1864
1865/**
1866 * Set the reverse-wraparound mode bit.
1867 *
1868 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1869 * to the end of the previous row. Otherwise, the cursor is clamped to column
1870 * 0.
1871 *
1872 * Defaults to off.
1873 *
1874 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1875 */
1876hterm.Terminal.prototype.setReverseWraparound = function(state) {
1877 this.options_.reverseWraparound = state;
1878};
1879
1880/**
1881 * Selects between the primary and alternate screens.
1882 *
1883 * If alternate mode is on, the alternate screen is active. Otherwise the
1884 * primary screen is active.
1885 *
1886 * Swapping screens has no effect on the scrollback buffer.
1887 *
1888 * Each screen maintains its own cursor position.
1889 *
1890 * Defaults to off.
1891 *
1892 * @param {boolean} state True to set alternate mode, false to unset.
1893 */
1894hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001895 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001896 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1897
rginda35c456b2012-02-09 17:29:05 -08001898 if (this.screen_.rowsArray.length &&
1899 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
1900 // If the screen changed sizes while we were away, our rowIndexes may
1901 // be incorrect.
1902 var offset = this.scrollbackRows_.length;
1903 var ary = this.screen_.rowsArray;
1904 for (i = 0; i < ary.length; i++) {
1905 ary[i].rowIndex = offset + i;
1906 }
1907 }
rginda8ba33642011-12-14 12:31:31 -08001908
rginda35c456b2012-02-09 17:29:05 -08001909 this.realizeWidth_(this.screenSize.width);
1910 this.realizeHeight_(this.screenSize.height);
1911 this.scrollPort_.syncScrollHeight();
1912 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001913
rginda6d397402012-01-17 10:58:29 -08001914 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08001915 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08001916};
1917
1918/**
1919 * Set the cursor-blink mode bit.
1920 *
1921 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1922 * a visible cursor does not blink.
1923 *
1924 * You should make sure to turn blinking off if you're going to dispose of a
1925 * terminal, otherwise you'll leak a timeout.
1926 *
1927 * Defaults to on.
1928 *
1929 * @param {boolean} state True to set cursor-blink mode, false to unset.
1930 */
1931hterm.Terminal.prototype.setCursorBlink = function(state) {
1932 this.options_.cursorBlink = state;
1933
1934 if (!state && this.timeouts_.cursorBlink) {
1935 clearTimeout(this.timeouts_.cursorBlink);
1936 delete this.timeouts_.cursorBlink;
1937 }
1938
1939 if (this.options_.cursorVisible)
1940 this.setCursorVisible(true);
1941};
1942
1943/**
1944 * Set the cursor-visible mode bit.
1945 *
1946 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1947 *
1948 * Defaults to on.
1949 *
1950 * @param {boolean} state True to set cursor-visible mode, false to unset.
1951 */
1952hterm.Terminal.prototype.setCursorVisible = function(state) {
1953 this.options_.cursorVisible = state;
1954
1955 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001956 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001957 return;
1958 }
1959
rginda87b86462011-12-14 13:48:03 -08001960 this.syncCursorPosition_();
1961
1962 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001963
1964 if (this.options_.cursorBlink) {
1965 if (this.timeouts_.cursorBlink)
1966 return;
1967
1968 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1969 500);
1970 } else {
1971 if (this.timeouts_.cursorBlink) {
1972 clearTimeout(this.timeouts_.cursorBlink);
1973 delete this.timeouts_.cursorBlink;
1974 }
1975 }
1976};
1977
1978/**
rginda87b86462011-12-14 13:48:03 -08001979 * Synchronizes the visible cursor and document selection with the current
1980 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08001981 */
1982hterm.Terminal.prototype.syncCursorPosition_ = function() {
1983 var topRowIndex = this.scrollPort_.getTopRowIndex();
1984 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
1985 var cursorRowIndex = this.scrollbackRows_.length +
1986 this.screen_.cursorPosition.row;
1987
1988 if (cursorRowIndex > bottomRowIndex) {
1989 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08001990 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08001991 return;
1992 }
1993
rginda35c456b2012-02-09 17:29:05 -08001994 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
1995 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
1996
rginda8ba33642011-12-14 12:31:31 -08001997 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08001998 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
1999 'px';
2000 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2001 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002002
2003 this.cursorNode_.setAttribute('title',
2004 '(' + this.screen_.cursorPosition.row +
2005 ', ' + this.screen_.cursorPosition.column +
2006 ')');
2007
2008 // Update the caret for a11y purposes.
2009 var selection = this.document_.getSelection();
2010 if (selection && selection.isCollapsed)
2011 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002012};
2013
2014/**
2015 * Synchronizes the visible cursor with the current cursor coordinates.
2016 *
2017 * The sync will happen asynchronously, soon after the call stack winds down.
2018 * Multiple calls will be coalesced into a single sync.
2019 */
2020hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2021 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002022 return;
rginda8ba33642011-12-14 12:31:31 -08002023
2024 var self = this;
2025 this.timeouts_.syncCursor = setTimeout(function() {
2026 self.syncCursorPosition_();
2027 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002028 }, 0);
2029};
2030
rgindacc2996c2012-02-24 14:59:31 -08002031/**
rgindaf522ce02012-04-17 17:49:17 -07002032 * Show or hide the zoom warning.
2033 *
2034 * The zoom warning is a message warning the user that their browser zoom must
2035 * be set to 100% in order for hterm to function properly.
2036 *
2037 * @param {boolean} state True to show the message, false to hide it.
2038 */
2039hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2040 if (!this.zoomWarningNode_) {
2041 if (!state)
2042 return;
2043
2044 this.zoomWarningNode_ = this.document_.createElement('div');
2045 this.zoomWarningNode_.style.cssText = (
2046 'color: black;' +
2047 'background-color: #ff2222;' +
2048 'font-size: large;' +
2049 'border-radius: 8px;' +
2050 'opacity: 0.75;' +
2051 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2052 'top: 0.5em;' +
2053 'right: 1.2em;' +
2054 'position: absolute;' +
2055 '-webkit-text-size-adjust: none;' +
2056 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002057 }
2058
rgindade84e382012-04-20 15:39:31 -07002059 this.zoomWarningNode_.textContent = hterm.msg('ZOOM_WARNING') ||
2060 ('!! ' + parseInt(this.scrollPort_.characterSize.zoomFactor * 100) +
2061 '% !!');
rgindaf522ce02012-04-17 17:49:17 -07002062 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2063
2064 if (state) {
2065 if (!this.zoomWarningNode_.parentNode)
2066 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2067 } else if (this.zoomWarningNode_.parentNode) {
2068 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2069 }
2070};
2071
2072/**
rgindacc2996c2012-02-24 14:59:31 -08002073 * Show the terminal overlay for a given amount of time.
2074 *
2075 * The terminal overlay appears in inverse video in a large font, centered
2076 * over the terminal. You should probably keep the overlay message brief,
2077 * since it's in a large font and you probably aren't going to check the size
2078 * of the terminal first.
2079 *
2080 * @param {string} msg The text (not HTML) message to display in the overlay.
2081 * @param {number} opt_timeout The amount of time to wait before fading out
2082 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2083 * stay up forever (or until the next overlay).
2084 */
2085hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002086 if (!this.overlayNode_) {
2087 if (!this.div_)
2088 return;
2089
2090 this.overlayNode_ = this.document_.createElement('div');
2091 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002092 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002093 'font-size: xx-large;' +
2094 'opacity: 0.75;' +
2095 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2096 'position: absolute;' +
2097 '-webkit-user-select: none;' +
2098 '-webkit-transition: opacity 180ms ease-in;');
2099 }
2100
rginda9f5222b2012-03-05 11:53:28 -08002101 this.overlayNode_.style.color = this.prefs_.get('background-color');
2102 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2103 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2104
rgindaf0090c92012-02-10 14:58:52 -08002105 this.overlayNode_.textContent = msg;
2106 this.overlayNode_.style.opacity = '0.75';
2107
2108 if (!this.overlayNode_.parentNode)
2109 this.div_.appendChild(this.overlayNode_);
2110
2111 this.overlayNode_.style.top = (
2112 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
2113 this.overlayNode_.style.left = (
2114 this.div_.clientWidth - this.overlayNode_.clientWidth -
2115 this.scrollbarWidthPx) / 2;
2116
2117 var self = this;
2118
2119 if (this.overlayTimeout_)
2120 clearTimeout(this.overlayTimeout_);
2121
rgindacc2996c2012-02-24 14:59:31 -08002122 if (opt_timeout === null)
2123 return;
2124
rgindaf0090c92012-02-10 14:58:52 -08002125 this.overlayTimeout_ = setTimeout(function() {
2126 self.overlayNode_.style.opacity = '0';
2127 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002128 if (self.overlayNode_.parentNode)
2129 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002130 self.overlayTimeout_ = null;
2131 self.overlayNode_.style.opacity = '0.75';
2132 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002133 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002134};
2135
2136hterm.Terminal.prototype.overlaySize = function() {
2137 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2138};
2139
rginda87b86462011-12-14 13:48:03 -08002140/**
2141 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2142 *
2143 * @param {string} string The VT string representing the keystroke.
2144 */
2145hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002146 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002147 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2148
2149 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08002150};
2151
2152/**
rginda8e92a692012-05-20 19:37:20 -07002153 * React when focus changes.
2154 */
2155hterm.Terminal.prototype.onFocusChange_ = function(state) {
2156 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
2157};
2158
2159/**
rginda8ba33642011-12-14 12:31:31 -08002160 * React when the ScrollPort is scrolled.
2161 */
2162hterm.Terminal.prototype.onScroll_ = function() {
2163 this.scheduleSyncCursorPosition_();
2164};
2165
2166/**
rginda9846e2f2012-01-27 13:53:33 -08002167 * React when text is pasted into the scrollPort.
2168 */
2169hterm.Terminal.prototype.onPaste_ = function(e) {
2170 this.io.onVTKeystroke(e.text);
2171};
2172
2173/**
rginda8ba33642011-12-14 12:31:31 -08002174 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002175 *
2176 * Note: This function should not directly contain code that alters the internal
2177 * state of the terminal. That kind of code belongs in realizeWidth or
2178 * realizeHeight, so that it can be executed synchronously in the case of a
2179 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002180 */
2181hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002182 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002183 this.scrollPort_.characterSize.width);
2184 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2185 this.scrollPort_.characterSize.height);
2186
2187 if (!(columnCount || rowCount)) {
2188 // We avoid these situations since they happen sometimes when the terminal
2189 // gets removed from the document, and we can't deal with that.
2190 return;
2191 }
2192
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002193 this.realizeSize_(columnCount, rowCount);
rgindac9bc5502012-01-18 11:48:44 -08002194 this.scheduleSyncCursorPosition_();
rgindaf522ce02012-04-17 17:49:17 -07002195 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaf0090c92012-02-10 14:58:52 -08002196 this.overlaySize();
rginda8ba33642011-12-14 12:31:31 -08002197};
2198
2199/**
2200 * Service the cursor blink timeout.
2201 */
2202hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002203 if (this.cursorNode_.style.opacity == '0') {
2204 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002205 } else {
rginda87b86462011-12-14 13:48:03 -08002206 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002207 }
2208};
David Reveman8f552492012-03-28 12:18:41 -04002209
2210/**
2211 * Set the scrollbar-visible mode bit.
2212 *
2213 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2214 * Otherwise it will not.
2215 *
2216 * Defaults to on.
2217 *
2218 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2219 */
2220hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2221 this.scrollPort_.setScrollbarVisible(state);
2222};