blob: 0a10263d453ca8383a089a8249cb8334ee5bda1e [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
134 ([/**
135 * The default colors for text with no other color attributes.
136 */
137 ['foreground-color', 'white', function(v) {
138 self.scrollPort_.setForegroundColor(v);
139 }
140 ],
141
142 ['background-color', 'black', function(v) {
143 self.scrollPort_.setBackgroundColor(v);
144 }
145 ],
146
147 /**
148 * Default font family for the terminal text.
149 */
150 ['font-family', ('"DejaVu Sans Mono", "Everson Mono", ' +
151 'FreeMono, "Menlo", "Lucida Console", ' +
152 'monospace'),
153 function(v) { self.syncFontFamily() }
154 ],
155
156 /**
157 * Anti-aliasing.
158 */
159 ['font-smoothing', 'antialiased',
160 function(v) { self.syncFontFamily() }
161 ],
162
163 /**
164 * True if we should use bold weight font for text with the bold/bright
165 * attribute. False to use bright colors only. Null to autodetect.
166 */
167 ['enable-bold', null, function(v) {
168 self.syncBoldSafeState();
169 }
170 ],
171
172 /**
173 * The color of the visible cursor.
174 */
175 ['cursor-color', 'rgba(255,0,0,0.5)', function(v) {
176 self.cursorNode_.style.backgroundColor = v;
177 }
178 ],
179
180 /**
181 * If true, scroll to the bottom on any keystroke.
182 */
183 ['scroll-on-keystroke', true, function(v) {
184 self.scrollOnKeystroke_ = v;
185 }
186 ],
187
188 /**
189 * If true, scroll to the bottom on terminal output.
190 */
191 ['scroll-on-output', false, function(v) {
192 self.scrollOnOutput_ = v;
193 }
194 ],
195
196 /**
197 * The default font size in pixels.
198 */
199 ['font-size', 15, function(v) {
200 self.setFontSize(v);
201 }
202 ],
203
204 /**
205 * Terminal bell sound. Empty string for no audible bell.
206 */
207 ['audible-bell-sound', '../audio/bell.ogg', function(v) {
208 self.bellAudio_.setAttribute('src', v);
209 }
210 ],
211 ]);
212
213 if (needSync)
214 this.prefs_.notifyAll();
215};
216
217/**
218 * Return the current terminal background color.
219 *
220 * Intended for use by other classes, so we don't have to expose the entire
221 * prefs_ object.
222 */
223hterm.Terminal.prototype.getBackgroundColor = function() {
224 return this.prefs_.get('background-color');
225};
226
227/**
228 * Return the current terminal foreground color.
229 *
230 * Intended for use by other classes, so we don't have to expose the entire
231 * prefs_ object.
232 */
233hterm.Terminal.prototype.getForegroundColor = function() {
234 return this.prefs_.get('foreground-color');
235};
236
237/**
rginda87b86462011-12-14 13:48:03 -0800238 * Create a new instance of a terminal command and run it with a given
239 * argument string.
240 *
241 * @param {function} commandClass The constructor for a terminal command.
242 * @param {string} argString The argument string to pass to the command.
243 */
244hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
245 var self = this;
246 this.command = new commandClass(
247 { argString: argString || '',
248 io: this.io.push(),
249 onExit: function(code) {
250 self.io.pop();
251 self.io.println(hterm.msg('COMMAND_COMPLETE',
252 [self.command.commandName, code]));
rgindafeaf3142012-01-31 15:14:20 -0800253 self.uninstallKeyboard();
rginda87b86462011-12-14 13:48:03 -0800254 }
255 });
256
rgindafeaf3142012-01-31 15:14:20 -0800257 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800258 this.command.run();
259};
260
261/**
rgindafeaf3142012-01-31 15:14:20 -0800262 * Returns true if the current screen is the primary screen, false otherwise.
263 */
264hterm.Terminal.prototype.isPrimaryScreen = function() {
265 return this.screen_ = this.primaryScreen_;
266};
267
268/**
269 * Install the keyboard handler for this terminal.
270 *
271 * This will prevent the browser from seeing any keystrokes sent to the
272 * terminal.
273 */
274hterm.Terminal.prototype.installKeyboard = function() {
275 this.keyboard.installKeyboard(this.document_.body.firstChild);
276}
277
278/**
279 * Uninstall the keyboard handler for this terminal.
280 */
281hterm.Terminal.prototype.uninstallKeyboard = function() {
282 this.keyboard.installKeyboard(null);
283}
284
285/**
rginda35c456b2012-02-09 17:29:05 -0800286 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800287 *
288 * Call setFontSize(0) to reset to the default font size.
289 *
290 * This function does not modify the font-size preference.
291 *
292 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800293 */
294hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800295 if (px === 0)
296 px = this.prefs_.get('font-size');
297
rginda35c456b2012-02-09 17:29:05 -0800298 this.scrollPort_.setFontSize(px);
299};
300
301/**
302 * Get the current font size.
303 */
304hterm.Terminal.prototype.getFontSize = function() {
305 return this.scrollPort_.getFontSize();
306};
307
308/**
309 * Set the CSS "font-family" for this terminal.
310 */
rginda9f5222b2012-03-05 11:53:28 -0800311hterm.Terminal.prototype.syncFontFamily = function() {
312 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
313 this.prefs_.get('font-smoothing'));
314 this.syncBoldSafeState();
315};
316
317hterm.Terminal.prototype.syncBoldSafeState = function() {
318 var enableBold = this.prefs_.get('enable-bold');
319 if (enableBold !== null) {
320 this.screen_.textAttributes.enableBold = enableBold;
321 return;
322 }
323
rgindaf7521392012-02-28 17:20:34 -0800324 var normalSize = this.scrollPort_.measureCharacterSize();
325 var boldSize = this.scrollPort_.measureCharacterSize('bold');
326
327 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800328 if (!isBoldSafe) {
329 console.warn('Bold characters disabled: Size of bold weight differs ' +
330 'from normal. Font family is: ' + str);
331 }
rginda9f5222b2012-03-05 11:53:28 -0800332
333 this.screen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800334};
335
336/**
rginda87b86462011-12-14 13:48:03 -0800337 * Return a copy of the current cursor position.
338 *
339 * @return {hterm.RowCol} The RowCol object representing the current position.
340 */
341hterm.Terminal.prototype.saveCursor = function() {
342 return this.screen_.cursorPosition.clone();
343};
344
rgindaa19afe22012-01-25 15:40:22 -0800345hterm.Terminal.prototype.getTextAttributes = function() {
346 return this.screen_.textAttributes;
347};
348
rginda87b86462011-12-14 13:48:03 -0800349/**
rginda9846e2f2012-01-27 13:53:33 -0800350 * Change the title of this terminal's window.
351 */
352hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800353 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800354};
355
356/**
rginda87b86462011-12-14 13:48:03 -0800357 * Restore a previously saved cursor position.
358 *
359 * @param {hterm.RowCol} cursor The position to restore.
360 */
361hterm.Terminal.prototype.restoreCursor = function(cursor) {
rginda35c456b2012-02-09 17:29:05 -0800362 var row = hterm.clamp(cursor.row, 0, this.screenSize.height - 1);
363 var column = hterm.clamp(cursor.column, 0, this.screenSize.width - 1);
364 this.screen_.setCursorPosition(row, column);
365 if (cursor.column > column ||
366 cursor.column == column && cursor.overflow) {
367 this.screen_.cursorPosition.overflow = true;
368 }
rginda87b86462011-12-14 13:48:03 -0800369};
370
371/**
372 * Set the width of the terminal, resizing the UI to match.
373 */
374hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800375 if (columnCount == null) {
376 this.div_.style.width = '100%';
377 return;
378 }
379
rginda35c456b2012-02-09 17:29:05 -0800380 this.div_.style.width = this.scrollPort_.characterSize.width *
381 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400382 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800383 this.scheduleSyncCursorPosition_();
384};
rginda87b86462011-12-14 13:48:03 -0800385
rgindac9bc5502012-01-18 11:48:44 -0800386/**
rginda35c456b2012-02-09 17:29:05 -0800387 * Set the height of the terminal, resizing the UI to match.
388 */
389hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800390 if (rowCount == null) {
391 this.div_.style.height = '100%';
392 return;
393 }
394
rginda35c456b2012-02-09 17:29:05 -0800395 this.div_.style.height =
rginda9f5222b2012-03-05 11:53:28 -0800396 this.scrollPort_.characterSize.height * rowCount + 1 + 'px';
rginda35c456b2012-02-09 17:29:05 -0800397 this.realizeSize_(this.screenSize.width, rowCount);
398 this.scheduleSyncCursorPosition_();
399};
400
401/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400402 * Deal with terminal size changes.
403 *
404 */
405hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
406 if (columnCount != this.screenSize.width)
407 this.realizeWidth_(columnCount);
408
409 if (rowCount != this.screenSize.height)
410 this.realizeHeight_(rowCount);
411
412 // Send new terminal size to plugin.
413 this.io.onTerminalResize(columnCount, rowCount);
414};
415
416/**
rgindac9bc5502012-01-18 11:48:44 -0800417 * Deal with terminal width changes.
418 *
419 * This function does what needs to be done when the terminal width changes
420 * out from under us. It happens here rather than in onResize_() because this
421 * code may need to run synchronously to handle programmatic changes of
422 * terminal width.
423 *
424 * Relying on the browser to send us an async resize event means we may not be
425 * in the correct state yet when the next escape sequence hits.
426 */
427hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
428 var deltaColumns = columnCount - this.screen_.getWidth();
429
rginda87b86462011-12-14 13:48:03 -0800430 this.screenSize.width = columnCount;
431 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800432
433 if (deltaColumns > 0) {
434 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
435 } else {
436 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
437 if (this.tabStops_[i] <= columnCount)
438 break;
439
440 this.tabStops_.pop();
441 }
442 }
443
444 this.screen_.setColumnCount(this.screenSize.width);
445};
446
447/**
448 * Deal with terminal height changes.
449 *
450 * This function does what needs to be done when the terminal height changes
451 * out from under us. It happens here rather than in onResize_() because this
452 * code may need to run synchronously to handle programmatic changes of
453 * terminal height.
454 *
455 * Relying on the browser to send us an async resize event means we may not be
456 * in the correct state yet when the next escape sequence hits.
457 */
458hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
459 var deltaRows = rowCount - this.screen_.getHeight();
460
461 this.screenSize.height = rowCount;
462
463 var cursor = this.saveCursor();
464
465 if (deltaRows < 0) {
466 // Screen got smaller.
467 deltaRows *= -1;
468 while (deltaRows) {
469 var lastRow = this.getRowCount() - 1;
470 if (lastRow - this.scrollbackRows_.length == cursor.row)
471 break;
472
473 if (this.getRowText(lastRow))
474 break;
475
476 this.screen_.popRow();
477 deltaRows--;
478 }
479
480 var ary = this.screen_.shiftRows(deltaRows);
481 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
482
483 // We just removed rows from the top of the screen, we need to update
484 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800485 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800486 } else if (deltaRows > 0) {
487 // Screen got larger.
488
489 if (deltaRows <= this.scrollbackRows_.length) {
490 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
491 var rows = this.scrollbackRows_.splice(
492 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
493 this.screen_.unshiftRows(rows);
494 deltaRows -= scrollbackCount;
495 cursor.row += scrollbackCount;
496 }
497
498 if (deltaRows)
499 this.appendRows_(deltaRows);
500 }
501
rginda35c456b2012-02-09 17:29:05 -0800502 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800503 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800504};
505
506/**
507 * Scroll the terminal to the top of the scrollback buffer.
508 */
509hterm.Terminal.prototype.scrollHome = function() {
510 this.scrollPort_.scrollRowToTop(0);
511};
512
513/**
514 * Scroll the terminal to the end.
515 */
516hterm.Terminal.prototype.scrollEnd = function() {
517 this.scrollPort_.scrollRowToBottom(this.getRowCount());
518};
519
520/**
521 * Scroll the terminal one page up (minus one line) relative to the current
522 * position.
523 */
524hterm.Terminal.prototype.scrollPageUp = function() {
525 var i = this.scrollPort_.getTopRowIndex();
526 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
527};
528
529/**
530 * Scroll the terminal one page down (minus one line) relative to the current
531 * position.
532 */
533hterm.Terminal.prototype.scrollPageDown = function() {
534 var i = this.scrollPort_.getTopRowIndex();
535 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800536};
537
rgindac9bc5502012-01-18 11:48:44 -0800538/**
539 * Full terminal reset.
540 */
rginda87b86462011-12-14 13:48:03 -0800541hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800542 this.clearAllTabStops();
543 this.setDefaultTabStops();
544 this.clearColorAndAttributes();
545 this.setVTScrollRegion(null, null);
546 this.clear();
547 this.setAbsoluteCursorPosition(0, 0);
548 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800549};
550
rgindac9bc5502012-01-18 11:48:44 -0800551/**
552 * Soft terminal reset.
553 */
rginda0f5c0292012-01-13 11:00:13 -0800554hterm.Terminal.prototype.softReset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800555 this.options_ = new hterm.Options();
rgindaa19afe22012-01-25 15:40:22 -0800556 this.setCursorVisible(true);
557 this.setCursorBlink(false);
rginda0f5c0292012-01-13 11:00:13 -0800558};
559
rgindac9bc5502012-01-18 11:48:44 -0800560/**
561 * Move the cursor forward to the next tab stop, or to the last column
562 * if no more tab stops are set.
563 */
564hterm.Terminal.prototype.forwardTabStop = function() {
565 var column = this.screen_.cursorPosition.column;
566
567 for (var i = 0; i < this.tabStops_.length; i++) {
568 if (this.tabStops_[i] > column) {
569 this.setCursorColumn(this.tabStops_[i]);
570 return;
571 }
572 }
573
574 this.setCursorColumn(this.screenSize.width - 1);
rginda0f5c0292012-01-13 11:00:13 -0800575};
576
rgindac9bc5502012-01-18 11:48:44 -0800577/**
578 * Move the cursor backward to the previous tab stop, or to the first column
579 * if no previous tab stops are set.
580 */
581hterm.Terminal.prototype.backwardTabStop = function() {
582 var column = this.screen_.cursorPosition.column;
583
584 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
585 if (this.tabStops_[i] < column) {
586 this.setCursorColumn(this.tabStops_[i]);
587 return;
588 }
589 }
590
591 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800592};
593
rgindac9bc5502012-01-18 11:48:44 -0800594/**
595 * Set a tab stop at the given column.
596 *
597 * @param {int} column Zero based column.
598 */
599hterm.Terminal.prototype.setTabStop = function(column) {
600 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
601 if (this.tabStops_[i] == column)
602 return;
603
604 if (this.tabStops_[i] < column) {
605 this.tabStops_.splice(i + 1, 0, column);
606 return;
607 }
608 }
609
610 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800611};
612
rgindac9bc5502012-01-18 11:48:44 -0800613/**
614 * Clear the tab stop at the current cursor position.
615 *
616 * No effect if there is no tab stop at the current cursor position.
617 */
618hterm.Terminal.prototype.clearTabStopAtCursor = function() {
619 var column = this.screen_.cursorPosition.column;
620
621 var i = this.tabStops_.indexOf(column);
622 if (i == -1)
623 return;
624
625 this.tabStops_.splice(i, 1);
626};
627
628/**
629 * Clear all tab stops.
630 */
631hterm.Terminal.prototype.clearAllTabStops = function() {
632 this.tabStops_.length = 0;
633};
634
635/**
636 * Set up the default tab stops, starting from a given column.
637 *
638 * This sets a tabstop every (column % this.tabWidth) column, starting
639 * from the specified column, or 0 if no column is provided.
640 *
641 * This does not clear the existing tab stops first, use clearAllTabStops
642 * for that.
643 *
644 * @param {int} opt_start Optional starting zero based starting column, useful
645 * for filling out missing tab stops when the terminal is resized.
646 */
647hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
648 var start = opt_start || 0;
649 var w = this.tabWidth;
650 var stopCount = Math.floor((this.screenSize.width - start) / this.tabWidth)
651 for (var i = 0; i < stopCount; i++) {
652 this.setTabStop(Math.floor((start + i * w) / w) * w + w);
653 }
rginda87b86462011-12-14 13:48:03 -0800654};
655
rginda6d397402012-01-17 10:58:29 -0800656/**
657 * Save cursor position and attributes.
658 *
659 * TODO(rginda): Save attributes once we support them.
660 */
rginda87b86462011-12-14 13:48:03 -0800661hterm.Terminal.prototype.saveOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800662 this.savedOptions_.cursor = this.saveCursor();
rgindaa19afe22012-01-25 15:40:22 -0800663 this.savedOptions_.textAttributes = this.screen_.textAttributes.clone();
rginda87b86462011-12-14 13:48:03 -0800664};
665
rginda6d397402012-01-17 10:58:29 -0800666/**
667 * Restore cursor position and attributes.
668 *
669 * TODO(rginda): Restore attributes once we support them.
670 */
rginda87b86462011-12-14 13:48:03 -0800671hterm.Terminal.prototype.restoreOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800672 if (this.savedOptions_.cursor)
673 this.restoreCursor(this.savedOptions_.cursor);
rgindaa19afe22012-01-25 15:40:22 -0800674 if (this.savedOptions_.textAttributes)
675 this.screen_.textAttributes = this.savedOptions_.textAttributes;
rginda8ba33642011-12-14 12:31:31 -0800676};
677
678/**
679 * Interpret a sequence of characters.
680 *
681 * Incomplete escape sequences are buffered until the next call.
682 *
683 * @param {string} str Sequence of characters to interpret or pass through.
684 */
685hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800686 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800687 this.scheduleSyncCursorPosition_();
688};
689
690/**
691 * Take over the given DIV for use as the terminal display.
692 *
693 * @param {HTMLDivElement} div The div to use as the terminal display.
694 */
695hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800696 this.div_ = div;
697
rginda8ba33642011-12-14 12:31:31 -0800698 this.scrollPort_.decorate(div);
rgindaf7521392012-02-28 17:20:34 -0800699
rginda9f5222b2012-03-05 11:53:28 -0800700 this.setFontSize(this.prefs_.get('font-size'));
701 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -0800702
rginda8ba33642011-12-14 12:31:31 -0800703 this.document_ = this.scrollPort_.getDocument();
704
rginda8ba33642011-12-14 12:31:31 -0800705 this.cursorNode_ = this.document_.createElement('div');
706 this.cursorNode_.style.cssText =
707 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800708 'top: -99px;' +
709 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -0800710 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
711 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda6d397402012-01-17 10:58:29 -0800712 '-webkit-transition: opacity, background-color 100ms linear;' +
rginda9f5222b2012-03-05 11:53:28 -0800713 'background-color: ' + this.prefs_.get('cursor-color'));
rginda8ba33642011-12-14 12:31:31 -0800714 this.document_.body.appendChild(this.cursorNode_);
715
716 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -0800717
rginda87b86462011-12-14 13:48:03 -0800718 var link = this.document_.createElement('link');
719 link.setAttribute('href', '../css/dialogs.css');
720 link.setAttribute('rel', 'stylesheet');
721 this.document_.head.appendChild(link);
722
723 this.alertDialog = new AlertDialog(this.document_.body);
724 this.promptDialog = new PromptDialog(this.document_.body);
725 this.confirmDialog = new ConfirmDialog(this.document_.body);
726
727 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -0800728 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -0800729};
730
731hterm.Terminal.prototype.getDocument = function() {
732 return this.document_;
rginda8ba33642011-12-14 12:31:31 -0800733};
734
735/**
736 * Return the HTML Element for a given row index.
737 *
738 * This is a method from the RowProvider interface. The ScrollPort uses
739 * it to fetch rows on demand as they are scrolled into view.
740 *
741 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
742 * pairs to conserve memory.
743 *
744 * @param {integer} index The zero-based row index, measured relative to the
745 * start of the scrollback buffer. On-screen rows will always have the
746 * largest indicies.
747 * @return {HTMLElement} The 'x-row' element containing for the requested row.
748 */
749hterm.Terminal.prototype.getRowNode = function(index) {
750 if (index < this.scrollbackRows_.length)
751 return this.scrollbackRows_[index];
752
753 var screenIndex = index - this.scrollbackRows_.length;
754 return this.screen_.rowsArray[screenIndex];
755};
756
757/**
758 * Return the text content for a given range of rows.
759 *
760 * This is a method from the RowProvider interface. The ScrollPort uses
761 * it to fetch text content on demand when the user attempts to copy their
762 * selection to the clipboard.
763 *
764 * @param {integer} start The zero-based row index to start from, measured
765 * relative to the start of the scrollback buffer. On-screen rows will
766 * always have the largest indicies.
767 * @param {integer} end The zero-based row index to end on, measured
768 * relative to the start of the scrollback buffer.
769 * @return {string} A single string containing the text value of the range of
770 * rows. Lines will be newline delimited, with no trailing newline.
771 */
772hterm.Terminal.prototype.getRowsText = function(start, end) {
773 var ary = [];
774 for (var i = start; i < end; i++) {
775 var node = this.getRowNode(i);
776 ary.push(node.textContent);
777 }
778
779 return ary.join('\n');
780};
781
782/**
783 * Return the text content for a given row.
784 *
785 * This is a method from the RowProvider interface. The ScrollPort uses
786 * it to fetch text content on demand when the user attempts to copy their
787 * selection to the clipboard.
788 *
789 * @param {integer} index The zero-based row index to return, measured
790 * relative to the start of the scrollback buffer. On-screen rows will
791 * always have the largest indicies.
792 * @return {string} A string containing the text value of the selected row.
793 */
794hterm.Terminal.prototype.getRowText = function(index) {
795 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -0800796 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -0800797};
798
799/**
800 * Return the total number of rows in the addressable screen and in the
801 * scrollback buffer of this terminal.
802 *
803 * This is a method from the RowProvider interface. The ScrollPort uses
804 * it to compute the size of the scrollbar.
805 *
806 * @return {integer} The number of rows in this terminal.
807 */
808hterm.Terminal.prototype.getRowCount = function() {
809 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
810};
811
812/**
813 * Create DOM nodes for new rows and append them to the end of the terminal.
814 *
815 * This is the only correct way to add a new DOM node for a row. Notice that
816 * the new row is appended to the bottom of the list of rows, and does not
817 * require renumbering (of the rowIndex property) of previous rows.
818 *
819 * If you think you want a new blank row somewhere in the middle of the
820 * terminal, look into moveRows_().
821 *
822 * This method does not pay attention to vtScrollTop/Bottom, since you should
823 * be using moveRows() in cases where they would matter.
824 *
825 * The cursor will be positioned at column 0 of the first inserted line.
826 */
827hterm.Terminal.prototype.appendRows_ = function(count) {
828 var cursorRow = this.screen_.rowsArray.length;
829 var offset = this.scrollbackRows_.length + cursorRow;
830 for (var i = 0; i < count; i++) {
831 var row = this.document_.createElement('x-row');
832 row.appendChild(this.document_.createTextNode(''));
833 row.rowIndex = offset + i;
834 this.screen_.pushRow(row);
835 }
836
837 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
838 if (extraRows > 0) {
839 var ary = this.screen_.shiftRows(extraRows);
840 Array.prototype.push.apply(this.scrollbackRows_, ary);
841 this.scheduleScrollDown_();
842 }
843
844 if (cursorRow >= this.screen_.rowsArray.length)
845 cursorRow = this.screen_.rowsArray.length - 1;
846
rginda87b86462011-12-14 13:48:03 -0800847 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -0800848};
849
850/**
851 * Relocate rows from one part of the addressable screen to another.
852 *
853 * This is used to recycle rows during VT scrolls (those which are driven
854 * by VT commands, rather than by the user manipulating the scrollbar.)
855 *
856 * In this case, the blank lines scrolled into the scroll region are made of
857 * the nodes we scrolled off. These have their rowIndex properties carefully
858 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -0800859 */
860hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
861 var ary = this.screen_.removeRows(fromIndex, count);
862 this.screen_.insertRows(toIndex, ary);
863
864 var start, end;
865 if (fromIndex < toIndex) {
866 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -0800867 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800868 } else {
869 start = toIndex;
rginda87b86462011-12-14 13:48:03 -0800870 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800871 }
872
873 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -0800874 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -0800875};
876
877/**
878 * Renumber the rowIndex property of the given range of rows.
879 *
880 * The start and end indicies are relative to the screen, not the scrollback.
881 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -0800882 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -0800883 * no need to renumber scrollback rows.
884 */
885hterm.Terminal.prototype.renumberRows_ = function(start, end) {
886 var offset = this.scrollbackRows_.length;
887 for (var i = start; i < end; i++) {
888 this.screen_.rowsArray[i].rowIndex = offset + i;
889 }
890};
891
892/**
893 * Print a string to the terminal.
894 *
895 * This respects the current insert and wraparound modes. It will add new lines
896 * to the end of the terminal, scrolling off the top into the scrollback buffer
897 * if necessary.
898 *
899 * The string is *not* parsed for escape codes. Use the interpret() method if
900 * that's what you're after.
901 *
902 * @param{string} str The string to print.
903 */
904hterm.Terminal.prototype.print = function(str) {
rgindaa19afe22012-01-25 15:40:22 -0800905 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
906 this.newLine();
rginda2312fff2012-01-05 16:20:52 -0800907
rgindaa19afe22012-01-25 15:40:22 -0800908 if (this.options_.insertMode) {
909 this.screen_.insertString(str);
910 } else {
911 this.screen_.overwriteString(str);
912 }
913
914 var overflow = this.screen_.maybeClipCurrentRow();
915
916 if (this.options_.wraparound && overflow) {
917 var lastColumn;
918
919 do {
rginda35c456b2012-02-09 17:29:05 -0800920 this.newLine();
921 lastColumn = overflow.characterLength;
rgindaa19afe22012-01-25 15:40:22 -0800922
923 if (!this.options_.insertMode)
924 this.screen_.deleteChars(overflow.characterLength);
925
926 this.screen_.prependNodes(overflow);
rgindaa19afe22012-01-25 15:40:22 -0800927
928 overflow = this.screen_.maybeClipCurrentRow();
929 } while (overflow);
930
931 this.setCursorColumn(lastColumn);
932 }
rginda8ba33642011-12-14 12:31:31 -0800933
934 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -0800935
rginda9f5222b2012-03-05 11:53:28 -0800936 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -0800937 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -0800938};
939
940/**
rginda87b86462011-12-14 13:48:03 -0800941 * Set the VT scroll region.
942 *
rginda87b86462011-12-14 13:48:03 -0800943 * This also resets the cursor position to the absolute (0, 0) position, since
944 * that's what xterm appears to do.
945 *
946 * @param {integer} scrollTop The zero-based top of the scroll region.
947 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
948 * inclusive.
949 */
950hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
951 this.vtScrollTop_ = scrollTop;
952 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -0800953};
954
955/**
rginda8ba33642011-12-14 12:31:31 -0800956 * Return the top row index according to the VT.
957 *
958 * This will return 0 unless the terminal has been told to restrict scrolling
959 * to some lower row. It is used for some VT cursor positioning and scrolling
960 * commands.
961 *
962 * @return {integer} The topmost row in the terminal's scroll region.
963 */
964hterm.Terminal.prototype.getVTScrollTop = function() {
965 if (this.vtScrollTop_ != null)
966 return this.vtScrollTop_;
967
968 return 0;
rginda87b86462011-12-14 13:48:03 -0800969};
rginda8ba33642011-12-14 12:31:31 -0800970
971/**
972 * Return the bottom row index according to the VT.
973 *
974 * This will return the height of the terminal unless the it has been told to
975 * restrict scrolling to some higher row. It is used for some VT cursor
976 * positioning and scrolling commands.
977 *
978 * @return {integer} The bottommost row in the terminal's scroll region.
979 */
980hterm.Terminal.prototype.getVTScrollBottom = function() {
981 if (this.vtScrollBottom_ != null)
982 return this.vtScrollBottom_;
983
rginda87b86462011-12-14 13:48:03 -0800984 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -0800985}
986
987/**
988 * Process a '\n' character.
989 *
990 * If the cursor is on the final row of the terminal this will append a new
991 * blank row to the screen and scroll the topmost row into the scrollback
992 * buffer.
993 *
994 * Otherwise, this moves the cursor to column zero of the next row.
995 */
996hterm.Terminal.prototype.newLine = function() {
997 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -0800998 // If we're at the end of the screen we need to append a new line and
999 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001000 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001001 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1002 // End of the scroll region does not affect the scrollback buffer.
1003 this.vtScrollUp(1);
1004 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001005 } else {
rginda87b86462011-12-14 13:48:03 -08001006 // Anywhere else in the screen just moves the cursor.
1007 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001008 }
1009};
1010
1011/**
1012 * Like newLine(), except maintain the cursor column.
1013 */
1014hterm.Terminal.prototype.lineFeed = function() {
1015 var column = this.screen_.cursorPosition.column;
1016 this.newLine();
1017 this.setCursorColumn(column);
1018};
1019
1020/**
rginda87b86462011-12-14 13:48:03 -08001021 * If autoCarriageReturn is set then newLine(), else lineFeed().
1022 */
1023hterm.Terminal.prototype.formFeed = function() {
1024 if (this.options_.autoCarriageReturn) {
1025 this.newLine();
1026 } else {
1027 this.lineFeed();
1028 }
1029};
1030
1031/**
1032 * Move the cursor up one row, possibly inserting a blank line.
1033 *
1034 * The cursor column is not changed.
1035 */
1036hterm.Terminal.prototype.reverseLineFeed = function() {
1037 var scrollTop = this.getVTScrollTop();
1038 var currentRow = this.screen_.cursorPosition.row;
1039
1040 if (currentRow == scrollTop) {
1041 this.insertLines(1);
1042 } else {
1043 this.setAbsoluteCursorRow(currentRow - 1);
1044 }
1045};
1046
1047/**
rginda8ba33642011-12-14 12:31:31 -08001048 * Replace all characters to the left of the current cursor with the space
1049 * character.
1050 *
1051 * TODO(rginda): This should probably *remove* the characters (not just replace
1052 * with a space) if there are no characters at or beyond the current cursor
1053 * position. Once it does that, it'll have the same text-attribute related
1054 * issues as hterm.Screen.prototype.clearCursorRow :/
1055 */
1056hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001057 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001058 this.setCursorColumn(0);
rginda87b86462011-12-14 13:48:03 -08001059 this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1));
1060 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001061};
1062
1063/**
1064 * Erase a given number of characters to the right of the cursor, shifting
1065 * remaining characters to the left.
1066 *
1067 * The cursor position is unchanged.
1068 *
1069 * TODO(rginda): Test that this works even when the cursor is positioned beyond
1070 * the end of the text.
1071 *
1072 * TODO(rginda): This likely has text-attribute related troubles similar to the
1073 * todo on hterm.Screen.prototype.clearCursorRow.
1074 */
1075hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001076 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001077
rginda87b86462011-12-14 13:48:03 -08001078 var maxCount = this.screenSize.width - cursor.column;
rginda8ba33642011-12-14 12:31:31 -08001079 var count = (opt_count && opt_count < maxCount) ? opt_count : maxCount;
1080 this.screen_.deleteChars(count);
rginda87b86462011-12-14 13:48:03 -08001081 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001082};
1083
1084/**
1085 * Erase the current line.
1086 *
1087 * The cursor position is unchanged.
1088 *
1089 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1090 * has a text-attribute related TODO.
1091 */
1092hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001093 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001094 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001095 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001096};
1097
1098/**
1099 * Erase all characters from the start of the scroll region to the current
1100 * cursor position.
1101 *
1102 * The cursor position is unchanged.
1103 *
1104 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1105 * has a text-attribute related TODO.
1106 */
1107hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001108 var cursor = this.saveCursor();
1109
1110 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001111
1112 var top = this.getVTScrollTop();
rginda87b86462011-12-14 13:48:03 -08001113 for (var i = top; i < cursor.row; i++) {
1114 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001115 this.screen_.clearCursorRow();
1116 }
1117
rginda87b86462011-12-14 13:48:03 -08001118 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001119};
1120
1121/**
1122 * Erase all characters from the current cursor position to the end of the
1123 * scroll region.
1124 *
1125 * The cursor position is unchanged.
1126 *
1127 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1128 * has a text-attribute related TODO.
1129 */
1130hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001131 var cursor = this.saveCursor();
1132
1133 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001134
1135 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001136 for (var i = cursor.row + 1; i <= bottom; i++) {
1137 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001138 this.screen_.clearCursorRow();
1139 }
1140
rginda87b86462011-12-14 13:48:03 -08001141 this.restoreCursor(cursor);
1142};
1143
1144/**
1145 * Fill the terminal with a given character.
1146 *
1147 * This methods does not respect the VT scroll region.
1148 *
1149 * @param {string} ch The character to use for the fill.
1150 */
1151hterm.Terminal.prototype.fill = function(ch) {
1152 var cursor = this.saveCursor();
1153
1154 this.setAbsoluteCursorPosition(0, 0);
1155 for (var row = 0; row < this.screenSize.height; row++) {
1156 for (var col = 0; col < this.screenSize.width; col++) {
1157 this.setAbsoluteCursorPosition(row, col);
1158 this.screen_.overwriteString(ch);
1159 }
1160 }
1161
1162 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001163};
1164
1165/**
rgindae4d29232012-01-19 10:47:13 -08001166 * Erase the entire display.
rginda8ba33642011-12-14 12:31:31 -08001167 *
rgindae4d29232012-01-19 10:47:13 -08001168 * The cursor position is unchanged. This does not respect the scroll
1169 * region.
rginda8ba33642011-12-14 12:31:31 -08001170 *
1171 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1172 * has a text-attribute related TODO.
1173 */
1174hterm.Terminal.prototype.clear = function() {
rginda87b86462011-12-14 13:48:03 -08001175 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001176
rgindae4d29232012-01-19 10:47:13 -08001177 var bottom = this.screenSize.height;
rginda8ba33642011-12-14 12:31:31 -08001178
rgindae4d29232012-01-19 10:47:13 -08001179 for (var i = 0; i < bottom; i++) {
rginda87b86462011-12-14 13:48:03 -08001180 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001181 this.screen_.clearCursorRow();
1182 }
1183
rginda87b86462011-12-14 13:48:03 -08001184 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001185};
1186
1187/**
1188 * VT command to insert lines at the current cursor row.
1189 *
1190 * This respects the current scroll region. Rows pushed off the bottom are
1191 * lost (they won't show up in the scrollback buffer).
1192 *
1193 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1194 * has a text-attribute related TODO.
1195 *
1196 * @param {integer} count The number of lines to insert.
1197 */
1198hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001199 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001200
1201 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001202 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001203
rgindae4d29232012-01-19 10:47:13 -08001204 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001205 if (start != cursor.row)
1206 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001207
1208 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001209 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001210 this.screen_.clearCursorRow();
1211 }
1212
rginda87b86462011-12-14 13:48:03 -08001213 cursor.column = 0;
1214 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001215};
1216
1217/**
1218 * VT command to delete lines at the current cursor row.
1219 *
1220 * New rows are added to the bottom of scroll region to take their place. New
1221 * rows are strictly there to take up space and have no content or style.
1222 */
1223hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001224 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001225
rginda87b86462011-12-14 13:48:03 -08001226 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001227 var bottom = this.getVTScrollBottom();
1228
rginda87b86462011-12-14 13:48:03 -08001229 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001230 count = Math.min(count, maxCount);
1231
rginda87b86462011-12-14 13:48:03 -08001232 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001233 if (count != maxCount)
1234 this.moveRows_(top, count, moveStart);
1235
1236 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001237 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001238 this.screen_.clearCursorRow();
1239 }
1240
rginda87b86462011-12-14 13:48:03 -08001241 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001242};
1243
1244/**
1245 * Inserts the given number of spaces at the current cursor position.
1246 *
rginda87b86462011-12-14 13:48:03 -08001247 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001248 */
1249hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001250 var cursor = this.saveCursor();
1251
rginda0f5c0292012-01-13 11:00:13 -08001252 var ws = hterm.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001253 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001254 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001255
1256 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001257};
1258
1259/**
1260 * Forward-delete the specified number of characters starting at the cursor
1261 * position.
1262 *
1263 * @param {integer} count The number of characters to delete.
1264 */
1265hterm.Terminal.prototype.deleteChars = function(count) {
1266 this.screen_.deleteChars(count);
1267};
1268
1269/**
1270 * Shift rows in the scroll region upwards by a given number of lines.
1271 *
1272 * New rows are inserted at the bottom of the scroll region to fill the
1273 * vacated rows. The new rows not filled out with the current text attributes.
1274 *
1275 * This function does not affect the scrollback rows at all. Rows shifted
1276 * off the top are lost.
1277 *
rginda87b86462011-12-14 13:48:03 -08001278 * The cursor position is not altered.
1279 *
rginda8ba33642011-12-14 12:31:31 -08001280 * @param {integer} count The number of rows to scroll.
1281 */
1282hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001283 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001284
rginda87b86462011-12-14 13:48:03 -08001285 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001286 this.deleteLines(count);
1287
rginda87b86462011-12-14 13:48:03 -08001288 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001289};
1290
1291/**
1292 * Shift rows below the cursor down by a given number of lines.
1293 *
1294 * This function respects the current scroll region.
1295 *
1296 * New rows are inserted at the top of the scroll region to fill the
1297 * vacated rows. The new rows not filled out with the current text attributes.
1298 *
1299 * This function does not affect the scrollback rows at all. Rows shifted
1300 * off the bottom are lost.
1301 *
1302 * @param {integer} count The number of rows to scroll.
1303 */
1304hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001305 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001306
rginda87b86462011-12-14 13:48:03 -08001307 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001308 this.insertLines(opt_count);
1309
rginda87b86462011-12-14 13:48:03 -08001310 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001311};
1312
rginda87b86462011-12-14 13:48:03 -08001313
rginda8ba33642011-12-14 12:31:31 -08001314/**
1315 * Set the cursor position.
1316 *
1317 * The cursor row is relative to the scroll region if the terminal has
1318 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1319 *
1320 * @param {integer} row The new zero-based cursor row.
1321 * @param {integer} row The new zero-based cursor column.
1322 */
1323hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1324 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001325 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001326 } else {
rginda87b86462011-12-14 13:48:03 -08001327 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001328 }
rginda87b86462011-12-14 13:48:03 -08001329};
rginda8ba33642011-12-14 12:31:31 -08001330
rginda87b86462011-12-14 13:48:03 -08001331hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1332 var scrollTop = this.getVTScrollTop();
1333 row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
rginda2312fff2012-01-05 16:20:52 -08001334 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001335 this.screen_.setCursorPosition(row, column);
1336};
1337
1338hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rginda2312fff2012-01-05 16:20:52 -08001339 row = hterm.clamp(row, 0, this.screenSize.height - 1);
1340 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001341 this.screen_.setCursorPosition(row, column);
1342};
1343
1344/**
1345 * Set the cursor column.
1346 *
1347 * @param {integer} column The new zero-based cursor column.
1348 */
1349hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001350 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001351};
1352
1353/**
1354 * Return the cursor column.
1355 *
1356 * @return {integer} The zero-based cursor column.
1357 */
1358hterm.Terminal.prototype.getCursorColumn = function() {
1359 return this.screen_.cursorPosition.column;
1360};
1361
1362/**
1363 * Set the cursor row.
1364 *
1365 * The cursor row is relative to the scroll region if the terminal has
1366 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1367 *
1368 * @param {integer} row The new cursor row.
1369 */
rginda87b86462011-12-14 13:48:03 -08001370hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1371 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001372};
1373
1374/**
1375 * Return the cursor row.
1376 *
1377 * @return {integer} The zero-based cursor row.
1378 */
1379hterm.Terminal.prototype.getCursorRow = function(row) {
1380 return this.screen_.cursorPosition.row;
1381};
1382
1383/**
1384 * Request that the ScrollPort redraw itself soon.
1385 *
1386 * The redraw will happen asynchronously, soon after the call stack winds down.
1387 * Multiple calls will be coalesced into a single redraw.
1388 */
1389hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001390 if (this.timeouts_.redraw)
1391 return;
rginda8ba33642011-12-14 12:31:31 -08001392
1393 var self = this;
rginda87b86462011-12-14 13:48:03 -08001394 this.timeouts_.redraw = setTimeout(function() {
1395 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001396 self.scrollPort_.redraw_();
1397 }, 0);
1398};
1399
1400/**
1401 * Request that the ScrollPort be scrolled to the bottom.
1402 *
1403 * The scroll will happen asynchronously, soon after the call stack winds down.
1404 * Multiple calls will be coalesced into a single scroll.
1405 *
1406 * This affects the scrollbar position of the ScrollPort, and has nothing to
1407 * do with the VT scroll commands.
1408 */
1409hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1410 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001411 return;
rginda8ba33642011-12-14 12:31:31 -08001412
1413 var self = this;
1414 this.timeouts_.scrollDown = setTimeout(function() {
1415 delete self.timeouts_.scrollDown;
1416 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1417 }, 10);
1418};
1419
1420/**
1421 * Move the cursor up a specified number of rows.
1422 *
1423 * @param {integer} count The number of rows to move the cursor.
1424 */
1425hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001426 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001427};
1428
1429/**
1430 * Move the cursor down a specified number of rows.
1431 *
1432 * @param {integer} count The number of rows to move the cursor.
1433 */
1434hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001435 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001436 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1437 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1438 this.screenSize.height - 1);
1439
1440 var row = hterm.clamp(this.screen_.cursorPosition.row + count,
1441 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001442 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001443};
1444
1445/**
1446 * Move the cursor left a specified number of columns.
1447 *
1448 * @param {integer} count The number of columns to move the cursor.
1449 */
1450hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001451 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001452};
1453
1454/**
1455 * Move the cursor right a specified number of columns.
1456 *
1457 * @param {integer} count The number of columns to move the cursor.
1458 */
1459hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001460 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001461 var column = hterm.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001462 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001463 this.setCursorColumn(column);
1464};
1465
1466/**
1467 * Reverse the foreground and background colors of the terminal.
1468 *
1469 * This only affects text that was drawn with no attributes.
1470 *
1471 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1472 * been drawn with attributes that happen to coincide with the default
1473 * 'no-attribute' colors. My guess is probably not.
1474 */
1475hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001476 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001477 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001478 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1479 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001480 } else {
rginda9f5222b2012-03-05 11:53:28 -08001481 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1482 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001483 }
1484};
1485
1486/**
rginda87b86462011-12-14 13:48:03 -08001487 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001488 */
1489hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001490 if (this.bellAudio_.getAttribute('src'))
1491 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001492
rginda6d397402012-01-17 10:58:29 -08001493 this.cursorNode_.style.backgroundColor =
1494 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001495
1496 var self = this;
1497 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001498 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001499 }, 200);
rginda87b86462011-12-14 13:48:03 -08001500};
1501
1502/**
rginda8ba33642011-12-14 12:31:31 -08001503 * Set the origin mode bit.
1504 *
1505 * If origin mode is on, certain VT cursor and scrolling commands measure their
1506 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1507 * to the top of the addressable screen.
1508 *
1509 * Defaults to off.
1510 *
1511 * @param {boolean} state True to set origin mode, false to unset.
1512 */
1513hterm.Terminal.prototype.setOriginMode = function(state) {
1514 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001515 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001516};
1517
1518/**
1519 * Set the insert mode bit.
1520 *
1521 * If insert mode is on, existing text beyond the cursor position will be
1522 * shifted right to make room for new text. Otherwise, new text overwrites
1523 * any existing text.
1524 *
1525 * Defaults to off.
1526 *
1527 * @param {boolean} state True to set insert mode, false to unset.
1528 */
1529hterm.Terminal.prototype.setInsertMode = function(state) {
1530 this.options_.insertMode = state;
1531};
1532
1533/**
rginda87b86462011-12-14 13:48:03 -08001534 * Set the auto carriage return bit.
1535 *
1536 * If auto carriage return is on then a formfeed character is interpreted
1537 * as a newline, otherwise it's the same as a linefeed. The difference boils
1538 * down to whether or not the cursor column is reset.
1539 */
1540hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1541 this.options_.autoCarriageReturn = state;
1542};
1543
1544/**
rginda8ba33642011-12-14 12:31:31 -08001545 * Set the wraparound mode bit.
1546 *
1547 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1548 * to the start of the following row. Otherwise, the cursor is clamped to the
1549 * end of the screen and attempts to write past it are ignored.
1550 *
1551 * Defaults to on.
1552 *
1553 * @param {boolean} state True to set wraparound mode, false to unset.
1554 */
1555hterm.Terminal.prototype.setWraparound = function(state) {
1556 this.options_.wraparound = state;
1557};
1558
1559/**
1560 * Set the reverse-wraparound mode bit.
1561 *
1562 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1563 * to the end of the previous row. Otherwise, the cursor is clamped to column
1564 * 0.
1565 *
1566 * Defaults to off.
1567 *
1568 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1569 */
1570hterm.Terminal.prototype.setReverseWraparound = function(state) {
1571 this.options_.reverseWraparound = state;
1572};
1573
1574/**
1575 * Selects between the primary and alternate screens.
1576 *
1577 * If alternate mode is on, the alternate screen is active. Otherwise the
1578 * primary screen is active.
1579 *
1580 * Swapping screens has no effect on the scrollback buffer.
1581 *
1582 * Each screen maintains its own cursor position.
1583 *
1584 * Defaults to off.
1585 *
1586 * @param {boolean} state True to set alternate mode, false to unset.
1587 */
1588hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001589 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001590 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1591
rginda35c456b2012-02-09 17:29:05 -08001592 if (this.screen_.rowsArray.length &&
1593 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
1594 // If the screen changed sizes while we were away, our rowIndexes may
1595 // be incorrect.
1596 var offset = this.scrollbackRows_.length;
1597 var ary = this.screen_.rowsArray;
1598 for (i = 0; i < ary.length; i++) {
1599 ary[i].rowIndex = offset + i;
1600 }
1601 }
rginda8ba33642011-12-14 12:31:31 -08001602
rginda35c456b2012-02-09 17:29:05 -08001603 this.realizeWidth_(this.screenSize.width);
1604 this.realizeHeight_(this.screenSize.height);
1605 this.scrollPort_.syncScrollHeight();
1606 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001607
rginda6d397402012-01-17 10:58:29 -08001608 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08001609 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08001610};
1611
1612/**
1613 * Set the cursor-blink mode bit.
1614 *
1615 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1616 * a visible cursor does not blink.
1617 *
1618 * You should make sure to turn blinking off if you're going to dispose of a
1619 * terminal, otherwise you'll leak a timeout.
1620 *
1621 * Defaults to on.
1622 *
1623 * @param {boolean} state True to set cursor-blink mode, false to unset.
1624 */
1625hterm.Terminal.prototype.setCursorBlink = function(state) {
1626 this.options_.cursorBlink = state;
1627
1628 if (!state && this.timeouts_.cursorBlink) {
1629 clearTimeout(this.timeouts_.cursorBlink);
1630 delete this.timeouts_.cursorBlink;
1631 }
1632
1633 if (this.options_.cursorVisible)
1634 this.setCursorVisible(true);
1635};
1636
1637/**
1638 * Set the cursor-visible mode bit.
1639 *
1640 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1641 *
1642 * Defaults to on.
1643 *
1644 * @param {boolean} state True to set cursor-visible mode, false to unset.
1645 */
1646hterm.Terminal.prototype.setCursorVisible = function(state) {
1647 this.options_.cursorVisible = state;
1648
1649 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001650 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001651 return;
1652 }
1653
rginda87b86462011-12-14 13:48:03 -08001654 this.syncCursorPosition_();
1655
1656 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001657
1658 if (this.options_.cursorBlink) {
1659 if (this.timeouts_.cursorBlink)
1660 return;
1661
1662 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1663 500);
1664 } else {
1665 if (this.timeouts_.cursorBlink) {
1666 clearTimeout(this.timeouts_.cursorBlink);
1667 delete this.timeouts_.cursorBlink;
1668 }
1669 }
1670};
1671
1672/**
rginda87b86462011-12-14 13:48:03 -08001673 * Synchronizes the visible cursor and document selection with the current
1674 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08001675 */
1676hterm.Terminal.prototype.syncCursorPosition_ = function() {
1677 var topRowIndex = this.scrollPort_.getTopRowIndex();
1678 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
1679 var cursorRowIndex = this.scrollbackRows_.length +
1680 this.screen_.cursorPosition.row;
1681
1682 if (cursorRowIndex > bottomRowIndex) {
1683 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08001684 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08001685 return;
1686 }
1687
rginda35c456b2012-02-09 17:29:05 -08001688 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
1689 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
1690
rginda8ba33642011-12-14 12:31:31 -08001691 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08001692 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
1693 'px';
1694 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
1695 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08001696
1697 this.cursorNode_.setAttribute('title',
1698 '(' + this.screen_.cursorPosition.row +
1699 ', ' + this.screen_.cursorPosition.column +
1700 ')');
1701
1702 // Update the caret for a11y purposes.
1703 var selection = this.document_.getSelection();
1704 if (selection && selection.isCollapsed)
1705 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08001706};
1707
1708/**
1709 * Synchronizes the visible cursor with the current cursor coordinates.
1710 *
1711 * The sync will happen asynchronously, soon after the call stack winds down.
1712 * Multiple calls will be coalesced into a single sync.
1713 */
1714hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
1715 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08001716 return;
rginda8ba33642011-12-14 12:31:31 -08001717
1718 var self = this;
1719 this.timeouts_.syncCursor = setTimeout(function() {
1720 self.syncCursorPosition_();
1721 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08001722 }, 0);
1723};
1724
rgindacc2996c2012-02-24 14:59:31 -08001725/**
1726 * Show the terminal overlay for a given amount of time.
1727 *
1728 * The terminal overlay appears in inverse video in a large font, centered
1729 * over the terminal. You should probably keep the overlay message brief,
1730 * since it's in a large font and you probably aren't going to check the size
1731 * of the terminal first.
1732 *
1733 * @param {string} msg The text (not HTML) message to display in the overlay.
1734 * @param {number} opt_timeout The amount of time to wait before fading out
1735 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
1736 * stay up forever (or until the next overlay).
1737 */
1738hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08001739 if (!this.overlayNode_) {
1740 if (!this.div_)
1741 return;
1742
1743 this.overlayNode_ = this.document_.createElement('div');
1744 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08001745 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08001746 'font-size: xx-large;' +
1747 'opacity: 0.75;' +
1748 'padding: 0.2em 0.5em 0.2em 0.5em;' +
1749 'position: absolute;' +
1750 '-webkit-user-select: none;' +
1751 '-webkit-transition: opacity 180ms ease-in;');
1752 }
1753
rginda9f5222b2012-03-05 11:53:28 -08001754 this.overlayNode_.style.color = this.prefs_.get('background-color');
1755 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
1756 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
1757
rgindaf0090c92012-02-10 14:58:52 -08001758 this.overlayNode_.textContent = msg;
1759 this.overlayNode_.style.opacity = '0.75';
1760
1761 if (!this.overlayNode_.parentNode)
1762 this.div_.appendChild(this.overlayNode_);
1763
1764 this.overlayNode_.style.top = (
1765 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
1766 this.overlayNode_.style.left = (
1767 this.div_.clientWidth - this.overlayNode_.clientWidth -
1768 this.scrollbarWidthPx) / 2;
1769
1770 var self = this;
1771
1772 if (this.overlayTimeout_)
1773 clearTimeout(this.overlayTimeout_);
1774
rgindacc2996c2012-02-24 14:59:31 -08001775 if (opt_timeout === null)
1776 return;
1777
rgindaf0090c92012-02-10 14:58:52 -08001778 this.overlayTimeout_ = setTimeout(function() {
1779 self.overlayNode_.style.opacity = '0';
1780 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07001781 if (self.overlayNode_.parentNode)
1782 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08001783 self.overlayTimeout_ = null;
1784 self.overlayNode_.style.opacity = '0.75';
1785 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08001786 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08001787};
1788
1789hterm.Terminal.prototype.overlaySize = function() {
1790 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
1791};
1792
rginda87b86462011-12-14 13:48:03 -08001793/**
1794 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
1795 *
1796 * @param {string} string The VT string representing the keystroke.
1797 */
1798hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08001799 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08001800 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1801
1802 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08001803};
1804
1805/**
1806 * React when the ScrollPort is scrolled.
1807 */
1808hterm.Terminal.prototype.onScroll_ = function() {
1809 this.scheduleSyncCursorPosition_();
1810};
1811
1812/**
rginda9846e2f2012-01-27 13:53:33 -08001813 * React when text is pasted into the scrollPort.
1814 */
1815hterm.Terminal.prototype.onPaste_ = function(e) {
1816 this.io.onVTKeystroke(e.text);
1817};
1818
1819/**
rginda8ba33642011-12-14 12:31:31 -08001820 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08001821 *
1822 * Note: This function should not directly contain code that alters the internal
1823 * state of the terminal. That kind of code belongs in realizeWidth or
1824 * realizeHeight, so that it can be executed synchronously in the case of a
1825 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08001826 */
1827hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08001828 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08001829 this.scrollPort_.characterSize.width);
1830 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
1831 this.scrollPort_.characterSize.height);
1832
1833 if (!(columnCount || rowCount)) {
1834 // We avoid these situations since they happen sometimes when the terminal
1835 // gets removed from the document, and we can't deal with that.
1836 return;
1837 }
1838
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001839 this.realizeSize_(columnCount, rowCount);
rgindac9bc5502012-01-18 11:48:44 -08001840 this.scheduleSyncCursorPosition_();
rgindaf0090c92012-02-10 14:58:52 -08001841 this.overlaySize();
rginda8ba33642011-12-14 12:31:31 -08001842};
1843
1844/**
1845 * Service the cursor blink timeout.
1846 */
1847hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08001848 if (this.cursorNode_.style.opacity == '0') {
1849 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001850 } else {
rginda87b86462011-12-14 13:48:03 -08001851 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001852 }
1853};