blob: bfb4516dc511515b91473053a0085237c8e91b42 [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
rginda8ba33642011-12-14 12:31:31 -080062 // The VT's notion of the top and bottom rows. Used during some VT
63 // cursor positioning and scrolling commands.
64 this.vtScrollTop_ = null;
65 this.vtScrollBottom_ = null;
66
67 // The DIV element for the visible cursor.
68 this.cursorNode_ = null;
69
rginda9f5222b2012-03-05 11:53:28 -080070 // These prefs are cached so we don't have to read from local storage with
71 // each output and keystroke.
72 this.scrollOnOutput_ = this.prefs_.get('scroll-on-output');
73 this.scrollOnKeystroke_ = this.prefs_.get('scroll-on-keystroke');
74
rgindaf0090c92012-02-10 14:58:52 -080075 // Terminal bell sound.
76 this.bellAudio_ = this.document_.createElement('audio');
rginda9f5222b2012-03-05 11:53:28 -080077 this.bellAudio_.setAttribute('src', this.prefs_.get('audible-bell-sound'));
rgindaf0090c92012-02-10 14:58:52 -080078 this.bellAudio_.setAttribute('preload', 'auto');
79
rginda6d397402012-01-17 10:58:29 -080080 // Cursor position and attributes saved with DECSC.
81 this.savedOptions_ = {};
82
rginda8ba33642011-12-14 12:31:31 -080083 // The current mode bits for the terminal.
84 this.options_ = new hterm.Options();
85
86 // Timeouts we might need to clear.
87 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -080088
89 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -080090 this.vt = new hterm.VT(this);
rginda11057d52012-04-25 12:29:56 -070091 this.vt.enable8BitControl = this.prefs_.get('enable-8-bit-control');
92 this.vt.maxStringSequence = this.prefs_.get('max-string-sequence');
rginda87b86462011-12-14 13:48:03 -080093
rgindafeaf3142012-01-31 15:14:20 -080094 // The keyboard hander.
95 this.keyboard = new hterm.Keyboard(this);
96
rginda87b86462011-12-14 13:48:03 -080097 // General IO interface that can be given to third parties without exposing
98 // the entire terminal object.
99 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800100
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400101 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800102 this.setDefaultTabStops();
rginda87b86462011-12-14 13:48:03 -0800103};
104
105/**
rginda35c456b2012-02-09 17:29:05 -0800106 * Default tab with of 8 to match xterm.
107 */
108hterm.Terminal.prototype.tabWidth = 8;
109
110/**
rginda35c456b2012-02-09 17:29:05 -0800111 * The assumed width of a scrollbar.
112 */
113hterm.Terminal.prototype.scrollbarWidthPx = 16;
114
115/**
rginda9f5222b2012-03-05 11:53:28 -0800116 * Select a preference profile.
117 *
118 * This will load the terminal preferences for the given profile name and
119 * associate subsequent preference changes with the new preference profile.
120 *
121 * @param {string} newName The name of the preference profile. Forward slash
122 * characters will be removed from the name.
123 */
124hterm.Terminal.prototype.setProfile = function(profileName) {
125 // If we already have a profile selected, we're going to need to re-sync
126 // with the new profile.
127 var needSync = !!this.profileName_;
128
129 this.profileName_ = profileName.replace(/\//g, '');
130
131 this.prefs_ = new hterm.PreferenceManager(
132 '/hterm/prefs/profiles/' + this.profileName_);
133
134 var self = this;
135 this.prefs_.definePreferences
rginda30f20f62012-04-05 16:36:19 -0700136 ([
137 /**
138 * Set whether the alt key acts as a meta key or as a distinct alt key.
rginda9f5222b2012-03-05 11:53:28 -0800139 */
rginda30f20f62012-04-05 16:36:19 -0700140 ['alt-is-meta', false, function(v) {
141 self.vt.keyboard.altIsMeta = v;
rginda9f5222b2012-03-05 11:53:28 -0800142 }
143 ],
144
rginda30f20f62012-04-05 16:36:19 -0700145 /**
rginda39bdf6f2012-04-10 16:50:55 -0700146 * Controls how the alt key is handled.
147 *
148 * escape....... Send an ESC prefix.
149 * 8-bit........ Add 128 to the unshifted character as in xterm.
150 * browser-key.. Wait for the keypress event and see what the browser says.
151 * (This won't work well on platforms where the browser
152 * performs a default action for some alt sequences.)
rginda30f20f62012-04-05 16:36:19 -0700153 */
rginda39bdf6f2012-04-10 16:50:55 -0700154 ['alt-sends-what', 'escape', function(v) {
155 if (!/^(escape|8-bit|browser-key)$/.test(v))
156 v = 'escape';
157
158 self.vt.keyboard.altSendsWhat = v;
rginda30f20f62012-04-05 16:36:19 -0700159 }
160 ],
161
162 /**
163 * Terminal bell sound. Empty string for no audible bell.
164 */
165 ['audible-bell-sound', '../audio/bell.ogg', function(v) {
166 self.bellAudio_.setAttribute('src', v);
167 }
168 ],
169
170 /**
171 * The background color for text with no other color attributes.
172 */
173 ['background-color', 'rgb(16, 16, 16)', function(v) {
rginda9f5222b2012-03-05 11:53:28 -0800174 self.scrollPort_.setBackgroundColor(v);
175 }
176 ],
177
178 /**
rginda30f20f62012-04-05 16:36:19 -0700179 * The background image.
180 *
181 * Defaults to a subtle light-to-transparent-to-dark gradient that is
182 * mostly transparent.
183 */
184 ['background-image',
185 ('-webkit-linear-gradient(bottom, ' +
186 'rgba(0,0,0,0.01) 0%, ' +
187 'rgba(0,0,0,0) 30%, ' +
188 'rgba(255,255,255,0) 70%, ' +
189 'rgba(255,255,255,0.05) 100%)'),
190 function(v) {
191 self.scrollPort_.setBackgroundImage(v);
192 }
193 ],
194
195 /**
196 * If true, the backspace should send BS ('\x08', aka ^H). Otherwise
197 * the backspace key should send '\x7f'.
198 */
199 ['backspace-sends-backspace', false, function(v) {
200 self.keyboard.backspaceSendsBackspace = v;
201 }
202 ],
203
204 /**
rgindade84e382012-04-20 15:39:31 -0700205 * Whether or not to blink the cursor by default.
206 */
207 ['cursor-blink', false, function(v) {
208 self.setCursorBlink(!!v);
209 }
210 ],
211
212 /**
rginda30f20f62012-04-05 16:36:19 -0700213 * The color of the visible cursor.
214 */
215 ['cursor-color', 'rgba(255,0,0,0.5)', function(v) {
216 self.cursorNode_.style.backgroundColor = v;
217 }
218 ],
219
220 /**
rginda11057d52012-04-25 12:29:56 -0700221 * True to enable 8-bit control characters, false to ignore them.
222 *
223 * We'll respect the two-byte versions of these control characters
224 * regardless of this setting.
225 */
226 ['enable-8-bit-control', false, function(v) {
227 self.vt.enable8BitControl = !!v;
228 }
229 ],
230
231 /**
rginda30f20f62012-04-05 16:36:19 -0700232 * True if we should use bold weight font for text with the bold/bright
233 * attribute. False to use bright colors only. Null to autodetect.
234 */
235 ['enable-bold', null, function(v) {
236 self.syncBoldSafeState();
237 }
238 ],
239
240 /**
rginda9f5222b2012-03-05 11:53:28 -0800241 * Default font family for the terminal text.
242 */
243 ['font-family', ('"DejaVu Sans Mono", "Everson Mono", ' +
244 'FreeMono, "Menlo", "Lucida Console", ' +
245 'monospace'),
246 function(v) { self.syncFontFamily() }
247 ],
248
249 /**
rginda30f20f62012-04-05 16:36:19 -0700250 * The default font size in pixels.
251 */
252 ['font-size', 15, function(v) {
253 self.setFontSize(v);
254 }
255 ],
256
257 /**
rginda9f5222b2012-03-05 11:53:28 -0800258 * Anti-aliasing.
259 */
260 ['font-smoothing', 'antialiased',
261 function(v) { self.syncFontFamily() }
262 ],
263
264 /**
rginda30f20f62012-04-05 16:36:19 -0700265 * The foreground color for text with no other color attributes.
rginda9f5222b2012-03-05 11:53:28 -0800266 */
rginda30f20f62012-04-05 16:36:19 -0700267 ['foreground-color', 'rgb(240, 240, 240)', function(v) {
268 self.scrollPort_.setForegroundColor(v);
rginda9f5222b2012-03-05 11:53:28 -0800269 }
270 ],
271
272 /**
rginda30f20f62012-04-05 16:36:19 -0700273 * If true, home/end will control the terminal scrollbar and shift home/end
274 * will send the VT keycodes. If false then home/end sends VT codes and
275 * shift home/end scrolls.
rginda9f5222b2012-03-05 11:53:28 -0800276 */
rginda30f20f62012-04-05 16:36:19 -0700277 ['home-keys-scroll', false, function(v) {
278 self.keyboard.homeKeysScroll = v;
279 }
280 ],
281
282 /**
rginda11057d52012-04-25 12:29:56 -0700283 * Max length of a DCS, OSC, PM, or APS sequence before we give up and
284 * ignore the code.
285 */
286 ['max-string-sequence', 1024, function(v) {
287 self.vt.maxStringSequence = v;
288 }
289 ],
290
291 /**
rginda30f20f62012-04-05 16:36:19 -0700292 * Set whether the meta key sends a leading escape or not.
293 */
294 ['meta-sends-escape', true, function(v) {
295 self.keyboard.metaSendsEscape = v;
rginda9f5222b2012-03-05 11:53:28 -0800296 }
297 ],
298
299 /**
300 * If true, scroll to the bottom on any keystroke.
301 */
302 ['scroll-on-keystroke', true, function(v) {
303 self.scrollOnKeystroke_ = v;
304 }
305 ],
306
307 /**
308 * If true, scroll to the bottom on terminal output.
309 */
310 ['scroll-on-output', false, function(v) {
311 self.scrollOnOutput_ = v;
312 }
313 ],
314
315 /**
David Reveman8f552492012-03-28 12:18:41 -0400316 * The vertical scrollbar mode.
317 */
318 ['scrollbar-visible', true, function(v) {
319 self.setScrollbarVisible(v);
320 }
321 ],
rginda30f20f62012-04-05 16:36:19 -0700322
323 /**
rgindaf522ce02012-04-17 17:49:17 -0700324 * The default environment variables.
325 */
326 ['environment', {TERM: 'xterm-256color'}, null],
327
328 /**
rginda30f20f62012-04-05 16:36:19 -0700329 * If true, page up/down will control the terminal scrollbar and shift
330 * page up/down will send the VT keycodes. If false then page up/down
331 * sends VT codes and shift page up/down scrolls.
332 */
333 ['page-keys-scroll', false, function(v) {
334 self.keyboard.pageKeysScroll = v;
335 }
336 ],
337
rginda9f5222b2012-03-05 11:53:28 -0800338 ]);
339
340 if (needSync)
341 this.prefs_.notifyAll();
342};
343
344/**
345 * Return the current terminal background color.
346 *
347 * Intended for use by other classes, so we don't have to expose the entire
348 * prefs_ object.
349 */
350hterm.Terminal.prototype.getBackgroundColor = function() {
351 return this.prefs_.get('background-color');
352};
353
354/**
355 * Return the current terminal foreground color.
356 *
357 * Intended for use by other classes, so we don't have to expose the entire
358 * prefs_ object.
359 */
360hterm.Terminal.prototype.getForegroundColor = function() {
361 return this.prefs_.get('foreground-color');
362};
363
364/**
rginda87b86462011-12-14 13:48:03 -0800365 * Create a new instance of a terminal command and run it with a given
366 * argument string.
367 *
368 * @param {function} commandClass The constructor for a terminal command.
369 * @param {string} argString The argument string to pass to the command.
370 */
371hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700372 var environment = this.prefs_.get('environment');
373 if (typeof environment != 'object' || environment == null)
374 environment = {};
375
rginda87b86462011-12-14 13:48:03 -0800376 var self = this;
377 this.command = new commandClass(
378 { argString: argString || '',
379 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700380 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800381 onExit: function(code) {
382 self.io.pop();
383 self.io.println(hterm.msg('COMMAND_COMPLETE',
384 [self.command.commandName, code]));
rgindafeaf3142012-01-31 15:14:20 -0800385 self.uninstallKeyboard();
rginda87b86462011-12-14 13:48:03 -0800386 }
387 });
388
rgindafeaf3142012-01-31 15:14:20 -0800389 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800390 this.command.run();
391};
392
393/**
rgindafeaf3142012-01-31 15:14:20 -0800394 * Returns true if the current screen is the primary screen, false otherwise.
395 */
396hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700397 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800398};
399
400/**
401 * Install the keyboard handler for this terminal.
402 *
403 * This will prevent the browser from seeing any keystrokes sent to the
404 * terminal.
405 */
406hterm.Terminal.prototype.installKeyboard = function() {
407 this.keyboard.installKeyboard(this.document_.body.firstChild);
408}
409
410/**
411 * Uninstall the keyboard handler for this terminal.
412 */
413hterm.Terminal.prototype.uninstallKeyboard = function() {
414 this.keyboard.installKeyboard(null);
415}
416
417/**
rginda35c456b2012-02-09 17:29:05 -0800418 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800419 *
420 * Call setFontSize(0) to reset to the default font size.
421 *
422 * This function does not modify the font-size preference.
423 *
424 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800425 */
426hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800427 if (px === 0)
428 px = this.prefs_.get('font-size');
429
rginda35c456b2012-02-09 17:29:05 -0800430 this.scrollPort_.setFontSize(px);
431};
432
433/**
434 * Get the current font size.
435 */
436hterm.Terminal.prototype.getFontSize = function() {
437 return this.scrollPort_.getFontSize();
438};
439
440/**
441 * Set the CSS "font-family" for this terminal.
442 */
rginda9f5222b2012-03-05 11:53:28 -0800443hterm.Terminal.prototype.syncFontFamily = function() {
444 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
445 this.prefs_.get('font-smoothing'));
446 this.syncBoldSafeState();
447};
448
449hterm.Terminal.prototype.syncBoldSafeState = function() {
450 var enableBold = this.prefs_.get('enable-bold');
451 if (enableBold !== null) {
452 this.screen_.textAttributes.enableBold = enableBold;
453 return;
454 }
455
rgindaf7521392012-02-28 17:20:34 -0800456 var normalSize = this.scrollPort_.measureCharacterSize();
457 var boldSize = this.scrollPort_.measureCharacterSize('bold');
458
459 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800460 if (!isBoldSafe) {
461 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700462 'from normal. Font family is: ' +
463 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800464 }
rginda9f5222b2012-03-05 11:53:28 -0800465
466 this.screen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800467};
468
469/**
rginda87b86462011-12-14 13:48:03 -0800470 * Return a copy of the current cursor position.
471 *
472 * @return {hterm.RowCol} The RowCol object representing the current position.
473 */
474hterm.Terminal.prototype.saveCursor = function() {
475 return this.screen_.cursorPosition.clone();
476};
477
rgindaa19afe22012-01-25 15:40:22 -0800478hterm.Terminal.prototype.getTextAttributes = function() {
479 return this.screen_.textAttributes;
480};
481
rginda87b86462011-12-14 13:48:03 -0800482/**
rgindaf522ce02012-04-17 17:49:17 -0700483 * Return the current browser zoom factor applied to the terminal.
484 *
485 * @return {number} The current browser zoom factor.
486 */
487hterm.Terminal.prototype.getZoomFactor = function() {
488 return this.scrollPort_.characterSize.zoomFactor;
489};
490
491/**
rginda9846e2f2012-01-27 13:53:33 -0800492 * Change the title of this terminal's window.
493 */
494hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800495 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800496};
497
498/**
rginda87b86462011-12-14 13:48:03 -0800499 * Restore a previously saved cursor position.
500 *
501 * @param {hterm.RowCol} cursor The position to restore.
502 */
503hterm.Terminal.prototype.restoreCursor = function(cursor) {
rginda35c456b2012-02-09 17:29:05 -0800504 var row = hterm.clamp(cursor.row, 0, this.screenSize.height - 1);
505 var column = hterm.clamp(cursor.column, 0, this.screenSize.width - 1);
506 this.screen_.setCursorPosition(row, column);
507 if (cursor.column > column ||
508 cursor.column == column && cursor.overflow) {
509 this.screen_.cursorPosition.overflow = true;
510 }
rginda87b86462011-12-14 13:48:03 -0800511};
512
513/**
514 * Set the width of the terminal, resizing the UI to match.
515 */
516hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800517 if (columnCount == null) {
518 this.div_.style.width = '100%';
519 return;
520 }
521
rginda35c456b2012-02-09 17:29:05 -0800522 this.div_.style.width = this.scrollPort_.characterSize.width *
523 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400524 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800525 this.scheduleSyncCursorPosition_();
526};
rginda87b86462011-12-14 13:48:03 -0800527
rgindac9bc5502012-01-18 11:48:44 -0800528/**
rginda35c456b2012-02-09 17:29:05 -0800529 * Set the height of the terminal, resizing the UI to match.
530 */
531hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800532 if (rowCount == null) {
533 this.div_.style.height = '100%';
534 return;
535 }
536
rginda35c456b2012-02-09 17:29:05 -0800537 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700538 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800539 this.realizeSize_(this.screenSize.width, rowCount);
540 this.scheduleSyncCursorPosition_();
541};
542
543/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400544 * Deal with terminal size changes.
545 *
546 */
547hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
548 if (columnCount != this.screenSize.width)
549 this.realizeWidth_(columnCount);
550
551 if (rowCount != this.screenSize.height)
552 this.realizeHeight_(rowCount);
553
554 // Send new terminal size to plugin.
555 this.io.onTerminalResize(columnCount, rowCount);
556};
557
558/**
rgindac9bc5502012-01-18 11:48:44 -0800559 * Deal with terminal width changes.
560 *
561 * This function does what needs to be done when the terminal width changes
562 * out from under us. It happens here rather than in onResize_() because this
563 * code may need to run synchronously to handle programmatic changes of
564 * terminal width.
565 *
566 * Relying on the browser to send us an async resize event means we may not be
567 * in the correct state yet when the next escape sequence hits.
568 */
569hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
570 var deltaColumns = columnCount - this.screen_.getWidth();
571
rginda87b86462011-12-14 13:48:03 -0800572 this.screenSize.width = columnCount;
573 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800574
575 if (deltaColumns > 0) {
576 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
577 } else {
578 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
579 if (this.tabStops_[i] <= columnCount)
580 break;
581
582 this.tabStops_.pop();
583 }
584 }
585
586 this.screen_.setColumnCount(this.screenSize.width);
587};
588
589/**
590 * Deal with terminal height changes.
591 *
592 * This function does what needs to be done when the terminal height changes
593 * out from under us. It happens here rather than in onResize_() because this
594 * code may need to run synchronously to handle programmatic changes of
595 * terminal height.
596 *
597 * Relying on the browser to send us an async resize event means we may not be
598 * in the correct state yet when the next escape sequence hits.
599 */
600hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
601 var deltaRows = rowCount - this.screen_.getHeight();
602
603 this.screenSize.height = rowCount;
604
605 var cursor = this.saveCursor();
606
607 if (deltaRows < 0) {
608 // Screen got smaller.
609 deltaRows *= -1;
610 while (deltaRows) {
611 var lastRow = this.getRowCount() - 1;
612 if (lastRow - this.scrollbackRows_.length == cursor.row)
613 break;
614
615 if (this.getRowText(lastRow))
616 break;
617
618 this.screen_.popRow();
619 deltaRows--;
620 }
621
622 var ary = this.screen_.shiftRows(deltaRows);
623 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
624
625 // We just removed rows from the top of the screen, we need to update
626 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800627 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800628 } else if (deltaRows > 0) {
629 // Screen got larger.
630
631 if (deltaRows <= this.scrollbackRows_.length) {
632 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
633 var rows = this.scrollbackRows_.splice(
634 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
635 this.screen_.unshiftRows(rows);
636 deltaRows -= scrollbackCount;
637 cursor.row += scrollbackCount;
638 }
639
640 if (deltaRows)
641 this.appendRows_(deltaRows);
642 }
643
rginda35c456b2012-02-09 17:29:05 -0800644 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800645 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800646};
647
648/**
649 * Scroll the terminal to the top of the scrollback buffer.
650 */
651hterm.Terminal.prototype.scrollHome = function() {
652 this.scrollPort_.scrollRowToTop(0);
653};
654
655/**
656 * Scroll the terminal to the end.
657 */
658hterm.Terminal.prototype.scrollEnd = function() {
659 this.scrollPort_.scrollRowToBottom(this.getRowCount());
660};
661
662/**
663 * Scroll the terminal one page up (minus one line) relative to the current
664 * position.
665 */
666hterm.Terminal.prototype.scrollPageUp = function() {
667 var i = this.scrollPort_.getTopRowIndex();
668 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
669};
670
671/**
672 * Scroll the terminal one page down (minus one line) relative to the current
673 * position.
674 */
675hterm.Terminal.prototype.scrollPageDown = function() {
676 var i = this.scrollPort_.getTopRowIndex();
677 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800678};
679
rgindac9bc5502012-01-18 11:48:44 -0800680/**
681 * Full terminal reset.
682 */
rginda87b86462011-12-14 13:48:03 -0800683hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800684 this.clearAllTabStops();
685 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700686
687 this.clearHome(this.primaryScreen_);
688 this.primaryScreen_.textAttributes.reset();
689
690 this.clearHome(this.alternateScreen_);
691 this.alternateScreen_.textAttributes.reset();
692
rgindab8bc8932012-04-27 12:45:03 -0700693 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
694
rgindac9bc5502012-01-18 11:48:44 -0800695 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800696};
697
rgindac9bc5502012-01-18 11:48:44 -0800698/**
699 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700700 *
701 * Perform a soft reset to the default values listed in
702 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800703 */
rginda0f5c0292012-01-13 11:00:13 -0800704hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700705 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800706 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700707
rgindab8bc8932012-04-27 12:45:03 -0700708 // Xterm also resets the color palette on soft reset, even though it doesn't
709 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700710 this.primaryScreen_.textAttributes.resetColorPalette();
711 this.alternateScreen_.textAttributes.resetColorPalette();
712
rgindab8bc8932012-04-27 12:45:03 -0700713 // The xterm man page explicitly says this will happen on soft reset.
714 this.setVTScrollRegion(null, null);
715
716 // Xterm also shows the cursor on soft reset, but does not alter the blink
717 // state.
rgindaa19afe22012-01-25 15:40:22 -0800718 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800719};
720
rgindac9bc5502012-01-18 11:48:44 -0800721/**
722 * Move the cursor forward to the next tab stop, or to the last column
723 * if no more tab stops are set.
724 */
725hterm.Terminal.prototype.forwardTabStop = function() {
726 var column = this.screen_.cursorPosition.column;
727
728 for (var i = 0; i < this.tabStops_.length; i++) {
729 if (this.tabStops_[i] > column) {
730 this.setCursorColumn(this.tabStops_[i]);
731 return;
732 }
733 }
734
735 this.setCursorColumn(this.screenSize.width - 1);
rginda0f5c0292012-01-13 11:00:13 -0800736};
737
rgindac9bc5502012-01-18 11:48:44 -0800738/**
739 * Move the cursor backward to the previous tab stop, or to the first column
740 * if no previous tab stops are set.
741 */
742hterm.Terminal.prototype.backwardTabStop = function() {
743 var column = this.screen_.cursorPosition.column;
744
745 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
746 if (this.tabStops_[i] < column) {
747 this.setCursorColumn(this.tabStops_[i]);
748 return;
749 }
750 }
751
752 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800753};
754
rgindac9bc5502012-01-18 11:48:44 -0800755/**
756 * Set a tab stop at the given column.
757 *
758 * @param {int} column Zero based column.
759 */
760hterm.Terminal.prototype.setTabStop = function(column) {
761 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
762 if (this.tabStops_[i] == column)
763 return;
764
765 if (this.tabStops_[i] < column) {
766 this.tabStops_.splice(i + 1, 0, column);
767 return;
768 }
769 }
770
771 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800772};
773
rgindac9bc5502012-01-18 11:48:44 -0800774/**
775 * Clear the tab stop at the current cursor position.
776 *
777 * No effect if there is no tab stop at the current cursor position.
778 */
779hterm.Terminal.prototype.clearTabStopAtCursor = function() {
780 var column = this.screen_.cursorPosition.column;
781
782 var i = this.tabStops_.indexOf(column);
783 if (i == -1)
784 return;
785
786 this.tabStops_.splice(i, 1);
787};
788
789/**
790 * Clear all tab stops.
791 */
792hterm.Terminal.prototype.clearAllTabStops = function() {
793 this.tabStops_.length = 0;
794};
795
796/**
797 * Set up the default tab stops, starting from a given column.
798 *
799 * This sets a tabstop every (column % this.tabWidth) column, starting
800 * from the specified column, or 0 if no column is provided.
801 *
802 * This does not clear the existing tab stops first, use clearAllTabStops
803 * for that.
804 *
805 * @param {int} opt_start Optional starting zero based starting column, useful
806 * for filling out missing tab stops when the terminal is resized.
807 */
808hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
809 var start = opt_start || 0;
810 var w = this.tabWidth;
811 var stopCount = Math.floor((this.screenSize.width - start) / this.tabWidth)
812 for (var i = 0; i < stopCount; i++) {
813 this.setTabStop(Math.floor((start + i * w) / w) * w + w);
814 }
rginda87b86462011-12-14 13:48:03 -0800815};
816
rginda6d397402012-01-17 10:58:29 -0800817/**
818 * Save cursor position and attributes.
819 *
820 * TODO(rginda): Save attributes once we support them.
821 */
rginda87b86462011-12-14 13:48:03 -0800822hterm.Terminal.prototype.saveOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800823 this.savedOptions_.cursor = this.saveCursor();
rgindaa19afe22012-01-25 15:40:22 -0800824 this.savedOptions_.textAttributes = this.screen_.textAttributes.clone();
rginda87b86462011-12-14 13:48:03 -0800825};
826
rginda6d397402012-01-17 10:58:29 -0800827/**
828 * Restore cursor position and attributes.
829 *
830 * TODO(rginda): Restore attributes once we support them.
831 */
rginda87b86462011-12-14 13:48:03 -0800832hterm.Terminal.prototype.restoreOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800833 if (this.savedOptions_.cursor)
834 this.restoreCursor(this.savedOptions_.cursor);
rgindaa19afe22012-01-25 15:40:22 -0800835 if (this.savedOptions_.textAttributes)
836 this.screen_.textAttributes = this.savedOptions_.textAttributes;
rginda8ba33642011-12-14 12:31:31 -0800837};
838
839/**
840 * Interpret a sequence of characters.
841 *
842 * Incomplete escape sequences are buffered until the next call.
843 *
844 * @param {string} str Sequence of characters to interpret or pass through.
845 */
846hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800847 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800848 this.scheduleSyncCursorPosition_();
849};
850
851/**
852 * Take over the given DIV for use as the terminal display.
853 *
854 * @param {HTMLDivElement} div The div to use as the terminal display.
855 */
856hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800857 this.div_ = div;
858
rginda8ba33642011-12-14 12:31:31 -0800859 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -0700860 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
861
rginda0918b652012-04-04 11:26:24 -0700862 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -0800863
rginda9f5222b2012-03-05 11:53:28 -0800864 this.setFontSize(this.prefs_.get('font-size'));
865 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -0800866
David Reveman8f552492012-03-28 12:18:41 -0400867 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
868
rginda8ba33642011-12-14 12:31:31 -0800869 this.document_ = this.scrollPort_.getDocument();
870
rginda8ba33642011-12-14 12:31:31 -0800871 this.cursorNode_ = this.document_.createElement('div');
872 this.cursorNode_.style.cssText =
873 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800874 'top: -99px;' +
875 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -0800876 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
877 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda6d397402012-01-17 10:58:29 -0800878 '-webkit-transition: opacity, background-color 100ms linear;' +
rginda9f5222b2012-03-05 11:53:28 -0800879 'background-color: ' + this.prefs_.get('cursor-color'));
rginda8ba33642011-12-14 12:31:31 -0800880 this.document_.body.appendChild(this.cursorNode_);
881
rgindade84e382012-04-20 15:39:31 -0700882 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda8ba33642011-12-14 12:31:31 -0800883 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -0800884
rginda87b86462011-12-14 13:48:03 -0800885 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -0800886 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -0800887};
888
rginda0918b652012-04-04 11:26:24 -0700889/**
890 * Return the HTML document that contains the terminal DOM nodes.
891 */
rginda87b86462011-12-14 13:48:03 -0800892hterm.Terminal.prototype.getDocument = function() {
893 return this.document_;
rginda8ba33642011-12-14 12:31:31 -0800894};
895
896/**
rginda0918b652012-04-04 11:26:24 -0700897 * Focus the terminal.
898 */
899hterm.Terminal.prototype.focus = function() {
900 this.scrollPort_.focus();
901};
902
903/**
rginda8ba33642011-12-14 12:31:31 -0800904 * Return the HTML Element for a given row index.
905 *
906 * This is a method from the RowProvider interface. The ScrollPort uses
907 * it to fetch rows on demand as they are scrolled into view.
908 *
909 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
910 * pairs to conserve memory.
911 *
912 * @param {integer} index The zero-based row index, measured relative to the
913 * start of the scrollback buffer. On-screen rows will always have the
914 * largest indicies.
915 * @return {HTMLElement} The 'x-row' element containing for the requested row.
916 */
917hterm.Terminal.prototype.getRowNode = function(index) {
918 if (index < this.scrollbackRows_.length)
919 return this.scrollbackRows_[index];
920
921 var screenIndex = index - this.scrollbackRows_.length;
922 return this.screen_.rowsArray[screenIndex];
923};
924
925/**
926 * Return the text content for a given range of rows.
927 *
928 * This is a method from the RowProvider interface. The ScrollPort uses
929 * it to fetch text content on demand when the user attempts to copy their
930 * selection to the clipboard.
931 *
932 * @param {integer} start The zero-based row index to start from, measured
933 * relative to the start of the scrollback buffer. On-screen rows will
934 * always have the largest indicies.
935 * @param {integer} end The zero-based row index to end on, measured
936 * relative to the start of the scrollback buffer.
937 * @return {string} A single string containing the text value of the range of
938 * rows. Lines will be newline delimited, with no trailing newline.
939 */
940hterm.Terminal.prototype.getRowsText = function(start, end) {
941 var ary = [];
942 for (var i = start; i < end; i++) {
943 var node = this.getRowNode(i);
944 ary.push(node.textContent);
945 }
946
947 return ary.join('\n');
948};
949
950/**
951 * Return the text content for a given row.
952 *
953 * This is a method from the RowProvider interface. The ScrollPort uses
954 * it to fetch text content on demand when the user attempts to copy their
955 * selection to the clipboard.
956 *
957 * @param {integer} index The zero-based row index to return, measured
958 * relative to the start of the scrollback buffer. On-screen rows will
959 * always have the largest indicies.
960 * @return {string} A string containing the text value of the selected row.
961 */
962hterm.Terminal.prototype.getRowText = function(index) {
963 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -0800964 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -0800965};
966
967/**
968 * Return the total number of rows in the addressable screen and in the
969 * scrollback buffer of this terminal.
970 *
971 * This is a method from the RowProvider interface. The ScrollPort uses
972 * it to compute the size of the scrollbar.
973 *
974 * @return {integer} The number of rows in this terminal.
975 */
976hterm.Terminal.prototype.getRowCount = function() {
977 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
978};
979
980/**
981 * Create DOM nodes for new rows and append them to the end of the terminal.
982 *
983 * This is the only correct way to add a new DOM node for a row. Notice that
984 * the new row is appended to the bottom of the list of rows, and does not
985 * require renumbering (of the rowIndex property) of previous rows.
986 *
987 * If you think you want a new blank row somewhere in the middle of the
988 * terminal, look into moveRows_().
989 *
990 * This method does not pay attention to vtScrollTop/Bottom, since you should
991 * be using moveRows() in cases where they would matter.
992 *
993 * The cursor will be positioned at column 0 of the first inserted line.
994 */
995hterm.Terminal.prototype.appendRows_ = function(count) {
996 var cursorRow = this.screen_.rowsArray.length;
997 var offset = this.scrollbackRows_.length + cursorRow;
998 for (var i = 0; i < count; i++) {
999 var row = this.document_.createElement('x-row');
1000 row.appendChild(this.document_.createTextNode(''));
1001 row.rowIndex = offset + i;
1002 this.screen_.pushRow(row);
1003 }
1004
1005 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1006 if (extraRows > 0) {
1007 var ary = this.screen_.shiftRows(extraRows);
1008 Array.prototype.push.apply(this.scrollbackRows_, ary);
1009 this.scheduleScrollDown_();
1010 }
1011
1012 if (cursorRow >= this.screen_.rowsArray.length)
1013 cursorRow = this.screen_.rowsArray.length - 1;
1014
rginda87b86462011-12-14 13:48:03 -08001015 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001016};
1017
1018/**
1019 * Relocate rows from one part of the addressable screen to another.
1020 *
1021 * This is used to recycle rows during VT scrolls (those which are driven
1022 * by VT commands, rather than by the user manipulating the scrollbar.)
1023 *
1024 * In this case, the blank lines scrolled into the scroll region are made of
1025 * the nodes we scrolled off. These have their rowIndex properties carefully
1026 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001027 */
1028hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1029 var ary = this.screen_.removeRows(fromIndex, count);
1030 this.screen_.insertRows(toIndex, ary);
1031
1032 var start, end;
1033 if (fromIndex < toIndex) {
1034 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001035 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001036 } else {
1037 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001038 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001039 }
1040
1041 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001042 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001043};
1044
1045/**
1046 * Renumber the rowIndex property of the given range of rows.
1047 *
1048 * The start and end indicies are relative to the screen, not the scrollback.
1049 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001050 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001051 * no need to renumber scrollback rows.
1052 */
1053hterm.Terminal.prototype.renumberRows_ = function(start, end) {
1054 var offset = this.scrollbackRows_.length;
1055 for (var i = start; i < end; i++) {
1056 this.screen_.rowsArray[i].rowIndex = offset + i;
1057 }
1058};
1059
1060/**
1061 * Print a string to the terminal.
1062 *
1063 * This respects the current insert and wraparound modes. It will add new lines
1064 * to the end of the terminal, scrolling off the top into the scrollback buffer
1065 * if necessary.
1066 *
1067 * The string is *not* parsed for escape codes. Use the interpret() method if
1068 * that's what you're after.
1069 *
1070 * @param{string} str The string to print.
1071 */
1072hterm.Terminal.prototype.print = function(str) {
rgindaa19afe22012-01-25 15:40:22 -08001073 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
1074 this.newLine();
rginda2312fff2012-01-05 16:20:52 -08001075
rgindaa19afe22012-01-25 15:40:22 -08001076 if (this.options_.insertMode) {
1077 this.screen_.insertString(str);
1078 } else {
1079 this.screen_.overwriteString(str);
1080 }
1081
1082 var overflow = this.screen_.maybeClipCurrentRow();
1083
1084 if (this.options_.wraparound && overflow) {
1085 var lastColumn;
1086
1087 do {
rginda35c456b2012-02-09 17:29:05 -08001088 this.newLine();
1089 lastColumn = overflow.characterLength;
rgindaa19afe22012-01-25 15:40:22 -08001090
1091 if (!this.options_.insertMode)
1092 this.screen_.deleteChars(overflow.characterLength);
1093
1094 this.screen_.prependNodes(overflow);
rgindaa19afe22012-01-25 15:40:22 -08001095
1096 overflow = this.screen_.maybeClipCurrentRow();
1097 } while (overflow);
1098
1099 this.setCursorColumn(lastColumn);
1100 }
rginda8ba33642011-12-14 12:31:31 -08001101
1102 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001103
rginda9f5222b2012-03-05 11:53:28 -08001104 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001105 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001106};
1107
1108/**
rginda87b86462011-12-14 13:48:03 -08001109 * Set the VT scroll region.
1110 *
rginda87b86462011-12-14 13:48:03 -08001111 * This also resets the cursor position to the absolute (0, 0) position, since
1112 * that's what xterm appears to do.
1113 *
1114 * @param {integer} scrollTop The zero-based top of the scroll region.
1115 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1116 * inclusive.
1117 */
1118hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1119 this.vtScrollTop_ = scrollTop;
1120 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001121};
1122
1123/**
rginda8ba33642011-12-14 12:31:31 -08001124 * Return the top row index according to the VT.
1125 *
1126 * This will return 0 unless the terminal has been told to restrict scrolling
1127 * to some lower row. It is used for some VT cursor positioning and scrolling
1128 * commands.
1129 *
1130 * @return {integer} The topmost row in the terminal's scroll region.
1131 */
1132hterm.Terminal.prototype.getVTScrollTop = function() {
1133 if (this.vtScrollTop_ != null)
1134 return this.vtScrollTop_;
1135
1136 return 0;
rginda87b86462011-12-14 13:48:03 -08001137};
rginda8ba33642011-12-14 12:31:31 -08001138
1139/**
1140 * Return the bottom row index according to the VT.
1141 *
1142 * This will return the height of the terminal unless the it has been told to
1143 * restrict scrolling to some higher row. It is used for some VT cursor
1144 * positioning and scrolling commands.
1145 *
1146 * @return {integer} The bottommost row in the terminal's scroll region.
1147 */
1148hterm.Terminal.prototype.getVTScrollBottom = function() {
1149 if (this.vtScrollBottom_ != null)
1150 return this.vtScrollBottom_;
1151
rginda87b86462011-12-14 13:48:03 -08001152 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001153}
1154
1155/**
1156 * Process a '\n' character.
1157 *
1158 * If the cursor is on the final row of the terminal this will append a new
1159 * blank row to the screen and scroll the topmost row into the scrollback
1160 * buffer.
1161 *
1162 * Otherwise, this moves the cursor to column zero of the next row.
1163 */
1164hterm.Terminal.prototype.newLine = function() {
1165 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001166 // If we're at the end of the screen we need to append a new line and
1167 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001168 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001169 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1170 // End of the scroll region does not affect the scrollback buffer.
1171 this.vtScrollUp(1);
1172 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001173 } else {
rginda87b86462011-12-14 13:48:03 -08001174 // Anywhere else in the screen just moves the cursor.
1175 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001176 }
1177};
1178
1179/**
1180 * Like newLine(), except maintain the cursor column.
1181 */
1182hterm.Terminal.prototype.lineFeed = function() {
1183 var column = this.screen_.cursorPosition.column;
1184 this.newLine();
1185 this.setCursorColumn(column);
1186};
1187
1188/**
rginda87b86462011-12-14 13:48:03 -08001189 * If autoCarriageReturn is set then newLine(), else lineFeed().
1190 */
1191hterm.Terminal.prototype.formFeed = function() {
1192 if (this.options_.autoCarriageReturn) {
1193 this.newLine();
1194 } else {
1195 this.lineFeed();
1196 }
1197};
1198
1199/**
1200 * Move the cursor up one row, possibly inserting a blank line.
1201 *
1202 * The cursor column is not changed.
1203 */
1204hterm.Terminal.prototype.reverseLineFeed = function() {
1205 var scrollTop = this.getVTScrollTop();
1206 var currentRow = this.screen_.cursorPosition.row;
1207
1208 if (currentRow == scrollTop) {
1209 this.insertLines(1);
1210 } else {
1211 this.setAbsoluteCursorRow(currentRow - 1);
1212 }
1213};
1214
1215/**
rginda8ba33642011-12-14 12:31:31 -08001216 * Replace all characters to the left of the current cursor with the space
1217 * character.
1218 *
1219 * TODO(rginda): This should probably *remove* the characters (not just replace
1220 * with a space) if there are no characters at or beyond the current cursor
1221 * position. Once it does that, it'll have the same text-attribute related
1222 * issues as hterm.Screen.prototype.clearCursorRow :/
1223 */
1224hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001225 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001226 this.setCursorColumn(0);
rginda87b86462011-12-14 13:48:03 -08001227 this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1));
1228 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001229};
1230
1231/**
1232 * Erase a given number of characters to the right of the cursor, shifting
1233 * remaining characters to the left.
1234 *
1235 * The cursor position is unchanged.
1236 *
1237 * TODO(rginda): Test that this works even when the cursor is positioned beyond
1238 * the end of the text.
1239 *
1240 * TODO(rginda): This likely has text-attribute related troubles similar to the
1241 * todo on hterm.Screen.prototype.clearCursorRow.
1242 */
1243hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001244 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001245
rginda87b86462011-12-14 13:48:03 -08001246 var maxCount = this.screenSize.width - cursor.column;
rginda8ba33642011-12-14 12:31:31 -08001247 var count = (opt_count && opt_count < maxCount) ? opt_count : maxCount;
1248 this.screen_.deleteChars(count);
rginda87b86462011-12-14 13:48:03 -08001249 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001250};
1251
1252/**
1253 * Erase the current line.
1254 *
1255 * The cursor position is unchanged.
1256 *
1257 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1258 * has a text-attribute related TODO.
1259 */
1260hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001261 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001262 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001263 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001264};
1265
1266/**
1267 * Erase all characters from the start of the scroll region to the current
1268 * cursor position.
1269 *
1270 * The cursor position is unchanged.
1271 *
1272 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1273 * has a text-attribute related TODO.
1274 */
1275hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001276 var cursor = this.saveCursor();
1277
1278 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001279
1280 var top = this.getVTScrollTop();
rginda87b86462011-12-14 13:48:03 -08001281 for (var i = top; i < cursor.row; i++) {
1282 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001283 this.screen_.clearCursorRow();
1284 }
1285
rginda87b86462011-12-14 13:48:03 -08001286 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001287};
1288
1289/**
1290 * Erase all characters from the current cursor position to the end of the
1291 * scroll region.
1292 *
1293 * The cursor position is unchanged.
1294 *
1295 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1296 * has a text-attribute related TODO.
1297 */
1298hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001299 var cursor = this.saveCursor();
1300
1301 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001302
1303 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001304 for (var i = cursor.row + 1; i <= bottom; i++) {
1305 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001306 this.screen_.clearCursorRow();
1307 }
1308
rginda87b86462011-12-14 13:48:03 -08001309 this.restoreCursor(cursor);
1310};
1311
1312/**
1313 * Fill the terminal with a given character.
1314 *
1315 * This methods does not respect the VT scroll region.
1316 *
1317 * @param {string} ch The character to use for the fill.
1318 */
1319hterm.Terminal.prototype.fill = function(ch) {
1320 var cursor = this.saveCursor();
1321
1322 this.setAbsoluteCursorPosition(0, 0);
1323 for (var row = 0; row < this.screenSize.height; row++) {
1324 for (var col = 0; col < this.screenSize.width; col++) {
1325 this.setAbsoluteCursorPosition(row, col);
1326 this.screen_.overwriteString(ch);
1327 }
1328 }
1329
1330 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001331};
1332
1333/**
rginda9ea433c2012-03-16 11:57:00 -07001334 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001335 *
rginda9ea433c2012-03-16 11:57:00 -07001336 * This does not respect the scroll region.
1337 *
1338 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1339 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001340 *
1341 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1342 * has a text-attribute related TODO.
1343 */
rginda9ea433c2012-03-16 11:57:00 -07001344hterm.Terminal.prototype.clearHome = function(opt_screen) {
1345 var screen = opt_screen || this.screen_;
1346 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001347
rginda11057d52012-04-25 12:29:56 -07001348 if (bottom == 0) {
1349 // Empty screen, nothing to do.
1350 return;
1351 }
1352
rgindae4d29232012-01-19 10:47:13 -08001353 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001354 screen.setCursorPosition(i, 0);
1355 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001356 }
1357
rginda9ea433c2012-03-16 11:57:00 -07001358 screen.setCursorPosition(0, 0);
1359};
1360
1361/**
1362 * Erase the entire display without changing the cursor position.
1363 *
1364 * The cursor position is unchanged. This does not respect the scroll
1365 * region.
1366 *
1367 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1368 * to the current screen.
1369 *
1370 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1371 * has a text-attribute related TODO.
1372 */
1373hterm.Terminal.prototype.clear = function(opt_screen) {
1374 var screen = opt_screen || this.screen_;
1375 var cursor = screen.cursorPosition.clone();
1376 this.clearHome(screen);
1377 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001378};
1379
1380/**
1381 * VT command to insert lines at the current cursor row.
1382 *
1383 * This respects the current scroll region. Rows pushed off the bottom are
1384 * lost (they won't show up in the scrollback buffer).
1385 *
1386 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1387 * has a text-attribute related TODO.
1388 *
1389 * @param {integer} count The number of lines to insert.
1390 */
1391hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001392 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001393
1394 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001395 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001396
rgindae4d29232012-01-19 10:47:13 -08001397 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001398 if (start != cursor.row)
1399 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001400
1401 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001402 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001403 this.screen_.clearCursorRow();
1404 }
1405
rginda87b86462011-12-14 13:48:03 -08001406 cursor.column = 0;
1407 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001408};
1409
1410/**
1411 * VT command to delete lines at the current cursor row.
1412 *
1413 * New rows are added to the bottom of scroll region to take their place. New
1414 * rows are strictly there to take up space and have no content or style.
1415 */
1416hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001417 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001418
rginda87b86462011-12-14 13:48:03 -08001419 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001420 var bottom = this.getVTScrollBottom();
1421
rginda87b86462011-12-14 13:48:03 -08001422 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001423 count = Math.min(count, maxCount);
1424
rginda87b86462011-12-14 13:48:03 -08001425 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001426 if (count != maxCount)
1427 this.moveRows_(top, count, moveStart);
1428
1429 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001430 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001431 this.screen_.clearCursorRow();
1432 }
1433
rginda87b86462011-12-14 13:48:03 -08001434 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001435};
1436
1437/**
1438 * Inserts the given number of spaces at the current cursor position.
1439 *
rginda87b86462011-12-14 13:48:03 -08001440 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001441 */
1442hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001443 var cursor = this.saveCursor();
1444
rginda0f5c0292012-01-13 11:00:13 -08001445 var ws = hterm.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001446 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001447 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001448
1449 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001450};
1451
1452/**
1453 * Forward-delete the specified number of characters starting at the cursor
1454 * position.
1455 *
1456 * @param {integer} count The number of characters to delete.
1457 */
1458hterm.Terminal.prototype.deleteChars = function(count) {
1459 this.screen_.deleteChars(count);
1460};
1461
1462/**
1463 * Shift rows in the scroll region upwards by a given number of lines.
1464 *
1465 * New rows are inserted at the bottom of the scroll region to fill the
1466 * vacated rows. The new rows not filled out with the current text attributes.
1467 *
1468 * This function does not affect the scrollback rows at all. Rows shifted
1469 * off the top are lost.
1470 *
rginda87b86462011-12-14 13:48:03 -08001471 * The cursor position is not altered.
1472 *
rginda8ba33642011-12-14 12:31:31 -08001473 * @param {integer} count The number of rows to scroll.
1474 */
1475hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001476 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001477
rginda87b86462011-12-14 13:48:03 -08001478 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001479 this.deleteLines(count);
1480
rginda87b86462011-12-14 13:48:03 -08001481 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001482};
1483
1484/**
1485 * Shift rows below the cursor down by a given number of lines.
1486 *
1487 * This function respects the current scroll region.
1488 *
1489 * New rows are inserted at the top of the scroll region to fill the
1490 * vacated rows. The new rows not filled out with the current text attributes.
1491 *
1492 * This function does not affect the scrollback rows at all. Rows shifted
1493 * off the bottom are lost.
1494 *
1495 * @param {integer} count The number of rows to scroll.
1496 */
1497hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001498 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001499
rginda87b86462011-12-14 13:48:03 -08001500 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001501 this.insertLines(opt_count);
1502
rginda87b86462011-12-14 13:48:03 -08001503 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001504};
1505
rginda87b86462011-12-14 13:48:03 -08001506
rginda8ba33642011-12-14 12:31:31 -08001507/**
1508 * Set the cursor position.
1509 *
1510 * The cursor row is relative to the scroll region if the terminal has
1511 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1512 *
1513 * @param {integer} row The new zero-based cursor row.
1514 * @param {integer} row The new zero-based cursor column.
1515 */
1516hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1517 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001518 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001519 } else {
rginda87b86462011-12-14 13:48:03 -08001520 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001521 }
rginda87b86462011-12-14 13:48:03 -08001522};
rginda8ba33642011-12-14 12:31:31 -08001523
rginda87b86462011-12-14 13:48:03 -08001524hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1525 var scrollTop = this.getVTScrollTop();
1526 row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
rginda2312fff2012-01-05 16:20:52 -08001527 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001528 this.screen_.setCursorPosition(row, column);
1529};
1530
1531hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rginda2312fff2012-01-05 16:20:52 -08001532 row = hterm.clamp(row, 0, this.screenSize.height - 1);
1533 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001534 this.screen_.setCursorPosition(row, column);
1535};
1536
1537/**
1538 * Set the cursor column.
1539 *
1540 * @param {integer} column The new zero-based cursor column.
1541 */
1542hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001543 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001544};
1545
1546/**
1547 * Return the cursor column.
1548 *
1549 * @return {integer} The zero-based cursor column.
1550 */
1551hterm.Terminal.prototype.getCursorColumn = function() {
1552 return this.screen_.cursorPosition.column;
1553};
1554
1555/**
1556 * Set the cursor row.
1557 *
1558 * The cursor row is relative to the scroll region if the terminal has
1559 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1560 *
1561 * @param {integer} row The new cursor row.
1562 */
rginda87b86462011-12-14 13:48:03 -08001563hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1564 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001565};
1566
1567/**
1568 * Return the cursor row.
1569 *
1570 * @return {integer} The zero-based cursor row.
1571 */
1572hterm.Terminal.prototype.getCursorRow = function(row) {
1573 return this.screen_.cursorPosition.row;
1574};
1575
1576/**
1577 * Request that the ScrollPort redraw itself soon.
1578 *
1579 * The redraw will happen asynchronously, soon after the call stack winds down.
1580 * Multiple calls will be coalesced into a single redraw.
1581 */
1582hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001583 if (this.timeouts_.redraw)
1584 return;
rginda8ba33642011-12-14 12:31:31 -08001585
1586 var self = this;
rginda87b86462011-12-14 13:48:03 -08001587 this.timeouts_.redraw = setTimeout(function() {
1588 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001589 self.scrollPort_.redraw_();
1590 }, 0);
1591};
1592
1593/**
1594 * Request that the ScrollPort be scrolled to the bottom.
1595 *
1596 * The scroll will happen asynchronously, soon after the call stack winds down.
1597 * Multiple calls will be coalesced into a single scroll.
1598 *
1599 * This affects the scrollbar position of the ScrollPort, and has nothing to
1600 * do with the VT scroll commands.
1601 */
1602hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1603 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001604 return;
rginda8ba33642011-12-14 12:31:31 -08001605
1606 var self = this;
1607 this.timeouts_.scrollDown = setTimeout(function() {
1608 delete self.timeouts_.scrollDown;
1609 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1610 }, 10);
1611};
1612
1613/**
1614 * Move the cursor up a specified number of rows.
1615 *
1616 * @param {integer} count The number of rows to move the cursor.
1617 */
1618hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001619 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001620};
1621
1622/**
1623 * Move the cursor down a specified number of rows.
1624 *
1625 * @param {integer} count The number of rows to move the cursor.
1626 */
1627hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001628 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001629 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1630 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1631 this.screenSize.height - 1);
1632
1633 var row = hterm.clamp(this.screen_.cursorPosition.row + count,
1634 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001635 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001636};
1637
1638/**
1639 * Move the cursor left a specified number of columns.
1640 *
1641 * @param {integer} count The number of columns to move the cursor.
1642 */
1643hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001644 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001645};
1646
1647/**
1648 * Move the cursor right a specified number of columns.
1649 *
1650 * @param {integer} count The number of columns to move the cursor.
1651 */
1652hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001653 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001654 var column = hterm.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001655 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001656 this.setCursorColumn(column);
1657};
1658
1659/**
1660 * Reverse the foreground and background colors of the terminal.
1661 *
1662 * This only affects text that was drawn with no attributes.
1663 *
1664 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1665 * been drawn with attributes that happen to coincide with the default
1666 * 'no-attribute' colors. My guess is probably not.
1667 */
1668hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001669 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001670 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001671 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1672 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001673 } else {
rginda9f5222b2012-03-05 11:53:28 -08001674 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1675 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001676 }
1677};
1678
1679/**
rginda87b86462011-12-14 13:48:03 -08001680 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001681 */
1682hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001683 if (this.bellAudio_.getAttribute('src'))
1684 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001685
rginda6d397402012-01-17 10:58:29 -08001686 this.cursorNode_.style.backgroundColor =
1687 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001688
1689 var self = this;
1690 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001691 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001692 }, 200);
rginda87b86462011-12-14 13:48:03 -08001693};
1694
1695/**
rginda8ba33642011-12-14 12:31:31 -08001696 * Set the origin mode bit.
1697 *
1698 * If origin mode is on, certain VT cursor and scrolling commands measure their
1699 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1700 * to the top of the addressable screen.
1701 *
1702 * Defaults to off.
1703 *
1704 * @param {boolean} state True to set origin mode, false to unset.
1705 */
1706hterm.Terminal.prototype.setOriginMode = function(state) {
1707 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001708 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001709};
1710
1711/**
1712 * Set the insert mode bit.
1713 *
1714 * If insert mode is on, existing text beyond the cursor position will be
1715 * shifted right to make room for new text. Otherwise, new text overwrites
1716 * any existing text.
1717 *
1718 * Defaults to off.
1719 *
1720 * @param {boolean} state True to set insert mode, false to unset.
1721 */
1722hterm.Terminal.prototype.setInsertMode = function(state) {
1723 this.options_.insertMode = state;
1724};
1725
1726/**
rginda87b86462011-12-14 13:48:03 -08001727 * Set the auto carriage return bit.
1728 *
1729 * If auto carriage return is on then a formfeed character is interpreted
1730 * as a newline, otherwise it's the same as a linefeed. The difference boils
1731 * down to whether or not the cursor column is reset.
1732 */
1733hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1734 this.options_.autoCarriageReturn = state;
1735};
1736
1737/**
rginda8ba33642011-12-14 12:31:31 -08001738 * Set the wraparound mode bit.
1739 *
1740 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1741 * to the start of the following row. Otherwise, the cursor is clamped to the
1742 * end of the screen and attempts to write past it are ignored.
1743 *
1744 * Defaults to on.
1745 *
1746 * @param {boolean} state True to set wraparound mode, false to unset.
1747 */
1748hterm.Terminal.prototype.setWraparound = function(state) {
1749 this.options_.wraparound = state;
1750};
1751
1752/**
1753 * Set the reverse-wraparound mode bit.
1754 *
1755 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1756 * to the end of the previous row. Otherwise, the cursor is clamped to column
1757 * 0.
1758 *
1759 * Defaults to off.
1760 *
1761 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1762 */
1763hterm.Terminal.prototype.setReverseWraparound = function(state) {
1764 this.options_.reverseWraparound = state;
1765};
1766
1767/**
1768 * Selects between the primary and alternate screens.
1769 *
1770 * If alternate mode is on, the alternate screen is active. Otherwise the
1771 * primary screen is active.
1772 *
1773 * Swapping screens has no effect on the scrollback buffer.
1774 *
1775 * Each screen maintains its own cursor position.
1776 *
1777 * Defaults to off.
1778 *
1779 * @param {boolean} state True to set alternate mode, false to unset.
1780 */
1781hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001782 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001783 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1784
rginda35c456b2012-02-09 17:29:05 -08001785 if (this.screen_.rowsArray.length &&
1786 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
1787 // If the screen changed sizes while we were away, our rowIndexes may
1788 // be incorrect.
1789 var offset = this.scrollbackRows_.length;
1790 var ary = this.screen_.rowsArray;
1791 for (i = 0; i < ary.length; i++) {
1792 ary[i].rowIndex = offset + i;
1793 }
1794 }
rginda8ba33642011-12-14 12:31:31 -08001795
rginda35c456b2012-02-09 17:29:05 -08001796 this.realizeWidth_(this.screenSize.width);
1797 this.realizeHeight_(this.screenSize.height);
1798 this.scrollPort_.syncScrollHeight();
1799 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001800
rginda6d397402012-01-17 10:58:29 -08001801 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08001802 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08001803};
1804
1805/**
1806 * Set the cursor-blink mode bit.
1807 *
1808 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1809 * a visible cursor does not blink.
1810 *
1811 * You should make sure to turn blinking off if you're going to dispose of a
1812 * terminal, otherwise you'll leak a timeout.
1813 *
1814 * Defaults to on.
1815 *
1816 * @param {boolean} state True to set cursor-blink mode, false to unset.
1817 */
1818hterm.Terminal.prototype.setCursorBlink = function(state) {
1819 this.options_.cursorBlink = state;
1820
1821 if (!state && this.timeouts_.cursorBlink) {
1822 clearTimeout(this.timeouts_.cursorBlink);
1823 delete this.timeouts_.cursorBlink;
1824 }
1825
1826 if (this.options_.cursorVisible)
1827 this.setCursorVisible(true);
1828};
1829
1830/**
1831 * Set the cursor-visible mode bit.
1832 *
1833 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1834 *
1835 * Defaults to on.
1836 *
1837 * @param {boolean} state True to set cursor-visible mode, false to unset.
1838 */
1839hterm.Terminal.prototype.setCursorVisible = function(state) {
1840 this.options_.cursorVisible = state;
1841
1842 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001843 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001844 return;
1845 }
1846
rginda87b86462011-12-14 13:48:03 -08001847 this.syncCursorPosition_();
1848
1849 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001850
1851 if (this.options_.cursorBlink) {
1852 if (this.timeouts_.cursorBlink)
1853 return;
1854
1855 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1856 500);
1857 } else {
1858 if (this.timeouts_.cursorBlink) {
1859 clearTimeout(this.timeouts_.cursorBlink);
1860 delete this.timeouts_.cursorBlink;
1861 }
1862 }
1863};
1864
1865/**
rginda87b86462011-12-14 13:48:03 -08001866 * Synchronizes the visible cursor and document selection with the current
1867 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08001868 */
1869hterm.Terminal.prototype.syncCursorPosition_ = function() {
1870 var topRowIndex = this.scrollPort_.getTopRowIndex();
1871 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
1872 var cursorRowIndex = this.scrollbackRows_.length +
1873 this.screen_.cursorPosition.row;
1874
1875 if (cursorRowIndex > bottomRowIndex) {
1876 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08001877 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08001878 return;
1879 }
1880
rginda35c456b2012-02-09 17:29:05 -08001881 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
1882 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
1883
rginda8ba33642011-12-14 12:31:31 -08001884 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08001885 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
1886 'px';
1887 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
1888 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08001889
1890 this.cursorNode_.setAttribute('title',
1891 '(' + this.screen_.cursorPosition.row +
1892 ', ' + this.screen_.cursorPosition.column +
1893 ')');
1894
1895 // Update the caret for a11y purposes.
1896 var selection = this.document_.getSelection();
1897 if (selection && selection.isCollapsed)
1898 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08001899};
1900
1901/**
1902 * Synchronizes the visible cursor with the current cursor coordinates.
1903 *
1904 * The sync will happen asynchronously, soon after the call stack winds down.
1905 * Multiple calls will be coalesced into a single sync.
1906 */
1907hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
1908 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08001909 return;
rginda8ba33642011-12-14 12:31:31 -08001910
1911 var self = this;
1912 this.timeouts_.syncCursor = setTimeout(function() {
1913 self.syncCursorPosition_();
1914 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08001915 }, 0);
1916};
1917
rgindacc2996c2012-02-24 14:59:31 -08001918/**
rgindaf522ce02012-04-17 17:49:17 -07001919 * Show or hide the zoom warning.
1920 *
1921 * The zoom warning is a message warning the user that their browser zoom must
1922 * be set to 100% in order for hterm to function properly.
1923 *
1924 * @param {boolean} state True to show the message, false to hide it.
1925 */
1926hterm.Terminal.prototype.showZoomWarning_ = function(state) {
1927 if (!this.zoomWarningNode_) {
1928 if (!state)
1929 return;
1930
1931 this.zoomWarningNode_ = this.document_.createElement('div');
1932 this.zoomWarningNode_.style.cssText = (
1933 'color: black;' +
1934 'background-color: #ff2222;' +
1935 'font-size: large;' +
1936 'border-radius: 8px;' +
1937 'opacity: 0.75;' +
1938 'padding: 0.2em 0.5em 0.2em 0.5em;' +
1939 'top: 0.5em;' +
1940 'right: 1.2em;' +
1941 'position: absolute;' +
1942 '-webkit-text-size-adjust: none;' +
1943 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07001944 }
1945
rgindade84e382012-04-20 15:39:31 -07001946 this.zoomWarningNode_.textContent = hterm.msg('ZOOM_WARNING') ||
1947 ('!! ' + parseInt(this.scrollPort_.characterSize.zoomFactor * 100) +
1948 '% !!');
rgindaf522ce02012-04-17 17:49:17 -07001949 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
1950
1951 if (state) {
1952 if (!this.zoomWarningNode_.parentNode)
1953 this.div_.parentNode.appendChild(this.zoomWarningNode_);
1954 } else if (this.zoomWarningNode_.parentNode) {
1955 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
1956 }
1957};
1958
1959/**
rgindacc2996c2012-02-24 14:59:31 -08001960 * Show the terminal overlay for a given amount of time.
1961 *
1962 * The terminal overlay appears in inverse video in a large font, centered
1963 * over the terminal. You should probably keep the overlay message brief,
1964 * since it's in a large font and you probably aren't going to check the size
1965 * of the terminal first.
1966 *
1967 * @param {string} msg The text (not HTML) message to display in the overlay.
1968 * @param {number} opt_timeout The amount of time to wait before fading out
1969 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
1970 * stay up forever (or until the next overlay).
1971 */
1972hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08001973 if (!this.overlayNode_) {
1974 if (!this.div_)
1975 return;
1976
1977 this.overlayNode_ = this.document_.createElement('div');
1978 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08001979 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08001980 'font-size: xx-large;' +
1981 'opacity: 0.75;' +
1982 'padding: 0.2em 0.5em 0.2em 0.5em;' +
1983 'position: absolute;' +
1984 '-webkit-user-select: none;' +
1985 '-webkit-transition: opacity 180ms ease-in;');
1986 }
1987
rginda9f5222b2012-03-05 11:53:28 -08001988 this.overlayNode_.style.color = this.prefs_.get('background-color');
1989 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
1990 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
1991
rgindaf0090c92012-02-10 14:58:52 -08001992 this.overlayNode_.textContent = msg;
1993 this.overlayNode_.style.opacity = '0.75';
1994
1995 if (!this.overlayNode_.parentNode)
1996 this.div_.appendChild(this.overlayNode_);
1997
1998 this.overlayNode_.style.top = (
1999 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
2000 this.overlayNode_.style.left = (
2001 this.div_.clientWidth - this.overlayNode_.clientWidth -
2002 this.scrollbarWidthPx) / 2;
2003
2004 var self = this;
2005
2006 if (this.overlayTimeout_)
2007 clearTimeout(this.overlayTimeout_);
2008
rgindacc2996c2012-02-24 14:59:31 -08002009 if (opt_timeout === null)
2010 return;
2011
rgindaf0090c92012-02-10 14:58:52 -08002012 this.overlayTimeout_ = setTimeout(function() {
2013 self.overlayNode_.style.opacity = '0';
2014 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002015 if (self.overlayNode_.parentNode)
2016 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002017 self.overlayTimeout_ = null;
2018 self.overlayNode_.style.opacity = '0.75';
2019 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002020 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002021};
2022
2023hterm.Terminal.prototype.overlaySize = function() {
2024 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2025};
2026
rginda87b86462011-12-14 13:48:03 -08002027/**
2028 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2029 *
2030 * @param {string} string The VT string representing the keystroke.
2031 */
2032hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002033 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002034 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2035
2036 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08002037};
2038
2039/**
2040 * React when the ScrollPort is scrolled.
2041 */
2042hterm.Terminal.prototype.onScroll_ = function() {
2043 this.scheduleSyncCursorPosition_();
2044};
2045
2046/**
rginda9846e2f2012-01-27 13:53:33 -08002047 * React when text is pasted into the scrollPort.
2048 */
2049hterm.Terminal.prototype.onPaste_ = function(e) {
2050 this.io.onVTKeystroke(e.text);
2051};
2052
2053/**
rginda8ba33642011-12-14 12:31:31 -08002054 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002055 *
2056 * Note: This function should not directly contain code that alters the internal
2057 * state of the terminal. That kind of code belongs in realizeWidth or
2058 * realizeHeight, so that it can be executed synchronously in the case of a
2059 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002060 */
2061hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002062 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002063 this.scrollPort_.characterSize.width);
2064 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2065 this.scrollPort_.characterSize.height);
2066
2067 if (!(columnCount || rowCount)) {
2068 // We avoid these situations since they happen sometimes when the terminal
2069 // gets removed from the document, and we can't deal with that.
2070 return;
2071 }
2072
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002073 this.realizeSize_(columnCount, rowCount);
rgindac9bc5502012-01-18 11:48:44 -08002074 this.scheduleSyncCursorPosition_();
rgindaf522ce02012-04-17 17:49:17 -07002075 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaf0090c92012-02-10 14:58:52 -08002076 this.overlaySize();
rginda8ba33642011-12-14 12:31:31 -08002077};
2078
2079/**
2080 * Service the cursor blink timeout.
2081 */
2082hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002083 if (this.cursorNode_.style.opacity == '0') {
2084 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002085 } else {
rginda87b86462011-12-14 13:48:03 -08002086 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002087 }
2088};
David Reveman8f552492012-03-28 12:18:41 -04002089
2090/**
2091 * Set the scrollbar-visible mode bit.
2092 *
2093 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2094 * Otherwise it will not.
2095 *
2096 * Defaults to on.
2097 *
2098 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2099 */
2100hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2101 this.scrollPort_.setScrollbarVisible(state);
2102};