blob: 75b10f1cd0e1ce6386fce966d65cd3d32f904b06 [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5/**
6 * Constructor for the Terminal class.
7 *
8 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
9 * classes to provide the complete terminal functionality.
10 *
11 * There are a number of lower-level Terminal methods that can be called
12 * directly to manipulate the cursor, text, scroll region, and other terminal
13 * attributes. However, the primary method is interpret(), which parses VT
14 * escape sequences and invokes the appropriate Terminal methods.
15 *
16 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
17 *
18 * TODO(rginda): Eventually we're going to need to support characters which are
19 * displayed twice as wide as standard latin characters. This is to support
20 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080021 *
22 * @param {string} opt_profileName Optional preference profile name. If not
23 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080024 */
rginda9f5222b2012-03-05 11:53:28 -080025hterm.Terminal = function(opt_profileName) {
26 this.profileName_ = null;
27 this.setProfile(opt_profileName || 'default');
28
rginda8ba33642011-12-14 12:31:31 -080029 // Two screen instances.
30 this.primaryScreen_ = new hterm.Screen();
31 this.alternateScreen_ = new hterm.Screen();
32
33 // The "current" screen.
34 this.screen_ = this.primaryScreen_;
35
rginda8ba33642011-12-14 12:31:31 -080036 // The local notion of the screen size. ScreenBuffers also have a size which
37 // indicates their present size. During size changes, the two may disagree.
38 // Also, the inactive screen's size is not altered until it is made the active
39 // screen.
40 this.screenSize = new hterm.Size(0, 0);
41
rginda8ba33642011-12-14 12:31:31 -080042 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080043 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080044 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
45 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080046 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rginda8ba33642011-12-14 12:31:31 -080047
rginda87b86462011-12-14 13:48:03 -080048 // The div that contains this terminal.
49 this.div_ = null;
50
rgindac9bc5502012-01-18 11:48:44 -080051 // The document that contains the scrollPort. Defaulted to the global
52 // document here so that the terminal is functional even if it hasn't been
53 // inserted into a document yet, but re-set in decorate().
54 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080055
rginda8ba33642011-12-14 12:31:31 -080056 // The rows that have scrolled off screen and are no longer addressable.
57 this.scrollbackRows_ = [];
58
rgindac9bc5502012-01-18 11:48:44 -080059 // Saved tab stops.
60 this.tabStops_ = [];
61
rginda8ba33642011-12-14 12:31:31 -080062 // The VT's notion of the top and bottom rows. Used during some VT
63 // cursor positioning and scrolling commands.
64 this.vtScrollTop_ = null;
65 this.vtScrollBottom_ = null;
66
67 // The DIV element for the visible cursor.
68 this.cursorNode_ = null;
69
rginda9f5222b2012-03-05 11:53:28 -080070 // These prefs are cached so we don't have to read from local storage with
71 // each output and keystroke.
72 this.scrollOnOutput_ = this.prefs_.get('scroll-on-output');
73 this.scrollOnKeystroke_ = this.prefs_.get('scroll-on-keystroke');
74
rgindaf0090c92012-02-10 14:58:52 -080075 // Terminal bell sound.
76 this.bellAudio_ = this.document_.createElement('audio');
rginda9f5222b2012-03-05 11:53:28 -080077 this.bellAudio_.setAttribute('src', this.prefs_.get('audible-bell-sound'));
rgindaf0090c92012-02-10 14:58:52 -080078 this.bellAudio_.setAttribute('preload', 'auto');
79
rginda6d397402012-01-17 10:58:29 -080080 // Cursor position and attributes saved with DECSC.
81 this.savedOptions_ = {};
82
rginda8ba33642011-12-14 12:31:31 -080083 // The current mode bits for the terminal.
84 this.options_ = new hterm.Options();
85
86 // Timeouts we might need to clear.
87 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -080088
89 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -080090 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -080091
rgindafeaf3142012-01-31 15:14:20 -080092 // The keyboard hander.
93 this.keyboard = new hterm.Keyboard(this);
94
rginda87b86462011-12-14 13:48:03 -080095 // General IO interface that can be given to third parties without exposing
96 // the entire terminal object.
97 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -080098
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +040099 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800100 this.setDefaultTabStops();
rginda87b86462011-12-14 13:48:03 -0800101};
102
103/**
rginda35c456b2012-02-09 17:29:05 -0800104 * Default tab with of 8 to match xterm.
105 */
106hterm.Terminal.prototype.tabWidth = 8;
107
108/**
rginda35c456b2012-02-09 17:29:05 -0800109 * The assumed width of a scrollbar.
110 */
111hterm.Terminal.prototype.scrollbarWidthPx = 16;
112
113/**
rginda9f5222b2012-03-05 11:53:28 -0800114 * Select a preference profile.
115 *
116 * This will load the terminal preferences for the given profile name and
117 * associate subsequent preference changes with the new preference profile.
118 *
119 * @param {string} newName The name of the preference profile. Forward slash
120 * characters will be removed from the name.
121 */
122hterm.Terminal.prototype.setProfile = function(profileName) {
123 // If we already have a profile selected, we're going to need to re-sync
124 // with the new profile.
125 var needSync = !!this.profileName_;
126
127 this.profileName_ = profileName.replace(/\//g, '');
128
129 this.prefs_ = new hterm.PreferenceManager(
130 '/hterm/prefs/profiles/' + this.profileName_);
131
132 var self = this;
133 this.prefs_.definePreferences
rginda30f20f62012-04-05 16:36:19 -0700134 ([
135 /**
136 * Set whether the alt key acts as a meta key or as a distinct alt key.
rginda9f5222b2012-03-05 11:53:28 -0800137 */
rginda30f20f62012-04-05 16:36:19 -0700138 ['alt-is-meta', false, function(v) {
139 self.vt.keyboard.altIsMeta = v;
rginda9f5222b2012-03-05 11:53:28 -0800140 }
141 ],
142
rginda30f20f62012-04-05 16:36:19 -0700143 /**
rginda39bdf6f2012-04-10 16:50:55 -0700144 * Controls how the alt key is handled.
145 *
146 * escape....... Send an ESC prefix.
147 * 8-bit........ Add 128 to the unshifted character as in xterm.
148 * browser-key.. Wait for the keypress event and see what the browser says.
149 * (This won't work well on platforms where the browser
150 * performs a default action for some alt sequences.)
rginda30f20f62012-04-05 16:36:19 -0700151 */
rginda39bdf6f2012-04-10 16:50:55 -0700152 ['alt-sends-what', 'escape', function(v) {
153 if (!/^(escape|8-bit|browser-key)$/.test(v))
154 v = 'escape';
155
156 self.vt.keyboard.altSendsWhat = v;
rginda30f20f62012-04-05 16:36:19 -0700157 }
158 ],
159
160 /**
161 * Terminal bell sound. Empty string for no audible bell.
162 */
163 ['audible-bell-sound', '../audio/bell.ogg', function(v) {
164 self.bellAudio_.setAttribute('src', v);
165 }
166 ],
167
168 /**
169 * The background color for text with no other color attributes.
170 */
171 ['background-color', 'rgb(16, 16, 16)', function(v) {
rginda9f5222b2012-03-05 11:53:28 -0800172 self.scrollPort_.setBackgroundColor(v);
173 }
174 ],
175
176 /**
rginda30f20f62012-04-05 16:36:19 -0700177 * The background image.
178 *
179 * Defaults to a subtle light-to-transparent-to-dark gradient that is
180 * mostly transparent.
181 */
182 ['background-image',
183 ('-webkit-linear-gradient(bottom, ' +
184 'rgba(0,0,0,0.01) 0%, ' +
185 'rgba(0,0,0,0) 30%, ' +
186 'rgba(255,255,255,0) 70%, ' +
187 'rgba(255,255,255,0.05) 100%)'),
188 function(v) {
189 self.scrollPort_.setBackgroundImage(v);
190 }
191 ],
192
193 /**
194 * If true, the backspace should send BS ('\x08', aka ^H). Otherwise
195 * the backspace key should send '\x7f'.
196 */
197 ['backspace-sends-backspace', false, function(v) {
198 self.keyboard.backspaceSendsBackspace = v;
199 }
200 ],
201
202 /**
203 * The color of the visible cursor.
204 */
205 ['cursor-color', 'rgba(255,0,0,0.5)', function(v) {
206 self.cursorNode_.style.backgroundColor = v;
207 }
208 ],
209
210 /**
211 * True if we should use bold weight font for text with the bold/bright
212 * attribute. False to use bright colors only. Null to autodetect.
213 */
214 ['enable-bold', null, function(v) {
215 self.syncBoldSafeState();
216 }
217 ],
218
219 /**
rginda9f5222b2012-03-05 11:53:28 -0800220 * Default font family for the terminal text.
221 */
222 ['font-family', ('"DejaVu Sans Mono", "Everson Mono", ' +
223 'FreeMono, "Menlo", "Lucida Console", ' +
224 'monospace'),
225 function(v) { self.syncFontFamily() }
226 ],
227
228 /**
rginda30f20f62012-04-05 16:36:19 -0700229 * The default font size in pixels.
230 */
231 ['font-size', 15, function(v) {
232 self.setFontSize(v);
233 }
234 ],
235
236 /**
rginda9f5222b2012-03-05 11:53:28 -0800237 * Anti-aliasing.
238 */
239 ['font-smoothing', 'antialiased',
240 function(v) { self.syncFontFamily() }
241 ],
242
243 /**
rginda30f20f62012-04-05 16:36:19 -0700244 * The foreground color for text with no other color attributes.
rginda9f5222b2012-03-05 11:53:28 -0800245 */
rginda30f20f62012-04-05 16:36:19 -0700246 ['foreground-color', 'rgb(240, 240, 240)', function(v) {
247 self.scrollPort_.setForegroundColor(v);
rginda9f5222b2012-03-05 11:53:28 -0800248 }
249 ],
250
251 /**
rginda30f20f62012-04-05 16:36:19 -0700252 * If true, home/end will control the terminal scrollbar and shift home/end
253 * will send the VT keycodes. If false then home/end sends VT codes and
254 * shift home/end scrolls.
rginda9f5222b2012-03-05 11:53:28 -0800255 */
rginda30f20f62012-04-05 16:36:19 -0700256 ['home-keys-scroll', false, function(v) {
257 self.keyboard.homeKeysScroll = v;
258 }
259 ],
260
261 /**
262 * Set whether the meta key sends a leading escape or not.
263 */
264 ['meta-sends-escape', true, function(v) {
265 self.keyboard.metaSendsEscape = v;
rginda9f5222b2012-03-05 11:53:28 -0800266 }
267 ],
268
269 /**
270 * If true, scroll to the bottom on any keystroke.
271 */
272 ['scroll-on-keystroke', true, function(v) {
273 self.scrollOnKeystroke_ = v;
274 }
275 ],
276
277 /**
278 * If true, scroll to the bottom on terminal output.
279 */
280 ['scroll-on-output', false, function(v) {
281 self.scrollOnOutput_ = v;
282 }
283 ],
284
285 /**
David Reveman8f552492012-03-28 12:18:41 -0400286 * The vertical scrollbar mode.
287 */
288 ['scrollbar-visible', true, function(v) {
289 self.setScrollbarVisible(v);
290 }
291 ],
rginda30f20f62012-04-05 16:36:19 -0700292
293 /**
294 * If true, page up/down will control the terminal scrollbar and shift
295 * page up/down will send the VT keycodes. If false then page up/down
296 * sends VT codes and shift page up/down scrolls.
297 */
298 ['page-keys-scroll', false, function(v) {
299 self.keyboard.pageKeysScroll = v;
300 }
301 ],
302
rginda9f5222b2012-03-05 11:53:28 -0800303 ]);
304
305 if (needSync)
306 this.prefs_.notifyAll();
307};
308
309/**
310 * Return the current terminal background color.
311 *
312 * Intended for use by other classes, so we don't have to expose the entire
313 * prefs_ object.
314 */
315hterm.Terminal.prototype.getBackgroundColor = function() {
316 return this.prefs_.get('background-color');
317};
318
319/**
320 * Return the current terminal foreground color.
321 *
322 * Intended for use by other classes, so we don't have to expose the entire
323 * prefs_ object.
324 */
325hterm.Terminal.prototype.getForegroundColor = function() {
326 return this.prefs_.get('foreground-color');
327};
328
329/**
rginda87b86462011-12-14 13:48:03 -0800330 * Create a new instance of a terminal command and run it with a given
331 * argument string.
332 *
333 * @param {function} commandClass The constructor for a terminal command.
334 * @param {string} argString The argument string to pass to the command.
335 */
336hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
337 var self = this;
338 this.command = new commandClass(
339 { argString: argString || '',
340 io: this.io.push(),
341 onExit: function(code) {
342 self.io.pop();
343 self.io.println(hterm.msg('COMMAND_COMPLETE',
344 [self.command.commandName, code]));
rgindafeaf3142012-01-31 15:14:20 -0800345 self.uninstallKeyboard();
rginda87b86462011-12-14 13:48:03 -0800346 }
347 });
348
rgindafeaf3142012-01-31 15:14:20 -0800349 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800350 this.command.run();
351};
352
353/**
rgindafeaf3142012-01-31 15:14:20 -0800354 * Returns true if the current screen is the primary screen, false otherwise.
355 */
356hterm.Terminal.prototype.isPrimaryScreen = function() {
357 return this.screen_ = this.primaryScreen_;
358};
359
360/**
361 * Install the keyboard handler for this terminal.
362 *
363 * This will prevent the browser from seeing any keystrokes sent to the
364 * terminal.
365 */
366hterm.Terminal.prototype.installKeyboard = function() {
367 this.keyboard.installKeyboard(this.document_.body.firstChild);
368}
369
370/**
371 * Uninstall the keyboard handler for this terminal.
372 */
373hterm.Terminal.prototype.uninstallKeyboard = function() {
374 this.keyboard.installKeyboard(null);
375}
376
377/**
rginda35c456b2012-02-09 17:29:05 -0800378 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800379 *
380 * Call setFontSize(0) to reset to the default font size.
381 *
382 * This function does not modify the font-size preference.
383 *
384 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800385 */
386hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800387 if (px === 0)
388 px = this.prefs_.get('font-size');
389
rginda35c456b2012-02-09 17:29:05 -0800390 this.scrollPort_.setFontSize(px);
391};
392
393/**
394 * Get the current font size.
395 */
396hterm.Terminal.prototype.getFontSize = function() {
397 return this.scrollPort_.getFontSize();
398};
399
400/**
401 * Set the CSS "font-family" for this terminal.
402 */
rginda9f5222b2012-03-05 11:53:28 -0800403hterm.Terminal.prototype.syncFontFamily = function() {
404 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
405 this.prefs_.get('font-smoothing'));
406 this.syncBoldSafeState();
407};
408
409hterm.Terminal.prototype.syncBoldSafeState = function() {
410 var enableBold = this.prefs_.get('enable-bold');
411 if (enableBold !== null) {
412 this.screen_.textAttributes.enableBold = enableBold;
413 return;
414 }
415
rgindaf7521392012-02-28 17:20:34 -0800416 var normalSize = this.scrollPort_.measureCharacterSize();
417 var boldSize = this.scrollPort_.measureCharacterSize('bold');
418
419 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800420 if (!isBoldSafe) {
421 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700422 'from normal. Font family is: ' +
423 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800424 }
rginda9f5222b2012-03-05 11:53:28 -0800425
426 this.screen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800427};
428
429/**
rginda87b86462011-12-14 13:48:03 -0800430 * Return a copy of the current cursor position.
431 *
432 * @return {hterm.RowCol} The RowCol object representing the current position.
433 */
434hterm.Terminal.prototype.saveCursor = function() {
435 return this.screen_.cursorPosition.clone();
436};
437
rgindaa19afe22012-01-25 15:40:22 -0800438hterm.Terminal.prototype.getTextAttributes = function() {
439 return this.screen_.textAttributes;
440};
441
rginda87b86462011-12-14 13:48:03 -0800442/**
rginda9846e2f2012-01-27 13:53:33 -0800443 * Change the title of this terminal's window.
444 */
445hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800446 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800447};
448
449/**
rginda87b86462011-12-14 13:48:03 -0800450 * Restore a previously saved cursor position.
451 *
452 * @param {hterm.RowCol} cursor The position to restore.
453 */
454hterm.Terminal.prototype.restoreCursor = function(cursor) {
rginda35c456b2012-02-09 17:29:05 -0800455 var row = hterm.clamp(cursor.row, 0, this.screenSize.height - 1);
456 var column = hterm.clamp(cursor.column, 0, this.screenSize.width - 1);
457 this.screen_.setCursorPosition(row, column);
458 if (cursor.column > column ||
459 cursor.column == column && cursor.overflow) {
460 this.screen_.cursorPosition.overflow = true;
461 }
rginda87b86462011-12-14 13:48:03 -0800462};
463
464/**
465 * Set the width of the terminal, resizing the UI to match.
466 */
467hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800468 if (columnCount == null) {
469 this.div_.style.width = '100%';
470 return;
471 }
472
rginda35c456b2012-02-09 17:29:05 -0800473 this.div_.style.width = this.scrollPort_.characterSize.width *
474 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400475 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800476 this.scheduleSyncCursorPosition_();
477};
rginda87b86462011-12-14 13:48:03 -0800478
rgindac9bc5502012-01-18 11:48:44 -0800479/**
rginda35c456b2012-02-09 17:29:05 -0800480 * Set the height of the terminal, resizing the UI to match.
481 */
482hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800483 if (rowCount == null) {
484 this.div_.style.height = '100%';
485 return;
486 }
487
rginda35c456b2012-02-09 17:29:05 -0800488 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700489 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800490 this.realizeSize_(this.screenSize.width, rowCount);
491 this.scheduleSyncCursorPosition_();
492};
493
494/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400495 * Deal with terminal size changes.
496 *
497 */
498hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
499 if (columnCount != this.screenSize.width)
500 this.realizeWidth_(columnCount);
501
502 if (rowCount != this.screenSize.height)
503 this.realizeHeight_(rowCount);
504
505 // Send new terminal size to plugin.
506 this.io.onTerminalResize(columnCount, rowCount);
507};
508
509/**
rgindac9bc5502012-01-18 11:48:44 -0800510 * Deal with terminal width changes.
511 *
512 * This function does what needs to be done when the terminal width changes
513 * out from under us. It happens here rather than in onResize_() because this
514 * code may need to run synchronously to handle programmatic changes of
515 * terminal width.
516 *
517 * Relying on the browser to send us an async resize event means we may not be
518 * in the correct state yet when the next escape sequence hits.
519 */
520hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
521 var deltaColumns = columnCount - this.screen_.getWidth();
522
rginda87b86462011-12-14 13:48:03 -0800523 this.screenSize.width = columnCount;
524 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800525
526 if (deltaColumns > 0) {
527 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
528 } else {
529 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
530 if (this.tabStops_[i] <= columnCount)
531 break;
532
533 this.tabStops_.pop();
534 }
535 }
536
537 this.screen_.setColumnCount(this.screenSize.width);
538};
539
540/**
541 * Deal with terminal height changes.
542 *
543 * This function does what needs to be done when the terminal height changes
544 * out from under us. It happens here rather than in onResize_() because this
545 * code may need to run synchronously to handle programmatic changes of
546 * terminal height.
547 *
548 * Relying on the browser to send us an async resize event means we may not be
549 * in the correct state yet when the next escape sequence hits.
550 */
551hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
552 var deltaRows = rowCount - this.screen_.getHeight();
553
554 this.screenSize.height = rowCount;
555
556 var cursor = this.saveCursor();
557
558 if (deltaRows < 0) {
559 // Screen got smaller.
560 deltaRows *= -1;
561 while (deltaRows) {
562 var lastRow = this.getRowCount() - 1;
563 if (lastRow - this.scrollbackRows_.length == cursor.row)
564 break;
565
566 if (this.getRowText(lastRow))
567 break;
568
569 this.screen_.popRow();
570 deltaRows--;
571 }
572
573 var ary = this.screen_.shiftRows(deltaRows);
574 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
575
576 // We just removed rows from the top of the screen, we need to update
577 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800578 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800579 } else if (deltaRows > 0) {
580 // Screen got larger.
581
582 if (deltaRows <= this.scrollbackRows_.length) {
583 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
584 var rows = this.scrollbackRows_.splice(
585 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
586 this.screen_.unshiftRows(rows);
587 deltaRows -= scrollbackCount;
588 cursor.row += scrollbackCount;
589 }
590
591 if (deltaRows)
592 this.appendRows_(deltaRows);
593 }
594
rginda35c456b2012-02-09 17:29:05 -0800595 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800596 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800597};
598
599/**
600 * Scroll the terminal to the top of the scrollback buffer.
601 */
602hterm.Terminal.prototype.scrollHome = function() {
603 this.scrollPort_.scrollRowToTop(0);
604};
605
606/**
607 * Scroll the terminal to the end.
608 */
609hterm.Terminal.prototype.scrollEnd = function() {
610 this.scrollPort_.scrollRowToBottom(this.getRowCount());
611};
612
613/**
614 * Scroll the terminal one page up (minus one line) relative to the current
615 * position.
616 */
617hterm.Terminal.prototype.scrollPageUp = function() {
618 var i = this.scrollPort_.getTopRowIndex();
619 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
620};
621
622/**
623 * Scroll the terminal one page down (minus one line) relative to the current
624 * position.
625 */
626hterm.Terminal.prototype.scrollPageDown = function() {
627 var i = this.scrollPort_.getTopRowIndex();
628 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800629};
630
rgindac9bc5502012-01-18 11:48:44 -0800631/**
632 * Full terminal reset.
633 */
rginda87b86462011-12-14 13:48:03 -0800634hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800635 this.clearAllTabStops();
636 this.setDefaultTabStops();
rgindac9bc5502012-01-18 11:48:44 -0800637 this.setVTScrollRegion(null, null);
rginda9ea433c2012-03-16 11:57:00 -0700638
639 this.clearHome(this.primaryScreen_);
640 this.primaryScreen_.textAttributes.reset();
641
642 this.clearHome(this.alternateScreen_);
643 this.alternateScreen_.textAttributes.reset();
644
rgindac9bc5502012-01-18 11:48:44 -0800645 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800646};
647
rgindac9bc5502012-01-18 11:48:44 -0800648/**
649 * Soft terminal reset.
650 */
rginda0f5c0292012-01-13 11:00:13 -0800651hterm.Terminal.prototype.softReset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800652 this.options_ = new hterm.Options();
rgindaa19afe22012-01-25 15:40:22 -0800653 this.setCursorVisible(true);
654 this.setCursorBlink(false);
rginda0f5c0292012-01-13 11:00:13 -0800655};
656
rgindac9bc5502012-01-18 11:48:44 -0800657/**
658 * Move the cursor forward to the next tab stop, or to the last column
659 * if no more tab stops are set.
660 */
661hterm.Terminal.prototype.forwardTabStop = function() {
662 var column = this.screen_.cursorPosition.column;
663
664 for (var i = 0; i < this.tabStops_.length; i++) {
665 if (this.tabStops_[i] > column) {
666 this.setCursorColumn(this.tabStops_[i]);
667 return;
668 }
669 }
670
671 this.setCursorColumn(this.screenSize.width - 1);
rginda0f5c0292012-01-13 11:00:13 -0800672};
673
rgindac9bc5502012-01-18 11:48:44 -0800674/**
675 * Move the cursor backward to the previous tab stop, or to the first column
676 * if no previous tab stops are set.
677 */
678hterm.Terminal.prototype.backwardTabStop = function() {
679 var column = this.screen_.cursorPosition.column;
680
681 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
682 if (this.tabStops_[i] < column) {
683 this.setCursorColumn(this.tabStops_[i]);
684 return;
685 }
686 }
687
688 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800689};
690
rgindac9bc5502012-01-18 11:48:44 -0800691/**
692 * Set a tab stop at the given column.
693 *
694 * @param {int} column Zero based column.
695 */
696hterm.Terminal.prototype.setTabStop = function(column) {
697 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
698 if (this.tabStops_[i] == column)
699 return;
700
701 if (this.tabStops_[i] < column) {
702 this.tabStops_.splice(i + 1, 0, column);
703 return;
704 }
705 }
706
707 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800708};
709
rgindac9bc5502012-01-18 11:48:44 -0800710/**
711 * Clear the tab stop at the current cursor position.
712 *
713 * No effect if there is no tab stop at the current cursor position.
714 */
715hterm.Terminal.prototype.clearTabStopAtCursor = function() {
716 var column = this.screen_.cursorPosition.column;
717
718 var i = this.tabStops_.indexOf(column);
719 if (i == -1)
720 return;
721
722 this.tabStops_.splice(i, 1);
723};
724
725/**
726 * Clear all tab stops.
727 */
728hterm.Terminal.prototype.clearAllTabStops = function() {
729 this.tabStops_.length = 0;
730};
731
732/**
733 * Set up the default tab stops, starting from a given column.
734 *
735 * This sets a tabstop every (column % this.tabWidth) column, starting
736 * from the specified column, or 0 if no column is provided.
737 *
738 * This does not clear the existing tab stops first, use clearAllTabStops
739 * for that.
740 *
741 * @param {int} opt_start Optional starting zero based starting column, useful
742 * for filling out missing tab stops when the terminal is resized.
743 */
744hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
745 var start = opt_start || 0;
746 var w = this.tabWidth;
747 var stopCount = Math.floor((this.screenSize.width - start) / this.tabWidth)
748 for (var i = 0; i < stopCount; i++) {
749 this.setTabStop(Math.floor((start + i * w) / w) * w + w);
750 }
rginda87b86462011-12-14 13:48:03 -0800751};
752
rginda6d397402012-01-17 10:58:29 -0800753/**
754 * Save cursor position and attributes.
755 *
756 * TODO(rginda): Save attributes once we support them.
757 */
rginda87b86462011-12-14 13:48:03 -0800758hterm.Terminal.prototype.saveOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800759 this.savedOptions_.cursor = this.saveCursor();
rgindaa19afe22012-01-25 15:40:22 -0800760 this.savedOptions_.textAttributes = this.screen_.textAttributes.clone();
rginda87b86462011-12-14 13:48:03 -0800761};
762
rginda6d397402012-01-17 10:58:29 -0800763/**
764 * Restore cursor position and attributes.
765 *
766 * TODO(rginda): Restore attributes once we support them.
767 */
rginda87b86462011-12-14 13:48:03 -0800768hterm.Terminal.prototype.restoreOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800769 if (this.savedOptions_.cursor)
770 this.restoreCursor(this.savedOptions_.cursor);
rgindaa19afe22012-01-25 15:40:22 -0800771 if (this.savedOptions_.textAttributes)
772 this.screen_.textAttributes = this.savedOptions_.textAttributes;
rginda8ba33642011-12-14 12:31:31 -0800773};
774
775/**
776 * Interpret a sequence of characters.
777 *
778 * Incomplete escape sequences are buffered until the next call.
779 *
780 * @param {string} str Sequence of characters to interpret or pass through.
781 */
782hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800783 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800784 this.scheduleSyncCursorPosition_();
785};
786
787/**
788 * Take over the given DIV for use as the terminal display.
789 *
790 * @param {HTMLDivElement} div The div to use as the terminal display.
791 */
792hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800793 this.div_ = div;
794
rginda8ba33642011-12-14 12:31:31 -0800795 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -0700796 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
797
rginda0918b652012-04-04 11:26:24 -0700798 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -0800799
rginda9f5222b2012-03-05 11:53:28 -0800800 this.setFontSize(this.prefs_.get('font-size'));
801 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -0800802
David Reveman8f552492012-03-28 12:18:41 -0400803 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
804
rginda8ba33642011-12-14 12:31:31 -0800805 this.document_ = this.scrollPort_.getDocument();
806
rginda8ba33642011-12-14 12:31:31 -0800807 this.cursorNode_ = this.document_.createElement('div');
808 this.cursorNode_.style.cssText =
809 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800810 'top: -99px;' +
811 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -0800812 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
813 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda6d397402012-01-17 10:58:29 -0800814 '-webkit-transition: opacity, background-color 100ms linear;' +
rginda9f5222b2012-03-05 11:53:28 -0800815 'background-color: ' + this.prefs_.get('cursor-color'));
rginda8ba33642011-12-14 12:31:31 -0800816 this.document_.body.appendChild(this.cursorNode_);
817
818 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -0800819
rginda87b86462011-12-14 13:48:03 -0800820 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -0800821 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -0800822};
823
rginda0918b652012-04-04 11:26:24 -0700824/**
825 * Return the HTML document that contains the terminal DOM nodes.
826 */
rginda87b86462011-12-14 13:48:03 -0800827hterm.Terminal.prototype.getDocument = function() {
828 return this.document_;
rginda8ba33642011-12-14 12:31:31 -0800829};
830
831/**
rginda0918b652012-04-04 11:26:24 -0700832 * Focus the terminal.
833 */
834hterm.Terminal.prototype.focus = function() {
835 this.scrollPort_.focus();
836};
837
838/**
rginda8ba33642011-12-14 12:31:31 -0800839 * Return the HTML Element for a given row index.
840 *
841 * This is a method from the RowProvider interface. The ScrollPort uses
842 * it to fetch rows on demand as they are scrolled into view.
843 *
844 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
845 * pairs to conserve memory.
846 *
847 * @param {integer} index The zero-based row index, measured relative to the
848 * start of the scrollback buffer. On-screen rows will always have the
849 * largest indicies.
850 * @return {HTMLElement} The 'x-row' element containing for the requested row.
851 */
852hterm.Terminal.prototype.getRowNode = function(index) {
853 if (index < this.scrollbackRows_.length)
854 return this.scrollbackRows_[index];
855
856 var screenIndex = index - this.scrollbackRows_.length;
857 return this.screen_.rowsArray[screenIndex];
858};
859
860/**
861 * Return the text content for a given range of rows.
862 *
863 * This is a method from the RowProvider interface. The ScrollPort uses
864 * it to fetch text content on demand when the user attempts to copy their
865 * selection to the clipboard.
866 *
867 * @param {integer} start The zero-based row index to start from, measured
868 * relative to the start of the scrollback buffer. On-screen rows will
869 * always have the largest indicies.
870 * @param {integer} end The zero-based row index to end on, measured
871 * relative to the start of the scrollback buffer.
872 * @return {string} A single string containing the text value of the range of
873 * rows. Lines will be newline delimited, with no trailing newline.
874 */
875hterm.Terminal.prototype.getRowsText = function(start, end) {
876 var ary = [];
877 for (var i = start; i < end; i++) {
878 var node = this.getRowNode(i);
879 ary.push(node.textContent);
880 }
881
882 return ary.join('\n');
883};
884
885/**
886 * Return the text content for a given row.
887 *
888 * This is a method from the RowProvider interface. The ScrollPort uses
889 * it to fetch text content on demand when the user attempts to copy their
890 * selection to the clipboard.
891 *
892 * @param {integer} index The zero-based row index to return, measured
893 * relative to the start of the scrollback buffer. On-screen rows will
894 * always have the largest indicies.
895 * @return {string} A string containing the text value of the selected row.
896 */
897hterm.Terminal.prototype.getRowText = function(index) {
898 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -0800899 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -0800900};
901
902/**
903 * Return the total number of rows in the addressable screen and in the
904 * scrollback buffer of this terminal.
905 *
906 * This is a method from the RowProvider interface. The ScrollPort uses
907 * it to compute the size of the scrollbar.
908 *
909 * @return {integer} The number of rows in this terminal.
910 */
911hterm.Terminal.prototype.getRowCount = function() {
912 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
913};
914
915/**
916 * Create DOM nodes for new rows and append them to the end of the terminal.
917 *
918 * This is the only correct way to add a new DOM node for a row. Notice that
919 * the new row is appended to the bottom of the list of rows, and does not
920 * require renumbering (of the rowIndex property) of previous rows.
921 *
922 * If you think you want a new blank row somewhere in the middle of the
923 * terminal, look into moveRows_().
924 *
925 * This method does not pay attention to vtScrollTop/Bottom, since you should
926 * be using moveRows() in cases where they would matter.
927 *
928 * The cursor will be positioned at column 0 of the first inserted line.
929 */
930hterm.Terminal.prototype.appendRows_ = function(count) {
931 var cursorRow = this.screen_.rowsArray.length;
932 var offset = this.scrollbackRows_.length + cursorRow;
933 for (var i = 0; i < count; i++) {
934 var row = this.document_.createElement('x-row');
935 row.appendChild(this.document_.createTextNode(''));
936 row.rowIndex = offset + i;
937 this.screen_.pushRow(row);
938 }
939
940 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
941 if (extraRows > 0) {
942 var ary = this.screen_.shiftRows(extraRows);
943 Array.prototype.push.apply(this.scrollbackRows_, ary);
944 this.scheduleScrollDown_();
945 }
946
947 if (cursorRow >= this.screen_.rowsArray.length)
948 cursorRow = this.screen_.rowsArray.length - 1;
949
rginda87b86462011-12-14 13:48:03 -0800950 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -0800951};
952
953/**
954 * Relocate rows from one part of the addressable screen to another.
955 *
956 * This is used to recycle rows during VT scrolls (those which are driven
957 * by VT commands, rather than by the user manipulating the scrollbar.)
958 *
959 * In this case, the blank lines scrolled into the scroll region are made of
960 * the nodes we scrolled off. These have their rowIndex properties carefully
961 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -0800962 */
963hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
964 var ary = this.screen_.removeRows(fromIndex, count);
965 this.screen_.insertRows(toIndex, ary);
966
967 var start, end;
968 if (fromIndex < toIndex) {
969 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -0800970 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800971 } else {
972 start = toIndex;
rginda87b86462011-12-14 13:48:03 -0800973 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800974 }
975
976 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -0800977 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -0800978};
979
980/**
981 * Renumber the rowIndex property of the given range of rows.
982 *
983 * The start and end indicies are relative to the screen, not the scrollback.
984 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -0800985 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -0800986 * no need to renumber scrollback rows.
987 */
988hterm.Terminal.prototype.renumberRows_ = function(start, end) {
989 var offset = this.scrollbackRows_.length;
990 for (var i = start; i < end; i++) {
991 this.screen_.rowsArray[i].rowIndex = offset + i;
992 }
993};
994
995/**
996 * Print a string to the terminal.
997 *
998 * This respects the current insert and wraparound modes. It will add new lines
999 * to the end of the terminal, scrolling off the top into the scrollback buffer
1000 * if necessary.
1001 *
1002 * The string is *not* parsed for escape codes. Use the interpret() method if
1003 * that's what you're after.
1004 *
1005 * @param{string} str The string to print.
1006 */
1007hterm.Terminal.prototype.print = function(str) {
rgindaa19afe22012-01-25 15:40:22 -08001008 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
1009 this.newLine();
rginda2312fff2012-01-05 16:20:52 -08001010
rgindaa19afe22012-01-25 15:40:22 -08001011 if (this.options_.insertMode) {
1012 this.screen_.insertString(str);
1013 } else {
1014 this.screen_.overwriteString(str);
1015 }
1016
1017 var overflow = this.screen_.maybeClipCurrentRow();
1018
1019 if (this.options_.wraparound && overflow) {
1020 var lastColumn;
1021
1022 do {
rginda35c456b2012-02-09 17:29:05 -08001023 this.newLine();
1024 lastColumn = overflow.characterLength;
rgindaa19afe22012-01-25 15:40:22 -08001025
1026 if (!this.options_.insertMode)
1027 this.screen_.deleteChars(overflow.characterLength);
1028
1029 this.screen_.prependNodes(overflow);
rgindaa19afe22012-01-25 15:40:22 -08001030
1031 overflow = this.screen_.maybeClipCurrentRow();
1032 } while (overflow);
1033
1034 this.setCursorColumn(lastColumn);
1035 }
rginda8ba33642011-12-14 12:31:31 -08001036
1037 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001038
rginda9f5222b2012-03-05 11:53:28 -08001039 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001040 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001041};
1042
1043/**
rginda87b86462011-12-14 13:48:03 -08001044 * Set the VT scroll region.
1045 *
rginda87b86462011-12-14 13:48:03 -08001046 * This also resets the cursor position to the absolute (0, 0) position, since
1047 * that's what xterm appears to do.
1048 *
1049 * @param {integer} scrollTop The zero-based top of the scroll region.
1050 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1051 * inclusive.
1052 */
1053hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1054 this.vtScrollTop_ = scrollTop;
1055 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001056};
1057
1058/**
rginda8ba33642011-12-14 12:31:31 -08001059 * Return the top row index according to the VT.
1060 *
1061 * This will return 0 unless the terminal has been told to restrict scrolling
1062 * to some lower row. It is used for some VT cursor positioning and scrolling
1063 * commands.
1064 *
1065 * @return {integer} The topmost row in the terminal's scroll region.
1066 */
1067hterm.Terminal.prototype.getVTScrollTop = function() {
1068 if (this.vtScrollTop_ != null)
1069 return this.vtScrollTop_;
1070
1071 return 0;
rginda87b86462011-12-14 13:48:03 -08001072};
rginda8ba33642011-12-14 12:31:31 -08001073
1074/**
1075 * Return the bottom row index according to the VT.
1076 *
1077 * This will return the height of the terminal unless the it has been told to
1078 * restrict scrolling to some higher row. It is used for some VT cursor
1079 * positioning and scrolling commands.
1080 *
1081 * @return {integer} The bottommost row in the terminal's scroll region.
1082 */
1083hterm.Terminal.prototype.getVTScrollBottom = function() {
1084 if (this.vtScrollBottom_ != null)
1085 return this.vtScrollBottom_;
1086
rginda87b86462011-12-14 13:48:03 -08001087 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001088}
1089
1090/**
1091 * Process a '\n' character.
1092 *
1093 * If the cursor is on the final row of the terminal this will append a new
1094 * blank row to the screen and scroll the topmost row into the scrollback
1095 * buffer.
1096 *
1097 * Otherwise, this moves the cursor to column zero of the next row.
1098 */
1099hterm.Terminal.prototype.newLine = function() {
1100 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001101 // If we're at the end of the screen we need to append a new line and
1102 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001103 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001104 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1105 // End of the scroll region does not affect the scrollback buffer.
1106 this.vtScrollUp(1);
1107 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001108 } else {
rginda87b86462011-12-14 13:48:03 -08001109 // Anywhere else in the screen just moves the cursor.
1110 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001111 }
1112};
1113
1114/**
1115 * Like newLine(), except maintain the cursor column.
1116 */
1117hterm.Terminal.prototype.lineFeed = function() {
1118 var column = this.screen_.cursorPosition.column;
1119 this.newLine();
1120 this.setCursorColumn(column);
1121};
1122
1123/**
rginda87b86462011-12-14 13:48:03 -08001124 * If autoCarriageReturn is set then newLine(), else lineFeed().
1125 */
1126hterm.Terminal.prototype.formFeed = function() {
1127 if (this.options_.autoCarriageReturn) {
1128 this.newLine();
1129 } else {
1130 this.lineFeed();
1131 }
1132};
1133
1134/**
1135 * Move the cursor up one row, possibly inserting a blank line.
1136 *
1137 * The cursor column is not changed.
1138 */
1139hterm.Terminal.prototype.reverseLineFeed = function() {
1140 var scrollTop = this.getVTScrollTop();
1141 var currentRow = this.screen_.cursorPosition.row;
1142
1143 if (currentRow == scrollTop) {
1144 this.insertLines(1);
1145 } else {
1146 this.setAbsoluteCursorRow(currentRow - 1);
1147 }
1148};
1149
1150/**
rginda8ba33642011-12-14 12:31:31 -08001151 * Replace all characters to the left of the current cursor with the space
1152 * character.
1153 *
1154 * TODO(rginda): This should probably *remove* the characters (not just replace
1155 * with a space) if there are no characters at or beyond the current cursor
1156 * position. Once it does that, it'll have the same text-attribute related
1157 * issues as hterm.Screen.prototype.clearCursorRow :/
1158 */
1159hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001160 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001161 this.setCursorColumn(0);
rginda87b86462011-12-14 13:48:03 -08001162 this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1));
1163 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001164};
1165
1166/**
1167 * Erase a given number of characters to the right of the cursor, shifting
1168 * remaining characters to the left.
1169 *
1170 * The cursor position is unchanged.
1171 *
1172 * TODO(rginda): Test that this works even when the cursor is positioned beyond
1173 * the end of the text.
1174 *
1175 * TODO(rginda): This likely has text-attribute related troubles similar to the
1176 * todo on hterm.Screen.prototype.clearCursorRow.
1177 */
1178hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001179 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001180
rginda87b86462011-12-14 13:48:03 -08001181 var maxCount = this.screenSize.width - cursor.column;
rginda8ba33642011-12-14 12:31:31 -08001182 var count = (opt_count && opt_count < maxCount) ? opt_count : maxCount;
1183 this.screen_.deleteChars(count);
rginda87b86462011-12-14 13:48:03 -08001184 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001185};
1186
1187/**
1188 * Erase the current line.
1189 *
1190 * The cursor position is unchanged.
1191 *
1192 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1193 * has a text-attribute related TODO.
1194 */
1195hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001196 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001197 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001198 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001199};
1200
1201/**
1202 * Erase all characters from the start of the scroll region to the current
1203 * cursor position.
1204 *
1205 * The cursor position is unchanged.
1206 *
1207 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1208 * has a text-attribute related TODO.
1209 */
1210hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001211 var cursor = this.saveCursor();
1212
1213 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001214
1215 var top = this.getVTScrollTop();
rginda87b86462011-12-14 13:48:03 -08001216 for (var i = top; i < cursor.row; i++) {
1217 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001218 this.screen_.clearCursorRow();
1219 }
1220
rginda87b86462011-12-14 13:48:03 -08001221 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001222};
1223
1224/**
1225 * Erase all characters from the current cursor position to the end of the
1226 * scroll region.
1227 *
1228 * The cursor position is unchanged.
1229 *
1230 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1231 * has a text-attribute related TODO.
1232 */
1233hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001234 var cursor = this.saveCursor();
1235
1236 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001237
1238 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001239 for (var i = cursor.row + 1; i <= bottom; i++) {
1240 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001241 this.screen_.clearCursorRow();
1242 }
1243
rginda87b86462011-12-14 13:48:03 -08001244 this.restoreCursor(cursor);
1245};
1246
1247/**
1248 * Fill the terminal with a given character.
1249 *
1250 * This methods does not respect the VT scroll region.
1251 *
1252 * @param {string} ch The character to use for the fill.
1253 */
1254hterm.Terminal.prototype.fill = function(ch) {
1255 var cursor = this.saveCursor();
1256
1257 this.setAbsoluteCursorPosition(0, 0);
1258 for (var row = 0; row < this.screenSize.height; row++) {
1259 for (var col = 0; col < this.screenSize.width; col++) {
1260 this.setAbsoluteCursorPosition(row, col);
1261 this.screen_.overwriteString(ch);
1262 }
1263 }
1264
1265 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001266};
1267
1268/**
rginda9ea433c2012-03-16 11:57:00 -07001269 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001270 *
rginda9ea433c2012-03-16 11:57:00 -07001271 * This does not respect the scroll region.
1272 *
1273 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1274 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001275 *
1276 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1277 * has a text-attribute related TODO.
1278 */
rginda9ea433c2012-03-16 11:57:00 -07001279hterm.Terminal.prototype.clearHome = function(opt_screen) {
1280 var screen = opt_screen || this.screen_;
1281 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001282
rgindae4d29232012-01-19 10:47:13 -08001283 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001284 screen.setCursorPosition(i, 0);
1285 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001286 }
1287
rginda9ea433c2012-03-16 11:57:00 -07001288 screen.setCursorPosition(0, 0);
1289};
1290
1291/**
1292 * Erase the entire display without changing the cursor position.
1293 *
1294 * The cursor position is unchanged. This does not respect the scroll
1295 * region.
1296 *
1297 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1298 * to the current screen.
1299 *
1300 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1301 * has a text-attribute related TODO.
1302 */
1303hterm.Terminal.prototype.clear = function(opt_screen) {
1304 var screen = opt_screen || this.screen_;
1305 var cursor = screen.cursorPosition.clone();
1306 this.clearHome(screen);
1307 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001308};
1309
1310/**
1311 * VT command to insert lines at the current cursor row.
1312 *
1313 * This respects the current scroll region. Rows pushed off the bottom are
1314 * lost (they won't show up in the scrollback buffer).
1315 *
1316 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1317 * has a text-attribute related TODO.
1318 *
1319 * @param {integer} count The number of lines to insert.
1320 */
1321hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001322 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001323
1324 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001325 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001326
rgindae4d29232012-01-19 10:47:13 -08001327 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001328 if (start != cursor.row)
1329 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001330
1331 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001332 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001333 this.screen_.clearCursorRow();
1334 }
1335
rginda87b86462011-12-14 13:48:03 -08001336 cursor.column = 0;
1337 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001338};
1339
1340/**
1341 * VT command to delete lines at the current cursor row.
1342 *
1343 * New rows are added to the bottom of scroll region to take their place. New
1344 * rows are strictly there to take up space and have no content or style.
1345 */
1346hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001347 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001348
rginda87b86462011-12-14 13:48:03 -08001349 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001350 var bottom = this.getVTScrollBottom();
1351
rginda87b86462011-12-14 13:48:03 -08001352 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001353 count = Math.min(count, maxCount);
1354
rginda87b86462011-12-14 13:48:03 -08001355 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001356 if (count != maxCount)
1357 this.moveRows_(top, count, moveStart);
1358
1359 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001360 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001361 this.screen_.clearCursorRow();
1362 }
1363
rginda87b86462011-12-14 13:48:03 -08001364 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001365};
1366
1367/**
1368 * Inserts the given number of spaces at the current cursor position.
1369 *
rginda87b86462011-12-14 13:48:03 -08001370 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001371 */
1372hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001373 var cursor = this.saveCursor();
1374
rginda0f5c0292012-01-13 11:00:13 -08001375 var ws = hterm.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001376 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001377 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001378
1379 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001380};
1381
1382/**
1383 * Forward-delete the specified number of characters starting at the cursor
1384 * position.
1385 *
1386 * @param {integer} count The number of characters to delete.
1387 */
1388hterm.Terminal.prototype.deleteChars = function(count) {
1389 this.screen_.deleteChars(count);
1390};
1391
1392/**
1393 * Shift rows in the scroll region upwards by a given number of lines.
1394 *
1395 * New rows are inserted at the bottom of the scroll region to fill the
1396 * vacated rows. The new rows not filled out with the current text attributes.
1397 *
1398 * This function does not affect the scrollback rows at all. Rows shifted
1399 * off the top are lost.
1400 *
rginda87b86462011-12-14 13:48:03 -08001401 * The cursor position is not altered.
1402 *
rginda8ba33642011-12-14 12:31:31 -08001403 * @param {integer} count The number of rows to scroll.
1404 */
1405hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001406 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001407
rginda87b86462011-12-14 13:48:03 -08001408 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001409 this.deleteLines(count);
1410
rginda87b86462011-12-14 13:48:03 -08001411 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001412};
1413
1414/**
1415 * Shift rows below the cursor down by a given number of lines.
1416 *
1417 * This function respects the current scroll region.
1418 *
1419 * New rows are inserted at the top of the scroll region to fill the
1420 * vacated rows. The new rows not filled out with the current text attributes.
1421 *
1422 * This function does not affect the scrollback rows at all. Rows shifted
1423 * off the bottom are lost.
1424 *
1425 * @param {integer} count The number of rows to scroll.
1426 */
1427hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001428 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001429
rginda87b86462011-12-14 13:48:03 -08001430 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001431 this.insertLines(opt_count);
1432
rginda87b86462011-12-14 13:48:03 -08001433 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001434};
1435
rginda87b86462011-12-14 13:48:03 -08001436
rginda8ba33642011-12-14 12:31:31 -08001437/**
1438 * Set the cursor position.
1439 *
1440 * The cursor row is relative to the scroll region if the terminal has
1441 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1442 *
1443 * @param {integer} row The new zero-based cursor row.
1444 * @param {integer} row The new zero-based cursor column.
1445 */
1446hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1447 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001448 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001449 } else {
rginda87b86462011-12-14 13:48:03 -08001450 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001451 }
rginda87b86462011-12-14 13:48:03 -08001452};
rginda8ba33642011-12-14 12:31:31 -08001453
rginda87b86462011-12-14 13:48:03 -08001454hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1455 var scrollTop = this.getVTScrollTop();
1456 row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
rginda2312fff2012-01-05 16:20:52 -08001457 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001458 this.screen_.setCursorPosition(row, column);
1459};
1460
1461hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rginda2312fff2012-01-05 16:20:52 -08001462 row = hterm.clamp(row, 0, this.screenSize.height - 1);
1463 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001464 this.screen_.setCursorPosition(row, column);
1465};
1466
1467/**
1468 * Set the cursor column.
1469 *
1470 * @param {integer} column The new zero-based cursor column.
1471 */
1472hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001473 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001474};
1475
1476/**
1477 * Return the cursor column.
1478 *
1479 * @return {integer} The zero-based cursor column.
1480 */
1481hterm.Terminal.prototype.getCursorColumn = function() {
1482 return this.screen_.cursorPosition.column;
1483};
1484
1485/**
1486 * Set the cursor row.
1487 *
1488 * The cursor row is relative to the scroll region if the terminal has
1489 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1490 *
1491 * @param {integer} row The new cursor row.
1492 */
rginda87b86462011-12-14 13:48:03 -08001493hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1494 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001495};
1496
1497/**
1498 * Return the cursor row.
1499 *
1500 * @return {integer} The zero-based cursor row.
1501 */
1502hterm.Terminal.prototype.getCursorRow = function(row) {
1503 return this.screen_.cursorPosition.row;
1504};
1505
1506/**
1507 * Request that the ScrollPort redraw itself soon.
1508 *
1509 * The redraw will happen asynchronously, soon after the call stack winds down.
1510 * Multiple calls will be coalesced into a single redraw.
1511 */
1512hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001513 if (this.timeouts_.redraw)
1514 return;
rginda8ba33642011-12-14 12:31:31 -08001515
1516 var self = this;
rginda87b86462011-12-14 13:48:03 -08001517 this.timeouts_.redraw = setTimeout(function() {
1518 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001519 self.scrollPort_.redraw_();
1520 }, 0);
1521};
1522
1523/**
1524 * Request that the ScrollPort be scrolled to the bottom.
1525 *
1526 * The scroll will happen asynchronously, soon after the call stack winds down.
1527 * Multiple calls will be coalesced into a single scroll.
1528 *
1529 * This affects the scrollbar position of the ScrollPort, and has nothing to
1530 * do with the VT scroll commands.
1531 */
1532hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1533 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001534 return;
rginda8ba33642011-12-14 12:31:31 -08001535
1536 var self = this;
1537 this.timeouts_.scrollDown = setTimeout(function() {
1538 delete self.timeouts_.scrollDown;
1539 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1540 }, 10);
1541};
1542
1543/**
1544 * Move the cursor up a specified number of rows.
1545 *
1546 * @param {integer} count The number of rows to move the cursor.
1547 */
1548hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001549 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001550};
1551
1552/**
1553 * Move the cursor down a specified number of rows.
1554 *
1555 * @param {integer} count The number of rows to move the cursor.
1556 */
1557hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001558 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001559 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1560 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1561 this.screenSize.height - 1);
1562
1563 var row = hterm.clamp(this.screen_.cursorPosition.row + count,
1564 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001565 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001566};
1567
1568/**
1569 * Move the cursor left a specified number of columns.
1570 *
1571 * @param {integer} count The number of columns to move the cursor.
1572 */
1573hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001574 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001575};
1576
1577/**
1578 * Move the cursor right a specified number of columns.
1579 *
1580 * @param {integer} count The number of columns to move the cursor.
1581 */
1582hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001583 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001584 var column = hterm.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001585 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001586 this.setCursorColumn(column);
1587};
1588
1589/**
1590 * Reverse the foreground and background colors of the terminal.
1591 *
1592 * This only affects text that was drawn with no attributes.
1593 *
1594 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1595 * been drawn with attributes that happen to coincide with the default
1596 * 'no-attribute' colors. My guess is probably not.
1597 */
1598hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001599 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001600 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001601 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1602 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001603 } else {
rginda9f5222b2012-03-05 11:53:28 -08001604 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1605 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001606 }
1607};
1608
1609/**
rginda87b86462011-12-14 13:48:03 -08001610 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001611 */
1612hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001613 if (this.bellAudio_.getAttribute('src'))
1614 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001615
rginda6d397402012-01-17 10:58:29 -08001616 this.cursorNode_.style.backgroundColor =
1617 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001618
1619 var self = this;
1620 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001621 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001622 }, 200);
rginda87b86462011-12-14 13:48:03 -08001623};
1624
1625/**
rginda8ba33642011-12-14 12:31:31 -08001626 * Set the origin mode bit.
1627 *
1628 * If origin mode is on, certain VT cursor and scrolling commands measure their
1629 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1630 * to the top of the addressable screen.
1631 *
1632 * Defaults to off.
1633 *
1634 * @param {boolean} state True to set origin mode, false to unset.
1635 */
1636hterm.Terminal.prototype.setOriginMode = function(state) {
1637 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001638 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001639};
1640
1641/**
1642 * Set the insert mode bit.
1643 *
1644 * If insert mode is on, existing text beyond the cursor position will be
1645 * shifted right to make room for new text. Otherwise, new text overwrites
1646 * any existing text.
1647 *
1648 * Defaults to off.
1649 *
1650 * @param {boolean} state True to set insert mode, false to unset.
1651 */
1652hterm.Terminal.prototype.setInsertMode = function(state) {
1653 this.options_.insertMode = state;
1654};
1655
1656/**
rginda87b86462011-12-14 13:48:03 -08001657 * Set the auto carriage return bit.
1658 *
1659 * If auto carriage return is on then a formfeed character is interpreted
1660 * as a newline, otherwise it's the same as a linefeed. The difference boils
1661 * down to whether or not the cursor column is reset.
1662 */
1663hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1664 this.options_.autoCarriageReturn = state;
1665};
1666
1667/**
rginda8ba33642011-12-14 12:31:31 -08001668 * Set the wraparound mode bit.
1669 *
1670 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1671 * to the start of the following row. Otherwise, the cursor is clamped to the
1672 * end of the screen and attempts to write past it are ignored.
1673 *
1674 * Defaults to on.
1675 *
1676 * @param {boolean} state True to set wraparound mode, false to unset.
1677 */
1678hterm.Terminal.prototype.setWraparound = function(state) {
1679 this.options_.wraparound = state;
1680};
1681
1682/**
1683 * Set the reverse-wraparound mode bit.
1684 *
1685 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1686 * to the end of the previous row. Otherwise, the cursor is clamped to column
1687 * 0.
1688 *
1689 * Defaults to off.
1690 *
1691 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1692 */
1693hterm.Terminal.prototype.setReverseWraparound = function(state) {
1694 this.options_.reverseWraparound = state;
1695};
1696
1697/**
1698 * Selects between the primary and alternate screens.
1699 *
1700 * If alternate mode is on, the alternate screen is active. Otherwise the
1701 * primary screen is active.
1702 *
1703 * Swapping screens has no effect on the scrollback buffer.
1704 *
1705 * Each screen maintains its own cursor position.
1706 *
1707 * Defaults to off.
1708 *
1709 * @param {boolean} state True to set alternate mode, false to unset.
1710 */
1711hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001712 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001713 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1714
rginda35c456b2012-02-09 17:29:05 -08001715 if (this.screen_.rowsArray.length &&
1716 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
1717 // If the screen changed sizes while we were away, our rowIndexes may
1718 // be incorrect.
1719 var offset = this.scrollbackRows_.length;
1720 var ary = this.screen_.rowsArray;
1721 for (i = 0; i < ary.length; i++) {
1722 ary[i].rowIndex = offset + i;
1723 }
1724 }
rginda8ba33642011-12-14 12:31:31 -08001725
rginda35c456b2012-02-09 17:29:05 -08001726 this.realizeWidth_(this.screenSize.width);
1727 this.realizeHeight_(this.screenSize.height);
1728 this.scrollPort_.syncScrollHeight();
1729 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001730
rginda6d397402012-01-17 10:58:29 -08001731 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08001732 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08001733};
1734
1735/**
1736 * Set the cursor-blink mode bit.
1737 *
1738 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1739 * a visible cursor does not blink.
1740 *
1741 * You should make sure to turn blinking off if you're going to dispose of a
1742 * terminal, otherwise you'll leak a timeout.
1743 *
1744 * Defaults to on.
1745 *
1746 * @param {boolean} state True to set cursor-blink mode, false to unset.
1747 */
1748hterm.Terminal.prototype.setCursorBlink = function(state) {
1749 this.options_.cursorBlink = state;
1750
1751 if (!state && this.timeouts_.cursorBlink) {
1752 clearTimeout(this.timeouts_.cursorBlink);
1753 delete this.timeouts_.cursorBlink;
1754 }
1755
1756 if (this.options_.cursorVisible)
1757 this.setCursorVisible(true);
1758};
1759
1760/**
1761 * Set the cursor-visible mode bit.
1762 *
1763 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1764 *
1765 * Defaults to on.
1766 *
1767 * @param {boolean} state True to set cursor-visible mode, false to unset.
1768 */
1769hterm.Terminal.prototype.setCursorVisible = function(state) {
1770 this.options_.cursorVisible = state;
1771
1772 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001773 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001774 return;
1775 }
1776
rginda87b86462011-12-14 13:48:03 -08001777 this.syncCursorPosition_();
1778
1779 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001780
1781 if (this.options_.cursorBlink) {
1782 if (this.timeouts_.cursorBlink)
1783 return;
1784
1785 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1786 500);
1787 } else {
1788 if (this.timeouts_.cursorBlink) {
1789 clearTimeout(this.timeouts_.cursorBlink);
1790 delete this.timeouts_.cursorBlink;
1791 }
1792 }
1793};
1794
1795/**
rginda87b86462011-12-14 13:48:03 -08001796 * Synchronizes the visible cursor and document selection with the current
1797 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08001798 */
1799hterm.Terminal.prototype.syncCursorPosition_ = function() {
1800 var topRowIndex = this.scrollPort_.getTopRowIndex();
1801 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
1802 var cursorRowIndex = this.scrollbackRows_.length +
1803 this.screen_.cursorPosition.row;
1804
1805 if (cursorRowIndex > bottomRowIndex) {
1806 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08001807 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08001808 return;
1809 }
1810
rginda35c456b2012-02-09 17:29:05 -08001811 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
1812 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
1813
rginda8ba33642011-12-14 12:31:31 -08001814 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08001815 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
1816 'px';
1817 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
1818 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08001819
1820 this.cursorNode_.setAttribute('title',
1821 '(' + this.screen_.cursorPosition.row +
1822 ', ' + this.screen_.cursorPosition.column +
1823 ')');
1824
1825 // Update the caret for a11y purposes.
1826 var selection = this.document_.getSelection();
1827 if (selection && selection.isCollapsed)
1828 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08001829};
1830
1831/**
1832 * Synchronizes the visible cursor with the current cursor coordinates.
1833 *
1834 * The sync will happen asynchronously, soon after the call stack winds down.
1835 * Multiple calls will be coalesced into a single sync.
1836 */
1837hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
1838 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08001839 return;
rginda8ba33642011-12-14 12:31:31 -08001840
1841 var self = this;
1842 this.timeouts_.syncCursor = setTimeout(function() {
1843 self.syncCursorPosition_();
1844 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08001845 }, 0);
1846};
1847
rgindacc2996c2012-02-24 14:59:31 -08001848/**
1849 * Show the terminal overlay for a given amount of time.
1850 *
1851 * The terminal overlay appears in inverse video in a large font, centered
1852 * over the terminal. You should probably keep the overlay message brief,
1853 * since it's in a large font and you probably aren't going to check the size
1854 * of the terminal first.
1855 *
1856 * @param {string} msg The text (not HTML) message to display in the overlay.
1857 * @param {number} opt_timeout The amount of time to wait before fading out
1858 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
1859 * stay up forever (or until the next overlay).
1860 */
1861hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08001862 if (!this.overlayNode_) {
1863 if (!this.div_)
1864 return;
1865
1866 this.overlayNode_ = this.document_.createElement('div');
1867 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08001868 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08001869 'font-size: xx-large;' +
1870 'opacity: 0.75;' +
1871 'padding: 0.2em 0.5em 0.2em 0.5em;' +
1872 'position: absolute;' +
1873 '-webkit-user-select: none;' +
1874 '-webkit-transition: opacity 180ms ease-in;');
1875 }
1876
rginda9f5222b2012-03-05 11:53:28 -08001877 this.overlayNode_.style.color = this.prefs_.get('background-color');
1878 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
1879 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
1880
rgindaf0090c92012-02-10 14:58:52 -08001881 this.overlayNode_.textContent = msg;
1882 this.overlayNode_.style.opacity = '0.75';
1883
1884 if (!this.overlayNode_.parentNode)
1885 this.div_.appendChild(this.overlayNode_);
1886
1887 this.overlayNode_.style.top = (
1888 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
1889 this.overlayNode_.style.left = (
1890 this.div_.clientWidth - this.overlayNode_.clientWidth -
1891 this.scrollbarWidthPx) / 2;
1892
1893 var self = this;
1894
1895 if (this.overlayTimeout_)
1896 clearTimeout(this.overlayTimeout_);
1897
rgindacc2996c2012-02-24 14:59:31 -08001898 if (opt_timeout === null)
1899 return;
1900
rgindaf0090c92012-02-10 14:58:52 -08001901 this.overlayTimeout_ = setTimeout(function() {
1902 self.overlayNode_.style.opacity = '0';
1903 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07001904 if (self.overlayNode_.parentNode)
1905 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08001906 self.overlayTimeout_ = null;
1907 self.overlayNode_.style.opacity = '0.75';
1908 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08001909 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08001910};
1911
1912hterm.Terminal.prototype.overlaySize = function() {
1913 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
1914};
1915
rginda87b86462011-12-14 13:48:03 -08001916/**
1917 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
1918 *
1919 * @param {string} string The VT string representing the keystroke.
1920 */
1921hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08001922 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08001923 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1924
1925 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08001926};
1927
1928/**
1929 * React when the ScrollPort is scrolled.
1930 */
1931hterm.Terminal.prototype.onScroll_ = function() {
1932 this.scheduleSyncCursorPosition_();
1933};
1934
1935/**
rginda9846e2f2012-01-27 13:53:33 -08001936 * React when text is pasted into the scrollPort.
1937 */
1938hterm.Terminal.prototype.onPaste_ = function(e) {
1939 this.io.onVTKeystroke(e.text);
1940};
1941
1942/**
rginda8ba33642011-12-14 12:31:31 -08001943 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08001944 *
1945 * Note: This function should not directly contain code that alters the internal
1946 * state of the terminal. That kind of code belongs in realizeWidth or
1947 * realizeHeight, so that it can be executed synchronously in the case of a
1948 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08001949 */
1950hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08001951 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08001952 this.scrollPort_.characterSize.width);
1953 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
1954 this.scrollPort_.characterSize.height);
1955
1956 if (!(columnCount || rowCount)) {
1957 // We avoid these situations since they happen sometimes when the terminal
1958 // gets removed from the document, and we can't deal with that.
1959 return;
1960 }
1961
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001962 this.realizeSize_(columnCount, rowCount);
rgindac9bc5502012-01-18 11:48:44 -08001963 this.scheduleSyncCursorPosition_();
rgindaf0090c92012-02-10 14:58:52 -08001964 this.overlaySize();
rginda8ba33642011-12-14 12:31:31 -08001965};
1966
1967/**
1968 * Service the cursor blink timeout.
1969 */
1970hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08001971 if (this.cursorNode_.style.opacity == '0') {
1972 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001973 } else {
rginda87b86462011-12-14 13:48:03 -08001974 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001975 }
1976};
David Reveman8f552492012-03-28 12:18:41 -04001977
1978/**
1979 * Set the scrollbar-visible mode bit.
1980 *
1981 * If scrollbar-visible is on, the vertical scrollbar will be visible.
1982 * Otherwise it will not.
1983 *
1984 * Defaults to on.
1985 *
1986 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
1987 */
1988hterm.Terminal.prototype.setScrollbarVisible = function(state) {
1989 this.scrollPort_.setScrollbarVisible(state);
1990};