blob: cb4ecc7071aa5752927d17cdaf868fe3b3b11782 [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).
21 */
rginda35c456b2012-02-09 17:29:05 -080022hterm.Terminal = function() {
rginda8ba33642011-12-14 12:31:31 -080023 // Two screen instances.
24 this.primaryScreen_ = new hterm.Screen();
25 this.alternateScreen_ = new hterm.Screen();
26
27 // The "current" screen.
28 this.screen_ = this.primaryScreen_;
29
rginda8ba33642011-12-14 12:31:31 -080030 // The local notion of the screen size. ScreenBuffers also have a size which
31 // indicates their present size. During size changes, the two may disagree.
32 // Also, the inactive screen's size is not altered until it is made the active
33 // screen.
34 this.screenSize = new hterm.Size(0, 0);
35
rginda8ba33642011-12-14 12:31:31 -080036 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080037 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080038 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
39 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080040 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rginda8ba33642011-12-14 12:31:31 -080041
rginda87b86462011-12-14 13:48:03 -080042 // The div that contains this terminal.
43 this.div_ = null;
44
rgindac9bc5502012-01-18 11:48:44 -080045 // The document that contains the scrollPort. Defaulted to the global
46 // document here so that the terminal is functional even if it hasn't been
47 // inserted into a document yet, but re-set in decorate().
48 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080049
rginda8ba33642011-12-14 12:31:31 -080050 // The rows that have scrolled off screen and are no longer addressable.
51 this.scrollbackRows_ = [];
52
rgindac9bc5502012-01-18 11:48:44 -080053 // Saved tab stops.
54 this.tabStops_ = [];
55
rginda8ba33642011-12-14 12:31:31 -080056 // The VT's notion of the top and bottom rows. Used during some VT
57 // cursor positioning and scrolling commands.
58 this.vtScrollTop_ = null;
59 this.vtScrollBottom_ = null;
60
61 // The DIV element for the visible cursor.
62 this.cursorNode_ = null;
63
rginda6d397402012-01-17 10:58:29 -080064 // Cursor position and attributes saved with DECSC.
65 this.savedOptions_ = {};
66
rginda8ba33642011-12-14 12:31:31 -080067 // The current mode bits for the terminal.
68 this.options_ = new hterm.Options();
69
70 // Timeouts we might need to clear.
71 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -080072
73 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -080074 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -080075
rgindafeaf3142012-01-31 15:14:20 -080076 // The keyboard hander.
77 this.keyboard = new hterm.Keyboard(this);
78
rginda87b86462011-12-14 13:48:03 -080079 // General IO interface that can be given to third parties without exposing
80 // the entire terminal object.
81 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -080082
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +040083 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -080084 this.setDefaultTabStops();
rginda87b86462011-12-14 13:48:03 -080085};
86
87/**
rginda35c456b2012-02-09 17:29:05 -080088 * Default font family for the terminal text.
89 */
90
91hterm.Terminal.prototype.defaultFontFamily =
92 '"DejaVu Sans Mono", "Everson Mono", FreeMono, ' +
93 '"Andale Mono", "Lucida Console", monospace';
94
95/**
96 * The default colors for text with no other color attributes.
97 */
98hterm.Terminal.prototype.backgroundColor = 'black';
99hterm.Terminal.prototype.foregroundColor = 'white';
100
101/**
102 * Default tab with of 8 to match xterm.
103 */
104hterm.Terminal.prototype.tabWidth = 8;
105
106/**
107 * The color of the visible cursor.
108 */
109hterm.Terminal.prototype.cursorColor = 'rgba(255,0,0,0.5)';
110
111/**
112 * If true, scroll to the bottom on any keystroke.
113 */
114hterm.Terminal.prototype.scrollOnKeystroke = true;
115
116/**
117 * If true, scroll to the bottom on terminal output.
118 */
119hterm.Terminal.prototype.scrollOnOutput = false;
120
121/**
122 * The default font size in pixels.
123 */
124hterm.Terminal.prototype.defaultFontSizePx = 15;
125
126/**
127 * The assumed width of a scrollbar.
128 */
129hterm.Terminal.prototype.scrollbarWidthPx = 16;
130
131/**
rginda87b86462011-12-14 13:48:03 -0800132 * Create a new instance of a terminal command and run it with a given
133 * argument string.
134 *
135 * @param {function} commandClass The constructor for a terminal command.
136 * @param {string} argString The argument string to pass to the command.
137 */
138hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
139 var self = this;
140 this.command = new commandClass(
141 { argString: argString || '',
142 io: this.io.push(),
143 onExit: function(code) {
144 self.io.pop();
145 self.io.println(hterm.msg('COMMAND_COMPLETE',
146 [self.command.commandName, code]));
rgindafeaf3142012-01-31 15:14:20 -0800147 self.uninstallKeyboard();
rginda87b86462011-12-14 13:48:03 -0800148 }
149 });
150
rgindafeaf3142012-01-31 15:14:20 -0800151 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800152 this.command.run();
153};
154
155/**
rgindafeaf3142012-01-31 15:14:20 -0800156 * Returns true if the current screen is the primary screen, false otherwise.
157 */
158hterm.Terminal.prototype.isPrimaryScreen = function() {
159 return this.screen_ = this.primaryScreen_;
160};
161
162/**
163 * Install the keyboard handler for this terminal.
164 *
165 * This will prevent the browser from seeing any keystrokes sent to the
166 * terminal.
167 */
168hterm.Terminal.prototype.installKeyboard = function() {
169 this.keyboard.installKeyboard(this.document_.body.firstChild);
170}
171
172/**
173 * Uninstall the keyboard handler for this terminal.
174 */
175hterm.Terminal.prototype.uninstallKeyboard = function() {
176 this.keyboard.installKeyboard(null);
177}
178
179/**
rginda35c456b2012-02-09 17:29:05 -0800180 * Set the font size for this terminal.
181 */
182hterm.Terminal.prototype.setFontSize = function(px) {
183 this.scrollPort_.setFontSize(px);
184};
185
186/**
187 * Get the current font size.
188 */
189hterm.Terminal.prototype.getFontSize = function() {
190 return this.scrollPort_.getFontSize();
191};
192
193/**
194 * Set the CSS "font-family" for this terminal.
195 */
196hterm.Terminal.prototype.setFontFamily = function(str) {
197 this.scrollPort_.setFontFamily(str);
198};
199
200/**
rginda87b86462011-12-14 13:48:03 -0800201 * Return a copy of the current cursor position.
202 *
203 * @return {hterm.RowCol} The RowCol object representing the current position.
204 */
205hterm.Terminal.prototype.saveCursor = function() {
206 return this.screen_.cursorPosition.clone();
207};
208
rgindaa19afe22012-01-25 15:40:22 -0800209hterm.Terminal.prototype.getTextAttributes = function() {
210 return this.screen_.textAttributes;
211};
212
rginda87b86462011-12-14 13:48:03 -0800213/**
rginda9846e2f2012-01-27 13:53:33 -0800214 * Change the title of this terminal's window.
215 */
216hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800217 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800218};
219
220/**
rginda87b86462011-12-14 13:48:03 -0800221 * Restore a previously saved cursor position.
222 *
223 * @param {hterm.RowCol} cursor The position to restore.
224 */
225hterm.Terminal.prototype.restoreCursor = function(cursor) {
rginda35c456b2012-02-09 17:29:05 -0800226 var row = hterm.clamp(cursor.row, 0, this.screenSize.height - 1);
227 var column = hterm.clamp(cursor.column, 0, this.screenSize.width - 1);
228 this.screen_.setCursorPosition(row, column);
229 if (cursor.column > column ||
230 cursor.column == column && cursor.overflow) {
231 this.screen_.cursorPosition.overflow = true;
232 }
rginda87b86462011-12-14 13:48:03 -0800233};
234
235/**
236 * Set the width of the terminal, resizing the UI to match.
237 */
238hterm.Terminal.prototype.setWidth = function(columnCount) {
rginda35c456b2012-02-09 17:29:05 -0800239 this.div_.style.width = this.scrollPort_.characterSize.width *
240 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400241 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800242 this.scheduleSyncCursorPosition_();
243};
rginda87b86462011-12-14 13:48:03 -0800244
rgindac9bc5502012-01-18 11:48:44 -0800245/**
rginda35c456b2012-02-09 17:29:05 -0800246 * Set the height of the terminal, resizing the UI to match.
247 */
248hterm.Terminal.prototype.setHeight = function(rowCount) {
249 this.div_.style.height =
250 this.scrollPort_.characterSize.height * rowCount + 'px';
251 this.realizeSize_(this.screenSize.width, rowCount);
252 this.scheduleSyncCursorPosition_();
253};
254
255/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400256 * Deal with terminal size changes.
257 *
258 */
259hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
260 if (columnCount != this.screenSize.width)
261 this.realizeWidth_(columnCount);
262
263 if (rowCount != this.screenSize.height)
264 this.realizeHeight_(rowCount);
265
266 // Send new terminal size to plugin.
267 this.io.onTerminalResize(columnCount, rowCount);
268};
269
270/**
rgindac9bc5502012-01-18 11:48:44 -0800271 * Deal with terminal width changes.
272 *
273 * This function does what needs to be done when the terminal width changes
274 * out from under us. It happens here rather than in onResize_() because this
275 * code may need to run synchronously to handle programmatic changes of
276 * terminal width.
277 *
278 * Relying on the browser to send us an async resize event means we may not be
279 * in the correct state yet when the next escape sequence hits.
280 */
281hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
282 var deltaColumns = columnCount - this.screen_.getWidth();
283
rginda87b86462011-12-14 13:48:03 -0800284 this.screenSize.width = columnCount;
285 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800286
287 if (deltaColumns > 0) {
288 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
289 } else {
290 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
291 if (this.tabStops_[i] <= columnCount)
292 break;
293
294 this.tabStops_.pop();
295 }
296 }
297
298 this.screen_.setColumnCount(this.screenSize.width);
299};
300
301/**
302 * Deal with terminal height changes.
303 *
304 * This function does what needs to be done when the terminal height changes
305 * out from under us. It happens here rather than in onResize_() because this
306 * code may need to run synchronously to handle programmatic changes of
307 * terminal height.
308 *
309 * Relying on the browser to send us an async resize event means we may not be
310 * in the correct state yet when the next escape sequence hits.
311 */
312hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
313 var deltaRows = rowCount - this.screen_.getHeight();
314
315 this.screenSize.height = rowCount;
316
317 var cursor = this.saveCursor();
318
319 if (deltaRows < 0) {
320 // Screen got smaller.
321 deltaRows *= -1;
322 while (deltaRows) {
323 var lastRow = this.getRowCount() - 1;
324 if (lastRow - this.scrollbackRows_.length == cursor.row)
325 break;
326
327 if (this.getRowText(lastRow))
328 break;
329
330 this.screen_.popRow();
331 deltaRows--;
332 }
333
334 var ary = this.screen_.shiftRows(deltaRows);
335 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
336
337 // We just removed rows from the top of the screen, we need to update
338 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800339 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800340 } else if (deltaRows > 0) {
341 // Screen got larger.
342
343 if (deltaRows <= this.scrollbackRows_.length) {
344 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
345 var rows = this.scrollbackRows_.splice(
346 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
347 this.screen_.unshiftRows(rows);
348 deltaRows -= scrollbackCount;
349 cursor.row += scrollbackCount;
350 }
351
352 if (deltaRows)
353 this.appendRows_(deltaRows);
354 }
355
rginda35c456b2012-02-09 17:29:05 -0800356 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800357 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800358};
359
360/**
361 * Scroll the terminal to the top of the scrollback buffer.
362 */
363hterm.Terminal.prototype.scrollHome = function() {
364 this.scrollPort_.scrollRowToTop(0);
365};
366
367/**
368 * Scroll the terminal to the end.
369 */
370hterm.Terminal.prototype.scrollEnd = function() {
371 this.scrollPort_.scrollRowToBottom(this.getRowCount());
372};
373
374/**
375 * Scroll the terminal one page up (minus one line) relative to the current
376 * position.
377 */
378hterm.Terminal.prototype.scrollPageUp = function() {
379 var i = this.scrollPort_.getTopRowIndex();
380 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
381};
382
383/**
384 * Scroll the terminal one page down (minus one line) relative to the current
385 * position.
386 */
387hterm.Terminal.prototype.scrollPageDown = function() {
388 var i = this.scrollPort_.getTopRowIndex();
389 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800390};
391
rgindac9bc5502012-01-18 11:48:44 -0800392/**
393 * Full terminal reset.
394 */
rginda87b86462011-12-14 13:48:03 -0800395hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800396 this.clearAllTabStops();
397 this.setDefaultTabStops();
398 this.clearColorAndAttributes();
399 this.setVTScrollRegion(null, null);
400 this.clear();
401 this.setAbsoluteCursorPosition(0, 0);
402 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800403};
404
rgindac9bc5502012-01-18 11:48:44 -0800405/**
406 * Soft terminal reset.
407 */
rginda0f5c0292012-01-13 11:00:13 -0800408hterm.Terminal.prototype.softReset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800409 this.options_ = new hterm.Options();
rgindaa19afe22012-01-25 15:40:22 -0800410 this.setCursorVisible(true);
411 this.setCursorBlink(false);
rginda0f5c0292012-01-13 11:00:13 -0800412};
413
rginda87b86462011-12-14 13:48:03 -0800414hterm.Terminal.prototype.clearColorAndAttributes = function() {
415 //console.log('clearColorAndAttributes');
416};
417
418hterm.Terminal.prototype.setForegroundColor256 = function() {
419 console.log('setForegroundColor256');
420};
421
422hterm.Terminal.prototype.setBackgroundColor256 = function() {
423 console.log('setBackgroundColor256');
424};
425
426hterm.Terminal.prototype.setForegroundColor = function() {
427 //console.log('setForegroundColor');
428};
429
430hterm.Terminal.prototype.setBackgroundColor = function() {
431 //console.log('setBackgroundColor');
432};
433
434hterm.Terminal.prototype.setAttributes = function() {
435 //console.log('setAttributes');
436};
437
438hterm.Terminal.prototype.resize = function() {
439 console.log('resize');
440};
441
rgindae4d29232012-01-19 10:47:13 -0800442hterm.Terminal.prototype.setCharacterSet = function() {
443 //console.log('setCharacterSet');
rginda87b86462011-12-14 13:48:03 -0800444};
445
rgindac9bc5502012-01-18 11:48:44 -0800446/**
447 * Move the cursor forward to the next tab stop, or to the last column
448 * if no more tab stops are set.
449 */
450hterm.Terminal.prototype.forwardTabStop = function() {
451 var column = this.screen_.cursorPosition.column;
452
453 for (var i = 0; i < this.tabStops_.length; i++) {
454 if (this.tabStops_[i] > column) {
455 this.setCursorColumn(this.tabStops_[i]);
456 return;
457 }
458 }
459
460 this.setCursorColumn(this.screenSize.width - 1);
rginda0f5c0292012-01-13 11:00:13 -0800461};
462
rgindac9bc5502012-01-18 11:48:44 -0800463/**
464 * Move the cursor backward to the previous tab stop, or to the first column
465 * if no previous tab stops are set.
466 */
467hterm.Terminal.prototype.backwardTabStop = function() {
468 var column = this.screen_.cursorPosition.column;
469
470 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
471 if (this.tabStops_[i] < column) {
472 this.setCursorColumn(this.tabStops_[i]);
473 return;
474 }
475 }
476
477 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800478};
479
rgindac9bc5502012-01-18 11:48:44 -0800480/**
481 * Set a tab stop at the given column.
482 *
483 * @param {int} column Zero based column.
484 */
485hterm.Terminal.prototype.setTabStop = function(column) {
486 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
487 if (this.tabStops_[i] == column)
488 return;
489
490 if (this.tabStops_[i] < column) {
491 this.tabStops_.splice(i + 1, 0, column);
492 return;
493 }
494 }
495
496 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800497};
498
rgindac9bc5502012-01-18 11:48:44 -0800499/**
500 * Clear the tab stop at the current cursor position.
501 *
502 * No effect if there is no tab stop at the current cursor position.
503 */
504hterm.Terminal.prototype.clearTabStopAtCursor = function() {
505 var column = this.screen_.cursorPosition.column;
506
507 var i = this.tabStops_.indexOf(column);
508 if (i == -1)
509 return;
510
511 this.tabStops_.splice(i, 1);
512};
513
514/**
515 * Clear all tab stops.
516 */
517hterm.Terminal.prototype.clearAllTabStops = function() {
518 this.tabStops_.length = 0;
519};
520
521/**
522 * Set up the default tab stops, starting from a given column.
523 *
524 * This sets a tabstop every (column % this.tabWidth) column, starting
525 * from the specified column, or 0 if no column is provided.
526 *
527 * This does not clear the existing tab stops first, use clearAllTabStops
528 * for that.
529 *
530 * @param {int} opt_start Optional starting zero based starting column, useful
531 * for filling out missing tab stops when the terminal is resized.
532 */
533hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
534 var start = opt_start || 0;
535 var w = this.tabWidth;
536 var stopCount = Math.floor((this.screenSize.width - start) / this.tabWidth)
537 for (var i = 0; i < stopCount; i++) {
538 this.setTabStop(Math.floor((start + i * w) / w) * w + w);
539 }
rginda87b86462011-12-14 13:48:03 -0800540};
541
rginda6d397402012-01-17 10:58:29 -0800542/**
543 * Save cursor position and attributes.
544 *
545 * TODO(rginda): Save attributes once we support them.
546 */
rginda87b86462011-12-14 13:48:03 -0800547hterm.Terminal.prototype.saveOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800548 this.savedOptions_.cursor = this.saveCursor();
rgindaa19afe22012-01-25 15:40:22 -0800549 this.savedOptions_.textAttributes = this.screen_.textAttributes.clone();
rginda87b86462011-12-14 13:48:03 -0800550};
551
rginda6d397402012-01-17 10:58:29 -0800552/**
553 * Restore cursor position and attributes.
554 *
555 * TODO(rginda): Restore attributes once we support them.
556 */
rginda87b86462011-12-14 13:48:03 -0800557hterm.Terminal.prototype.restoreOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800558 if (this.savedOptions_.cursor)
559 this.restoreCursor(this.savedOptions_.cursor);
rgindaa19afe22012-01-25 15:40:22 -0800560 if (this.savedOptions_.textAttributes)
561 this.screen_.textAttributes = this.savedOptions_.textAttributes;
rginda8ba33642011-12-14 12:31:31 -0800562};
563
564/**
565 * Interpret a sequence of characters.
566 *
567 * Incomplete escape sequences are buffered until the next call.
568 *
569 * @param {string} str Sequence of characters to interpret or pass through.
570 */
571hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800572 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800573 this.scheduleSyncCursorPosition_();
574};
575
576/**
577 * Take over the given DIV for use as the terminal display.
578 *
579 * @param {HTMLDivElement} div The div to use as the terminal display.
580 */
581hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800582 this.div_ = div;
583
rginda8ba33642011-12-14 12:31:31 -0800584 this.scrollPort_.decorate(div);
rginda35c456b2012-02-09 17:29:05 -0800585 this.scrollPort_.setFontFamily(this.defaultFontFamily);
586 this.scrollPort_.setFontSize(this.defaultFontSize);
rgindaa19afe22012-01-25 15:40:22 -0800587
rginda8ba33642011-12-14 12:31:31 -0800588 this.document_ = this.scrollPort_.getDocument();
589
rginda8ba33642011-12-14 12:31:31 -0800590 this.cursorNode_ = this.document_.createElement('div');
591 this.cursorNode_.style.cssText =
592 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800593 'top: -99px;' +
594 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -0800595 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
596 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda6d397402012-01-17 10:58:29 -0800597 '-webkit-transition: opacity, background-color 100ms linear;' +
rginda8ba33642011-12-14 12:31:31 -0800598 'background-color: ' + this.cursorColor);
599 this.document_.body.appendChild(this.cursorNode_);
600
601 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -0800602
rginda87b86462011-12-14 13:48:03 -0800603 var link = this.document_.createElement('link');
604 link.setAttribute('href', '../css/dialogs.css');
605 link.setAttribute('rel', 'stylesheet');
606 this.document_.head.appendChild(link);
607
608 this.alertDialog = new AlertDialog(this.document_.body);
609 this.promptDialog = new PromptDialog(this.document_.body);
610 this.confirmDialog = new ConfirmDialog(this.document_.body);
611
612 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -0800613 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -0800614};
615
616hterm.Terminal.prototype.getDocument = function() {
617 return this.document_;
rginda8ba33642011-12-14 12:31:31 -0800618};
619
620/**
621 * Return the HTML Element for a given row index.
622 *
623 * This is a method from the RowProvider interface. The ScrollPort uses
624 * it to fetch rows on demand as they are scrolled into view.
625 *
626 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
627 * pairs to conserve memory.
628 *
629 * @param {integer} index The zero-based row index, measured relative to the
630 * start of the scrollback buffer. On-screen rows will always have the
631 * largest indicies.
632 * @return {HTMLElement} The 'x-row' element containing for the requested row.
633 */
634hterm.Terminal.prototype.getRowNode = function(index) {
635 if (index < this.scrollbackRows_.length)
636 return this.scrollbackRows_[index];
637
638 var screenIndex = index - this.scrollbackRows_.length;
639 return this.screen_.rowsArray[screenIndex];
640};
641
642/**
643 * Return the text content for a given range of rows.
644 *
645 * This is a method from the RowProvider interface. The ScrollPort uses
646 * it to fetch text content on demand when the user attempts to copy their
647 * selection to the clipboard.
648 *
649 * @param {integer} start The zero-based row index to start from, measured
650 * relative to the start of the scrollback buffer. On-screen rows will
651 * always have the largest indicies.
652 * @param {integer} end The zero-based row index to end on, measured
653 * relative to the start of the scrollback buffer.
654 * @return {string} A single string containing the text value of the range of
655 * rows. Lines will be newline delimited, with no trailing newline.
656 */
657hterm.Terminal.prototype.getRowsText = function(start, end) {
658 var ary = [];
659 for (var i = start; i < end; i++) {
660 var node = this.getRowNode(i);
661 ary.push(node.textContent);
662 }
663
664 return ary.join('\n');
665};
666
667/**
668 * Return the text content for a given row.
669 *
670 * This is a method from the RowProvider interface. The ScrollPort uses
671 * it to fetch text content on demand when the user attempts to copy their
672 * selection to the clipboard.
673 *
674 * @param {integer} index The zero-based row index to return, measured
675 * relative to the start of the scrollback buffer. On-screen rows will
676 * always have the largest indicies.
677 * @return {string} A string containing the text value of the selected row.
678 */
679hterm.Terminal.prototype.getRowText = function(index) {
680 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -0800681 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -0800682};
683
684/**
685 * Return the total number of rows in the addressable screen and in the
686 * scrollback buffer of this terminal.
687 *
688 * This is a method from the RowProvider interface. The ScrollPort uses
689 * it to compute the size of the scrollbar.
690 *
691 * @return {integer} The number of rows in this terminal.
692 */
693hterm.Terminal.prototype.getRowCount = function() {
694 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
695};
696
697/**
698 * Create DOM nodes for new rows and append them to the end of the terminal.
699 *
700 * This is the only correct way to add a new DOM node for a row. Notice that
701 * the new row is appended to the bottom of the list of rows, and does not
702 * require renumbering (of the rowIndex property) of previous rows.
703 *
704 * If you think you want a new blank row somewhere in the middle of the
705 * terminal, look into moveRows_().
706 *
707 * This method does not pay attention to vtScrollTop/Bottom, since you should
708 * be using moveRows() in cases where they would matter.
709 *
710 * The cursor will be positioned at column 0 of the first inserted line.
711 */
712hterm.Terminal.prototype.appendRows_ = function(count) {
713 var cursorRow = this.screen_.rowsArray.length;
714 var offset = this.scrollbackRows_.length + cursorRow;
715 for (var i = 0; i < count; i++) {
716 var row = this.document_.createElement('x-row');
717 row.appendChild(this.document_.createTextNode(''));
718 row.rowIndex = offset + i;
719 this.screen_.pushRow(row);
720 }
721
722 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
723 if (extraRows > 0) {
724 var ary = this.screen_.shiftRows(extraRows);
725 Array.prototype.push.apply(this.scrollbackRows_, ary);
726 this.scheduleScrollDown_();
727 }
728
729 if (cursorRow >= this.screen_.rowsArray.length)
730 cursorRow = this.screen_.rowsArray.length - 1;
731
rginda87b86462011-12-14 13:48:03 -0800732 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -0800733};
734
735/**
736 * Relocate rows from one part of the addressable screen to another.
737 *
738 * This is used to recycle rows during VT scrolls (those which are driven
739 * by VT commands, rather than by the user manipulating the scrollbar.)
740 *
741 * In this case, the blank lines scrolled into the scroll region are made of
742 * the nodes we scrolled off. These have their rowIndex properties carefully
743 * renumbered so as not to confuse the ScrollPort.
744 *
745 * TODO(rginda): I'm not sure why this doesn't require a scrollport repaint.
746 * It may just be luck. I wouldn't be surprised if we actually needed to call
747 * scrollPort_.invalidateRowRange, but I'm going to wait for evidence before
748 * adding it.
749 */
750hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
751 var ary = this.screen_.removeRows(fromIndex, count);
752 this.screen_.insertRows(toIndex, ary);
753
754 var start, end;
755 if (fromIndex < toIndex) {
756 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -0800757 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800758 } else {
759 start = toIndex;
rginda87b86462011-12-14 13:48:03 -0800760 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800761 }
762
763 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -0800764 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -0800765};
766
767/**
768 * Renumber the rowIndex property of the given range of rows.
769 *
770 * The start and end indicies are relative to the screen, not the scrollback.
771 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -0800772 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -0800773 * no need to renumber scrollback rows.
774 */
775hterm.Terminal.prototype.renumberRows_ = function(start, end) {
776 var offset = this.scrollbackRows_.length;
777 for (var i = start; i < end; i++) {
778 this.screen_.rowsArray[i].rowIndex = offset + i;
779 }
780};
781
782/**
783 * Print a string to the terminal.
784 *
785 * This respects the current insert and wraparound modes. It will add new lines
786 * to the end of the terminal, scrolling off the top into the scrollback buffer
787 * if necessary.
788 *
789 * The string is *not* parsed for escape codes. Use the interpret() method if
790 * that's what you're after.
791 *
792 * @param{string} str The string to print.
793 */
794hterm.Terminal.prototype.print = function(str) {
rgindaa19afe22012-01-25 15:40:22 -0800795 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
796 this.newLine();
rginda2312fff2012-01-05 16:20:52 -0800797
rgindaa19afe22012-01-25 15:40:22 -0800798 if (this.options_.insertMode) {
799 this.screen_.insertString(str);
800 } else {
801 this.screen_.overwriteString(str);
802 }
803
804 var overflow = this.screen_.maybeClipCurrentRow();
805
806 if (this.options_.wraparound && overflow) {
807 var lastColumn;
808
809 do {
rginda35c456b2012-02-09 17:29:05 -0800810 this.newLine();
811 lastColumn = overflow.characterLength;
rgindaa19afe22012-01-25 15:40:22 -0800812
813 if (!this.options_.insertMode)
814 this.screen_.deleteChars(overflow.characterLength);
815
816 this.screen_.prependNodes(overflow);
rgindaa19afe22012-01-25 15:40:22 -0800817
818 overflow = this.screen_.maybeClipCurrentRow();
819 } while (overflow);
820
821 this.setCursorColumn(lastColumn);
822 }
rginda8ba33642011-12-14 12:31:31 -0800823
824 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -0800825
826 if (this.scrollOnOutput)
827 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -0800828};
829
830/**
rginda87b86462011-12-14 13:48:03 -0800831 * Set the VT scroll region.
832 *
rginda87b86462011-12-14 13:48:03 -0800833 * This also resets the cursor position to the absolute (0, 0) position, since
834 * that's what xterm appears to do.
835 *
836 * @param {integer} scrollTop The zero-based top of the scroll region.
837 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
838 * inclusive.
839 */
840hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
841 this.vtScrollTop_ = scrollTop;
842 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -0800843};
844
845/**
rginda8ba33642011-12-14 12:31:31 -0800846 * Return the top row index according to the VT.
847 *
848 * This will return 0 unless the terminal has been told to restrict scrolling
849 * to some lower row. It is used for some VT cursor positioning and scrolling
850 * commands.
851 *
852 * @return {integer} The topmost row in the terminal's scroll region.
853 */
854hterm.Terminal.prototype.getVTScrollTop = function() {
855 if (this.vtScrollTop_ != null)
856 return this.vtScrollTop_;
857
858 return 0;
rginda87b86462011-12-14 13:48:03 -0800859};
rginda8ba33642011-12-14 12:31:31 -0800860
861/**
862 * Return the bottom row index according to the VT.
863 *
864 * This will return the height of the terminal unless the it has been told to
865 * restrict scrolling to some higher row. It is used for some VT cursor
866 * positioning and scrolling commands.
867 *
868 * @return {integer} The bottommost row in the terminal's scroll region.
869 */
870hterm.Terminal.prototype.getVTScrollBottom = function() {
871 if (this.vtScrollBottom_ != null)
872 return this.vtScrollBottom_;
873
rginda87b86462011-12-14 13:48:03 -0800874 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -0800875}
876
877/**
878 * Process a '\n' character.
879 *
880 * If the cursor is on the final row of the terminal this will append a new
881 * blank row to the screen and scroll the topmost row into the scrollback
882 * buffer.
883 *
884 * Otherwise, this moves the cursor to column zero of the next row.
885 */
886hterm.Terminal.prototype.newLine = function() {
887 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -0800888 // If we're at the end of the screen we need to append a new line and
889 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -0800890 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -0800891 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
892 // End of the scroll region does not affect the scrollback buffer.
893 this.vtScrollUp(1);
894 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -0800895 } else {
rginda87b86462011-12-14 13:48:03 -0800896 // Anywhere else in the screen just moves the cursor.
897 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -0800898 }
899};
900
901/**
902 * Like newLine(), except maintain the cursor column.
903 */
904hterm.Terminal.prototype.lineFeed = function() {
905 var column = this.screen_.cursorPosition.column;
906 this.newLine();
907 this.setCursorColumn(column);
908};
909
910/**
rginda87b86462011-12-14 13:48:03 -0800911 * If autoCarriageReturn is set then newLine(), else lineFeed().
912 */
913hterm.Terminal.prototype.formFeed = function() {
914 if (this.options_.autoCarriageReturn) {
915 this.newLine();
916 } else {
917 this.lineFeed();
918 }
919};
920
921/**
922 * Move the cursor up one row, possibly inserting a blank line.
923 *
924 * The cursor column is not changed.
925 */
926hterm.Terminal.prototype.reverseLineFeed = function() {
927 var scrollTop = this.getVTScrollTop();
928 var currentRow = this.screen_.cursorPosition.row;
929
930 if (currentRow == scrollTop) {
931 this.insertLines(1);
932 } else {
933 this.setAbsoluteCursorRow(currentRow - 1);
934 }
935};
936
937/**
rginda8ba33642011-12-14 12:31:31 -0800938 * Replace all characters to the left of the current cursor with the space
939 * character.
940 *
941 * TODO(rginda): This should probably *remove* the characters (not just replace
942 * with a space) if there are no characters at or beyond the current cursor
943 * position. Once it does that, it'll have the same text-attribute related
944 * issues as hterm.Screen.prototype.clearCursorRow :/
945 */
946hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -0800947 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800948 this.setCursorColumn(0);
rginda87b86462011-12-14 13:48:03 -0800949 this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1));
950 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800951};
952
953/**
954 * Erase a given number of characters to the right of the cursor, shifting
955 * remaining characters to the left.
956 *
957 * The cursor position is unchanged.
958 *
959 * TODO(rginda): Test that this works even when the cursor is positioned beyond
960 * the end of the text.
961 *
962 * TODO(rginda): This likely has text-attribute related troubles similar to the
963 * todo on hterm.Screen.prototype.clearCursorRow.
964 */
965hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -0800966 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800967
rginda87b86462011-12-14 13:48:03 -0800968 var maxCount = this.screenSize.width - cursor.column;
rginda8ba33642011-12-14 12:31:31 -0800969 var count = (opt_count && opt_count < maxCount) ? opt_count : maxCount;
970 this.screen_.deleteChars(count);
rginda87b86462011-12-14 13:48:03 -0800971 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800972};
973
974/**
975 * Erase the current line.
976 *
977 * The cursor position is unchanged.
978 *
979 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
980 * has a text-attribute related TODO.
981 */
982hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -0800983 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -0800984 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -0800985 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -0800986};
987
988/**
989 * Erase all characters from the start of the scroll region to the current
990 * cursor position.
991 *
992 * The cursor position is unchanged.
993 *
994 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
995 * has a text-attribute related TODO.
996 */
997hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -0800998 var cursor = this.saveCursor();
999
1000 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001001
1002 var top = this.getVTScrollTop();
rginda87b86462011-12-14 13:48:03 -08001003 for (var i = top; i < cursor.row; i++) {
1004 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001005 this.screen_.clearCursorRow();
1006 }
1007
rginda87b86462011-12-14 13:48:03 -08001008 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001009};
1010
1011/**
1012 * Erase all characters from the current cursor position to the end of the
1013 * scroll region.
1014 *
1015 * The cursor position is unchanged.
1016 *
1017 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1018 * has a text-attribute related TODO.
1019 */
1020hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001021 var cursor = this.saveCursor();
1022
1023 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001024
1025 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001026 for (var i = cursor.row + 1; i <= bottom; i++) {
1027 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001028 this.screen_.clearCursorRow();
1029 }
1030
rginda87b86462011-12-14 13:48:03 -08001031 this.restoreCursor(cursor);
1032};
1033
1034/**
1035 * Fill the terminal with a given character.
1036 *
1037 * This methods does not respect the VT scroll region.
1038 *
1039 * @param {string} ch The character to use for the fill.
1040 */
1041hterm.Terminal.prototype.fill = function(ch) {
1042 var cursor = this.saveCursor();
1043
1044 this.setAbsoluteCursorPosition(0, 0);
1045 for (var row = 0; row < this.screenSize.height; row++) {
1046 for (var col = 0; col < this.screenSize.width; col++) {
1047 this.setAbsoluteCursorPosition(row, col);
1048 this.screen_.overwriteString(ch);
1049 }
1050 }
1051
1052 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001053};
1054
1055/**
rgindae4d29232012-01-19 10:47:13 -08001056 * Erase the entire display.
rginda8ba33642011-12-14 12:31:31 -08001057 *
rgindae4d29232012-01-19 10:47:13 -08001058 * The cursor position is unchanged. This does not respect the scroll
1059 * region.
rginda8ba33642011-12-14 12:31:31 -08001060 *
1061 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1062 * has a text-attribute related TODO.
1063 */
1064hterm.Terminal.prototype.clear = function() {
rginda87b86462011-12-14 13:48:03 -08001065 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001066
rgindae4d29232012-01-19 10:47:13 -08001067 var bottom = this.screenSize.height;
rginda8ba33642011-12-14 12:31:31 -08001068
rgindae4d29232012-01-19 10:47:13 -08001069 for (var i = 0; i < bottom; i++) {
rginda87b86462011-12-14 13:48:03 -08001070 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001071 this.screen_.clearCursorRow();
1072 }
1073
rginda87b86462011-12-14 13:48:03 -08001074 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001075};
1076
1077/**
1078 * VT command to insert lines at the current cursor row.
1079 *
1080 * This respects the current scroll region. Rows pushed off the bottom are
1081 * lost (they won't show up in the scrollback buffer).
1082 *
1083 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1084 * has a text-attribute related TODO.
1085 *
1086 * @param {integer} count The number of lines to insert.
1087 */
1088hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001089 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001090
1091 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001092 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001093
rgindae4d29232012-01-19 10:47:13 -08001094 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001095 if (start != cursor.row)
1096 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001097
1098 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001099 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001100 this.screen_.clearCursorRow();
1101 }
1102
rginda87b86462011-12-14 13:48:03 -08001103 cursor.column = 0;
1104 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001105};
1106
1107/**
1108 * VT command to delete lines at the current cursor row.
1109 *
1110 * New rows are added to the bottom of scroll region to take their place. New
1111 * rows are strictly there to take up space and have no content or style.
1112 */
1113hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001114 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001115
rginda87b86462011-12-14 13:48:03 -08001116 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001117 var bottom = this.getVTScrollBottom();
1118
rginda87b86462011-12-14 13:48:03 -08001119 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001120 count = Math.min(count, maxCount);
1121
rginda87b86462011-12-14 13:48:03 -08001122 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001123 if (count != maxCount)
1124 this.moveRows_(top, count, moveStart);
1125
1126 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001127 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001128 this.screen_.clearCursorRow();
1129 }
1130
rginda87b86462011-12-14 13:48:03 -08001131 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001132};
1133
1134/**
1135 * Inserts the given number of spaces at the current cursor position.
1136 *
rginda87b86462011-12-14 13:48:03 -08001137 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001138 */
1139hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001140 var cursor = this.saveCursor();
1141
rginda0f5c0292012-01-13 11:00:13 -08001142 var ws = hterm.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001143 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001144 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001145
1146 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001147};
1148
1149/**
1150 * Forward-delete the specified number of characters starting at the cursor
1151 * position.
1152 *
1153 * @param {integer} count The number of characters to delete.
1154 */
1155hterm.Terminal.prototype.deleteChars = function(count) {
1156 this.screen_.deleteChars(count);
1157};
1158
1159/**
1160 * Shift rows in the scroll region upwards by a given number of lines.
1161 *
1162 * New rows are inserted at the bottom of the scroll region to fill the
1163 * vacated rows. The new rows not filled out with the current text attributes.
1164 *
1165 * This function does not affect the scrollback rows at all. Rows shifted
1166 * off the top are lost.
1167 *
rginda87b86462011-12-14 13:48:03 -08001168 * The cursor position is not altered.
1169 *
rginda8ba33642011-12-14 12:31:31 -08001170 * @param {integer} count The number of rows to scroll.
1171 */
1172hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001173 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001174
rginda87b86462011-12-14 13:48:03 -08001175 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001176 this.deleteLines(count);
1177
rginda87b86462011-12-14 13:48:03 -08001178 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001179};
1180
1181/**
1182 * Shift rows below the cursor down by a given number of lines.
1183 *
1184 * This function respects the current scroll region.
1185 *
1186 * New rows are inserted at the top of the scroll region to fill the
1187 * vacated rows. The new rows not filled out with the current text attributes.
1188 *
1189 * This function does not affect the scrollback rows at all. Rows shifted
1190 * off the bottom are lost.
1191 *
1192 * @param {integer} count The number of rows to scroll.
1193 */
1194hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001195 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001196
rginda87b86462011-12-14 13:48:03 -08001197 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001198 this.insertLines(opt_count);
1199
rginda87b86462011-12-14 13:48:03 -08001200 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001201};
1202
rginda87b86462011-12-14 13:48:03 -08001203
rginda8ba33642011-12-14 12:31:31 -08001204/**
1205 * Set the cursor position.
1206 *
1207 * The cursor row is relative to the scroll region if the terminal has
1208 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1209 *
1210 * @param {integer} row The new zero-based cursor row.
1211 * @param {integer} row The new zero-based cursor column.
1212 */
1213hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1214 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001215 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001216 } else {
rginda87b86462011-12-14 13:48:03 -08001217 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001218 }
rginda87b86462011-12-14 13:48:03 -08001219};
rginda8ba33642011-12-14 12:31:31 -08001220
rginda87b86462011-12-14 13:48:03 -08001221hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1222 var scrollTop = this.getVTScrollTop();
1223 row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
rginda2312fff2012-01-05 16:20:52 -08001224 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001225 this.screen_.setCursorPosition(row, column);
1226};
1227
1228hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rginda2312fff2012-01-05 16:20:52 -08001229 row = hterm.clamp(row, 0, this.screenSize.height - 1);
1230 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001231 this.screen_.setCursorPosition(row, column);
1232};
1233
1234/**
1235 * Set the cursor column.
1236 *
1237 * @param {integer} column The new zero-based cursor column.
1238 */
1239hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001240 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001241};
1242
1243/**
1244 * Return the cursor column.
1245 *
1246 * @return {integer} The zero-based cursor column.
1247 */
1248hterm.Terminal.prototype.getCursorColumn = function() {
1249 return this.screen_.cursorPosition.column;
1250};
1251
1252/**
1253 * Set the cursor row.
1254 *
1255 * The cursor row is relative to the scroll region if the terminal has
1256 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1257 *
1258 * @param {integer} row The new cursor row.
1259 */
rginda87b86462011-12-14 13:48:03 -08001260hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1261 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001262};
1263
1264/**
1265 * Return the cursor row.
1266 *
1267 * @return {integer} The zero-based cursor row.
1268 */
1269hterm.Terminal.prototype.getCursorRow = function(row) {
1270 return this.screen_.cursorPosition.row;
1271};
1272
1273/**
1274 * Request that the ScrollPort redraw itself soon.
1275 *
1276 * The redraw will happen asynchronously, soon after the call stack winds down.
1277 * Multiple calls will be coalesced into a single redraw.
1278 */
1279hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001280 if (this.timeouts_.redraw)
1281 return;
rginda8ba33642011-12-14 12:31:31 -08001282
1283 var self = this;
rginda87b86462011-12-14 13:48:03 -08001284 this.timeouts_.redraw = setTimeout(function() {
1285 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001286 self.scrollPort_.redraw_();
1287 }, 0);
1288};
1289
1290/**
1291 * Request that the ScrollPort be scrolled to the bottom.
1292 *
1293 * The scroll will happen asynchronously, soon after the call stack winds down.
1294 * Multiple calls will be coalesced into a single scroll.
1295 *
1296 * This affects the scrollbar position of the ScrollPort, and has nothing to
1297 * do with the VT scroll commands.
1298 */
1299hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1300 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001301 return;
rginda8ba33642011-12-14 12:31:31 -08001302
1303 var self = this;
1304 this.timeouts_.scrollDown = setTimeout(function() {
1305 delete self.timeouts_.scrollDown;
1306 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1307 }, 10);
1308};
1309
1310/**
1311 * Move the cursor up a specified number of rows.
1312 *
1313 * @param {integer} count The number of rows to move the cursor.
1314 */
1315hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001316 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001317};
1318
1319/**
1320 * Move the cursor down a specified number of rows.
1321 *
1322 * @param {integer} count The number of rows to move the cursor.
1323 */
1324hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001325 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001326 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1327 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1328 this.screenSize.height - 1);
1329
1330 var row = hterm.clamp(this.screen_.cursorPosition.row + count,
1331 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001332 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001333};
1334
1335/**
1336 * Move the cursor left a specified number of columns.
1337 *
1338 * @param {integer} count The number of columns to move the cursor.
1339 */
1340hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001341 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001342};
1343
1344/**
1345 * Move the cursor right a specified number of columns.
1346 *
1347 * @param {integer} count The number of columns to move the cursor.
1348 */
1349hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001350 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001351 var column = hterm.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001352 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001353 this.setCursorColumn(column);
1354};
1355
1356/**
1357 * Reverse the foreground and background colors of the terminal.
1358 *
1359 * This only affects text that was drawn with no attributes.
1360 *
1361 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1362 * been drawn with attributes that happen to coincide with the default
1363 * 'no-attribute' colors. My guess is probably not.
1364 */
1365hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001366 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001367 if (state) {
1368 this.scrollPort_.setForegroundColor(this.backgroundColor);
1369 this.scrollPort_.setBackgroundColor(this.foregroundColor);
1370 } else {
1371 this.scrollPort_.setForegroundColor(this.foregroundColor);
1372 this.scrollPort_.setBackgroundColor(this.backgroundColor);
1373 }
1374};
1375
1376/**
rginda87b86462011-12-14 13:48:03 -08001377 * Ring the terminal bell.
1378 *
1379 * We only have a visual bell, which quickly toggles inverse video in the
1380 * terminal.
1381 */
1382hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08001383 this.cursorNode_.style.backgroundColor =
1384 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001385
1386 var self = this;
1387 setTimeout(function() {
rginda6d397402012-01-17 10:58:29 -08001388 self.cursorNode_.style.backgroundColor = self.cursorColor;
1389 }, 200);
rginda87b86462011-12-14 13:48:03 -08001390};
1391
1392/**
rginda8ba33642011-12-14 12:31:31 -08001393 * Set the origin mode bit.
1394 *
1395 * If origin mode is on, certain VT cursor and scrolling commands measure their
1396 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1397 * to the top of the addressable screen.
1398 *
1399 * Defaults to off.
1400 *
1401 * @param {boolean} state True to set origin mode, false to unset.
1402 */
1403hterm.Terminal.prototype.setOriginMode = function(state) {
1404 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001405 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001406};
1407
1408/**
1409 * Set the insert mode bit.
1410 *
1411 * If insert mode is on, existing text beyond the cursor position will be
1412 * shifted right to make room for new text. Otherwise, new text overwrites
1413 * any existing text.
1414 *
1415 * Defaults to off.
1416 *
1417 * @param {boolean} state True to set insert mode, false to unset.
1418 */
1419hterm.Terminal.prototype.setInsertMode = function(state) {
1420 this.options_.insertMode = state;
1421};
1422
1423/**
rginda87b86462011-12-14 13:48:03 -08001424 * Set the auto carriage return bit.
1425 *
1426 * If auto carriage return is on then a formfeed character is interpreted
1427 * as a newline, otherwise it's the same as a linefeed. The difference boils
1428 * down to whether or not the cursor column is reset.
1429 */
1430hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1431 this.options_.autoCarriageReturn = state;
1432};
1433
1434/**
rginda8ba33642011-12-14 12:31:31 -08001435 * Set the wraparound mode bit.
1436 *
1437 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1438 * to the start of the following row. Otherwise, the cursor is clamped to the
1439 * end of the screen and attempts to write past it are ignored.
1440 *
1441 * Defaults to on.
1442 *
1443 * @param {boolean} state True to set wraparound mode, false to unset.
1444 */
1445hterm.Terminal.prototype.setWraparound = function(state) {
1446 this.options_.wraparound = state;
1447};
1448
1449/**
1450 * Set the reverse-wraparound mode bit.
1451 *
1452 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1453 * to the end of the previous row. Otherwise, the cursor is clamped to column
1454 * 0.
1455 *
1456 * Defaults to off.
1457 *
1458 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1459 */
1460hterm.Terminal.prototype.setReverseWraparound = function(state) {
1461 this.options_.reverseWraparound = state;
1462};
1463
1464/**
1465 * Selects between the primary and alternate screens.
1466 *
1467 * If alternate mode is on, the alternate screen is active. Otherwise the
1468 * primary screen is active.
1469 *
1470 * Swapping screens has no effect on the scrollback buffer.
1471 *
1472 * Each screen maintains its own cursor position.
1473 *
1474 * Defaults to off.
1475 *
1476 * @param {boolean} state True to set alternate mode, false to unset.
1477 */
1478hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001479 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001480 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1481
rginda35c456b2012-02-09 17:29:05 -08001482 if (this.screen_.rowsArray.length &&
1483 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
1484 // If the screen changed sizes while we were away, our rowIndexes may
1485 // be incorrect.
1486 var offset = this.scrollbackRows_.length;
1487 var ary = this.screen_.rowsArray;
1488 for (i = 0; i < ary.length; i++) {
1489 ary[i].rowIndex = offset + i;
1490 }
1491 }
rginda8ba33642011-12-14 12:31:31 -08001492
rginda35c456b2012-02-09 17:29:05 -08001493 this.realizeWidth_(this.screenSize.width);
1494 this.realizeHeight_(this.screenSize.height);
1495 this.scrollPort_.syncScrollHeight();
1496 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001497
rginda6d397402012-01-17 10:58:29 -08001498 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08001499 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08001500};
1501
1502/**
1503 * Set the cursor-blink mode bit.
1504 *
1505 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1506 * a visible cursor does not blink.
1507 *
1508 * You should make sure to turn blinking off if you're going to dispose of a
1509 * terminal, otherwise you'll leak a timeout.
1510 *
1511 * Defaults to on.
1512 *
1513 * @param {boolean} state True to set cursor-blink mode, false to unset.
1514 */
1515hterm.Terminal.prototype.setCursorBlink = function(state) {
1516 this.options_.cursorBlink = state;
1517
1518 if (!state && this.timeouts_.cursorBlink) {
1519 clearTimeout(this.timeouts_.cursorBlink);
1520 delete this.timeouts_.cursorBlink;
1521 }
1522
1523 if (this.options_.cursorVisible)
1524 this.setCursorVisible(true);
1525};
1526
1527/**
1528 * Set the cursor-visible mode bit.
1529 *
1530 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1531 *
1532 * Defaults to on.
1533 *
1534 * @param {boolean} state True to set cursor-visible mode, false to unset.
1535 */
1536hterm.Terminal.prototype.setCursorVisible = function(state) {
1537 this.options_.cursorVisible = state;
1538
1539 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001540 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001541 return;
1542 }
1543
rginda87b86462011-12-14 13:48:03 -08001544 this.syncCursorPosition_();
1545
1546 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001547
1548 if (this.options_.cursorBlink) {
1549 if (this.timeouts_.cursorBlink)
1550 return;
1551
1552 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1553 500);
1554 } else {
1555 if (this.timeouts_.cursorBlink) {
1556 clearTimeout(this.timeouts_.cursorBlink);
1557 delete this.timeouts_.cursorBlink;
1558 }
1559 }
1560};
1561
1562/**
rginda87b86462011-12-14 13:48:03 -08001563 * Synchronizes the visible cursor and document selection with the current
1564 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08001565 */
1566hterm.Terminal.prototype.syncCursorPosition_ = function() {
1567 var topRowIndex = this.scrollPort_.getTopRowIndex();
1568 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
1569 var cursorRowIndex = this.scrollbackRows_.length +
1570 this.screen_.cursorPosition.row;
1571
1572 if (cursorRowIndex > bottomRowIndex) {
1573 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08001574 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08001575 return;
1576 }
1577
rginda35c456b2012-02-09 17:29:05 -08001578 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
1579 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
1580
rginda8ba33642011-12-14 12:31:31 -08001581 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08001582 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
1583 'px';
1584 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
1585 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08001586
1587 this.cursorNode_.setAttribute('title',
1588 '(' + this.screen_.cursorPosition.row +
1589 ', ' + this.screen_.cursorPosition.column +
1590 ')');
1591
1592 // Update the caret for a11y purposes.
1593 var selection = this.document_.getSelection();
1594 if (selection && selection.isCollapsed)
1595 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08001596};
1597
1598/**
1599 * Synchronizes the visible cursor with the current cursor coordinates.
1600 *
1601 * The sync will happen asynchronously, soon after the call stack winds down.
1602 * Multiple calls will be coalesced into a single sync.
1603 */
1604hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
1605 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08001606 return;
rginda8ba33642011-12-14 12:31:31 -08001607
1608 var self = this;
1609 this.timeouts_.syncCursor = setTimeout(function() {
1610 self.syncCursorPosition_();
1611 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08001612 }, 0);
1613};
1614
1615/**
1616 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
1617 *
1618 * @param {string} string The VT string representing the keystroke.
1619 */
1620hterm.Terminal.prototype.onVTKeystroke = function(string) {
1621 if (this.scrollOnKeystroke)
1622 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1623
1624 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08001625};
1626
1627/**
1628 * React when the ScrollPort is scrolled.
1629 */
1630hterm.Terminal.prototype.onScroll_ = function() {
1631 this.scheduleSyncCursorPosition_();
1632};
1633
1634/**
rginda9846e2f2012-01-27 13:53:33 -08001635 * React when text is pasted into the scrollPort.
1636 */
1637hterm.Terminal.prototype.onPaste_ = function(e) {
1638 this.io.onVTKeystroke(e.text);
1639};
1640
1641/**
rginda8ba33642011-12-14 12:31:31 -08001642 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08001643 *
1644 * Note: This function should not directly contain code that alters the internal
1645 * state of the terminal. That kind of code belongs in realizeWidth or
1646 * realizeHeight, so that it can be executed synchronously in the case of a
1647 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08001648 */
1649hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08001650 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08001651 this.scrollPort_.characterSize.width);
1652 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
1653 this.scrollPort_.characterSize.height);
1654
1655 if (!(columnCount || rowCount)) {
1656 // We avoid these situations since they happen sometimes when the terminal
1657 // gets removed from the document, and we can't deal with that.
1658 return;
1659 }
1660
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001661 this.realizeSize_(columnCount, rowCount);
rgindac9bc5502012-01-18 11:48:44 -08001662 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08001663};
1664
1665/**
1666 * Service the cursor blink timeout.
1667 */
1668hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08001669 if (this.cursorNode_.style.opacity == '0') {
1670 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001671 } else {
rginda87b86462011-12-14 13:48:03 -08001672 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001673 }
1674};