blob: f01e2fed0e7fdf64e4f8050ad52ab97ae997b7d2 [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 /**
rgindaf522ce02012-04-17 17:49:17 -0700294 * The default environment variables.
295 */
296 ['environment', {TERM: 'xterm-256color'}, null],
297
298 /**
rginda30f20f62012-04-05 16:36:19 -0700299 * If true, page up/down will control the terminal scrollbar and shift
300 * page up/down will send the VT keycodes. If false then page up/down
301 * sends VT codes and shift page up/down scrolls.
302 */
303 ['page-keys-scroll', false, function(v) {
304 self.keyboard.pageKeysScroll = v;
305 }
306 ],
307
rginda9f5222b2012-03-05 11:53:28 -0800308 ]);
309
310 if (needSync)
311 this.prefs_.notifyAll();
312};
313
314/**
315 * Return the current terminal background color.
316 *
317 * Intended for use by other classes, so we don't have to expose the entire
318 * prefs_ object.
319 */
320hterm.Terminal.prototype.getBackgroundColor = function() {
321 return this.prefs_.get('background-color');
322};
323
324/**
325 * Return the current terminal foreground color.
326 *
327 * Intended for use by other classes, so we don't have to expose the entire
328 * prefs_ object.
329 */
330hterm.Terminal.prototype.getForegroundColor = function() {
331 return this.prefs_.get('foreground-color');
332};
333
334/**
rginda87b86462011-12-14 13:48:03 -0800335 * Create a new instance of a terminal command and run it with a given
336 * argument string.
337 *
338 * @param {function} commandClass The constructor for a terminal command.
339 * @param {string} argString The argument string to pass to the command.
340 */
341hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700342 var environment = this.prefs_.get('environment');
343 if (typeof environment != 'object' || environment == null)
344 environment = {};
345
rginda87b86462011-12-14 13:48:03 -0800346 var self = this;
347 this.command = new commandClass(
348 { argString: argString || '',
349 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700350 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800351 onExit: function(code) {
352 self.io.pop();
353 self.io.println(hterm.msg('COMMAND_COMPLETE',
354 [self.command.commandName, code]));
rgindafeaf3142012-01-31 15:14:20 -0800355 self.uninstallKeyboard();
rginda87b86462011-12-14 13:48:03 -0800356 }
357 });
358
rgindafeaf3142012-01-31 15:14:20 -0800359 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800360 this.command.run();
361};
362
363/**
rgindafeaf3142012-01-31 15:14:20 -0800364 * Returns true if the current screen is the primary screen, false otherwise.
365 */
366hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700367 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800368};
369
370/**
371 * Install the keyboard handler for this terminal.
372 *
373 * This will prevent the browser from seeing any keystrokes sent to the
374 * terminal.
375 */
376hterm.Terminal.prototype.installKeyboard = function() {
377 this.keyboard.installKeyboard(this.document_.body.firstChild);
378}
379
380/**
381 * Uninstall the keyboard handler for this terminal.
382 */
383hterm.Terminal.prototype.uninstallKeyboard = function() {
384 this.keyboard.installKeyboard(null);
385}
386
387/**
rginda35c456b2012-02-09 17:29:05 -0800388 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800389 *
390 * Call setFontSize(0) to reset to the default font size.
391 *
392 * This function does not modify the font-size preference.
393 *
394 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800395 */
396hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800397 if (px === 0)
398 px = this.prefs_.get('font-size');
399
rginda35c456b2012-02-09 17:29:05 -0800400 this.scrollPort_.setFontSize(px);
401};
402
403/**
404 * Get the current font size.
405 */
406hterm.Terminal.prototype.getFontSize = function() {
407 return this.scrollPort_.getFontSize();
408};
409
410/**
411 * Set the CSS "font-family" for this terminal.
412 */
rginda9f5222b2012-03-05 11:53:28 -0800413hterm.Terminal.prototype.syncFontFamily = function() {
414 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
415 this.prefs_.get('font-smoothing'));
416 this.syncBoldSafeState();
417};
418
419hterm.Terminal.prototype.syncBoldSafeState = function() {
420 var enableBold = this.prefs_.get('enable-bold');
421 if (enableBold !== null) {
422 this.screen_.textAttributes.enableBold = enableBold;
423 return;
424 }
425
rgindaf7521392012-02-28 17:20:34 -0800426 var normalSize = this.scrollPort_.measureCharacterSize();
427 var boldSize = this.scrollPort_.measureCharacterSize('bold');
428
429 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800430 if (!isBoldSafe) {
431 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700432 'from normal. Font family is: ' +
433 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800434 }
rginda9f5222b2012-03-05 11:53:28 -0800435
436 this.screen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800437};
438
439/**
rginda87b86462011-12-14 13:48:03 -0800440 * Return a copy of the current cursor position.
441 *
442 * @return {hterm.RowCol} The RowCol object representing the current position.
443 */
444hterm.Terminal.prototype.saveCursor = function() {
445 return this.screen_.cursorPosition.clone();
446};
447
rgindaa19afe22012-01-25 15:40:22 -0800448hterm.Terminal.prototype.getTextAttributes = function() {
449 return this.screen_.textAttributes;
450};
451
rginda87b86462011-12-14 13:48:03 -0800452/**
rgindaf522ce02012-04-17 17:49:17 -0700453 * Return the current browser zoom factor applied to the terminal.
454 *
455 * @return {number} The current browser zoom factor.
456 */
457hterm.Terminal.prototype.getZoomFactor = function() {
458 return this.scrollPort_.characterSize.zoomFactor;
459};
460
461/**
rginda9846e2f2012-01-27 13:53:33 -0800462 * Change the title of this terminal's window.
463 */
464hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800465 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800466};
467
468/**
rginda87b86462011-12-14 13:48:03 -0800469 * Restore a previously saved cursor position.
470 *
471 * @param {hterm.RowCol} cursor The position to restore.
472 */
473hterm.Terminal.prototype.restoreCursor = function(cursor) {
rginda35c456b2012-02-09 17:29:05 -0800474 var row = hterm.clamp(cursor.row, 0, this.screenSize.height - 1);
475 var column = hterm.clamp(cursor.column, 0, this.screenSize.width - 1);
476 this.screen_.setCursorPosition(row, column);
477 if (cursor.column > column ||
478 cursor.column == column && cursor.overflow) {
479 this.screen_.cursorPosition.overflow = true;
480 }
rginda87b86462011-12-14 13:48:03 -0800481};
482
483/**
484 * Set the width of the terminal, resizing the UI to match.
485 */
486hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800487 if (columnCount == null) {
488 this.div_.style.width = '100%';
489 return;
490 }
491
rginda35c456b2012-02-09 17:29:05 -0800492 this.div_.style.width = this.scrollPort_.characterSize.width *
493 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400494 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800495 this.scheduleSyncCursorPosition_();
496};
rginda87b86462011-12-14 13:48:03 -0800497
rgindac9bc5502012-01-18 11:48:44 -0800498/**
rginda35c456b2012-02-09 17:29:05 -0800499 * Set the height of the terminal, resizing the UI to match.
500 */
501hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800502 if (rowCount == null) {
503 this.div_.style.height = '100%';
504 return;
505 }
506
rginda35c456b2012-02-09 17:29:05 -0800507 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700508 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800509 this.realizeSize_(this.screenSize.width, rowCount);
510 this.scheduleSyncCursorPosition_();
511};
512
513/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400514 * Deal with terminal size changes.
515 *
516 */
517hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
518 if (columnCount != this.screenSize.width)
519 this.realizeWidth_(columnCount);
520
521 if (rowCount != this.screenSize.height)
522 this.realizeHeight_(rowCount);
523
524 // Send new terminal size to plugin.
525 this.io.onTerminalResize(columnCount, rowCount);
526};
527
528/**
rgindac9bc5502012-01-18 11:48:44 -0800529 * Deal with terminal width changes.
530 *
531 * This function does what needs to be done when the terminal width changes
532 * out from under us. It happens here rather than in onResize_() because this
533 * code may need to run synchronously to handle programmatic changes of
534 * terminal width.
535 *
536 * Relying on the browser to send us an async resize event means we may not be
537 * in the correct state yet when the next escape sequence hits.
538 */
539hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
540 var deltaColumns = columnCount - this.screen_.getWidth();
541
rginda87b86462011-12-14 13:48:03 -0800542 this.screenSize.width = columnCount;
543 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800544
545 if (deltaColumns > 0) {
546 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
547 } else {
548 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
549 if (this.tabStops_[i] <= columnCount)
550 break;
551
552 this.tabStops_.pop();
553 }
554 }
555
556 this.screen_.setColumnCount(this.screenSize.width);
557};
558
559/**
560 * Deal with terminal height changes.
561 *
562 * This function does what needs to be done when the terminal height changes
563 * out from under us. It happens here rather than in onResize_() because this
564 * code may need to run synchronously to handle programmatic changes of
565 * terminal height.
566 *
567 * Relying on the browser to send us an async resize event means we may not be
568 * in the correct state yet when the next escape sequence hits.
569 */
570hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
571 var deltaRows = rowCount - this.screen_.getHeight();
572
573 this.screenSize.height = rowCount;
574
575 var cursor = this.saveCursor();
576
577 if (deltaRows < 0) {
578 // Screen got smaller.
579 deltaRows *= -1;
580 while (deltaRows) {
581 var lastRow = this.getRowCount() - 1;
582 if (lastRow - this.scrollbackRows_.length == cursor.row)
583 break;
584
585 if (this.getRowText(lastRow))
586 break;
587
588 this.screen_.popRow();
589 deltaRows--;
590 }
591
592 var ary = this.screen_.shiftRows(deltaRows);
593 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
594
595 // We just removed rows from the top of the screen, we need to update
596 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800597 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800598 } else if (deltaRows > 0) {
599 // Screen got larger.
600
601 if (deltaRows <= this.scrollbackRows_.length) {
602 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
603 var rows = this.scrollbackRows_.splice(
604 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
605 this.screen_.unshiftRows(rows);
606 deltaRows -= scrollbackCount;
607 cursor.row += scrollbackCount;
608 }
609
610 if (deltaRows)
611 this.appendRows_(deltaRows);
612 }
613
rginda35c456b2012-02-09 17:29:05 -0800614 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800615 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800616};
617
618/**
619 * Scroll the terminal to the top of the scrollback buffer.
620 */
621hterm.Terminal.prototype.scrollHome = function() {
622 this.scrollPort_.scrollRowToTop(0);
623};
624
625/**
626 * Scroll the terminal to the end.
627 */
628hterm.Terminal.prototype.scrollEnd = function() {
629 this.scrollPort_.scrollRowToBottom(this.getRowCount());
630};
631
632/**
633 * Scroll the terminal one page up (minus one line) relative to the current
634 * position.
635 */
636hterm.Terminal.prototype.scrollPageUp = function() {
637 var i = this.scrollPort_.getTopRowIndex();
638 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
639};
640
641/**
642 * Scroll the terminal one page down (minus one line) relative to the current
643 * position.
644 */
645hterm.Terminal.prototype.scrollPageDown = function() {
646 var i = this.scrollPort_.getTopRowIndex();
647 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800648};
649
rgindac9bc5502012-01-18 11:48:44 -0800650/**
651 * Full terminal reset.
652 */
rginda87b86462011-12-14 13:48:03 -0800653hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800654 this.clearAllTabStops();
655 this.setDefaultTabStops();
rgindac9bc5502012-01-18 11:48:44 -0800656 this.setVTScrollRegion(null, null);
rginda9ea433c2012-03-16 11:57:00 -0700657
658 this.clearHome(this.primaryScreen_);
659 this.primaryScreen_.textAttributes.reset();
660
661 this.clearHome(this.alternateScreen_);
662 this.alternateScreen_.textAttributes.reset();
663
rgindac9bc5502012-01-18 11:48:44 -0800664 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800665};
666
rgindac9bc5502012-01-18 11:48:44 -0800667/**
668 * Soft terminal reset.
669 */
rginda0f5c0292012-01-13 11:00:13 -0800670hterm.Terminal.prototype.softReset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800671 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700672
673 this.primaryScreen_.textAttributes.resetColorPalette();
674 this.alternateScreen_.textAttributes.resetColorPalette();
675
rgindaa19afe22012-01-25 15:40:22 -0800676 this.setCursorVisible(true);
677 this.setCursorBlink(false);
rginda0f5c0292012-01-13 11:00:13 -0800678};
679
rgindac9bc5502012-01-18 11:48:44 -0800680/**
681 * Move the cursor forward to the next tab stop, or to the last column
682 * if no more tab stops are set.
683 */
684hterm.Terminal.prototype.forwardTabStop = function() {
685 var column = this.screen_.cursorPosition.column;
686
687 for (var i = 0; i < this.tabStops_.length; i++) {
688 if (this.tabStops_[i] > column) {
689 this.setCursorColumn(this.tabStops_[i]);
690 return;
691 }
692 }
693
694 this.setCursorColumn(this.screenSize.width - 1);
rginda0f5c0292012-01-13 11:00:13 -0800695};
696
rgindac9bc5502012-01-18 11:48:44 -0800697/**
698 * Move the cursor backward to the previous tab stop, or to the first column
699 * if no previous tab stops are set.
700 */
701hterm.Terminal.prototype.backwardTabStop = function() {
702 var column = this.screen_.cursorPosition.column;
703
704 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
705 if (this.tabStops_[i] < column) {
706 this.setCursorColumn(this.tabStops_[i]);
707 return;
708 }
709 }
710
711 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800712};
713
rgindac9bc5502012-01-18 11:48:44 -0800714/**
715 * Set a tab stop at the given column.
716 *
717 * @param {int} column Zero based column.
718 */
719hterm.Terminal.prototype.setTabStop = function(column) {
720 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
721 if (this.tabStops_[i] == column)
722 return;
723
724 if (this.tabStops_[i] < column) {
725 this.tabStops_.splice(i + 1, 0, column);
726 return;
727 }
728 }
729
730 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800731};
732
rgindac9bc5502012-01-18 11:48:44 -0800733/**
734 * Clear the tab stop at the current cursor position.
735 *
736 * No effect if there is no tab stop at the current cursor position.
737 */
738hterm.Terminal.prototype.clearTabStopAtCursor = function() {
739 var column = this.screen_.cursorPosition.column;
740
741 var i = this.tabStops_.indexOf(column);
742 if (i == -1)
743 return;
744
745 this.tabStops_.splice(i, 1);
746};
747
748/**
749 * Clear all tab stops.
750 */
751hterm.Terminal.prototype.clearAllTabStops = function() {
752 this.tabStops_.length = 0;
753};
754
755/**
756 * Set up the default tab stops, starting from a given column.
757 *
758 * This sets a tabstop every (column % this.tabWidth) column, starting
759 * from the specified column, or 0 if no column is provided.
760 *
761 * This does not clear the existing tab stops first, use clearAllTabStops
762 * for that.
763 *
764 * @param {int} opt_start Optional starting zero based starting column, useful
765 * for filling out missing tab stops when the terminal is resized.
766 */
767hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
768 var start = opt_start || 0;
769 var w = this.tabWidth;
770 var stopCount = Math.floor((this.screenSize.width - start) / this.tabWidth)
771 for (var i = 0; i < stopCount; i++) {
772 this.setTabStop(Math.floor((start + i * w) / w) * w + w);
773 }
rginda87b86462011-12-14 13:48:03 -0800774};
775
rginda6d397402012-01-17 10:58:29 -0800776/**
777 * Save cursor position and attributes.
778 *
779 * TODO(rginda): Save attributes once we support them.
780 */
rginda87b86462011-12-14 13:48:03 -0800781hterm.Terminal.prototype.saveOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800782 this.savedOptions_.cursor = this.saveCursor();
rgindaa19afe22012-01-25 15:40:22 -0800783 this.savedOptions_.textAttributes = this.screen_.textAttributes.clone();
rginda87b86462011-12-14 13:48:03 -0800784};
785
rginda6d397402012-01-17 10:58:29 -0800786/**
787 * Restore cursor position and attributes.
788 *
789 * TODO(rginda): Restore attributes once we support them.
790 */
rginda87b86462011-12-14 13:48:03 -0800791hterm.Terminal.prototype.restoreOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800792 if (this.savedOptions_.cursor)
793 this.restoreCursor(this.savedOptions_.cursor);
rgindaa19afe22012-01-25 15:40:22 -0800794 if (this.savedOptions_.textAttributes)
795 this.screen_.textAttributes = this.savedOptions_.textAttributes;
rginda8ba33642011-12-14 12:31:31 -0800796};
797
798/**
799 * Interpret a sequence of characters.
800 *
801 * Incomplete escape sequences are buffered until the next call.
802 *
803 * @param {string} str Sequence of characters to interpret or pass through.
804 */
805hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800806 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800807 this.scheduleSyncCursorPosition_();
808};
809
810/**
811 * Take over the given DIV for use as the terminal display.
812 *
813 * @param {HTMLDivElement} div The div to use as the terminal display.
814 */
815hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800816 this.div_ = div;
817
rginda8ba33642011-12-14 12:31:31 -0800818 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -0700819 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
820
rginda0918b652012-04-04 11:26:24 -0700821 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -0800822
rginda9f5222b2012-03-05 11:53:28 -0800823 this.setFontSize(this.prefs_.get('font-size'));
824 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -0800825
David Reveman8f552492012-03-28 12:18:41 -0400826 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
827
rginda8ba33642011-12-14 12:31:31 -0800828 this.document_ = this.scrollPort_.getDocument();
829
rginda8ba33642011-12-14 12:31:31 -0800830 this.cursorNode_ = this.document_.createElement('div');
831 this.cursorNode_.style.cssText =
832 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800833 'top: -99px;' +
834 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -0800835 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
836 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda6d397402012-01-17 10:58:29 -0800837 '-webkit-transition: opacity, background-color 100ms linear;' +
rginda9f5222b2012-03-05 11:53:28 -0800838 'background-color: ' + this.prefs_.get('cursor-color'));
rginda8ba33642011-12-14 12:31:31 -0800839 this.document_.body.appendChild(this.cursorNode_);
840
841 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -0800842
rginda87b86462011-12-14 13:48:03 -0800843 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -0800844 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -0800845};
846
rginda0918b652012-04-04 11:26:24 -0700847/**
848 * Return the HTML document that contains the terminal DOM nodes.
849 */
rginda87b86462011-12-14 13:48:03 -0800850hterm.Terminal.prototype.getDocument = function() {
851 return this.document_;
rginda8ba33642011-12-14 12:31:31 -0800852};
853
854/**
rginda0918b652012-04-04 11:26:24 -0700855 * Focus the terminal.
856 */
857hterm.Terminal.prototype.focus = function() {
858 this.scrollPort_.focus();
859};
860
861/**
rginda8ba33642011-12-14 12:31:31 -0800862 * Return the HTML Element for a given row index.
863 *
864 * This is a method from the RowProvider interface. The ScrollPort uses
865 * it to fetch rows on demand as they are scrolled into view.
866 *
867 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
868 * pairs to conserve memory.
869 *
870 * @param {integer} index The zero-based row index, measured relative to the
871 * start of the scrollback buffer. On-screen rows will always have the
872 * largest indicies.
873 * @return {HTMLElement} The 'x-row' element containing for the requested row.
874 */
875hterm.Terminal.prototype.getRowNode = function(index) {
876 if (index < this.scrollbackRows_.length)
877 return this.scrollbackRows_[index];
878
879 var screenIndex = index - this.scrollbackRows_.length;
880 return this.screen_.rowsArray[screenIndex];
881};
882
883/**
884 * Return the text content for a given range of rows.
885 *
886 * This is a method from the RowProvider interface. The ScrollPort uses
887 * it to fetch text content on demand when the user attempts to copy their
888 * selection to the clipboard.
889 *
890 * @param {integer} start The zero-based row index to start from, measured
891 * relative to the start of the scrollback buffer. On-screen rows will
892 * always have the largest indicies.
893 * @param {integer} end The zero-based row index to end on, measured
894 * relative to the start of the scrollback buffer.
895 * @return {string} A single string containing the text value of the range of
896 * rows. Lines will be newline delimited, with no trailing newline.
897 */
898hterm.Terminal.prototype.getRowsText = function(start, end) {
899 var ary = [];
900 for (var i = start; i < end; i++) {
901 var node = this.getRowNode(i);
902 ary.push(node.textContent);
903 }
904
905 return ary.join('\n');
906};
907
908/**
909 * Return the text content for a given row.
910 *
911 * This is a method from the RowProvider interface. The ScrollPort uses
912 * it to fetch text content on demand when the user attempts to copy their
913 * selection to the clipboard.
914 *
915 * @param {integer} index The zero-based row index to return, measured
916 * relative to the start of the scrollback buffer. On-screen rows will
917 * always have the largest indicies.
918 * @return {string} A string containing the text value of the selected row.
919 */
920hterm.Terminal.prototype.getRowText = function(index) {
921 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -0800922 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -0800923};
924
925/**
926 * Return the total number of rows in the addressable screen and in the
927 * scrollback buffer of this terminal.
928 *
929 * This is a method from the RowProvider interface. The ScrollPort uses
930 * it to compute the size of the scrollbar.
931 *
932 * @return {integer} The number of rows in this terminal.
933 */
934hterm.Terminal.prototype.getRowCount = function() {
935 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
936};
937
938/**
939 * Create DOM nodes for new rows and append them to the end of the terminal.
940 *
941 * This is the only correct way to add a new DOM node for a row. Notice that
942 * the new row is appended to the bottom of the list of rows, and does not
943 * require renumbering (of the rowIndex property) of previous rows.
944 *
945 * If you think you want a new blank row somewhere in the middle of the
946 * terminal, look into moveRows_().
947 *
948 * This method does not pay attention to vtScrollTop/Bottom, since you should
949 * be using moveRows() in cases where they would matter.
950 *
951 * The cursor will be positioned at column 0 of the first inserted line.
952 */
953hterm.Terminal.prototype.appendRows_ = function(count) {
954 var cursorRow = this.screen_.rowsArray.length;
955 var offset = this.scrollbackRows_.length + cursorRow;
956 for (var i = 0; i < count; i++) {
957 var row = this.document_.createElement('x-row');
958 row.appendChild(this.document_.createTextNode(''));
959 row.rowIndex = offset + i;
960 this.screen_.pushRow(row);
961 }
962
963 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
964 if (extraRows > 0) {
965 var ary = this.screen_.shiftRows(extraRows);
966 Array.prototype.push.apply(this.scrollbackRows_, ary);
967 this.scheduleScrollDown_();
968 }
969
970 if (cursorRow >= this.screen_.rowsArray.length)
971 cursorRow = this.screen_.rowsArray.length - 1;
972
rginda87b86462011-12-14 13:48:03 -0800973 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -0800974};
975
976/**
977 * Relocate rows from one part of the addressable screen to another.
978 *
979 * This is used to recycle rows during VT scrolls (those which are driven
980 * by VT commands, rather than by the user manipulating the scrollbar.)
981 *
982 * In this case, the blank lines scrolled into the scroll region are made of
983 * the nodes we scrolled off. These have their rowIndex properties carefully
984 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -0800985 */
986hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
987 var ary = this.screen_.removeRows(fromIndex, count);
988 this.screen_.insertRows(toIndex, ary);
989
990 var start, end;
991 if (fromIndex < toIndex) {
992 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -0800993 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800994 } else {
995 start = toIndex;
rginda87b86462011-12-14 13:48:03 -0800996 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -0800997 }
998
999 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001000 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001001};
1002
1003/**
1004 * Renumber the rowIndex property of the given range of rows.
1005 *
1006 * The start and end indicies are relative to the screen, not the scrollback.
1007 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001008 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001009 * no need to renumber scrollback rows.
1010 */
1011hterm.Terminal.prototype.renumberRows_ = function(start, end) {
1012 var offset = this.scrollbackRows_.length;
1013 for (var i = start; i < end; i++) {
1014 this.screen_.rowsArray[i].rowIndex = offset + i;
1015 }
1016};
1017
1018/**
1019 * Print a string to the terminal.
1020 *
1021 * This respects the current insert and wraparound modes. It will add new lines
1022 * to the end of the terminal, scrolling off the top into the scrollback buffer
1023 * if necessary.
1024 *
1025 * The string is *not* parsed for escape codes. Use the interpret() method if
1026 * that's what you're after.
1027 *
1028 * @param{string} str The string to print.
1029 */
1030hterm.Terminal.prototype.print = function(str) {
rgindaa19afe22012-01-25 15:40:22 -08001031 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
1032 this.newLine();
rginda2312fff2012-01-05 16:20:52 -08001033
rgindaa19afe22012-01-25 15:40:22 -08001034 if (this.options_.insertMode) {
1035 this.screen_.insertString(str);
1036 } else {
1037 this.screen_.overwriteString(str);
1038 }
1039
1040 var overflow = this.screen_.maybeClipCurrentRow();
1041
1042 if (this.options_.wraparound && overflow) {
1043 var lastColumn;
1044
1045 do {
rginda35c456b2012-02-09 17:29:05 -08001046 this.newLine();
1047 lastColumn = overflow.characterLength;
rgindaa19afe22012-01-25 15:40:22 -08001048
1049 if (!this.options_.insertMode)
1050 this.screen_.deleteChars(overflow.characterLength);
1051
1052 this.screen_.prependNodes(overflow);
rgindaa19afe22012-01-25 15:40:22 -08001053
1054 overflow = this.screen_.maybeClipCurrentRow();
1055 } while (overflow);
1056
1057 this.setCursorColumn(lastColumn);
1058 }
rginda8ba33642011-12-14 12:31:31 -08001059
1060 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001061
rginda9f5222b2012-03-05 11:53:28 -08001062 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001063 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001064};
1065
1066/**
rginda87b86462011-12-14 13:48:03 -08001067 * Set the VT scroll region.
1068 *
rginda87b86462011-12-14 13:48:03 -08001069 * This also resets the cursor position to the absolute (0, 0) position, since
1070 * that's what xterm appears to do.
1071 *
1072 * @param {integer} scrollTop The zero-based top of the scroll region.
1073 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1074 * inclusive.
1075 */
1076hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1077 this.vtScrollTop_ = scrollTop;
1078 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001079};
1080
1081/**
rginda8ba33642011-12-14 12:31:31 -08001082 * Return the top row index according to the VT.
1083 *
1084 * This will return 0 unless the terminal has been told to restrict scrolling
1085 * to some lower row. It is used for some VT cursor positioning and scrolling
1086 * commands.
1087 *
1088 * @return {integer} The topmost row in the terminal's scroll region.
1089 */
1090hterm.Terminal.prototype.getVTScrollTop = function() {
1091 if (this.vtScrollTop_ != null)
1092 return this.vtScrollTop_;
1093
1094 return 0;
rginda87b86462011-12-14 13:48:03 -08001095};
rginda8ba33642011-12-14 12:31:31 -08001096
1097/**
1098 * Return the bottom row index according to the VT.
1099 *
1100 * This will return the height of the terminal unless the it has been told to
1101 * restrict scrolling to some higher row. It is used for some VT cursor
1102 * positioning and scrolling commands.
1103 *
1104 * @return {integer} The bottommost row in the terminal's scroll region.
1105 */
1106hterm.Terminal.prototype.getVTScrollBottom = function() {
1107 if (this.vtScrollBottom_ != null)
1108 return this.vtScrollBottom_;
1109
rginda87b86462011-12-14 13:48:03 -08001110 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001111}
1112
1113/**
1114 * Process a '\n' character.
1115 *
1116 * If the cursor is on the final row of the terminal this will append a new
1117 * blank row to the screen and scroll the topmost row into the scrollback
1118 * buffer.
1119 *
1120 * Otherwise, this moves the cursor to column zero of the next row.
1121 */
1122hterm.Terminal.prototype.newLine = function() {
1123 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001124 // If we're at the end of the screen we need to append a new line and
1125 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001126 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001127 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1128 // End of the scroll region does not affect the scrollback buffer.
1129 this.vtScrollUp(1);
1130 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001131 } else {
rginda87b86462011-12-14 13:48:03 -08001132 // Anywhere else in the screen just moves the cursor.
1133 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001134 }
1135};
1136
1137/**
1138 * Like newLine(), except maintain the cursor column.
1139 */
1140hterm.Terminal.prototype.lineFeed = function() {
1141 var column = this.screen_.cursorPosition.column;
1142 this.newLine();
1143 this.setCursorColumn(column);
1144};
1145
1146/**
rginda87b86462011-12-14 13:48:03 -08001147 * If autoCarriageReturn is set then newLine(), else lineFeed().
1148 */
1149hterm.Terminal.prototype.formFeed = function() {
1150 if (this.options_.autoCarriageReturn) {
1151 this.newLine();
1152 } else {
1153 this.lineFeed();
1154 }
1155};
1156
1157/**
1158 * Move the cursor up one row, possibly inserting a blank line.
1159 *
1160 * The cursor column is not changed.
1161 */
1162hterm.Terminal.prototype.reverseLineFeed = function() {
1163 var scrollTop = this.getVTScrollTop();
1164 var currentRow = this.screen_.cursorPosition.row;
1165
1166 if (currentRow == scrollTop) {
1167 this.insertLines(1);
1168 } else {
1169 this.setAbsoluteCursorRow(currentRow - 1);
1170 }
1171};
1172
1173/**
rginda8ba33642011-12-14 12:31:31 -08001174 * Replace all characters to the left of the current cursor with the space
1175 * character.
1176 *
1177 * TODO(rginda): This should probably *remove* the characters (not just replace
1178 * with a space) if there are no characters at or beyond the current cursor
1179 * position. Once it does that, it'll have the same text-attribute related
1180 * issues as hterm.Screen.prototype.clearCursorRow :/
1181 */
1182hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001183 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001184 this.setCursorColumn(0);
rginda87b86462011-12-14 13:48:03 -08001185 this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1));
1186 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001187};
1188
1189/**
1190 * Erase a given number of characters to the right of the cursor, shifting
1191 * remaining characters to the left.
1192 *
1193 * The cursor position is unchanged.
1194 *
1195 * TODO(rginda): Test that this works even when the cursor is positioned beyond
1196 * the end of the text.
1197 *
1198 * TODO(rginda): This likely has text-attribute related troubles similar to the
1199 * todo on hterm.Screen.prototype.clearCursorRow.
1200 */
1201hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001202 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001203
rginda87b86462011-12-14 13:48:03 -08001204 var maxCount = this.screenSize.width - cursor.column;
rginda8ba33642011-12-14 12:31:31 -08001205 var count = (opt_count && opt_count < maxCount) ? opt_count : maxCount;
1206 this.screen_.deleteChars(count);
rginda87b86462011-12-14 13:48:03 -08001207 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001208};
1209
1210/**
1211 * Erase the current line.
1212 *
1213 * The cursor position is unchanged.
1214 *
1215 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1216 * has a text-attribute related TODO.
1217 */
1218hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001219 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001220 this.screen_.clearCursorRow();
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 start of the scroll region to the current
1226 * cursor position.
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.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001234 var cursor = this.saveCursor();
1235
1236 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001237
1238 var top = this.getVTScrollTop();
rginda87b86462011-12-14 13:48:03 -08001239 for (var i = top; i < cursor.row; 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);
rginda8ba33642011-12-14 12:31:31 -08001245};
1246
1247/**
1248 * Erase all characters from the current cursor position to the end of the
1249 * scroll region.
1250 *
1251 * The cursor position is unchanged.
1252 *
1253 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1254 * has a text-attribute related TODO.
1255 */
1256hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001257 var cursor = this.saveCursor();
1258
1259 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001260
1261 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001262 for (var i = cursor.row + 1; i <= bottom; i++) {
1263 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001264 this.screen_.clearCursorRow();
1265 }
1266
rginda87b86462011-12-14 13:48:03 -08001267 this.restoreCursor(cursor);
1268};
1269
1270/**
1271 * Fill the terminal with a given character.
1272 *
1273 * This methods does not respect the VT scroll region.
1274 *
1275 * @param {string} ch The character to use for the fill.
1276 */
1277hterm.Terminal.prototype.fill = function(ch) {
1278 var cursor = this.saveCursor();
1279
1280 this.setAbsoluteCursorPosition(0, 0);
1281 for (var row = 0; row < this.screenSize.height; row++) {
1282 for (var col = 0; col < this.screenSize.width; col++) {
1283 this.setAbsoluteCursorPosition(row, col);
1284 this.screen_.overwriteString(ch);
1285 }
1286 }
1287
1288 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001289};
1290
1291/**
rginda9ea433c2012-03-16 11:57:00 -07001292 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001293 *
rginda9ea433c2012-03-16 11:57:00 -07001294 * This does not respect the scroll region.
1295 *
1296 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1297 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001298 *
1299 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1300 * has a text-attribute related TODO.
1301 */
rginda9ea433c2012-03-16 11:57:00 -07001302hterm.Terminal.prototype.clearHome = function(opt_screen) {
1303 var screen = opt_screen || this.screen_;
1304 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001305
rgindae4d29232012-01-19 10:47:13 -08001306 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001307 screen.setCursorPosition(i, 0);
1308 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001309 }
1310
rginda9ea433c2012-03-16 11:57:00 -07001311 screen.setCursorPosition(0, 0);
1312};
1313
1314/**
1315 * Erase the entire display without changing the cursor position.
1316 *
1317 * The cursor position is unchanged. This does not respect the scroll
1318 * region.
1319 *
1320 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1321 * to the current screen.
1322 *
1323 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1324 * has a text-attribute related TODO.
1325 */
1326hterm.Terminal.prototype.clear = function(opt_screen) {
1327 var screen = opt_screen || this.screen_;
1328 var cursor = screen.cursorPosition.clone();
1329 this.clearHome(screen);
1330 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001331};
1332
1333/**
1334 * VT command to insert lines at the current cursor row.
1335 *
1336 * This respects the current scroll region. Rows pushed off the bottom are
1337 * lost (they won't show up in the scrollback buffer).
1338 *
1339 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1340 * has a text-attribute related TODO.
1341 *
1342 * @param {integer} count The number of lines to insert.
1343 */
1344hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001345 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001346
1347 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001348 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001349
rgindae4d29232012-01-19 10:47:13 -08001350 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001351 if (start != cursor.row)
1352 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001353
1354 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001355 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001356 this.screen_.clearCursorRow();
1357 }
1358
rginda87b86462011-12-14 13:48:03 -08001359 cursor.column = 0;
1360 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001361};
1362
1363/**
1364 * VT command to delete lines at the current cursor row.
1365 *
1366 * New rows are added to the bottom of scroll region to take their place. New
1367 * rows are strictly there to take up space and have no content or style.
1368 */
1369hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001370 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001371
rginda87b86462011-12-14 13:48:03 -08001372 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001373 var bottom = this.getVTScrollBottom();
1374
rginda87b86462011-12-14 13:48:03 -08001375 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001376 count = Math.min(count, maxCount);
1377
rginda87b86462011-12-14 13:48:03 -08001378 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001379 if (count != maxCount)
1380 this.moveRows_(top, count, moveStart);
1381
1382 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001383 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001384 this.screen_.clearCursorRow();
1385 }
1386
rginda87b86462011-12-14 13:48:03 -08001387 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001388};
1389
1390/**
1391 * Inserts the given number of spaces at the current cursor position.
1392 *
rginda87b86462011-12-14 13:48:03 -08001393 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001394 */
1395hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001396 var cursor = this.saveCursor();
1397
rginda0f5c0292012-01-13 11:00:13 -08001398 var ws = hterm.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001399 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001400 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001401
1402 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001403};
1404
1405/**
1406 * Forward-delete the specified number of characters starting at the cursor
1407 * position.
1408 *
1409 * @param {integer} count The number of characters to delete.
1410 */
1411hterm.Terminal.prototype.deleteChars = function(count) {
1412 this.screen_.deleteChars(count);
1413};
1414
1415/**
1416 * Shift rows in the scroll region upwards by a given number of lines.
1417 *
1418 * New rows are inserted at the bottom of the scroll region to fill the
1419 * vacated rows. The new rows not filled out with the current text attributes.
1420 *
1421 * This function does not affect the scrollback rows at all. Rows shifted
1422 * off the top are lost.
1423 *
rginda87b86462011-12-14 13:48:03 -08001424 * The cursor position is not altered.
1425 *
rginda8ba33642011-12-14 12:31:31 -08001426 * @param {integer} count The number of rows to scroll.
1427 */
1428hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001429 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001430
rginda87b86462011-12-14 13:48:03 -08001431 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001432 this.deleteLines(count);
1433
rginda87b86462011-12-14 13:48:03 -08001434 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001435};
1436
1437/**
1438 * Shift rows below the cursor down by a given number of lines.
1439 *
1440 * This function respects the current scroll region.
1441 *
1442 * New rows are inserted at the top of the scroll region to fill the
1443 * vacated rows. The new rows not filled out with the current text attributes.
1444 *
1445 * This function does not affect the scrollback rows at all. Rows shifted
1446 * off the bottom are lost.
1447 *
1448 * @param {integer} count The number of rows to scroll.
1449 */
1450hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001451 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001452
rginda87b86462011-12-14 13:48:03 -08001453 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001454 this.insertLines(opt_count);
1455
rginda87b86462011-12-14 13:48:03 -08001456 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001457};
1458
rginda87b86462011-12-14 13:48:03 -08001459
rginda8ba33642011-12-14 12:31:31 -08001460/**
1461 * Set the cursor position.
1462 *
1463 * The cursor row is relative to the scroll region if the terminal has
1464 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1465 *
1466 * @param {integer} row The new zero-based cursor row.
1467 * @param {integer} row The new zero-based cursor column.
1468 */
1469hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1470 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001471 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001472 } else {
rginda87b86462011-12-14 13:48:03 -08001473 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001474 }
rginda87b86462011-12-14 13:48:03 -08001475};
rginda8ba33642011-12-14 12:31:31 -08001476
rginda87b86462011-12-14 13:48:03 -08001477hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1478 var scrollTop = this.getVTScrollTop();
1479 row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
rginda2312fff2012-01-05 16:20:52 -08001480 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001481 this.screen_.setCursorPosition(row, column);
1482};
1483
1484hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rginda2312fff2012-01-05 16:20:52 -08001485 row = hterm.clamp(row, 0, this.screenSize.height - 1);
1486 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001487 this.screen_.setCursorPosition(row, column);
1488};
1489
1490/**
1491 * Set the cursor column.
1492 *
1493 * @param {integer} column The new zero-based cursor column.
1494 */
1495hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001496 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001497};
1498
1499/**
1500 * Return the cursor column.
1501 *
1502 * @return {integer} The zero-based cursor column.
1503 */
1504hterm.Terminal.prototype.getCursorColumn = function() {
1505 return this.screen_.cursorPosition.column;
1506};
1507
1508/**
1509 * Set the cursor row.
1510 *
1511 * The cursor row is relative to the scroll region if the terminal has
1512 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1513 *
1514 * @param {integer} row The new cursor row.
1515 */
rginda87b86462011-12-14 13:48:03 -08001516hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1517 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001518};
1519
1520/**
1521 * Return the cursor row.
1522 *
1523 * @return {integer} The zero-based cursor row.
1524 */
1525hterm.Terminal.prototype.getCursorRow = function(row) {
1526 return this.screen_.cursorPosition.row;
1527};
1528
1529/**
1530 * Request that the ScrollPort redraw itself soon.
1531 *
1532 * The redraw will happen asynchronously, soon after the call stack winds down.
1533 * Multiple calls will be coalesced into a single redraw.
1534 */
1535hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001536 if (this.timeouts_.redraw)
1537 return;
rginda8ba33642011-12-14 12:31:31 -08001538
1539 var self = this;
rginda87b86462011-12-14 13:48:03 -08001540 this.timeouts_.redraw = setTimeout(function() {
1541 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001542 self.scrollPort_.redraw_();
1543 }, 0);
1544};
1545
1546/**
1547 * Request that the ScrollPort be scrolled to the bottom.
1548 *
1549 * The scroll will happen asynchronously, soon after the call stack winds down.
1550 * Multiple calls will be coalesced into a single scroll.
1551 *
1552 * This affects the scrollbar position of the ScrollPort, and has nothing to
1553 * do with the VT scroll commands.
1554 */
1555hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1556 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001557 return;
rginda8ba33642011-12-14 12:31:31 -08001558
1559 var self = this;
1560 this.timeouts_.scrollDown = setTimeout(function() {
1561 delete self.timeouts_.scrollDown;
1562 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1563 }, 10);
1564};
1565
1566/**
1567 * Move the cursor up a specified number of rows.
1568 *
1569 * @param {integer} count The number of rows to move the cursor.
1570 */
1571hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001572 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001573};
1574
1575/**
1576 * Move the cursor down a specified number of rows.
1577 *
1578 * @param {integer} count The number of rows to move the cursor.
1579 */
1580hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001581 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001582 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1583 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1584 this.screenSize.height - 1);
1585
1586 var row = hterm.clamp(this.screen_.cursorPosition.row + count,
1587 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001588 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001589};
1590
1591/**
1592 * Move the cursor left a specified number of columns.
1593 *
1594 * @param {integer} count The number of columns to move the cursor.
1595 */
1596hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001597 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001598};
1599
1600/**
1601 * Move the cursor right a specified number of columns.
1602 *
1603 * @param {integer} count The number of columns to move the cursor.
1604 */
1605hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001606 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001607 var column = hterm.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001608 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001609 this.setCursorColumn(column);
1610};
1611
1612/**
1613 * Reverse the foreground and background colors of the terminal.
1614 *
1615 * This only affects text that was drawn with no attributes.
1616 *
1617 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1618 * been drawn with attributes that happen to coincide with the default
1619 * 'no-attribute' colors. My guess is probably not.
1620 */
1621hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001622 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001623 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001624 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1625 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001626 } else {
rginda9f5222b2012-03-05 11:53:28 -08001627 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1628 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001629 }
1630};
1631
1632/**
rginda87b86462011-12-14 13:48:03 -08001633 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001634 */
1635hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001636 if (this.bellAudio_.getAttribute('src'))
1637 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001638
rginda6d397402012-01-17 10:58:29 -08001639 this.cursorNode_.style.backgroundColor =
1640 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001641
1642 var self = this;
1643 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001644 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001645 }, 200);
rginda87b86462011-12-14 13:48:03 -08001646};
1647
1648/**
rginda8ba33642011-12-14 12:31:31 -08001649 * Set the origin mode bit.
1650 *
1651 * If origin mode is on, certain VT cursor and scrolling commands measure their
1652 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1653 * to the top of the addressable screen.
1654 *
1655 * Defaults to off.
1656 *
1657 * @param {boolean} state True to set origin mode, false to unset.
1658 */
1659hterm.Terminal.prototype.setOriginMode = function(state) {
1660 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001661 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001662};
1663
1664/**
1665 * Set the insert mode bit.
1666 *
1667 * If insert mode is on, existing text beyond the cursor position will be
1668 * shifted right to make room for new text. Otherwise, new text overwrites
1669 * any existing text.
1670 *
1671 * Defaults to off.
1672 *
1673 * @param {boolean} state True to set insert mode, false to unset.
1674 */
1675hterm.Terminal.prototype.setInsertMode = function(state) {
1676 this.options_.insertMode = state;
1677};
1678
1679/**
rginda87b86462011-12-14 13:48:03 -08001680 * Set the auto carriage return bit.
1681 *
1682 * If auto carriage return is on then a formfeed character is interpreted
1683 * as a newline, otherwise it's the same as a linefeed. The difference boils
1684 * down to whether or not the cursor column is reset.
1685 */
1686hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1687 this.options_.autoCarriageReturn = state;
1688};
1689
1690/**
rginda8ba33642011-12-14 12:31:31 -08001691 * Set the wraparound mode bit.
1692 *
1693 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1694 * to the start of the following row. Otherwise, the cursor is clamped to the
1695 * end of the screen and attempts to write past it are ignored.
1696 *
1697 * Defaults to on.
1698 *
1699 * @param {boolean} state True to set wraparound mode, false to unset.
1700 */
1701hterm.Terminal.prototype.setWraparound = function(state) {
1702 this.options_.wraparound = state;
1703};
1704
1705/**
1706 * Set the reverse-wraparound mode bit.
1707 *
1708 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1709 * to the end of the previous row. Otherwise, the cursor is clamped to column
1710 * 0.
1711 *
1712 * Defaults to off.
1713 *
1714 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1715 */
1716hterm.Terminal.prototype.setReverseWraparound = function(state) {
1717 this.options_.reverseWraparound = state;
1718};
1719
1720/**
1721 * Selects between the primary and alternate screens.
1722 *
1723 * If alternate mode is on, the alternate screen is active. Otherwise the
1724 * primary screen is active.
1725 *
1726 * Swapping screens has no effect on the scrollback buffer.
1727 *
1728 * Each screen maintains its own cursor position.
1729 *
1730 * Defaults to off.
1731 *
1732 * @param {boolean} state True to set alternate mode, false to unset.
1733 */
1734hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001735 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001736 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1737
rginda35c456b2012-02-09 17:29:05 -08001738 if (this.screen_.rowsArray.length &&
1739 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
1740 // If the screen changed sizes while we were away, our rowIndexes may
1741 // be incorrect.
1742 var offset = this.scrollbackRows_.length;
1743 var ary = this.screen_.rowsArray;
1744 for (i = 0; i < ary.length; i++) {
1745 ary[i].rowIndex = offset + i;
1746 }
1747 }
rginda8ba33642011-12-14 12:31:31 -08001748
rginda35c456b2012-02-09 17:29:05 -08001749 this.realizeWidth_(this.screenSize.width);
1750 this.realizeHeight_(this.screenSize.height);
1751 this.scrollPort_.syncScrollHeight();
1752 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001753
rginda6d397402012-01-17 10:58:29 -08001754 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08001755 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08001756};
1757
1758/**
1759 * Set the cursor-blink mode bit.
1760 *
1761 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1762 * a visible cursor does not blink.
1763 *
1764 * You should make sure to turn blinking off if you're going to dispose of a
1765 * terminal, otherwise you'll leak a timeout.
1766 *
1767 * Defaults to on.
1768 *
1769 * @param {boolean} state True to set cursor-blink mode, false to unset.
1770 */
1771hterm.Terminal.prototype.setCursorBlink = function(state) {
1772 this.options_.cursorBlink = state;
1773
1774 if (!state && this.timeouts_.cursorBlink) {
1775 clearTimeout(this.timeouts_.cursorBlink);
1776 delete this.timeouts_.cursorBlink;
1777 }
1778
1779 if (this.options_.cursorVisible)
1780 this.setCursorVisible(true);
1781};
1782
1783/**
1784 * Set the cursor-visible mode bit.
1785 *
1786 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1787 *
1788 * Defaults to on.
1789 *
1790 * @param {boolean} state True to set cursor-visible mode, false to unset.
1791 */
1792hterm.Terminal.prototype.setCursorVisible = function(state) {
1793 this.options_.cursorVisible = state;
1794
1795 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001796 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001797 return;
1798 }
1799
rginda87b86462011-12-14 13:48:03 -08001800 this.syncCursorPosition_();
1801
1802 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001803
1804 if (this.options_.cursorBlink) {
1805 if (this.timeouts_.cursorBlink)
1806 return;
1807
1808 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1809 500);
1810 } else {
1811 if (this.timeouts_.cursorBlink) {
1812 clearTimeout(this.timeouts_.cursorBlink);
1813 delete this.timeouts_.cursorBlink;
1814 }
1815 }
1816};
1817
1818/**
rginda87b86462011-12-14 13:48:03 -08001819 * Synchronizes the visible cursor and document selection with the current
1820 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08001821 */
1822hterm.Terminal.prototype.syncCursorPosition_ = function() {
1823 var topRowIndex = this.scrollPort_.getTopRowIndex();
1824 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
1825 var cursorRowIndex = this.scrollbackRows_.length +
1826 this.screen_.cursorPosition.row;
1827
1828 if (cursorRowIndex > bottomRowIndex) {
1829 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08001830 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08001831 return;
1832 }
1833
rginda35c456b2012-02-09 17:29:05 -08001834 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
1835 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
1836
rginda8ba33642011-12-14 12:31:31 -08001837 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08001838 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
1839 'px';
1840 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
1841 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08001842
1843 this.cursorNode_.setAttribute('title',
1844 '(' + this.screen_.cursorPosition.row +
1845 ', ' + this.screen_.cursorPosition.column +
1846 ')');
1847
1848 // Update the caret for a11y purposes.
1849 var selection = this.document_.getSelection();
1850 if (selection && selection.isCollapsed)
1851 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08001852};
1853
1854/**
1855 * Synchronizes the visible cursor with the current cursor coordinates.
1856 *
1857 * The sync will happen asynchronously, soon after the call stack winds down.
1858 * Multiple calls will be coalesced into a single sync.
1859 */
1860hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
1861 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08001862 return;
rginda8ba33642011-12-14 12:31:31 -08001863
1864 var self = this;
1865 this.timeouts_.syncCursor = setTimeout(function() {
1866 self.syncCursorPosition_();
1867 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08001868 }, 0);
1869};
1870
rgindacc2996c2012-02-24 14:59:31 -08001871/**
rgindaf522ce02012-04-17 17:49:17 -07001872 * Show or hide the zoom warning.
1873 *
1874 * The zoom warning is a message warning the user that their browser zoom must
1875 * be set to 100% in order for hterm to function properly.
1876 *
1877 * @param {boolean} state True to show the message, false to hide it.
1878 */
1879hterm.Terminal.prototype.showZoomWarning_ = function(state) {
1880 if (!this.zoomWarningNode_) {
1881 if (!state)
1882 return;
1883
1884 this.zoomWarningNode_ = this.document_.createElement('div');
1885 this.zoomWarningNode_.style.cssText = (
1886 'color: black;' +
1887 'background-color: #ff2222;' +
1888 'font-size: large;' +
1889 'border-radius: 8px;' +
1890 'opacity: 0.75;' +
1891 'padding: 0.2em 0.5em 0.2em 0.5em;' +
1892 'top: 0.5em;' +
1893 'right: 1.2em;' +
1894 'position: absolute;' +
1895 '-webkit-text-size-adjust: none;' +
1896 '-webkit-user-select: none;');
1897
1898 this.zoomWarningNode_.textContent = hterm.msg('ZOOM_WARNING') ||
1899 ('!! ' + parseInt(this.scrollPort_.characterSize.zoomFactor * 100) +
1900 '% !!');
1901 }
1902
1903 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
1904
1905 if (state) {
1906 if (!this.zoomWarningNode_.parentNode)
1907 this.div_.parentNode.appendChild(this.zoomWarningNode_);
1908 } else if (this.zoomWarningNode_.parentNode) {
1909 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
1910 }
1911};
1912
1913/**
rgindacc2996c2012-02-24 14:59:31 -08001914 * Show the terminal overlay for a given amount of time.
1915 *
1916 * The terminal overlay appears in inverse video in a large font, centered
1917 * over the terminal. You should probably keep the overlay message brief,
1918 * since it's in a large font and you probably aren't going to check the size
1919 * of the terminal first.
1920 *
1921 * @param {string} msg The text (not HTML) message to display in the overlay.
1922 * @param {number} opt_timeout The amount of time to wait before fading out
1923 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
1924 * stay up forever (or until the next overlay).
1925 */
1926hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08001927 if (!this.overlayNode_) {
1928 if (!this.div_)
1929 return;
1930
1931 this.overlayNode_ = this.document_.createElement('div');
1932 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08001933 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08001934 'font-size: xx-large;' +
1935 'opacity: 0.75;' +
1936 'padding: 0.2em 0.5em 0.2em 0.5em;' +
1937 'position: absolute;' +
1938 '-webkit-user-select: none;' +
1939 '-webkit-transition: opacity 180ms ease-in;');
1940 }
1941
rginda9f5222b2012-03-05 11:53:28 -08001942 this.overlayNode_.style.color = this.prefs_.get('background-color');
1943 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
1944 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
1945
rgindaf0090c92012-02-10 14:58:52 -08001946 this.overlayNode_.textContent = msg;
1947 this.overlayNode_.style.opacity = '0.75';
1948
1949 if (!this.overlayNode_.parentNode)
1950 this.div_.appendChild(this.overlayNode_);
1951
1952 this.overlayNode_.style.top = (
1953 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
1954 this.overlayNode_.style.left = (
1955 this.div_.clientWidth - this.overlayNode_.clientWidth -
1956 this.scrollbarWidthPx) / 2;
1957
1958 var self = this;
1959
1960 if (this.overlayTimeout_)
1961 clearTimeout(this.overlayTimeout_);
1962
rgindacc2996c2012-02-24 14:59:31 -08001963 if (opt_timeout === null)
1964 return;
1965
rgindaf0090c92012-02-10 14:58:52 -08001966 this.overlayTimeout_ = setTimeout(function() {
1967 self.overlayNode_.style.opacity = '0';
1968 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07001969 if (self.overlayNode_.parentNode)
1970 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08001971 self.overlayTimeout_ = null;
1972 self.overlayNode_.style.opacity = '0.75';
1973 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08001974 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08001975};
1976
1977hterm.Terminal.prototype.overlaySize = function() {
1978 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
1979};
1980
rginda87b86462011-12-14 13:48:03 -08001981/**
1982 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
1983 *
1984 * @param {string} string The VT string representing the keystroke.
1985 */
1986hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08001987 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08001988 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1989
1990 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08001991};
1992
1993/**
1994 * React when the ScrollPort is scrolled.
1995 */
1996hterm.Terminal.prototype.onScroll_ = function() {
1997 this.scheduleSyncCursorPosition_();
1998};
1999
2000/**
rginda9846e2f2012-01-27 13:53:33 -08002001 * React when text is pasted into the scrollPort.
2002 */
2003hterm.Terminal.prototype.onPaste_ = function(e) {
2004 this.io.onVTKeystroke(e.text);
2005};
2006
2007/**
rginda8ba33642011-12-14 12:31:31 -08002008 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002009 *
2010 * Note: This function should not directly contain code that alters the internal
2011 * state of the terminal. That kind of code belongs in realizeWidth or
2012 * realizeHeight, so that it can be executed synchronously in the case of a
2013 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002014 */
2015hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002016 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002017 this.scrollPort_.characterSize.width);
2018 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2019 this.scrollPort_.characterSize.height);
2020
2021 if (!(columnCount || rowCount)) {
2022 // We avoid these situations since they happen sometimes when the terminal
2023 // gets removed from the document, and we can't deal with that.
2024 return;
2025 }
2026
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002027 this.realizeSize_(columnCount, rowCount);
rgindac9bc5502012-01-18 11:48:44 -08002028 this.scheduleSyncCursorPosition_();
rgindaf522ce02012-04-17 17:49:17 -07002029 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaf0090c92012-02-10 14:58:52 -08002030 this.overlaySize();
rginda8ba33642011-12-14 12:31:31 -08002031};
2032
2033/**
2034 * Service the cursor blink timeout.
2035 */
2036hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002037 if (this.cursorNode_.style.opacity == '0') {
2038 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002039 } else {
rginda87b86462011-12-14 13:48:03 -08002040 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002041 }
2042};
David Reveman8f552492012-03-28 12:18:41 -04002043
2044/**
2045 * Set the scrollbar-visible mode bit.
2046 *
2047 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2048 * Otherwise it will not.
2049 *
2050 * Defaults to on.
2051 *
2052 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2053 */
2054hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2055 this.scrollPort_.setScrollbarVisible(state);
2056};