blob: 45ebee4e1b1ac6d2e937c63ca8d6607697d93360 [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();
rgindac9bc5502012-01-18 11:48:44 -0800686 this.setVTScrollRegion(null, null);
rginda9ea433c2012-03-16 11:57:00 -0700687
688 this.clearHome(this.primaryScreen_);
689 this.primaryScreen_.textAttributes.reset();
690
691 this.clearHome(this.alternateScreen_);
692 this.alternateScreen_.textAttributes.reset();
693
rgindac9bc5502012-01-18 11:48:44 -0800694 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800695};
696
rgindac9bc5502012-01-18 11:48:44 -0800697/**
698 * Soft terminal reset.
699 */
rginda0f5c0292012-01-13 11:00:13 -0800700hterm.Terminal.prototype.softReset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800701 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700702
703 this.primaryScreen_.textAttributes.resetColorPalette();
704 this.alternateScreen_.textAttributes.resetColorPalette();
705
rgindaa19afe22012-01-25 15:40:22 -0800706 this.setCursorVisible(true);
rgindade84e382012-04-20 15:39:31 -0700707 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda0f5c0292012-01-13 11:00:13 -0800708};
709
rgindac9bc5502012-01-18 11:48:44 -0800710/**
711 * Move the cursor forward to the next tab stop, or to the last column
712 * if no more tab stops are set.
713 */
714hterm.Terminal.prototype.forwardTabStop = function() {
715 var column = this.screen_.cursorPosition.column;
716
717 for (var i = 0; i < this.tabStops_.length; i++) {
718 if (this.tabStops_[i] > column) {
719 this.setCursorColumn(this.tabStops_[i]);
720 return;
721 }
722 }
723
724 this.setCursorColumn(this.screenSize.width - 1);
rginda0f5c0292012-01-13 11:00:13 -0800725};
726
rgindac9bc5502012-01-18 11:48:44 -0800727/**
728 * Move the cursor backward to the previous tab stop, or to the first column
729 * if no previous tab stops are set.
730 */
731hterm.Terminal.prototype.backwardTabStop = function() {
732 var column = this.screen_.cursorPosition.column;
733
734 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
735 if (this.tabStops_[i] < column) {
736 this.setCursorColumn(this.tabStops_[i]);
737 return;
738 }
739 }
740
741 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800742};
743
rgindac9bc5502012-01-18 11:48:44 -0800744/**
745 * Set a tab stop at the given column.
746 *
747 * @param {int} column Zero based column.
748 */
749hterm.Terminal.prototype.setTabStop = function(column) {
750 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
751 if (this.tabStops_[i] == column)
752 return;
753
754 if (this.tabStops_[i] < column) {
755 this.tabStops_.splice(i + 1, 0, column);
756 return;
757 }
758 }
759
760 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800761};
762
rgindac9bc5502012-01-18 11:48:44 -0800763/**
764 * Clear the tab stop at the current cursor position.
765 *
766 * No effect if there is no tab stop at the current cursor position.
767 */
768hterm.Terminal.prototype.clearTabStopAtCursor = function() {
769 var column = this.screen_.cursorPosition.column;
770
771 var i = this.tabStops_.indexOf(column);
772 if (i == -1)
773 return;
774
775 this.tabStops_.splice(i, 1);
776};
777
778/**
779 * Clear all tab stops.
780 */
781hterm.Terminal.prototype.clearAllTabStops = function() {
782 this.tabStops_.length = 0;
783};
784
785/**
786 * Set up the default tab stops, starting from a given column.
787 *
788 * This sets a tabstop every (column % this.tabWidth) column, starting
789 * from the specified column, or 0 if no column is provided.
790 *
791 * This does not clear the existing tab stops first, use clearAllTabStops
792 * for that.
793 *
794 * @param {int} opt_start Optional starting zero based starting column, useful
795 * for filling out missing tab stops when the terminal is resized.
796 */
797hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
798 var start = opt_start || 0;
799 var w = this.tabWidth;
800 var stopCount = Math.floor((this.screenSize.width - start) / this.tabWidth)
801 for (var i = 0; i < stopCount; i++) {
802 this.setTabStop(Math.floor((start + i * w) / w) * w + w);
803 }
rginda87b86462011-12-14 13:48:03 -0800804};
805
rginda6d397402012-01-17 10:58:29 -0800806/**
807 * Save cursor position and attributes.
808 *
809 * TODO(rginda): Save attributes once we support them.
810 */
rginda87b86462011-12-14 13:48:03 -0800811hterm.Terminal.prototype.saveOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800812 this.savedOptions_.cursor = this.saveCursor();
rgindaa19afe22012-01-25 15:40:22 -0800813 this.savedOptions_.textAttributes = this.screen_.textAttributes.clone();
rginda87b86462011-12-14 13:48:03 -0800814};
815
rginda6d397402012-01-17 10:58:29 -0800816/**
817 * Restore cursor position and attributes.
818 *
819 * TODO(rginda): Restore attributes once we support them.
820 */
rginda87b86462011-12-14 13:48:03 -0800821hterm.Terminal.prototype.restoreOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800822 if (this.savedOptions_.cursor)
823 this.restoreCursor(this.savedOptions_.cursor);
rgindaa19afe22012-01-25 15:40:22 -0800824 if (this.savedOptions_.textAttributes)
825 this.screen_.textAttributes = this.savedOptions_.textAttributes;
rginda8ba33642011-12-14 12:31:31 -0800826};
827
828/**
829 * Interpret a sequence of characters.
830 *
831 * Incomplete escape sequences are buffered until the next call.
832 *
833 * @param {string} str Sequence of characters to interpret or pass through.
834 */
835hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800836 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800837 this.scheduleSyncCursorPosition_();
838};
839
840/**
841 * Take over the given DIV for use as the terminal display.
842 *
843 * @param {HTMLDivElement} div The div to use as the terminal display.
844 */
845hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800846 this.div_ = div;
847
rginda8ba33642011-12-14 12:31:31 -0800848 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -0700849 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
850
rginda0918b652012-04-04 11:26:24 -0700851 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -0800852
rginda9f5222b2012-03-05 11:53:28 -0800853 this.setFontSize(this.prefs_.get('font-size'));
854 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -0800855
David Reveman8f552492012-03-28 12:18:41 -0400856 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
857
rginda8ba33642011-12-14 12:31:31 -0800858 this.document_ = this.scrollPort_.getDocument();
859
rginda8ba33642011-12-14 12:31:31 -0800860 this.cursorNode_ = this.document_.createElement('div');
861 this.cursorNode_.style.cssText =
862 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800863 'top: -99px;' +
864 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -0800865 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
866 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda6d397402012-01-17 10:58:29 -0800867 '-webkit-transition: opacity, background-color 100ms linear;' +
rginda9f5222b2012-03-05 11:53:28 -0800868 'background-color: ' + this.prefs_.get('cursor-color'));
rginda8ba33642011-12-14 12:31:31 -0800869 this.document_.body.appendChild(this.cursorNode_);
870
rgindade84e382012-04-20 15:39:31 -0700871 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda8ba33642011-12-14 12:31:31 -0800872 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -0800873
rginda87b86462011-12-14 13:48:03 -0800874 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -0800875 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -0800876};
877
rginda0918b652012-04-04 11:26:24 -0700878/**
879 * Return the HTML document that contains the terminal DOM nodes.
880 */
rginda87b86462011-12-14 13:48:03 -0800881hterm.Terminal.prototype.getDocument = function() {
882 return this.document_;
rginda8ba33642011-12-14 12:31:31 -0800883};
884
885/**
rginda0918b652012-04-04 11:26:24 -0700886 * Focus the terminal.
887 */
888hterm.Terminal.prototype.focus = function() {
889 this.scrollPort_.focus();
890};
891
892/**
rginda8ba33642011-12-14 12:31:31 -0800893 * Return the HTML Element for a given row index.
894 *
895 * This is a method from the RowProvider interface. The ScrollPort uses
896 * it to fetch rows on demand as they are scrolled into view.
897 *
898 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
899 * pairs to conserve memory.
900 *
901 * @param {integer} index The zero-based row index, measured relative to the
902 * start of the scrollback buffer. On-screen rows will always have the
903 * largest indicies.
904 * @return {HTMLElement} The 'x-row' element containing for the requested row.
905 */
906hterm.Terminal.prototype.getRowNode = function(index) {
907 if (index < this.scrollbackRows_.length)
908 return this.scrollbackRows_[index];
909
910 var screenIndex = index - this.scrollbackRows_.length;
911 return this.screen_.rowsArray[screenIndex];
912};
913
914/**
915 * Return the text content for a given range of rows.
916 *
917 * This is a method from the RowProvider interface. The ScrollPort uses
918 * it to fetch text content on demand when the user attempts to copy their
919 * selection to the clipboard.
920 *
921 * @param {integer} start The zero-based row index to start from, measured
922 * relative to the start of the scrollback buffer. On-screen rows will
923 * always have the largest indicies.
924 * @param {integer} end The zero-based row index to end on, measured
925 * relative to the start of the scrollback buffer.
926 * @return {string} A single string containing the text value of the range of
927 * rows. Lines will be newline delimited, with no trailing newline.
928 */
929hterm.Terminal.prototype.getRowsText = function(start, end) {
930 var ary = [];
931 for (var i = start; i < end; i++) {
932 var node = this.getRowNode(i);
933 ary.push(node.textContent);
934 }
935
936 return ary.join('\n');
937};
938
939/**
940 * Return the text content for a given row.
941 *
942 * This is a method from the RowProvider interface. The ScrollPort uses
943 * it to fetch text content on demand when the user attempts to copy their
944 * selection to the clipboard.
945 *
946 * @param {integer} index The zero-based row index to return, measured
947 * relative to the start of the scrollback buffer. On-screen rows will
948 * always have the largest indicies.
949 * @return {string} A string containing the text value of the selected row.
950 */
951hterm.Terminal.prototype.getRowText = function(index) {
952 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -0800953 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -0800954};
955
956/**
957 * Return the total number of rows in the addressable screen and in the
958 * scrollback buffer of this terminal.
959 *
960 * This is a method from the RowProvider interface. The ScrollPort uses
961 * it to compute the size of the scrollbar.
962 *
963 * @return {integer} The number of rows in this terminal.
964 */
965hterm.Terminal.prototype.getRowCount = function() {
966 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
967};
968
969/**
970 * Create DOM nodes for new rows and append them to the end of the terminal.
971 *
972 * This is the only correct way to add a new DOM node for a row. Notice that
973 * the new row is appended to the bottom of the list of rows, and does not
974 * require renumbering (of the rowIndex property) of previous rows.
975 *
976 * If you think you want a new blank row somewhere in the middle of the
977 * terminal, look into moveRows_().
978 *
979 * This method does not pay attention to vtScrollTop/Bottom, since you should
980 * be using moveRows() in cases where they would matter.
981 *
982 * The cursor will be positioned at column 0 of the first inserted line.
983 */
984hterm.Terminal.prototype.appendRows_ = function(count) {
985 var cursorRow = this.screen_.rowsArray.length;
986 var offset = this.scrollbackRows_.length + cursorRow;
987 for (var i = 0; i < count; i++) {
988 var row = this.document_.createElement('x-row');
989 row.appendChild(this.document_.createTextNode(''));
990 row.rowIndex = offset + i;
991 this.screen_.pushRow(row);
992 }
993
994 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
995 if (extraRows > 0) {
996 var ary = this.screen_.shiftRows(extraRows);
997 Array.prototype.push.apply(this.scrollbackRows_, ary);
998 this.scheduleScrollDown_();
999 }
1000
1001 if (cursorRow >= this.screen_.rowsArray.length)
1002 cursorRow = this.screen_.rowsArray.length - 1;
1003
rginda87b86462011-12-14 13:48:03 -08001004 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001005};
1006
1007/**
1008 * Relocate rows from one part of the addressable screen to another.
1009 *
1010 * This is used to recycle rows during VT scrolls (those which are driven
1011 * by VT commands, rather than by the user manipulating the scrollbar.)
1012 *
1013 * In this case, the blank lines scrolled into the scroll region are made of
1014 * the nodes we scrolled off. These have their rowIndex properties carefully
1015 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001016 */
1017hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1018 var ary = this.screen_.removeRows(fromIndex, count);
1019 this.screen_.insertRows(toIndex, ary);
1020
1021 var start, end;
1022 if (fromIndex < toIndex) {
1023 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001024 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001025 } else {
1026 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001027 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001028 }
1029
1030 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001031 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001032};
1033
1034/**
1035 * Renumber the rowIndex property of the given range of rows.
1036 *
1037 * The start and end indicies are relative to the screen, not the scrollback.
1038 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001039 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001040 * no need to renumber scrollback rows.
1041 */
1042hterm.Terminal.prototype.renumberRows_ = function(start, end) {
1043 var offset = this.scrollbackRows_.length;
1044 for (var i = start; i < end; i++) {
1045 this.screen_.rowsArray[i].rowIndex = offset + i;
1046 }
1047};
1048
1049/**
1050 * Print a string to the terminal.
1051 *
1052 * This respects the current insert and wraparound modes. It will add new lines
1053 * to the end of the terminal, scrolling off the top into the scrollback buffer
1054 * if necessary.
1055 *
1056 * The string is *not* parsed for escape codes. Use the interpret() method if
1057 * that's what you're after.
1058 *
1059 * @param{string} str The string to print.
1060 */
1061hterm.Terminal.prototype.print = function(str) {
rgindaa19afe22012-01-25 15:40:22 -08001062 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
1063 this.newLine();
rginda2312fff2012-01-05 16:20:52 -08001064
rgindaa19afe22012-01-25 15:40:22 -08001065 if (this.options_.insertMode) {
1066 this.screen_.insertString(str);
1067 } else {
1068 this.screen_.overwriteString(str);
1069 }
1070
1071 var overflow = this.screen_.maybeClipCurrentRow();
1072
1073 if (this.options_.wraparound && overflow) {
1074 var lastColumn;
1075
1076 do {
rginda35c456b2012-02-09 17:29:05 -08001077 this.newLine();
1078 lastColumn = overflow.characterLength;
rgindaa19afe22012-01-25 15:40:22 -08001079
1080 if (!this.options_.insertMode)
1081 this.screen_.deleteChars(overflow.characterLength);
1082
1083 this.screen_.prependNodes(overflow);
rgindaa19afe22012-01-25 15:40:22 -08001084
1085 overflow = this.screen_.maybeClipCurrentRow();
1086 } while (overflow);
1087
1088 this.setCursorColumn(lastColumn);
1089 }
rginda8ba33642011-12-14 12:31:31 -08001090
1091 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001092
rginda9f5222b2012-03-05 11:53:28 -08001093 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001094 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001095};
1096
1097/**
rginda87b86462011-12-14 13:48:03 -08001098 * Set the VT scroll region.
1099 *
rginda87b86462011-12-14 13:48:03 -08001100 * This also resets the cursor position to the absolute (0, 0) position, since
1101 * that's what xterm appears to do.
1102 *
1103 * @param {integer} scrollTop The zero-based top of the scroll region.
1104 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1105 * inclusive.
1106 */
1107hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1108 this.vtScrollTop_ = scrollTop;
1109 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001110};
1111
1112/**
rginda8ba33642011-12-14 12:31:31 -08001113 * Return the top row index according to the VT.
1114 *
1115 * This will return 0 unless the terminal has been told to restrict scrolling
1116 * to some lower row. It is used for some VT cursor positioning and scrolling
1117 * commands.
1118 *
1119 * @return {integer} The topmost row in the terminal's scroll region.
1120 */
1121hterm.Terminal.prototype.getVTScrollTop = function() {
1122 if (this.vtScrollTop_ != null)
1123 return this.vtScrollTop_;
1124
1125 return 0;
rginda87b86462011-12-14 13:48:03 -08001126};
rginda8ba33642011-12-14 12:31:31 -08001127
1128/**
1129 * Return the bottom row index according to the VT.
1130 *
1131 * This will return the height of the terminal unless the it has been told to
1132 * restrict scrolling to some higher row. It is used for some VT cursor
1133 * positioning and scrolling commands.
1134 *
1135 * @return {integer} The bottommost row in the terminal's scroll region.
1136 */
1137hterm.Terminal.prototype.getVTScrollBottom = function() {
1138 if (this.vtScrollBottom_ != null)
1139 return this.vtScrollBottom_;
1140
rginda87b86462011-12-14 13:48:03 -08001141 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001142}
1143
1144/**
1145 * Process a '\n' character.
1146 *
1147 * If the cursor is on the final row of the terminal this will append a new
1148 * blank row to the screen and scroll the topmost row into the scrollback
1149 * buffer.
1150 *
1151 * Otherwise, this moves the cursor to column zero of the next row.
1152 */
1153hterm.Terminal.prototype.newLine = function() {
1154 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001155 // If we're at the end of the screen we need to append a new line and
1156 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001157 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001158 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1159 // End of the scroll region does not affect the scrollback buffer.
1160 this.vtScrollUp(1);
1161 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001162 } else {
rginda87b86462011-12-14 13:48:03 -08001163 // Anywhere else in the screen just moves the cursor.
1164 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001165 }
1166};
1167
1168/**
1169 * Like newLine(), except maintain the cursor column.
1170 */
1171hterm.Terminal.prototype.lineFeed = function() {
1172 var column = this.screen_.cursorPosition.column;
1173 this.newLine();
1174 this.setCursorColumn(column);
1175};
1176
1177/**
rginda87b86462011-12-14 13:48:03 -08001178 * If autoCarriageReturn is set then newLine(), else lineFeed().
1179 */
1180hterm.Terminal.prototype.formFeed = function() {
1181 if (this.options_.autoCarriageReturn) {
1182 this.newLine();
1183 } else {
1184 this.lineFeed();
1185 }
1186};
1187
1188/**
1189 * Move the cursor up one row, possibly inserting a blank line.
1190 *
1191 * The cursor column is not changed.
1192 */
1193hterm.Terminal.prototype.reverseLineFeed = function() {
1194 var scrollTop = this.getVTScrollTop();
1195 var currentRow = this.screen_.cursorPosition.row;
1196
1197 if (currentRow == scrollTop) {
1198 this.insertLines(1);
1199 } else {
1200 this.setAbsoluteCursorRow(currentRow - 1);
1201 }
1202};
1203
1204/**
rginda8ba33642011-12-14 12:31:31 -08001205 * Replace all characters to the left of the current cursor with the space
1206 * character.
1207 *
1208 * TODO(rginda): This should probably *remove* the characters (not just replace
1209 * with a space) if there are no characters at or beyond the current cursor
1210 * position. Once it does that, it'll have the same text-attribute related
1211 * issues as hterm.Screen.prototype.clearCursorRow :/
1212 */
1213hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001214 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001215 this.setCursorColumn(0);
rginda87b86462011-12-14 13:48:03 -08001216 this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1));
1217 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001218};
1219
1220/**
1221 * Erase a given number of characters to the right of the cursor, shifting
1222 * remaining characters to the left.
1223 *
1224 * The cursor position is unchanged.
1225 *
1226 * TODO(rginda): Test that this works even when the cursor is positioned beyond
1227 * the end of the text.
1228 *
1229 * TODO(rginda): This likely has text-attribute related troubles similar to the
1230 * todo on hterm.Screen.prototype.clearCursorRow.
1231 */
1232hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001233 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001234
rginda87b86462011-12-14 13:48:03 -08001235 var maxCount = this.screenSize.width - cursor.column;
rginda8ba33642011-12-14 12:31:31 -08001236 var count = (opt_count && opt_count < maxCount) ? opt_count : maxCount;
1237 this.screen_.deleteChars(count);
rginda87b86462011-12-14 13:48:03 -08001238 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001239};
1240
1241/**
1242 * Erase the current line.
1243 *
1244 * The cursor position is unchanged.
1245 *
1246 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1247 * has a text-attribute related TODO.
1248 */
1249hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001250 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001251 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001252 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001253};
1254
1255/**
1256 * Erase all characters from the start of the scroll region to the current
1257 * cursor position.
1258 *
1259 * The cursor position is unchanged.
1260 *
1261 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1262 * has a text-attribute related TODO.
1263 */
1264hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001265 var cursor = this.saveCursor();
1266
1267 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001268
1269 var top = this.getVTScrollTop();
rginda87b86462011-12-14 13:48:03 -08001270 for (var i = top; i < cursor.row; i++) {
1271 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001272 this.screen_.clearCursorRow();
1273 }
1274
rginda87b86462011-12-14 13:48:03 -08001275 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001276};
1277
1278/**
1279 * Erase all characters from the current cursor position to the end of the
1280 * scroll region.
1281 *
1282 * The cursor position is unchanged.
1283 *
1284 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1285 * has a text-attribute related TODO.
1286 */
1287hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001288 var cursor = this.saveCursor();
1289
1290 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001291
1292 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001293 for (var i = cursor.row + 1; i <= bottom; i++) {
1294 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001295 this.screen_.clearCursorRow();
1296 }
1297
rginda87b86462011-12-14 13:48:03 -08001298 this.restoreCursor(cursor);
1299};
1300
1301/**
1302 * Fill the terminal with a given character.
1303 *
1304 * This methods does not respect the VT scroll region.
1305 *
1306 * @param {string} ch The character to use for the fill.
1307 */
1308hterm.Terminal.prototype.fill = function(ch) {
1309 var cursor = this.saveCursor();
1310
1311 this.setAbsoluteCursorPosition(0, 0);
1312 for (var row = 0; row < this.screenSize.height; row++) {
1313 for (var col = 0; col < this.screenSize.width; col++) {
1314 this.setAbsoluteCursorPosition(row, col);
1315 this.screen_.overwriteString(ch);
1316 }
1317 }
1318
1319 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001320};
1321
1322/**
rginda9ea433c2012-03-16 11:57:00 -07001323 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001324 *
rginda9ea433c2012-03-16 11:57:00 -07001325 * This does not respect the scroll region.
1326 *
1327 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1328 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001329 *
1330 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1331 * has a text-attribute related TODO.
1332 */
rginda9ea433c2012-03-16 11:57:00 -07001333hterm.Terminal.prototype.clearHome = function(opt_screen) {
1334 var screen = opt_screen || this.screen_;
1335 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001336
rginda11057d52012-04-25 12:29:56 -07001337 if (bottom == 0) {
1338 // Empty screen, nothing to do.
1339 return;
1340 }
1341
rgindae4d29232012-01-19 10:47:13 -08001342 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001343 screen.setCursorPosition(i, 0);
1344 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001345 }
1346
rginda9ea433c2012-03-16 11:57:00 -07001347 screen.setCursorPosition(0, 0);
1348};
1349
1350/**
1351 * Erase the entire display without changing the cursor position.
1352 *
1353 * The cursor position is unchanged. This does not respect the scroll
1354 * region.
1355 *
1356 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1357 * to the current screen.
1358 *
1359 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1360 * has a text-attribute related TODO.
1361 */
1362hterm.Terminal.prototype.clear = function(opt_screen) {
1363 var screen = opt_screen || this.screen_;
1364 var cursor = screen.cursorPosition.clone();
1365 this.clearHome(screen);
1366 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001367};
1368
1369/**
1370 * VT command to insert lines at the current cursor row.
1371 *
1372 * This respects the current scroll region. Rows pushed off the bottom are
1373 * lost (they won't show up in the scrollback buffer).
1374 *
1375 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1376 * has a text-attribute related TODO.
1377 *
1378 * @param {integer} count The number of lines to insert.
1379 */
1380hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001381 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001382
1383 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001384 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001385
rgindae4d29232012-01-19 10:47:13 -08001386 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001387 if (start != cursor.row)
1388 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001389
1390 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001391 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001392 this.screen_.clearCursorRow();
1393 }
1394
rginda87b86462011-12-14 13:48:03 -08001395 cursor.column = 0;
1396 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001397};
1398
1399/**
1400 * VT command to delete lines at the current cursor row.
1401 *
1402 * New rows are added to the bottom of scroll region to take their place. New
1403 * rows are strictly there to take up space and have no content or style.
1404 */
1405hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001406 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001407
rginda87b86462011-12-14 13:48:03 -08001408 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001409 var bottom = this.getVTScrollBottom();
1410
rginda87b86462011-12-14 13:48:03 -08001411 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001412 count = Math.min(count, maxCount);
1413
rginda87b86462011-12-14 13:48:03 -08001414 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001415 if (count != maxCount)
1416 this.moveRows_(top, count, moveStart);
1417
1418 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001419 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001420 this.screen_.clearCursorRow();
1421 }
1422
rginda87b86462011-12-14 13:48:03 -08001423 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001424};
1425
1426/**
1427 * Inserts the given number of spaces at the current cursor position.
1428 *
rginda87b86462011-12-14 13:48:03 -08001429 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001430 */
1431hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001432 var cursor = this.saveCursor();
1433
rginda0f5c0292012-01-13 11:00:13 -08001434 var ws = hterm.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001435 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001436 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001437
1438 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001439};
1440
1441/**
1442 * Forward-delete the specified number of characters starting at the cursor
1443 * position.
1444 *
1445 * @param {integer} count The number of characters to delete.
1446 */
1447hterm.Terminal.prototype.deleteChars = function(count) {
1448 this.screen_.deleteChars(count);
1449};
1450
1451/**
1452 * Shift rows in the scroll region upwards by a given number of lines.
1453 *
1454 * New rows are inserted at the bottom of the scroll region to fill the
1455 * vacated rows. The new rows not filled out with the current text attributes.
1456 *
1457 * This function does not affect the scrollback rows at all. Rows shifted
1458 * off the top are lost.
1459 *
rginda87b86462011-12-14 13:48:03 -08001460 * The cursor position is not altered.
1461 *
rginda8ba33642011-12-14 12:31:31 -08001462 * @param {integer} count The number of rows to scroll.
1463 */
1464hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001465 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001466
rginda87b86462011-12-14 13:48:03 -08001467 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001468 this.deleteLines(count);
1469
rginda87b86462011-12-14 13:48:03 -08001470 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001471};
1472
1473/**
1474 * Shift rows below the cursor down by a given number of lines.
1475 *
1476 * This function respects the current scroll region.
1477 *
1478 * New rows are inserted at the top of the scroll region to fill the
1479 * vacated rows. The new rows not filled out with the current text attributes.
1480 *
1481 * This function does not affect the scrollback rows at all. Rows shifted
1482 * off the bottom are lost.
1483 *
1484 * @param {integer} count The number of rows to scroll.
1485 */
1486hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001487 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001488
rginda87b86462011-12-14 13:48:03 -08001489 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001490 this.insertLines(opt_count);
1491
rginda87b86462011-12-14 13:48:03 -08001492 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001493};
1494
rginda87b86462011-12-14 13:48:03 -08001495
rginda8ba33642011-12-14 12:31:31 -08001496/**
1497 * Set the cursor position.
1498 *
1499 * The cursor row is relative to the scroll region if the terminal has
1500 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1501 *
1502 * @param {integer} row The new zero-based cursor row.
1503 * @param {integer} row The new zero-based cursor column.
1504 */
1505hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1506 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001507 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001508 } else {
rginda87b86462011-12-14 13:48:03 -08001509 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001510 }
rginda87b86462011-12-14 13:48:03 -08001511};
rginda8ba33642011-12-14 12:31:31 -08001512
rginda87b86462011-12-14 13:48:03 -08001513hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1514 var scrollTop = this.getVTScrollTop();
1515 row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
rginda2312fff2012-01-05 16:20:52 -08001516 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001517 this.screen_.setCursorPosition(row, column);
1518};
1519
1520hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rginda2312fff2012-01-05 16:20:52 -08001521 row = hterm.clamp(row, 0, this.screenSize.height - 1);
1522 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001523 this.screen_.setCursorPosition(row, column);
1524};
1525
1526/**
1527 * Set the cursor column.
1528 *
1529 * @param {integer} column The new zero-based cursor column.
1530 */
1531hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001532 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001533};
1534
1535/**
1536 * Return the cursor column.
1537 *
1538 * @return {integer} The zero-based cursor column.
1539 */
1540hterm.Terminal.prototype.getCursorColumn = function() {
1541 return this.screen_.cursorPosition.column;
1542};
1543
1544/**
1545 * Set the cursor row.
1546 *
1547 * The cursor row is relative to the scroll region if the terminal has
1548 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1549 *
1550 * @param {integer} row The new cursor row.
1551 */
rginda87b86462011-12-14 13:48:03 -08001552hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1553 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001554};
1555
1556/**
1557 * Return the cursor row.
1558 *
1559 * @return {integer} The zero-based cursor row.
1560 */
1561hterm.Terminal.prototype.getCursorRow = function(row) {
1562 return this.screen_.cursorPosition.row;
1563};
1564
1565/**
1566 * Request that the ScrollPort redraw itself soon.
1567 *
1568 * The redraw will happen asynchronously, soon after the call stack winds down.
1569 * Multiple calls will be coalesced into a single redraw.
1570 */
1571hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001572 if (this.timeouts_.redraw)
1573 return;
rginda8ba33642011-12-14 12:31:31 -08001574
1575 var self = this;
rginda87b86462011-12-14 13:48:03 -08001576 this.timeouts_.redraw = setTimeout(function() {
1577 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001578 self.scrollPort_.redraw_();
1579 }, 0);
1580};
1581
1582/**
1583 * Request that the ScrollPort be scrolled to the bottom.
1584 *
1585 * The scroll will happen asynchronously, soon after the call stack winds down.
1586 * Multiple calls will be coalesced into a single scroll.
1587 *
1588 * This affects the scrollbar position of the ScrollPort, and has nothing to
1589 * do with the VT scroll commands.
1590 */
1591hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1592 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001593 return;
rginda8ba33642011-12-14 12:31:31 -08001594
1595 var self = this;
1596 this.timeouts_.scrollDown = setTimeout(function() {
1597 delete self.timeouts_.scrollDown;
1598 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1599 }, 10);
1600};
1601
1602/**
1603 * Move the cursor up a specified number of rows.
1604 *
1605 * @param {integer} count The number of rows to move the cursor.
1606 */
1607hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001608 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001609};
1610
1611/**
1612 * Move the cursor down a specified number of rows.
1613 *
1614 * @param {integer} count The number of rows to move the cursor.
1615 */
1616hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001617 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001618 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1619 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1620 this.screenSize.height - 1);
1621
1622 var row = hterm.clamp(this.screen_.cursorPosition.row + count,
1623 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001624 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001625};
1626
1627/**
1628 * Move the cursor left a specified number of columns.
1629 *
1630 * @param {integer} count The number of columns to move the cursor.
1631 */
1632hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001633 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001634};
1635
1636/**
1637 * Move the cursor right a specified number of columns.
1638 *
1639 * @param {integer} count The number of columns to move the cursor.
1640 */
1641hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001642 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001643 var column = hterm.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001644 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001645 this.setCursorColumn(column);
1646};
1647
1648/**
1649 * Reverse the foreground and background colors of the terminal.
1650 *
1651 * This only affects text that was drawn with no attributes.
1652 *
1653 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1654 * been drawn with attributes that happen to coincide with the default
1655 * 'no-attribute' colors. My guess is probably not.
1656 */
1657hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001658 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001659 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001660 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1661 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001662 } else {
rginda9f5222b2012-03-05 11:53:28 -08001663 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1664 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001665 }
1666};
1667
1668/**
rginda87b86462011-12-14 13:48:03 -08001669 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001670 */
1671hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001672 if (this.bellAudio_.getAttribute('src'))
1673 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001674
rginda6d397402012-01-17 10:58:29 -08001675 this.cursorNode_.style.backgroundColor =
1676 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001677
1678 var self = this;
1679 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001680 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001681 }, 200);
rginda87b86462011-12-14 13:48:03 -08001682};
1683
1684/**
rginda8ba33642011-12-14 12:31:31 -08001685 * Set the origin mode bit.
1686 *
1687 * If origin mode is on, certain VT cursor and scrolling commands measure their
1688 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1689 * to the top of the addressable screen.
1690 *
1691 * Defaults to off.
1692 *
1693 * @param {boolean} state True to set origin mode, false to unset.
1694 */
1695hterm.Terminal.prototype.setOriginMode = function(state) {
1696 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001697 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001698};
1699
1700/**
1701 * Set the insert mode bit.
1702 *
1703 * If insert mode is on, existing text beyond the cursor position will be
1704 * shifted right to make room for new text. Otherwise, new text overwrites
1705 * any existing text.
1706 *
1707 * Defaults to off.
1708 *
1709 * @param {boolean} state True to set insert mode, false to unset.
1710 */
1711hterm.Terminal.prototype.setInsertMode = function(state) {
1712 this.options_.insertMode = state;
1713};
1714
1715/**
rginda87b86462011-12-14 13:48:03 -08001716 * Set the auto carriage return bit.
1717 *
1718 * If auto carriage return is on then a formfeed character is interpreted
1719 * as a newline, otherwise it's the same as a linefeed. The difference boils
1720 * down to whether or not the cursor column is reset.
1721 */
1722hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1723 this.options_.autoCarriageReturn = state;
1724};
1725
1726/**
rginda8ba33642011-12-14 12:31:31 -08001727 * Set the wraparound mode bit.
1728 *
1729 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1730 * to the start of the following row. Otherwise, the cursor is clamped to the
1731 * end of the screen and attempts to write past it are ignored.
1732 *
1733 * Defaults to on.
1734 *
1735 * @param {boolean} state True to set wraparound mode, false to unset.
1736 */
1737hterm.Terminal.prototype.setWraparound = function(state) {
1738 this.options_.wraparound = state;
1739};
1740
1741/**
1742 * Set the reverse-wraparound mode bit.
1743 *
1744 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1745 * to the end of the previous row. Otherwise, the cursor is clamped to column
1746 * 0.
1747 *
1748 * Defaults to off.
1749 *
1750 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1751 */
1752hterm.Terminal.prototype.setReverseWraparound = function(state) {
1753 this.options_.reverseWraparound = state;
1754};
1755
1756/**
1757 * Selects between the primary and alternate screens.
1758 *
1759 * If alternate mode is on, the alternate screen is active. Otherwise the
1760 * primary screen is active.
1761 *
1762 * Swapping screens has no effect on the scrollback buffer.
1763 *
1764 * Each screen maintains its own cursor position.
1765 *
1766 * Defaults to off.
1767 *
1768 * @param {boolean} state True to set alternate mode, false to unset.
1769 */
1770hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001771 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001772 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1773
rginda35c456b2012-02-09 17:29:05 -08001774 if (this.screen_.rowsArray.length &&
1775 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
1776 // If the screen changed sizes while we were away, our rowIndexes may
1777 // be incorrect.
1778 var offset = this.scrollbackRows_.length;
1779 var ary = this.screen_.rowsArray;
1780 for (i = 0; i < ary.length; i++) {
1781 ary[i].rowIndex = offset + i;
1782 }
1783 }
rginda8ba33642011-12-14 12:31:31 -08001784
rginda35c456b2012-02-09 17:29:05 -08001785 this.realizeWidth_(this.screenSize.width);
1786 this.realizeHeight_(this.screenSize.height);
1787 this.scrollPort_.syncScrollHeight();
1788 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001789
rginda6d397402012-01-17 10:58:29 -08001790 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08001791 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08001792};
1793
1794/**
1795 * Set the cursor-blink mode bit.
1796 *
1797 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1798 * a visible cursor does not blink.
1799 *
1800 * You should make sure to turn blinking off if you're going to dispose of a
1801 * terminal, otherwise you'll leak a timeout.
1802 *
1803 * Defaults to on.
1804 *
1805 * @param {boolean} state True to set cursor-blink mode, false to unset.
1806 */
1807hterm.Terminal.prototype.setCursorBlink = function(state) {
1808 this.options_.cursorBlink = state;
1809
1810 if (!state && this.timeouts_.cursorBlink) {
1811 clearTimeout(this.timeouts_.cursorBlink);
1812 delete this.timeouts_.cursorBlink;
1813 }
1814
1815 if (this.options_.cursorVisible)
1816 this.setCursorVisible(true);
1817};
1818
1819/**
1820 * Set the cursor-visible mode bit.
1821 *
1822 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1823 *
1824 * Defaults to on.
1825 *
1826 * @param {boolean} state True to set cursor-visible mode, false to unset.
1827 */
1828hterm.Terminal.prototype.setCursorVisible = function(state) {
1829 this.options_.cursorVisible = state;
1830
1831 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001832 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001833 return;
1834 }
1835
rginda87b86462011-12-14 13:48:03 -08001836 this.syncCursorPosition_();
1837
1838 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001839
1840 if (this.options_.cursorBlink) {
1841 if (this.timeouts_.cursorBlink)
1842 return;
1843
1844 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1845 500);
1846 } else {
1847 if (this.timeouts_.cursorBlink) {
1848 clearTimeout(this.timeouts_.cursorBlink);
1849 delete this.timeouts_.cursorBlink;
1850 }
1851 }
1852};
1853
1854/**
rginda87b86462011-12-14 13:48:03 -08001855 * Synchronizes the visible cursor and document selection with the current
1856 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08001857 */
1858hterm.Terminal.prototype.syncCursorPosition_ = function() {
1859 var topRowIndex = this.scrollPort_.getTopRowIndex();
1860 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
1861 var cursorRowIndex = this.scrollbackRows_.length +
1862 this.screen_.cursorPosition.row;
1863
1864 if (cursorRowIndex > bottomRowIndex) {
1865 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08001866 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08001867 return;
1868 }
1869
rginda35c456b2012-02-09 17:29:05 -08001870 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
1871 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
1872
rginda8ba33642011-12-14 12:31:31 -08001873 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08001874 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
1875 'px';
1876 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
1877 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08001878
1879 this.cursorNode_.setAttribute('title',
1880 '(' + this.screen_.cursorPosition.row +
1881 ', ' + this.screen_.cursorPosition.column +
1882 ')');
1883
1884 // Update the caret for a11y purposes.
1885 var selection = this.document_.getSelection();
1886 if (selection && selection.isCollapsed)
1887 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08001888};
1889
1890/**
1891 * Synchronizes the visible cursor with the current cursor coordinates.
1892 *
1893 * The sync will happen asynchronously, soon after the call stack winds down.
1894 * Multiple calls will be coalesced into a single sync.
1895 */
1896hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
1897 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08001898 return;
rginda8ba33642011-12-14 12:31:31 -08001899
1900 var self = this;
1901 this.timeouts_.syncCursor = setTimeout(function() {
1902 self.syncCursorPosition_();
1903 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08001904 }, 0);
1905};
1906
rgindacc2996c2012-02-24 14:59:31 -08001907/**
rgindaf522ce02012-04-17 17:49:17 -07001908 * Show or hide the zoom warning.
1909 *
1910 * The zoom warning is a message warning the user that their browser zoom must
1911 * be set to 100% in order for hterm to function properly.
1912 *
1913 * @param {boolean} state True to show the message, false to hide it.
1914 */
1915hterm.Terminal.prototype.showZoomWarning_ = function(state) {
1916 if (!this.zoomWarningNode_) {
1917 if (!state)
1918 return;
1919
1920 this.zoomWarningNode_ = this.document_.createElement('div');
1921 this.zoomWarningNode_.style.cssText = (
1922 'color: black;' +
1923 'background-color: #ff2222;' +
1924 'font-size: large;' +
1925 'border-radius: 8px;' +
1926 'opacity: 0.75;' +
1927 'padding: 0.2em 0.5em 0.2em 0.5em;' +
1928 'top: 0.5em;' +
1929 'right: 1.2em;' +
1930 'position: absolute;' +
1931 '-webkit-text-size-adjust: none;' +
1932 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07001933 }
1934
rgindade84e382012-04-20 15:39:31 -07001935 this.zoomWarningNode_.textContent = hterm.msg('ZOOM_WARNING') ||
1936 ('!! ' + parseInt(this.scrollPort_.characterSize.zoomFactor * 100) +
1937 '% !!');
rgindaf522ce02012-04-17 17:49:17 -07001938 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
1939
1940 if (state) {
1941 if (!this.zoomWarningNode_.parentNode)
1942 this.div_.parentNode.appendChild(this.zoomWarningNode_);
1943 } else if (this.zoomWarningNode_.parentNode) {
1944 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
1945 }
1946};
1947
1948/**
rgindacc2996c2012-02-24 14:59:31 -08001949 * Show the terminal overlay for a given amount of time.
1950 *
1951 * The terminal overlay appears in inverse video in a large font, centered
1952 * over the terminal. You should probably keep the overlay message brief,
1953 * since it's in a large font and you probably aren't going to check the size
1954 * of the terminal first.
1955 *
1956 * @param {string} msg The text (not HTML) message to display in the overlay.
1957 * @param {number} opt_timeout The amount of time to wait before fading out
1958 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
1959 * stay up forever (or until the next overlay).
1960 */
1961hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08001962 if (!this.overlayNode_) {
1963 if (!this.div_)
1964 return;
1965
1966 this.overlayNode_ = this.document_.createElement('div');
1967 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08001968 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08001969 'font-size: xx-large;' +
1970 'opacity: 0.75;' +
1971 'padding: 0.2em 0.5em 0.2em 0.5em;' +
1972 'position: absolute;' +
1973 '-webkit-user-select: none;' +
1974 '-webkit-transition: opacity 180ms ease-in;');
1975 }
1976
rginda9f5222b2012-03-05 11:53:28 -08001977 this.overlayNode_.style.color = this.prefs_.get('background-color');
1978 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
1979 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
1980
rgindaf0090c92012-02-10 14:58:52 -08001981 this.overlayNode_.textContent = msg;
1982 this.overlayNode_.style.opacity = '0.75';
1983
1984 if (!this.overlayNode_.parentNode)
1985 this.div_.appendChild(this.overlayNode_);
1986
1987 this.overlayNode_.style.top = (
1988 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
1989 this.overlayNode_.style.left = (
1990 this.div_.clientWidth - this.overlayNode_.clientWidth -
1991 this.scrollbarWidthPx) / 2;
1992
1993 var self = this;
1994
1995 if (this.overlayTimeout_)
1996 clearTimeout(this.overlayTimeout_);
1997
rgindacc2996c2012-02-24 14:59:31 -08001998 if (opt_timeout === null)
1999 return;
2000
rgindaf0090c92012-02-10 14:58:52 -08002001 this.overlayTimeout_ = setTimeout(function() {
2002 self.overlayNode_.style.opacity = '0';
2003 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002004 if (self.overlayNode_.parentNode)
2005 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002006 self.overlayTimeout_ = null;
2007 self.overlayNode_.style.opacity = '0.75';
2008 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002009 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002010};
2011
2012hterm.Terminal.prototype.overlaySize = function() {
2013 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2014};
2015
rginda87b86462011-12-14 13:48:03 -08002016/**
2017 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2018 *
2019 * @param {string} string The VT string representing the keystroke.
2020 */
2021hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002022 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002023 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2024
2025 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08002026};
2027
2028/**
2029 * React when the ScrollPort is scrolled.
2030 */
2031hterm.Terminal.prototype.onScroll_ = function() {
2032 this.scheduleSyncCursorPosition_();
2033};
2034
2035/**
rginda9846e2f2012-01-27 13:53:33 -08002036 * React when text is pasted into the scrollPort.
2037 */
2038hterm.Terminal.prototype.onPaste_ = function(e) {
2039 this.io.onVTKeystroke(e.text);
2040};
2041
2042/**
rginda8ba33642011-12-14 12:31:31 -08002043 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002044 *
2045 * Note: This function should not directly contain code that alters the internal
2046 * state of the terminal. That kind of code belongs in realizeWidth or
2047 * realizeHeight, so that it can be executed synchronously in the case of a
2048 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002049 */
2050hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002051 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002052 this.scrollPort_.characterSize.width);
2053 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2054 this.scrollPort_.characterSize.height);
2055
2056 if (!(columnCount || rowCount)) {
2057 // We avoid these situations since they happen sometimes when the terminal
2058 // gets removed from the document, and we can't deal with that.
2059 return;
2060 }
2061
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002062 this.realizeSize_(columnCount, rowCount);
rgindac9bc5502012-01-18 11:48:44 -08002063 this.scheduleSyncCursorPosition_();
rgindaf522ce02012-04-17 17:49:17 -07002064 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaf0090c92012-02-10 14:58:52 -08002065 this.overlaySize();
rginda8ba33642011-12-14 12:31:31 -08002066};
2067
2068/**
2069 * Service the cursor blink timeout.
2070 */
2071hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002072 if (this.cursorNode_.style.opacity == '0') {
2073 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002074 } else {
rginda87b86462011-12-14 13:48:03 -08002075 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002076 }
2077};
David Reveman8f552492012-03-28 12:18:41 -04002078
2079/**
2080 * Set the scrollbar-visible mode bit.
2081 *
2082 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2083 * Otherwise it will not.
2084 *
2085 * Defaults to on.
2086 *
2087 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2088 */
2089hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2090 this.scrollPort_.setScrollbarVisible(state);
2091};