blob: 8c06c5307665720c8d1c15c14d440e24b37b9081 [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);
rginda87b86462011-12-14 13:48:03 -080091
rgindafeaf3142012-01-31 15:14:20 -080092 // The keyboard hander.
93 this.keyboard = new hterm.Keyboard(this);
94
rginda87b86462011-12-14 13:48:03 -080095 // General IO interface that can be given to third parties without exposing
96 // the entire terminal object.
97 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -080098
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +040099 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800100 this.setDefaultTabStops();
rginda87b86462011-12-14 13:48:03 -0800101};
102
103/**
rginda35c456b2012-02-09 17:29:05 -0800104 * Default tab with of 8 to match xterm.
105 */
106hterm.Terminal.prototype.tabWidth = 8;
107
108/**
rginda35c456b2012-02-09 17:29:05 -0800109 * The assumed width of a scrollbar.
110 */
111hterm.Terminal.prototype.scrollbarWidthPx = 16;
112
113/**
rginda9f5222b2012-03-05 11:53:28 -0800114 * Select a preference profile.
115 *
116 * This will load the terminal preferences for the given profile name and
117 * associate subsequent preference changes with the new preference profile.
118 *
119 * @param {string} newName The name of the preference profile. Forward slash
120 * characters will be removed from the name.
121 */
122hterm.Terminal.prototype.setProfile = function(profileName) {
123 // If we already have a profile selected, we're going to need to re-sync
124 // with the new profile.
125 var needSync = !!this.profileName_;
126
127 this.profileName_ = profileName.replace(/\//g, '');
128
129 this.prefs_ = new hterm.PreferenceManager(
130 '/hterm/prefs/profiles/' + this.profileName_);
131
132 var self = this;
133 this.prefs_.definePreferences
rginda30f20f62012-04-05 16:36:19 -0700134 ([
135 /**
136 * Set whether the alt key acts as a meta key or as a distinct alt key.
rginda9f5222b2012-03-05 11:53:28 -0800137 */
rginda30f20f62012-04-05 16:36:19 -0700138 ['alt-is-meta', false, function(v) {
139 self.vt.keyboard.altIsMeta = v;
rginda9f5222b2012-03-05 11:53:28 -0800140 }
141 ],
142
rginda30f20f62012-04-05 16:36:19 -0700143 /**
144 * Set whether the alt key acts as a meta key instead of producing 8-bit
145 * characters.
146 */
147 ['alt-sends-escape', true, function(v) {
148 self.vt.keyboard.altSendsEscape = v;
149 }
150 ],
151
152 /**
153 * Terminal bell sound. Empty string for no audible bell.
154 */
155 ['audible-bell-sound', '../audio/bell.ogg', function(v) {
156 self.bellAudio_.setAttribute('src', v);
157 }
158 ],
159
160 /**
161 * The background color for text with no other color attributes.
162 */
163 ['background-color', 'rgb(16, 16, 16)', function(v) {
rginda9f5222b2012-03-05 11:53:28 -0800164 self.scrollPort_.setBackgroundColor(v);
165 }
166 ],
167
168 /**
rginda30f20f62012-04-05 16:36:19 -0700169 * The background image.
170 *
171 * Defaults to a subtle light-to-transparent-to-dark gradient that is
172 * mostly transparent.
173 */
174 ['background-image',
175 ('-webkit-linear-gradient(bottom, ' +
176 'rgba(0,0,0,0.01) 0%, ' +
177 'rgba(0,0,0,0) 30%, ' +
178 'rgba(255,255,255,0) 70%, ' +
179 'rgba(255,255,255,0.05) 100%)'),
180 function(v) {
181 self.scrollPort_.setBackgroundImage(v);
182 }
183 ],
184
185 /**
186 * If true, the backspace should send BS ('\x08', aka ^H). Otherwise
187 * the backspace key should send '\x7f'.
188 */
189 ['backspace-sends-backspace', false, function(v) {
190 self.keyboard.backspaceSendsBackspace = v;
191 }
192 ],
193
194 /**
195 * The color of the visible cursor.
196 */
197 ['cursor-color', 'rgba(255,0,0,0.5)', function(v) {
198 self.cursorNode_.style.backgroundColor = v;
199 }
200 ],
201
202 /**
203 * True if we should use bold weight font for text with the bold/bright
204 * attribute. False to use bright colors only. Null to autodetect.
205 */
206 ['enable-bold', null, function(v) {
207 self.syncBoldSafeState();
208 }
209 ],
210
211 /**
rginda9f5222b2012-03-05 11:53:28 -0800212 * Default font family for the terminal text.
213 */
214 ['font-family', ('"DejaVu Sans Mono", "Everson Mono", ' +
215 'FreeMono, "Menlo", "Lucida Console", ' +
216 'monospace'),
217 function(v) { self.syncFontFamily() }
218 ],
219
220 /**
rginda30f20f62012-04-05 16:36:19 -0700221 * The default font size in pixels.
222 */
223 ['font-size', 15, function(v) {
224 self.setFontSize(v);
225 }
226 ],
227
228 /**
rginda9f5222b2012-03-05 11:53:28 -0800229 * Anti-aliasing.
230 */
231 ['font-smoothing', 'antialiased',
232 function(v) { self.syncFontFamily() }
233 ],
234
235 /**
rginda30f20f62012-04-05 16:36:19 -0700236 * The foreground color for text with no other color attributes.
rginda9f5222b2012-03-05 11:53:28 -0800237 */
rginda30f20f62012-04-05 16:36:19 -0700238 ['foreground-color', 'rgb(240, 240, 240)', function(v) {
239 self.scrollPort_.setForegroundColor(v);
rginda9f5222b2012-03-05 11:53:28 -0800240 }
241 ],
242
243 /**
rginda30f20f62012-04-05 16:36:19 -0700244 * If true, home/end will control the terminal scrollbar and shift home/end
245 * will send the VT keycodes. If false then home/end sends VT codes and
246 * shift home/end scrolls.
rginda9f5222b2012-03-05 11:53:28 -0800247 */
rginda30f20f62012-04-05 16:36:19 -0700248 ['home-keys-scroll', false, function(v) {
249 self.keyboard.homeKeysScroll = v;
250 }
251 ],
252
253 /**
254 * Set whether the meta key sends a leading escape or not.
255 */
256 ['meta-sends-escape', true, function(v) {
257 self.keyboard.metaSendsEscape = v;
rginda9f5222b2012-03-05 11:53:28 -0800258 }
259 ],
260
261 /**
262 * If true, scroll to the bottom on any keystroke.
263 */
264 ['scroll-on-keystroke', true, function(v) {
265 self.scrollOnKeystroke_ = v;
266 }
267 ],
268
269 /**
270 * If true, scroll to the bottom on terminal output.
271 */
272 ['scroll-on-output', false, function(v) {
273 self.scrollOnOutput_ = v;
274 }
275 ],
276
277 /**
David Reveman8f552492012-03-28 12:18:41 -0400278 * The vertical scrollbar mode.
279 */
280 ['scrollbar-visible', true, function(v) {
281 self.setScrollbarVisible(v);
282 }
283 ],
rginda30f20f62012-04-05 16:36:19 -0700284
285 /**
286 * If true, page up/down will control the terminal scrollbar and shift
287 * page up/down will send the VT keycodes. If false then page up/down
288 * sends VT codes and shift page up/down scrolls.
289 */
290 ['page-keys-scroll', false, function(v) {
291 self.keyboard.pageKeysScroll = v;
292 }
293 ],
294
rginda9f5222b2012-03-05 11:53:28 -0800295 ]);
296
297 if (needSync)
298 this.prefs_.notifyAll();
299};
300
301/**
302 * Return the current terminal background color.
303 *
304 * Intended for use by other classes, so we don't have to expose the entire
305 * prefs_ object.
306 */
307hterm.Terminal.prototype.getBackgroundColor = function() {
308 return this.prefs_.get('background-color');
309};
310
311/**
312 * Return the current terminal foreground color.
313 *
314 * Intended for use by other classes, so we don't have to expose the entire
315 * prefs_ object.
316 */
317hterm.Terminal.prototype.getForegroundColor = function() {
318 return this.prefs_.get('foreground-color');
319};
320
321/**
rginda87b86462011-12-14 13:48:03 -0800322 * Create a new instance of a terminal command and run it with a given
323 * argument string.
324 *
325 * @param {function} commandClass The constructor for a terminal command.
326 * @param {string} argString The argument string to pass to the command.
327 */
328hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
329 var self = this;
330 this.command = new commandClass(
331 { argString: argString || '',
332 io: this.io.push(),
333 onExit: function(code) {
334 self.io.pop();
335 self.io.println(hterm.msg('COMMAND_COMPLETE',
336 [self.command.commandName, code]));
rgindafeaf3142012-01-31 15:14:20 -0800337 self.uninstallKeyboard();
rginda87b86462011-12-14 13:48:03 -0800338 }
339 });
340
rgindafeaf3142012-01-31 15:14:20 -0800341 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800342 this.command.run();
343};
344
345/**
rgindafeaf3142012-01-31 15:14:20 -0800346 * Returns true if the current screen is the primary screen, false otherwise.
347 */
348hterm.Terminal.prototype.isPrimaryScreen = function() {
349 return this.screen_ = this.primaryScreen_;
350};
351
352/**
353 * Install the keyboard handler for this terminal.
354 *
355 * This will prevent the browser from seeing any keystrokes sent to the
356 * terminal.
357 */
358hterm.Terminal.prototype.installKeyboard = function() {
359 this.keyboard.installKeyboard(this.document_.body.firstChild);
360}
361
362/**
363 * Uninstall the keyboard handler for this terminal.
364 */
365hterm.Terminal.prototype.uninstallKeyboard = function() {
366 this.keyboard.installKeyboard(null);
367}
368
369/**
rginda35c456b2012-02-09 17:29:05 -0800370 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800371 *
372 * Call setFontSize(0) to reset to the default font size.
373 *
374 * This function does not modify the font-size preference.
375 *
376 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800377 */
378hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800379 if (px === 0)
380 px = this.prefs_.get('font-size');
381
rginda35c456b2012-02-09 17:29:05 -0800382 this.scrollPort_.setFontSize(px);
383};
384
385/**
386 * Get the current font size.
387 */
388hterm.Terminal.prototype.getFontSize = function() {
389 return this.scrollPort_.getFontSize();
390};
391
392/**
393 * Set the CSS "font-family" for this terminal.
394 */
rginda9f5222b2012-03-05 11:53:28 -0800395hterm.Terminal.prototype.syncFontFamily = function() {
396 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
397 this.prefs_.get('font-smoothing'));
398 this.syncBoldSafeState();
399};
400
401hterm.Terminal.prototype.syncBoldSafeState = function() {
402 var enableBold = this.prefs_.get('enable-bold');
403 if (enableBold !== null) {
404 this.screen_.textAttributes.enableBold = enableBold;
405 return;
406 }
407
rgindaf7521392012-02-28 17:20:34 -0800408 var normalSize = this.scrollPort_.measureCharacterSize();
409 var boldSize = this.scrollPort_.measureCharacterSize('bold');
410
411 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800412 if (!isBoldSafe) {
413 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700414 'from normal. Font family is: ' +
415 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800416 }
rginda9f5222b2012-03-05 11:53:28 -0800417
418 this.screen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800419};
420
421/**
rginda87b86462011-12-14 13:48:03 -0800422 * Return a copy of the current cursor position.
423 *
424 * @return {hterm.RowCol} The RowCol object representing the current position.
425 */
426hterm.Terminal.prototype.saveCursor = function() {
427 return this.screen_.cursorPosition.clone();
428};
429
rgindaa19afe22012-01-25 15:40:22 -0800430hterm.Terminal.prototype.getTextAttributes = function() {
431 return this.screen_.textAttributes;
432};
433
rginda87b86462011-12-14 13:48:03 -0800434/**
rginda9846e2f2012-01-27 13:53:33 -0800435 * Change the title of this terminal's window.
436 */
437hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800438 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800439};
440
441/**
rginda87b86462011-12-14 13:48:03 -0800442 * Restore a previously saved cursor position.
443 *
444 * @param {hterm.RowCol} cursor The position to restore.
445 */
446hterm.Terminal.prototype.restoreCursor = function(cursor) {
rginda35c456b2012-02-09 17:29:05 -0800447 var row = hterm.clamp(cursor.row, 0, this.screenSize.height - 1);
448 var column = hterm.clamp(cursor.column, 0, this.screenSize.width - 1);
449 this.screen_.setCursorPosition(row, column);
450 if (cursor.column > column ||
451 cursor.column == column && cursor.overflow) {
452 this.screen_.cursorPosition.overflow = true;
453 }
rginda87b86462011-12-14 13:48:03 -0800454};
455
456/**
457 * Set the width of the terminal, resizing the UI to match.
458 */
459hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800460 if (columnCount == null) {
461 this.div_.style.width = '100%';
462 return;
463 }
464
rginda35c456b2012-02-09 17:29:05 -0800465 this.div_.style.width = this.scrollPort_.characterSize.width *
466 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400467 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800468 this.scheduleSyncCursorPosition_();
469};
rginda87b86462011-12-14 13:48:03 -0800470
rgindac9bc5502012-01-18 11:48:44 -0800471/**
rginda35c456b2012-02-09 17:29:05 -0800472 * Set the height of the terminal, resizing the UI to match.
473 */
474hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800475 if (rowCount == null) {
476 this.div_.style.height = '100%';
477 return;
478 }
479
rginda35c456b2012-02-09 17:29:05 -0800480 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700481 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800482 this.realizeSize_(this.screenSize.width, rowCount);
483 this.scheduleSyncCursorPosition_();
484};
485
486/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400487 * Deal with terminal size changes.
488 *
489 */
490hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
491 if (columnCount != this.screenSize.width)
492 this.realizeWidth_(columnCount);
493
494 if (rowCount != this.screenSize.height)
495 this.realizeHeight_(rowCount);
496
497 // Send new terminal size to plugin.
498 this.io.onTerminalResize(columnCount, rowCount);
499};
500
501/**
rgindac9bc5502012-01-18 11:48:44 -0800502 * Deal with terminal width changes.
503 *
504 * This function does what needs to be done when the terminal width changes
505 * out from under us. It happens here rather than in onResize_() because this
506 * code may need to run synchronously to handle programmatic changes of
507 * terminal width.
508 *
509 * Relying on the browser to send us an async resize event means we may not be
510 * in the correct state yet when the next escape sequence hits.
511 */
512hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
513 var deltaColumns = columnCount - this.screen_.getWidth();
514
rginda87b86462011-12-14 13:48:03 -0800515 this.screenSize.width = columnCount;
516 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800517
518 if (deltaColumns > 0) {
519 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
520 } else {
521 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
522 if (this.tabStops_[i] <= columnCount)
523 break;
524
525 this.tabStops_.pop();
526 }
527 }
528
529 this.screen_.setColumnCount(this.screenSize.width);
530};
531
532/**
533 * Deal with terminal height changes.
534 *
535 * This function does what needs to be done when the terminal height changes
536 * out from under us. It happens here rather than in onResize_() because this
537 * code may need to run synchronously to handle programmatic changes of
538 * terminal height.
539 *
540 * Relying on the browser to send us an async resize event means we may not be
541 * in the correct state yet when the next escape sequence hits.
542 */
543hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
544 var deltaRows = rowCount - this.screen_.getHeight();
545
546 this.screenSize.height = rowCount;
547
548 var cursor = this.saveCursor();
549
550 if (deltaRows < 0) {
551 // Screen got smaller.
552 deltaRows *= -1;
553 while (deltaRows) {
554 var lastRow = this.getRowCount() - 1;
555 if (lastRow - this.scrollbackRows_.length == cursor.row)
556 break;
557
558 if (this.getRowText(lastRow))
559 break;
560
561 this.screen_.popRow();
562 deltaRows--;
563 }
564
565 var ary = this.screen_.shiftRows(deltaRows);
566 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
567
568 // We just removed rows from the top of the screen, we need to update
569 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800570 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800571 } else if (deltaRows > 0) {
572 // Screen got larger.
573
574 if (deltaRows <= this.scrollbackRows_.length) {
575 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
576 var rows = this.scrollbackRows_.splice(
577 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
578 this.screen_.unshiftRows(rows);
579 deltaRows -= scrollbackCount;
580 cursor.row += scrollbackCount;
581 }
582
583 if (deltaRows)
584 this.appendRows_(deltaRows);
585 }
586
rginda35c456b2012-02-09 17:29:05 -0800587 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800588 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800589};
590
591/**
592 * Scroll the terminal to the top of the scrollback buffer.
593 */
594hterm.Terminal.prototype.scrollHome = function() {
595 this.scrollPort_.scrollRowToTop(0);
596};
597
598/**
599 * Scroll the terminal to the end.
600 */
601hterm.Terminal.prototype.scrollEnd = function() {
602 this.scrollPort_.scrollRowToBottom(this.getRowCount());
603};
604
605/**
606 * Scroll the terminal one page up (minus one line) relative to the current
607 * position.
608 */
609hterm.Terminal.prototype.scrollPageUp = function() {
610 var i = this.scrollPort_.getTopRowIndex();
611 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
612};
613
614/**
615 * Scroll the terminal one page down (minus one line) relative to the current
616 * position.
617 */
618hterm.Terminal.prototype.scrollPageDown = function() {
619 var i = this.scrollPort_.getTopRowIndex();
620 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800621};
622
rgindac9bc5502012-01-18 11:48:44 -0800623/**
624 * Full terminal reset.
625 */
rginda87b86462011-12-14 13:48:03 -0800626hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800627 this.clearAllTabStops();
628 this.setDefaultTabStops();
rgindac9bc5502012-01-18 11:48:44 -0800629 this.setVTScrollRegion(null, null);
rginda9ea433c2012-03-16 11:57:00 -0700630
631 this.clearHome(this.primaryScreen_);
632 this.primaryScreen_.textAttributes.reset();
633
634 this.clearHome(this.alternateScreen_);
635 this.alternateScreen_.textAttributes.reset();
636
rgindac9bc5502012-01-18 11:48:44 -0800637 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800638};
639
rgindac9bc5502012-01-18 11:48:44 -0800640/**
641 * Soft terminal reset.
642 */
rginda0f5c0292012-01-13 11:00:13 -0800643hterm.Terminal.prototype.softReset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800644 this.options_ = new hterm.Options();
rgindaa19afe22012-01-25 15:40:22 -0800645 this.setCursorVisible(true);
646 this.setCursorBlink(false);
rginda0f5c0292012-01-13 11:00:13 -0800647};
648
rgindac9bc5502012-01-18 11:48:44 -0800649/**
650 * Move the cursor forward to the next tab stop, or to the last column
651 * if no more tab stops are set.
652 */
653hterm.Terminal.prototype.forwardTabStop = function() {
654 var column = this.screen_.cursorPosition.column;
655
656 for (var i = 0; i < this.tabStops_.length; i++) {
657 if (this.tabStops_[i] > column) {
658 this.setCursorColumn(this.tabStops_[i]);
659 return;
660 }
661 }
662
663 this.setCursorColumn(this.screenSize.width - 1);
rginda0f5c0292012-01-13 11:00:13 -0800664};
665
rgindac9bc5502012-01-18 11:48:44 -0800666/**
667 * Move the cursor backward to the previous tab stop, or to the first column
668 * if no previous tab stops are set.
669 */
670hterm.Terminal.prototype.backwardTabStop = function() {
671 var column = this.screen_.cursorPosition.column;
672
673 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
674 if (this.tabStops_[i] < column) {
675 this.setCursorColumn(this.tabStops_[i]);
676 return;
677 }
678 }
679
680 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800681};
682
rgindac9bc5502012-01-18 11:48:44 -0800683/**
684 * Set a tab stop at the given column.
685 *
686 * @param {int} column Zero based column.
687 */
688hterm.Terminal.prototype.setTabStop = function(column) {
689 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
690 if (this.tabStops_[i] == column)
691 return;
692
693 if (this.tabStops_[i] < column) {
694 this.tabStops_.splice(i + 1, 0, column);
695 return;
696 }
697 }
698
699 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800700};
701
rgindac9bc5502012-01-18 11:48:44 -0800702/**
703 * Clear the tab stop at the current cursor position.
704 *
705 * No effect if there is no tab stop at the current cursor position.
706 */
707hterm.Terminal.prototype.clearTabStopAtCursor = function() {
708 var column = this.screen_.cursorPosition.column;
709
710 var i = this.tabStops_.indexOf(column);
711 if (i == -1)
712 return;
713
714 this.tabStops_.splice(i, 1);
715};
716
717/**
718 * Clear all tab stops.
719 */
720hterm.Terminal.prototype.clearAllTabStops = function() {
721 this.tabStops_.length = 0;
722};
723
724/**
725 * Set up the default tab stops, starting from a given column.
726 *
727 * This sets a tabstop every (column % this.tabWidth) column, starting
728 * from the specified column, or 0 if no column is provided.
729 *
730 * This does not clear the existing tab stops first, use clearAllTabStops
731 * for that.
732 *
733 * @param {int} opt_start Optional starting zero based starting column, useful
734 * for filling out missing tab stops when the terminal is resized.
735 */
736hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
737 var start = opt_start || 0;
738 var w = this.tabWidth;
739 var stopCount = Math.floor((this.screenSize.width - start) / this.tabWidth)
740 for (var i = 0; i < stopCount; i++) {
741 this.setTabStop(Math.floor((start + i * w) / w) * w + w);
742 }
rginda87b86462011-12-14 13:48:03 -0800743};
744
rginda6d397402012-01-17 10:58:29 -0800745/**
746 * Save cursor position and attributes.
747 *
748 * TODO(rginda): Save attributes once we support them.
749 */
rginda87b86462011-12-14 13:48:03 -0800750hterm.Terminal.prototype.saveOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800751 this.savedOptions_.cursor = this.saveCursor();
rgindaa19afe22012-01-25 15:40:22 -0800752 this.savedOptions_.textAttributes = this.screen_.textAttributes.clone();
rginda87b86462011-12-14 13:48:03 -0800753};
754
rginda6d397402012-01-17 10:58:29 -0800755/**
756 * Restore cursor position and attributes.
757 *
758 * TODO(rginda): Restore attributes once we support them.
759 */
rginda87b86462011-12-14 13:48:03 -0800760hterm.Terminal.prototype.restoreOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800761 if (this.savedOptions_.cursor)
762 this.restoreCursor(this.savedOptions_.cursor);
rgindaa19afe22012-01-25 15:40:22 -0800763 if (this.savedOptions_.textAttributes)
764 this.screen_.textAttributes = this.savedOptions_.textAttributes;
rginda8ba33642011-12-14 12:31:31 -0800765};
766
767/**
768 * Interpret a sequence of characters.
769 *
770 * Incomplete escape sequences are buffered until the next call.
771 *
772 * @param {string} str Sequence of characters to interpret or pass through.
773 */
774hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800775 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800776 this.scheduleSyncCursorPosition_();
777};
778
779/**
780 * Take over the given DIV for use as the terminal display.
781 *
782 * @param {HTMLDivElement} div The div to use as the terminal display.
783 */
784hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800785 this.div_ = div;
786
rginda8ba33642011-12-14 12:31:31 -0800787 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -0700788 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
789
rginda0918b652012-04-04 11:26:24 -0700790 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -0800791
rginda9f5222b2012-03-05 11:53:28 -0800792 this.setFontSize(this.prefs_.get('font-size'));
793 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -0800794
David Reveman8f552492012-03-28 12:18:41 -0400795 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
796
rginda8ba33642011-12-14 12:31:31 -0800797 this.document_ = this.scrollPort_.getDocument();
798
rginda8ba33642011-12-14 12:31:31 -0800799 this.cursorNode_ = this.document_.createElement('div');
800 this.cursorNode_.style.cssText =
801 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800802 'top: -99px;' +
803 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -0800804 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
805 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda6d397402012-01-17 10:58:29 -0800806 '-webkit-transition: opacity, background-color 100ms linear;' +
rginda9f5222b2012-03-05 11:53:28 -0800807 'background-color: ' + this.prefs_.get('cursor-color'));
rginda8ba33642011-12-14 12:31:31 -0800808 this.document_.body.appendChild(this.cursorNode_);
809
810 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -0800811
rginda87b86462011-12-14 13:48:03 -0800812 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -0800813 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -0800814};
815
rginda0918b652012-04-04 11:26:24 -0700816/**
817 * Return the HTML document that contains the terminal DOM nodes.
818 */
rginda87b86462011-12-14 13:48:03 -0800819hterm.Terminal.prototype.getDocument = function() {
820 return this.document_;
rginda8ba33642011-12-14 12:31:31 -0800821};
822
823/**
rginda0918b652012-04-04 11:26:24 -0700824 * Focus the terminal.
825 */
826hterm.Terminal.prototype.focus = function() {
827 this.scrollPort_.focus();
828};
829
830/**
rginda8ba33642011-12-14 12:31:31 -0800831 * Return the HTML Element for a given row index.
832 *
833 * This is a method from the RowProvider interface. The ScrollPort uses
834 * it to fetch rows on demand as they are scrolled into view.
835 *
836 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
837 * pairs to conserve memory.
838 *
839 * @param {integer} index The zero-based row index, measured relative to the
840 * start of the scrollback buffer. On-screen rows will always have the
841 * largest indicies.
842 * @return {HTMLElement} The 'x-row' element containing for the requested row.
843 */
844hterm.Terminal.prototype.getRowNode = function(index) {
845 if (index < this.scrollbackRows_.length)
846 return this.scrollbackRows_[index];
847
848 var screenIndex = index - this.scrollbackRows_.length;
849 return this.screen_.rowsArray[screenIndex];
850};
851
852/**
853 * Return the text content for a given range of rows.
854 *
855 * This is a method from the RowProvider interface. The ScrollPort uses
856 * it to fetch text content on demand when the user attempts to copy their
857 * selection to the clipboard.
858 *
859 * @param {integer} start The zero-based row index to start from, measured
860 * relative to the start of the scrollback buffer. On-screen rows will
861 * always have the largest indicies.
862 * @param {integer} end The zero-based row index to end on, measured
863 * relative to the start of the scrollback buffer.
864 * @return {string} A single string containing the text value of the range of
865 * rows. Lines will be newline delimited, with no trailing newline.
866 */
867hterm.Terminal.prototype.getRowsText = function(start, end) {
868 var ary = [];
869 for (var i = start; i < end; i++) {
870 var node = this.getRowNode(i);
871 ary.push(node.textContent);
872 }
873
874 return ary.join('\n');
875};
876
877/**
878 * Return the text content for a given row.
879 *
880 * This is a method from the RowProvider interface. The ScrollPort uses
881 * it to fetch text content on demand when the user attempts to copy their
882 * selection to the clipboard.
883 *
884 * @param {integer} index The zero-based row index to return, measured
885 * relative to the start of the scrollback buffer. On-screen rows will
886 * always have the largest indicies.
887 * @return {string} A string containing the text value of the selected row.
888 */
889hterm.Terminal.prototype.getRowText = function(index) {
890 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -0800891 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -0800892};
893
894/**
895 * Return the total number of rows in the addressable screen and in the
896 * scrollback buffer of this terminal.
897 *
898 * This is a method from the RowProvider interface. The ScrollPort uses
899 * it to compute the size of the scrollbar.
900 *
901 * @return {integer} The number of rows in this terminal.
902 */
903hterm.Terminal.prototype.getRowCount = function() {
904 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
905};
906
907/**
908 * Create DOM nodes for new rows and append them to the end of the terminal.
909 *
910 * This is the only correct way to add a new DOM node for a row. Notice that
911 * the new row is appended to the bottom of the list of rows, and does not
912 * require renumbering (of the rowIndex property) of previous rows.
913 *
914 * If you think you want a new blank row somewhere in the middle of the
915 * terminal, look into moveRows_().
916 *
917 * This method does not pay attention to vtScrollTop/Bottom, since you should
918 * be using moveRows() in cases where they would matter.
919 *
920 * The cursor will be positioned at column 0 of the first inserted line.
921 */
922hterm.Terminal.prototype.appendRows_ = function(count) {
923 var cursorRow = this.screen_.rowsArray.length;
924 var offset = this.scrollbackRows_.length + cursorRow;
925 for (var i = 0; i < count; i++) {
926 var row = this.document_.createElement('x-row');
927 row.appendChild(this.document_.createTextNode(''));
928 row.rowIndex = offset + i;
929 this.screen_.pushRow(row);
930 }
931
932 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
933 if (extraRows > 0) {
934 var ary = this.screen_.shiftRows(extraRows);
935 Array.prototype.push.apply(this.scrollbackRows_, ary);
936 this.scheduleScrollDown_();
937 }
938
939 if (cursorRow >= this.screen_.rowsArray.length)
940 cursorRow = this.screen_.rowsArray.length - 1;
941
rginda87b86462011-12-14 13:48:03 -0800942 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -0800943};
944
945/**
946 * Relocate rows from one part of the addressable screen to another.
947 *
948 * This is used to recycle rows during VT scrolls (those which are driven
949 * by VT commands, rather than by the user manipulating the scrollbar.)
950 *
951 * In this case, the blank lines scrolled into the scroll region are made of
952 * the nodes we scrolled off. These have their rowIndex properties carefully
953 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -0800954 */
955hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
956 var ary = this.screen_.removeRows(fromIndex, count);
957 this.screen_.insertRows(toIndex, ary);
958
959 var start, end;
960 if (fromIndex < toIndex) {
961 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -0800962 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800963 } else {
964 start = toIndex;
rginda87b86462011-12-14 13:48:03 -0800965 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800966 }
967
968 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -0800969 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -0800970};
971
972/**
973 * Renumber the rowIndex property of the given range of rows.
974 *
975 * The start and end indicies are relative to the screen, not the scrollback.
976 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -0800977 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -0800978 * no need to renumber scrollback rows.
979 */
980hterm.Terminal.prototype.renumberRows_ = function(start, end) {
981 var offset = this.scrollbackRows_.length;
982 for (var i = start; i < end; i++) {
983 this.screen_.rowsArray[i].rowIndex = offset + i;
984 }
985};
986
987/**
988 * Print a string to the terminal.
989 *
990 * This respects the current insert and wraparound modes. It will add new lines
991 * to the end of the terminal, scrolling off the top into the scrollback buffer
992 * if necessary.
993 *
994 * The string is *not* parsed for escape codes. Use the interpret() method if
995 * that's what you're after.
996 *
997 * @param{string} str The string to print.
998 */
999hterm.Terminal.prototype.print = function(str) {
rgindaa19afe22012-01-25 15:40:22 -08001000 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
1001 this.newLine();
rginda2312fff2012-01-05 16:20:52 -08001002
rgindaa19afe22012-01-25 15:40:22 -08001003 if (this.options_.insertMode) {
1004 this.screen_.insertString(str);
1005 } else {
1006 this.screen_.overwriteString(str);
1007 }
1008
1009 var overflow = this.screen_.maybeClipCurrentRow();
1010
1011 if (this.options_.wraparound && overflow) {
1012 var lastColumn;
1013
1014 do {
rginda35c456b2012-02-09 17:29:05 -08001015 this.newLine();
1016 lastColumn = overflow.characterLength;
rgindaa19afe22012-01-25 15:40:22 -08001017
1018 if (!this.options_.insertMode)
1019 this.screen_.deleteChars(overflow.characterLength);
1020
1021 this.screen_.prependNodes(overflow);
rgindaa19afe22012-01-25 15:40:22 -08001022
1023 overflow = this.screen_.maybeClipCurrentRow();
1024 } while (overflow);
1025
1026 this.setCursorColumn(lastColumn);
1027 }
rginda8ba33642011-12-14 12:31:31 -08001028
1029 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001030
rginda9f5222b2012-03-05 11:53:28 -08001031 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001032 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001033};
1034
1035/**
rginda87b86462011-12-14 13:48:03 -08001036 * Set the VT scroll region.
1037 *
rginda87b86462011-12-14 13:48:03 -08001038 * This also resets the cursor position to the absolute (0, 0) position, since
1039 * that's what xterm appears to do.
1040 *
1041 * @param {integer} scrollTop The zero-based top of the scroll region.
1042 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1043 * inclusive.
1044 */
1045hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1046 this.vtScrollTop_ = scrollTop;
1047 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001048};
1049
1050/**
rginda8ba33642011-12-14 12:31:31 -08001051 * Return the top row index according to the VT.
1052 *
1053 * This will return 0 unless the terminal has been told to restrict scrolling
1054 * to some lower row. It is used for some VT cursor positioning and scrolling
1055 * commands.
1056 *
1057 * @return {integer} The topmost row in the terminal's scroll region.
1058 */
1059hterm.Terminal.prototype.getVTScrollTop = function() {
1060 if (this.vtScrollTop_ != null)
1061 return this.vtScrollTop_;
1062
1063 return 0;
rginda87b86462011-12-14 13:48:03 -08001064};
rginda8ba33642011-12-14 12:31:31 -08001065
1066/**
1067 * Return the bottom row index according to the VT.
1068 *
1069 * This will return the height of the terminal unless the it has been told to
1070 * restrict scrolling to some higher row. It is used for some VT cursor
1071 * positioning and scrolling commands.
1072 *
1073 * @return {integer} The bottommost row in the terminal's scroll region.
1074 */
1075hterm.Terminal.prototype.getVTScrollBottom = function() {
1076 if (this.vtScrollBottom_ != null)
1077 return this.vtScrollBottom_;
1078
rginda87b86462011-12-14 13:48:03 -08001079 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001080}
1081
1082/**
1083 * Process a '\n' character.
1084 *
1085 * If the cursor is on the final row of the terminal this will append a new
1086 * blank row to the screen and scroll the topmost row into the scrollback
1087 * buffer.
1088 *
1089 * Otherwise, this moves the cursor to column zero of the next row.
1090 */
1091hterm.Terminal.prototype.newLine = function() {
1092 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001093 // If we're at the end of the screen we need to append a new line and
1094 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001095 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001096 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1097 // End of the scroll region does not affect the scrollback buffer.
1098 this.vtScrollUp(1);
1099 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001100 } else {
rginda87b86462011-12-14 13:48:03 -08001101 // Anywhere else in the screen just moves the cursor.
1102 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001103 }
1104};
1105
1106/**
1107 * Like newLine(), except maintain the cursor column.
1108 */
1109hterm.Terminal.prototype.lineFeed = function() {
1110 var column = this.screen_.cursorPosition.column;
1111 this.newLine();
1112 this.setCursorColumn(column);
1113};
1114
1115/**
rginda87b86462011-12-14 13:48:03 -08001116 * If autoCarriageReturn is set then newLine(), else lineFeed().
1117 */
1118hterm.Terminal.prototype.formFeed = function() {
1119 if (this.options_.autoCarriageReturn) {
1120 this.newLine();
1121 } else {
1122 this.lineFeed();
1123 }
1124};
1125
1126/**
1127 * Move the cursor up one row, possibly inserting a blank line.
1128 *
1129 * The cursor column is not changed.
1130 */
1131hterm.Terminal.prototype.reverseLineFeed = function() {
1132 var scrollTop = this.getVTScrollTop();
1133 var currentRow = this.screen_.cursorPosition.row;
1134
1135 if (currentRow == scrollTop) {
1136 this.insertLines(1);
1137 } else {
1138 this.setAbsoluteCursorRow(currentRow - 1);
1139 }
1140};
1141
1142/**
rginda8ba33642011-12-14 12:31:31 -08001143 * Replace all characters to the left of the current cursor with the space
1144 * character.
1145 *
1146 * TODO(rginda): This should probably *remove* the characters (not just replace
1147 * with a space) if there are no characters at or beyond the current cursor
1148 * position. Once it does that, it'll have the same text-attribute related
1149 * issues as hterm.Screen.prototype.clearCursorRow :/
1150 */
1151hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001152 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001153 this.setCursorColumn(0);
rginda87b86462011-12-14 13:48:03 -08001154 this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1));
1155 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001156};
1157
1158/**
1159 * Erase a given number of characters to the right of the cursor, shifting
1160 * remaining characters to the left.
1161 *
1162 * The cursor position is unchanged.
1163 *
1164 * TODO(rginda): Test that this works even when the cursor is positioned beyond
1165 * the end of the text.
1166 *
1167 * TODO(rginda): This likely has text-attribute related troubles similar to the
1168 * todo on hterm.Screen.prototype.clearCursorRow.
1169 */
1170hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001171 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001172
rginda87b86462011-12-14 13:48:03 -08001173 var maxCount = this.screenSize.width - cursor.column;
rginda8ba33642011-12-14 12:31:31 -08001174 var count = (opt_count && opt_count < maxCount) ? opt_count : maxCount;
1175 this.screen_.deleteChars(count);
rginda87b86462011-12-14 13:48:03 -08001176 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001177};
1178
1179/**
1180 * Erase the current line.
1181 *
1182 * The cursor position is unchanged.
1183 *
1184 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1185 * has a text-attribute related TODO.
1186 */
1187hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001188 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001189 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001190 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001191};
1192
1193/**
1194 * Erase all characters from the start of the scroll region to the current
1195 * cursor position.
1196 *
1197 * The cursor position is unchanged.
1198 *
1199 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1200 * has a text-attribute related TODO.
1201 */
1202hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001203 var cursor = this.saveCursor();
1204
1205 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001206
1207 var top = this.getVTScrollTop();
rginda87b86462011-12-14 13:48:03 -08001208 for (var i = top; i < cursor.row; i++) {
1209 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001210 this.screen_.clearCursorRow();
1211 }
1212
rginda87b86462011-12-14 13:48:03 -08001213 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001214};
1215
1216/**
1217 * Erase all characters from the current cursor position to the end of the
1218 * scroll region.
1219 *
1220 * The cursor position is unchanged.
1221 *
1222 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1223 * has a text-attribute related TODO.
1224 */
1225hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001226 var cursor = this.saveCursor();
1227
1228 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001229
1230 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001231 for (var i = cursor.row + 1; i <= bottom; i++) {
1232 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001233 this.screen_.clearCursorRow();
1234 }
1235
rginda87b86462011-12-14 13:48:03 -08001236 this.restoreCursor(cursor);
1237};
1238
1239/**
1240 * Fill the terminal with a given character.
1241 *
1242 * This methods does not respect the VT scroll region.
1243 *
1244 * @param {string} ch The character to use for the fill.
1245 */
1246hterm.Terminal.prototype.fill = function(ch) {
1247 var cursor = this.saveCursor();
1248
1249 this.setAbsoluteCursorPosition(0, 0);
1250 for (var row = 0; row < this.screenSize.height; row++) {
1251 for (var col = 0; col < this.screenSize.width; col++) {
1252 this.setAbsoluteCursorPosition(row, col);
1253 this.screen_.overwriteString(ch);
1254 }
1255 }
1256
1257 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001258};
1259
1260/**
rginda9ea433c2012-03-16 11:57:00 -07001261 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001262 *
rginda9ea433c2012-03-16 11:57:00 -07001263 * This does not respect the scroll region.
1264 *
1265 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1266 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001267 *
1268 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1269 * has a text-attribute related TODO.
1270 */
rginda9ea433c2012-03-16 11:57:00 -07001271hterm.Terminal.prototype.clearHome = function(opt_screen) {
1272 var screen = opt_screen || this.screen_;
1273 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001274
rgindae4d29232012-01-19 10:47:13 -08001275 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001276 screen.setCursorPosition(i, 0);
1277 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001278 }
1279
rginda9ea433c2012-03-16 11:57:00 -07001280 screen.setCursorPosition(0, 0);
1281};
1282
1283/**
1284 * Erase the entire display without changing the cursor position.
1285 *
1286 * The cursor position is unchanged. This does not respect the scroll
1287 * region.
1288 *
1289 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1290 * to the current screen.
1291 *
1292 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1293 * has a text-attribute related TODO.
1294 */
1295hterm.Terminal.prototype.clear = function(opt_screen) {
1296 var screen = opt_screen || this.screen_;
1297 var cursor = screen.cursorPosition.clone();
1298 this.clearHome(screen);
1299 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001300};
1301
1302/**
1303 * VT command to insert lines at the current cursor row.
1304 *
1305 * This respects the current scroll region. Rows pushed off the bottom are
1306 * lost (they won't show up in the scrollback buffer).
1307 *
1308 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1309 * has a text-attribute related TODO.
1310 *
1311 * @param {integer} count The number of lines to insert.
1312 */
1313hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001314 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001315
1316 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001317 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001318
rgindae4d29232012-01-19 10:47:13 -08001319 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001320 if (start != cursor.row)
1321 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001322
1323 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001324 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001325 this.screen_.clearCursorRow();
1326 }
1327
rginda87b86462011-12-14 13:48:03 -08001328 cursor.column = 0;
1329 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001330};
1331
1332/**
1333 * VT command to delete lines at the current cursor row.
1334 *
1335 * New rows are added to the bottom of scroll region to take their place. New
1336 * rows are strictly there to take up space and have no content or style.
1337 */
1338hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001339 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001340
rginda87b86462011-12-14 13:48:03 -08001341 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001342 var bottom = this.getVTScrollBottom();
1343
rginda87b86462011-12-14 13:48:03 -08001344 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001345 count = Math.min(count, maxCount);
1346
rginda87b86462011-12-14 13:48:03 -08001347 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001348 if (count != maxCount)
1349 this.moveRows_(top, count, moveStart);
1350
1351 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001352 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001353 this.screen_.clearCursorRow();
1354 }
1355
rginda87b86462011-12-14 13:48:03 -08001356 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001357};
1358
1359/**
1360 * Inserts the given number of spaces at the current cursor position.
1361 *
rginda87b86462011-12-14 13:48:03 -08001362 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001363 */
1364hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001365 var cursor = this.saveCursor();
1366
rginda0f5c0292012-01-13 11:00:13 -08001367 var ws = hterm.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001368 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001369 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001370
1371 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001372};
1373
1374/**
1375 * Forward-delete the specified number of characters starting at the cursor
1376 * position.
1377 *
1378 * @param {integer} count The number of characters to delete.
1379 */
1380hterm.Terminal.prototype.deleteChars = function(count) {
1381 this.screen_.deleteChars(count);
1382};
1383
1384/**
1385 * Shift rows in the scroll region upwards by a given number of lines.
1386 *
1387 * New rows are inserted at the bottom of the scroll region to fill the
1388 * vacated rows. The new rows not filled out with the current text attributes.
1389 *
1390 * This function does not affect the scrollback rows at all. Rows shifted
1391 * off the top are lost.
1392 *
rginda87b86462011-12-14 13:48:03 -08001393 * The cursor position is not altered.
1394 *
rginda8ba33642011-12-14 12:31:31 -08001395 * @param {integer} count The number of rows to scroll.
1396 */
1397hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001398 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001399
rginda87b86462011-12-14 13:48:03 -08001400 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001401 this.deleteLines(count);
1402
rginda87b86462011-12-14 13:48:03 -08001403 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001404};
1405
1406/**
1407 * Shift rows below the cursor down by a given number of lines.
1408 *
1409 * This function respects the current scroll region.
1410 *
1411 * New rows are inserted at the top of the scroll region to fill the
1412 * vacated rows. The new rows not filled out with the current text attributes.
1413 *
1414 * This function does not affect the scrollback rows at all. Rows shifted
1415 * off the bottom are lost.
1416 *
1417 * @param {integer} count The number of rows to scroll.
1418 */
1419hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001420 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001421
rginda87b86462011-12-14 13:48:03 -08001422 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001423 this.insertLines(opt_count);
1424
rginda87b86462011-12-14 13:48:03 -08001425 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001426};
1427
rginda87b86462011-12-14 13:48:03 -08001428
rginda8ba33642011-12-14 12:31:31 -08001429/**
1430 * Set the cursor position.
1431 *
1432 * The cursor row is relative to the scroll region if the terminal has
1433 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1434 *
1435 * @param {integer} row The new zero-based cursor row.
1436 * @param {integer} row The new zero-based cursor column.
1437 */
1438hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1439 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001440 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001441 } else {
rginda87b86462011-12-14 13:48:03 -08001442 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001443 }
rginda87b86462011-12-14 13:48:03 -08001444};
rginda8ba33642011-12-14 12:31:31 -08001445
rginda87b86462011-12-14 13:48:03 -08001446hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1447 var scrollTop = this.getVTScrollTop();
1448 row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
rginda2312fff2012-01-05 16:20:52 -08001449 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001450 this.screen_.setCursorPosition(row, column);
1451};
1452
1453hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rginda2312fff2012-01-05 16:20:52 -08001454 row = hterm.clamp(row, 0, this.screenSize.height - 1);
1455 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001456 this.screen_.setCursorPosition(row, column);
1457};
1458
1459/**
1460 * Set the cursor column.
1461 *
1462 * @param {integer} column The new zero-based cursor column.
1463 */
1464hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001465 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001466};
1467
1468/**
1469 * Return the cursor column.
1470 *
1471 * @return {integer} The zero-based cursor column.
1472 */
1473hterm.Terminal.prototype.getCursorColumn = function() {
1474 return this.screen_.cursorPosition.column;
1475};
1476
1477/**
1478 * Set the cursor row.
1479 *
1480 * The cursor row is relative to the scroll region if the terminal has
1481 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1482 *
1483 * @param {integer} row The new cursor row.
1484 */
rginda87b86462011-12-14 13:48:03 -08001485hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1486 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001487};
1488
1489/**
1490 * Return the cursor row.
1491 *
1492 * @return {integer} The zero-based cursor row.
1493 */
1494hterm.Terminal.prototype.getCursorRow = function(row) {
1495 return this.screen_.cursorPosition.row;
1496};
1497
1498/**
1499 * Request that the ScrollPort redraw itself soon.
1500 *
1501 * The redraw will happen asynchronously, soon after the call stack winds down.
1502 * Multiple calls will be coalesced into a single redraw.
1503 */
1504hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001505 if (this.timeouts_.redraw)
1506 return;
rginda8ba33642011-12-14 12:31:31 -08001507
1508 var self = this;
rginda87b86462011-12-14 13:48:03 -08001509 this.timeouts_.redraw = setTimeout(function() {
1510 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001511 self.scrollPort_.redraw_();
1512 }, 0);
1513};
1514
1515/**
1516 * Request that the ScrollPort be scrolled to the bottom.
1517 *
1518 * The scroll will happen asynchronously, soon after the call stack winds down.
1519 * Multiple calls will be coalesced into a single scroll.
1520 *
1521 * This affects the scrollbar position of the ScrollPort, and has nothing to
1522 * do with the VT scroll commands.
1523 */
1524hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1525 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001526 return;
rginda8ba33642011-12-14 12:31:31 -08001527
1528 var self = this;
1529 this.timeouts_.scrollDown = setTimeout(function() {
1530 delete self.timeouts_.scrollDown;
1531 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1532 }, 10);
1533};
1534
1535/**
1536 * Move the cursor up a specified number of rows.
1537 *
1538 * @param {integer} count The number of rows to move the cursor.
1539 */
1540hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001541 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001542};
1543
1544/**
1545 * Move the cursor down a specified number of rows.
1546 *
1547 * @param {integer} count The number of rows to move the cursor.
1548 */
1549hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001550 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001551 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1552 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1553 this.screenSize.height - 1);
1554
1555 var row = hterm.clamp(this.screen_.cursorPosition.row + count,
1556 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001557 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001558};
1559
1560/**
1561 * Move the cursor left a specified number of columns.
1562 *
1563 * @param {integer} count The number of columns to move the cursor.
1564 */
1565hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001566 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001567};
1568
1569/**
1570 * Move the cursor right a specified number of columns.
1571 *
1572 * @param {integer} count The number of columns to move the cursor.
1573 */
1574hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001575 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001576 var column = hterm.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001577 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001578 this.setCursorColumn(column);
1579};
1580
1581/**
1582 * Reverse the foreground and background colors of the terminal.
1583 *
1584 * This only affects text that was drawn with no attributes.
1585 *
1586 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1587 * been drawn with attributes that happen to coincide with the default
1588 * 'no-attribute' colors. My guess is probably not.
1589 */
1590hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001591 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001592 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001593 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1594 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001595 } else {
rginda9f5222b2012-03-05 11:53:28 -08001596 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1597 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001598 }
1599};
1600
1601/**
rginda87b86462011-12-14 13:48:03 -08001602 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001603 */
1604hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001605 if (this.bellAudio_.getAttribute('src'))
1606 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001607
rginda6d397402012-01-17 10:58:29 -08001608 this.cursorNode_.style.backgroundColor =
1609 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001610
1611 var self = this;
1612 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001613 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001614 }, 200);
rginda87b86462011-12-14 13:48:03 -08001615};
1616
1617/**
rginda8ba33642011-12-14 12:31:31 -08001618 * Set the origin mode bit.
1619 *
1620 * If origin mode is on, certain VT cursor and scrolling commands measure their
1621 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1622 * to the top of the addressable screen.
1623 *
1624 * Defaults to off.
1625 *
1626 * @param {boolean} state True to set origin mode, false to unset.
1627 */
1628hterm.Terminal.prototype.setOriginMode = function(state) {
1629 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001630 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001631};
1632
1633/**
1634 * Set the insert mode bit.
1635 *
1636 * If insert mode is on, existing text beyond the cursor position will be
1637 * shifted right to make room for new text. Otherwise, new text overwrites
1638 * any existing text.
1639 *
1640 * Defaults to off.
1641 *
1642 * @param {boolean} state True to set insert mode, false to unset.
1643 */
1644hterm.Terminal.prototype.setInsertMode = function(state) {
1645 this.options_.insertMode = state;
1646};
1647
1648/**
rginda87b86462011-12-14 13:48:03 -08001649 * Set the auto carriage return bit.
1650 *
1651 * If auto carriage return is on then a formfeed character is interpreted
1652 * as a newline, otherwise it's the same as a linefeed. The difference boils
1653 * down to whether or not the cursor column is reset.
1654 */
1655hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1656 this.options_.autoCarriageReturn = state;
1657};
1658
1659/**
rginda8ba33642011-12-14 12:31:31 -08001660 * Set the wraparound mode bit.
1661 *
1662 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1663 * to the start of the following row. Otherwise, the cursor is clamped to the
1664 * end of the screen and attempts to write past it are ignored.
1665 *
1666 * Defaults to on.
1667 *
1668 * @param {boolean} state True to set wraparound mode, false to unset.
1669 */
1670hterm.Terminal.prototype.setWraparound = function(state) {
1671 this.options_.wraparound = state;
1672};
1673
1674/**
1675 * Set the reverse-wraparound mode bit.
1676 *
1677 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1678 * to the end of the previous row. Otherwise, the cursor is clamped to column
1679 * 0.
1680 *
1681 * Defaults to off.
1682 *
1683 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1684 */
1685hterm.Terminal.prototype.setReverseWraparound = function(state) {
1686 this.options_.reverseWraparound = state;
1687};
1688
1689/**
1690 * Selects between the primary and alternate screens.
1691 *
1692 * If alternate mode is on, the alternate screen is active. Otherwise the
1693 * primary screen is active.
1694 *
1695 * Swapping screens has no effect on the scrollback buffer.
1696 *
1697 * Each screen maintains its own cursor position.
1698 *
1699 * Defaults to off.
1700 *
1701 * @param {boolean} state True to set alternate mode, false to unset.
1702 */
1703hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001704 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001705 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1706
rginda35c456b2012-02-09 17:29:05 -08001707 if (this.screen_.rowsArray.length &&
1708 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
1709 // If the screen changed sizes while we were away, our rowIndexes may
1710 // be incorrect.
1711 var offset = this.scrollbackRows_.length;
1712 var ary = this.screen_.rowsArray;
1713 for (i = 0; i < ary.length; i++) {
1714 ary[i].rowIndex = offset + i;
1715 }
1716 }
rginda8ba33642011-12-14 12:31:31 -08001717
rginda35c456b2012-02-09 17:29:05 -08001718 this.realizeWidth_(this.screenSize.width);
1719 this.realizeHeight_(this.screenSize.height);
1720 this.scrollPort_.syncScrollHeight();
1721 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001722
rginda6d397402012-01-17 10:58:29 -08001723 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08001724 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08001725};
1726
1727/**
1728 * Set the cursor-blink mode bit.
1729 *
1730 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1731 * a visible cursor does not blink.
1732 *
1733 * You should make sure to turn blinking off if you're going to dispose of a
1734 * terminal, otherwise you'll leak a timeout.
1735 *
1736 * Defaults to on.
1737 *
1738 * @param {boolean} state True to set cursor-blink mode, false to unset.
1739 */
1740hterm.Terminal.prototype.setCursorBlink = function(state) {
1741 this.options_.cursorBlink = state;
1742
1743 if (!state && this.timeouts_.cursorBlink) {
1744 clearTimeout(this.timeouts_.cursorBlink);
1745 delete this.timeouts_.cursorBlink;
1746 }
1747
1748 if (this.options_.cursorVisible)
1749 this.setCursorVisible(true);
1750};
1751
1752/**
1753 * Set the cursor-visible mode bit.
1754 *
1755 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1756 *
1757 * Defaults to on.
1758 *
1759 * @param {boolean} state True to set cursor-visible mode, false to unset.
1760 */
1761hterm.Terminal.prototype.setCursorVisible = function(state) {
1762 this.options_.cursorVisible = state;
1763
1764 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001765 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001766 return;
1767 }
1768
rginda87b86462011-12-14 13:48:03 -08001769 this.syncCursorPosition_();
1770
1771 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001772
1773 if (this.options_.cursorBlink) {
1774 if (this.timeouts_.cursorBlink)
1775 return;
1776
1777 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1778 500);
1779 } else {
1780 if (this.timeouts_.cursorBlink) {
1781 clearTimeout(this.timeouts_.cursorBlink);
1782 delete this.timeouts_.cursorBlink;
1783 }
1784 }
1785};
1786
1787/**
rginda87b86462011-12-14 13:48:03 -08001788 * Synchronizes the visible cursor and document selection with the current
1789 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08001790 */
1791hterm.Terminal.prototype.syncCursorPosition_ = function() {
1792 var topRowIndex = this.scrollPort_.getTopRowIndex();
1793 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
1794 var cursorRowIndex = this.scrollbackRows_.length +
1795 this.screen_.cursorPosition.row;
1796
1797 if (cursorRowIndex > bottomRowIndex) {
1798 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08001799 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08001800 return;
1801 }
1802
rginda35c456b2012-02-09 17:29:05 -08001803 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
1804 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
1805
rginda8ba33642011-12-14 12:31:31 -08001806 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08001807 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
1808 'px';
1809 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
1810 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08001811
1812 this.cursorNode_.setAttribute('title',
1813 '(' + this.screen_.cursorPosition.row +
1814 ', ' + this.screen_.cursorPosition.column +
1815 ')');
1816
1817 // Update the caret for a11y purposes.
1818 var selection = this.document_.getSelection();
1819 if (selection && selection.isCollapsed)
1820 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08001821};
1822
1823/**
1824 * Synchronizes the visible cursor with the current cursor coordinates.
1825 *
1826 * The sync will happen asynchronously, soon after the call stack winds down.
1827 * Multiple calls will be coalesced into a single sync.
1828 */
1829hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
1830 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08001831 return;
rginda8ba33642011-12-14 12:31:31 -08001832
1833 var self = this;
1834 this.timeouts_.syncCursor = setTimeout(function() {
1835 self.syncCursorPosition_();
1836 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08001837 }, 0);
1838};
1839
rgindacc2996c2012-02-24 14:59:31 -08001840/**
1841 * Show the terminal overlay for a given amount of time.
1842 *
1843 * The terminal overlay appears in inverse video in a large font, centered
1844 * over the terminal. You should probably keep the overlay message brief,
1845 * since it's in a large font and you probably aren't going to check the size
1846 * of the terminal first.
1847 *
1848 * @param {string} msg The text (not HTML) message to display in the overlay.
1849 * @param {number} opt_timeout The amount of time to wait before fading out
1850 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
1851 * stay up forever (or until the next overlay).
1852 */
1853hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08001854 if (!this.overlayNode_) {
1855 if (!this.div_)
1856 return;
1857
1858 this.overlayNode_ = this.document_.createElement('div');
1859 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08001860 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08001861 'font-size: xx-large;' +
1862 'opacity: 0.75;' +
1863 'padding: 0.2em 0.5em 0.2em 0.5em;' +
1864 'position: absolute;' +
1865 '-webkit-user-select: none;' +
1866 '-webkit-transition: opacity 180ms ease-in;');
1867 }
1868
rginda9f5222b2012-03-05 11:53:28 -08001869 this.overlayNode_.style.color = this.prefs_.get('background-color');
1870 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
1871 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
1872
rgindaf0090c92012-02-10 14:58:52 -08001873 this.overlayNode_.textContent = msg;
1874 this.overlayNode_.style.opacity = '0.75';
1875
1876 if (!this.overlayNode_.parentNode)
1877 this.div_.appendChild(this.overlayNode_);
1878
1879 this.overlayNode_.style.top = (
1880 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
1881 this.overlayNode_.style.left = (
1882 this.div_.clientWidth - this.overlayNode_.clientWidth -
1883 this.scrollbarWidthPx) / 2;
1884
1885 var self = this;
1886
1887 if (this.overlayTimeout_)
1888 clearTimeout(this.overlayTimeout_);
1889
rgindacc2996c2012-02-24 14:59:31 -08001890 if (opt_timeout === null)
1891 return;
1892
rgindaf0090c92012-02-10 14:58:52 -08001893 this.overlayTimeout_ = setTimeout(function() {
1894 self.overlayNode_.style.opacity = '0';
1895 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07001896 if (self.overlayNode_.parentNode)
1897 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08001898 self.overlayTimeout_ = null;
1899 self.overlayNode_.style.opacity = '0.75';
1900 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08001901 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08001902};
1903
1904hterm.Terminal.prototype.overlaySize = function() {
1905 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
1906};
1907
rginda87b86462011-12-14 13:48:03 -08001908/**
1909 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
1910 *
1911 * @param {string} string The VT string representing the keystroke.
1912 */
1913hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08001914 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08001915 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1916
1917 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08001918};
1919
1920/**
1921 * React when the ScrollPort is scrolled.
1922 */
1923hterm.Terminal.prototype.onScroll_ = function() {
1924 this.scheduleSyncCursorPosition_();
1925};
1926
1927/**
rginda9846e2f2012-01-27 13:53:33 -08001928 * React when text is pasted into the scrollPort.
1929 */
1930hterm.Terminal.prototype.onPaste_ = function(e) {
1931 this.io.onVTKeystroke(e.text);
1932};
1933
1934/**
rginda8ba33642011-12-14 12:31:31 -08001935 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08001936 *
1937 * Note: This function should not directly contain code that alters the internal
1938 * state of the terminal. That kind of code belongs in realizeWidth or
1939 * realizeHeight, so that it can be executed synchronously in the case of a
1940 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08001941 */
1942hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08001943 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08001944 this.scrollPort_.characterSize.width);
1945 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
1946 this.scrollPort_.characterSize.height);
1947
1948 if (!(columnCount || rowCount)) {
1949 // We avoid these situations since they happen sometimes when the terminal
1950 // gets removed from the document, and we can't deal with that.
1951 return;
1952 }
1953
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001954 this.realizeSize_(columnCount, rowCount);
rgindac9bc5502012-01-18 11:48:44 -08001955 this.scheduleSyncCursorPosition_();
rgindaf0090c92012-02-10 14:58:52 -08001956 this.overlaySize();
rginda8ba33642011-12-14 12:31:31 -08001957};
1958
1959/**
1960 * Service the cursor blink timeout.
1961 */
1962hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08001963 if (this.cursorNode_.style.opacity == '0') {
1964 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001965 } else {
rginda87b86462011-12-14 13:48:03 -08001966 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001967 }
1968};
David Reveman8f552492012-03-28 12:18:41 -04001969
1970/**
1971 * Set the scrollbar-visible mode bit.
1972 *
1973 * If scrollbar-visible is on, the vertical scrollbar will be visible.
1974 * Otherwise it will not.
1975 *
1976 * Defaults to on.
1977 *
1978 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
1979 */
1980hterm.Terminal.prototype.setScrollbarVisible = function(state) {
1981 this.scrollPort_.setScrollbarVisible(state);
1982};