blob: a321a77d27ade9ab4fbd7dbd609ff23ef1dc6e80 [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
David Benjamin66e954d2012-05-05 21:08:12 -040062 // Keep track of whether default tab stops have been erased; after a TBC
63 // clears all tab stops, defaults aren't restored on resize until a reset.
64 this.defaultTabStops = true;
65
rginda8ba33642011-12-14 12:31:31 -080066 // The VT's notion of the top and bottom rows. Used during some VT
67 // cursor positioning and scrolling commands.
68 this.vtScrollTop_ = null;
69 this.vtScrollBottom_ = null;
70
71 // The DIV element for the visible cursor.
72 this.cursorNode_ = null;
73
rginda9f5222b2012-03-05 11:53:28 -080074 // These prefs are cached so we don't have to read from local storage with
75 // each output and keystroke.
76 this.scrollOnOutput_ = this.prefs_.get('scroll-on-output');
77 this.scrollOnKeystroke_ = this.prefs_.get('scroll-on-keystroke');
78
rgindaf0090c92012-02-10 14:58:52 -080079 // Terminal bell sound.
80 this.bellAudio_ = this.document_.createElement('audio');
rginda9f5222b2012-03-05 11:53:28 -080081 this.bellAudio_.setAttribute('src', this.prefs_.get('audible-bell-sound'));
rgindaf0090c92012-02-10 14:58:52 -080082 this.bellAudio_.setAttribute('preload', 'auto');
83
rginda6d397402012-01-17 10:58:29 -080084 // Cursor position and attributes saved with DECSC.
85 this.savedOptions_ = {};
86
rginda8ba33642011-12-14 12:31:31 -080087 // The current mode bits for the terminal.
88 this.options_ = new hterm.Options();
89
90 // Timeouts we might need to clear.
91 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -080092
93 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -080094 this.vt = new hterm.VT(this);
rginda11057d52012-04-25 12:29:56 -070095 this.vt.enable8BitControl = this.prefs_.get('enable-8-bit-control');
96 this.vt.maxStringSequence = this.prefs_.get('max-string-sequence');
rginda87b86462011-12-14 13:48:03 -080097
rgindafeaf3142012-01-31 15:14:20 -080098 // The keyboard hander.
99 this.keyboard = new hterm.Keyboard(this);
100
rginda87b86462011-12-14 13:48:03 -0800101 // General IO interface that can be given to third parties without exposing
102 // the entire terminal object.
103 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800104
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400105 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800106 this.setDefaultTabStops();
rginda87b86462011-12-14 13:48:03 -0800107};
108
109/**
rginda35c456b2012-02-09 17:29:05 -0800110 * Default tab with of 8 to match xterm.
111 */
112hterm.Terminal.prototype.tabWidth = 8;
113
114/**
rginda35c456b2012-02-09 17:29:05 -0800115 * The assumed width of a scrollbar.
116 */
117hterm.Terminal.prototype.scrollbarWidthPx = 16;
118
119/**
rginda9f5222b2012-03-05 11:53:28 -0800120 * Select a preference profile.
121 *
122 * This will load the terminal preferences for the given profile name and
123 * associate subsequent preference changes with the new preference profile.
124 *
125 * @param {string} newName The name of the preference profile. Forward slash
126 * characters will be removed from the name.
127 */
128hterm.Terminal.prototype.setProfile = function(profileName) {
129 // If we already have a profile selected, we're going to need to re-sync
130 // with the new profile.
131 var needSync = !!this.profileName_;
132
133 this.profileName_ = profileName.replace(/\//g, '');
134
135 this.prefs_ = new hterm.PreferenceManager(
136 '/hterm/prefs/profiles/' + this.profileName_);
137
138 var self = this;
139 this.prefs_.definePreferences
rginda30f20f62012-04-05 16:36:19 -0700140 ([
141 /**
142 * Set whether the alt key acts as a meta key or as a distinct alt key.
rginda9f5222b2012-03-05 11:53:28 -0800143 */
rginda30f20f62012-04-05 16:36:19 -0700144 ['alt-is-meta', false, function(v) {
rgindaf9c36852012-05-09 11:08:39 -0700145 self.keyboard.altIsMeta = v;
rginda9f5222b2012-03-05 11:53:28 -0800146 }
147 ],
148
rginda30f20f62012-04-05 16:36:19 -0700149 /**
rginda39bdf6f2012-04-10 16:50:55 -0700150 * Controls how the alt key is handled.
151 *
152 * escape....... Send an ESC prefix.
153 * 8-bit........ Add 128 to the unshifted character as in xterm.
154 * browser-key.. Wait for the keypress event and see what the browser says.
155 * (This won't work well on platforms where the browser
156 * performs a default action for some alt sequences.)
rginda30f20f62012-04-05 16:36:19 -0700157 */
rginda39bdf6f2012-04-10 16:50:55 -0700158 ['alt-sends-what', 'escape', function(v) {
159 if (!/^(escape|8-bit|browser-key)$/.test(v))
160 v = 'escape';
161
rgindaf9c36852012-05-09 11:08:39 -0700162 self.keyboard.altSendsWhat = v;
rginda30f20f62012-04-05 16:36:19 -0700163 }
164 ],
165
166 /**
167 * Terminal bell sound. Empty string for no audible bell.
168 */
169 ['audible-bell-sound', '../audio/bell.ogg', function(v) {
170 self.bellAudio_.setAttribute('src', v);
171 }
172 ],
173
174 /**
175 * The background color for text with no other color attributes.
176 */
177 ['background-color', 'rgb(16, 16, 16)', function(v) {
rginda9f5222b2012-03-05 11:53:28 -0800178 self.scrollPort_.setBackgroundColor(v);
179 }
180 ],
181
182 /**
rginda30f20f62012-04-05 16:36:19 -0700183 * The background image.
184 *
185 * Defaults to a subtle light-to-transparent-to-dark gradient that is
186 * mostly transparent.
187 */
188 ['background-image',
189 ('-webkit-linear-gradient(bottom, ' +
190 'rgba(0,0,0,0.01) 0%, ' +
191 'rgba(0,0,0,0) 30%, ' +
192 'rgba(255,255,255,0) 70%, ' +
193 'rgba(255,255,255,0.05) 100%)'),
194 function(v) {
195 self.scrollPort_.setBackgroundImage(v);
196 }
197 ],
198
199 /**
Philip Douglass959b49d2012-05-30 13:29:29 -0400200 * The background image size,
201 *
202 * Defaults to none.
203 */
204 ['background-size', '', function(v) {
205 self.scrollPort_.setBackgroundSize(v);
206 }
207 ],
208
209 /**
210 * The background image position,
211 *
212 * Defaults to none.
213 */
214 ['background-position', '', function(v) {
215 self.scrollPort_.setBackgroundPosition(v);
216 }
217 ],
218
219 /**
rginda30f20f62012-04-05 16:36:19 -0700220 * If true, the backspace should send BS ('\x08', aka ^H). Otherwise
221 * the backspace key should send '\x7f'.
222 */
223 ['backspace-sends-backspace', false, function(v) {
224 self.keyboard.backspaceSendsBackspace = v;
225 }
226 ],
227
228 /**
rgindade84e382012-04-20 15:39:31 -0700229 * Whether or not to blink the cursor by default.
230 */
231 ['cursor-blink', false, function(v) {
232 self.setCursorBlink(!!v);
233 }
234 ],
235
236 /**
rginda30f20f62012-04-05 16:36:19 -0700237 * The color of the visible cursor.
238 */
239 ['cursor-color', 'rgba(255,0,0,0.5)', function(v) {
240 self.cursorNode_.style.backgroundColor = v;
241 }
242 ],
243
244 /**
rginda11057d52012-04-25 12:29:56 -0700245 * True to enable 8-bit control characters, false to ignore them.
246 *
247 * We'll respect the two-byte versions of these control characters
248 * regardless of this setting.
249 */
250 ['enable-8-bit-control', false, function(v) {
251 self.vt.enable8BitControl = !!v;
252 }
253 ],
254
255 /**
rginda30f20f62012-04-05 16:36:19 -0700256 * True if we should use bold weight font for text with the bold/bright
257 * attribute. False to use bright colors only. Null to autodetect.
258 */
259 ['enable-bold', null, function(v) {
260 self.syncBoldSafeState();
261 }
262 ],
263
264 /**
rginda9f5222b2012-03-05 11:53:28 -0800265 * Default font family for the terminal text.
266 */
267 ['font-family', ('"DejaVu Sans Mono", "Everson Mono", ' +
268 'FreeMono, "Menlo", "Lucida Console", ' +
269 'monospace'),
270 function(v) { self.syncFontFamily() }
271 ],
272
273 /**
rginda30f20f62012-04-05 16:36:19 -0700274 * The default font size in pixels.
275 */
276 ['font-size', 15, function(v) {
277 self.setFontSize(v);
278 }
279 ],
280
281 /**
rginda9f5222b2012-03-05 11:53:28 -0800282 * Anti-aliasing.
283 */
284 ['font-smoothing', 'antialiased',
285 function(v) { self.syncFontFamily() }
286 ],
287
288 /**
rginda30f20f62012-04-05 16:36:19 -0700289 * The foreground color for text with no other color attributes.
rginda9f5222b2012-03-05 11:53:28 -0800290 */
rginda30f20f62012-04-05 16:36:19 -0700291 ['foreground-color', 'rgb(240, 240, 240)', function(v) {
292 self.scrollPort_.setForegroundColor(v);
rginda9f5222b2012-03-05 11:53:28 -0800293 }
294 ],
295
296 /**
rginda30f20f62012-04-05 16:36:19 -0700297 * If true, home/end will control the terminal scrollbar and shift home/end
298 * will send the VT keycodes. If false then home/end sends VT codes and
299 * shift home/end scrolls.
rginda9f5222b2012-03-05 11:53:28 -0800300 */
rginda30f20f62012-04-05 16:36:19 -0700301 ['home-keys-scroll', false, function(v) {
302 self.keyboard.homeKeysScroll = v;
303 }
304 ],
305
306 /**
rginda11057d52012-04-25 12:29:56 -0700307 * Max length of a DCS, OSC, PM, or APS sequence before we give up and
308 * ignore the code.
309 */
310 ['max-string-sequence', 1024, function(v) {
311 self.vt.maxStringSequence = v;
312 }
313 ],
314
315 /**
rginda30f20f62012-04-05 16:36:19 -0700316 * Set whether the meta key sends a leading escape or not.
317 */
318 ['meta-sends-escape', true, function(v) {
319 self.keyboard.metaSendsEscape = v;
rginda9f5222b2012-03-05 11:53:28 -0800320 }
321 ],
322
323 /**
324 * If true, scroll to the bottom on any keystroke.
325 */
326 ['scroll-on-keystroke', true, function(v) {
327 self.scrollOnKeystroke_ = v;
328 }
329 ],
330
331 /**
332 * If true, scroll to the bottom on terminal output.
333 */
334 ['scroll-on-output', false, function(v) {
335 self.scrollOnOutput_ = v;
336 }
337 ],
338
339 /**
David Reveman8f552492012-03-28 12:18:41 -0400340 * The vertical scrollbar mode.
341 */
342 ['scrollbar-visible', true, function(v) {
343 self.setScrollbarVisible(v);
344 }
345 ],
rginda30f20f62012-04-05 16:36:19 -0700346
347 /**
rgindaf522ce02012-04-17 17:49:17 -0700348 * The default environment variables.
349 */
350 ['environment', {TERM: 'xterm-256color'}, null],
351
352 /**
rginda30f20f62012-04-05 16:36:19 -0700353 * If true, page up/down will control the terminal scrollbar and shift
354 * page up/down will send the VT keycodes. If false then page up/down
355 * sends VT codes and shift page up/down scrolls.
356 */
357 ['page-keys-scroll', false, function(v) {
358 self.keyboard.pageKeysScroll = v;
359 }
360 ],
361
rginda9f5222b2012-03-05 11:53:28 -0800362 ]);
363
364 if (needSync)
365 this.prefs_.notifyAll();
366};
367
368/**
369 * Return the current terminal background color.
370 *
371 * Intended for use by other classes, so we don't have to expose the entire
372 * prefs_ object.
373 */
374hterm.Terminal.prototype.getBackgroundColor = function() {
375 return this.prefs_.get('background-color');
376};
377
378/**
379 * Return the current terminal foreground color.
380 *
381 * Intended for use by other classes, so we don't have to expose the entire
382 * prefs_ object.
383 */
384hterm.Terminal.prototype.getForegroundColor = function() {
385 return this.prefs_.get('foreground-color');
386};
387
388/**
rginda87b86462011-12-14 13:48:03 -0800389 * Create a new instance of a terminal command and run it with a given
390 * argument string.
391 *
392 * @param {function} commandClass The constructor for a terminal command.
393 * @param {string} argString The argument string to pass to the command.
394 */
395hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700396 var environment = this.prefs_.get('environment');
397 if (typeof environment != 'object' || environment == null)
398 environment = {};
399
rginda87b86462011-12-14 13:48:03 -0800400 var self = this;
401 this.command = new commandClass(
402 { argString: argString || '',
403 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700404 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800405 onExit: function(code) {
406 self.io.pop();
407 self.io.println(hterm.msg('COMMAND_COMPLETE',
408 [self.command.commandName, code]));
rgindafeaf3142012-01-31 15:14:20 -0800409 self.uninstallKeyboard();
rginda87b86462011-12-14 13:48:03 -0800410 }
411 });
412
rgindafeaf3142012-01-31 15:14:20 -0800413 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800414 this.command.run();
415};
416
417/**
rgindafeaf3142012-01-31 15:14:20 -0800418 * Returns true if the current screen is the primary screen, false otherwise.
419 */
420hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700421 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800422};
423
424/**
425 * Install the keyboard handler for this terminal.
426 *
427 * This will prevent the browser from seeing any keystrokes sent to the
428 * terminal.
429 */
430hterm.Terminal.prototype.installKeyboard = function() {
431 this.keyboard.installKeyboard(this.document_.body.firstChild);
432}
433
434/**
435 * Uninstall the keyboard handler for this terminal.
436 */
437hterm.Terminal.prototype.uninstallKeyboard = function() {
438 this.keyboard.installKeyboard(null);
439}
440
441/**
rginda35c456b2012-02-09 17:29:05 -0800442 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800443 *
444 * Call setFontSize(0) to reset to the default font size.
445 *
446 * This function does not modify the font-size preference.
447 *
448 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800449 */
450hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800451 if (px === 0)
452 px = this.prefs_.get('font-size');
453
rginda35c456b2012-02-09 17:29:05 -0800454 this.scrollPort_.setFontSize(px);
455};
456
457/**
458 * Get the current font size.
459 */
460hterm.Terminal.prototype.getFontSize = function() {
461 return this.scrollPort_.getFontSize();
462};
463
464/**
465 * Set the CSS "font-family" for this terminal.
466 */
rginda9f5222b2012-03-05 11:53:28 -0800467hterm.Terminal.prototype.syncFontFamily = function() {
468 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
469 this.prefs_.get('font-smoothing'));
470 this.syncBoldSafeState();
471};
472
473hterm.Terminal.prototype.syncBoldSafeState = function() {
474 var enableBold = this.prefs_.get('enable-bold');
475 if (enableBold !== null) {
476 this.screen_.textAttributes.enableBold = enableBold;
477 return;
478 }
479
rgindaf7521392012-02-28 17:20:34 -0800480 var normalSize = this.scrollPort_.measureCharacterSize();
481 var boldSize = this.scrollPort_.measureCharacterSize('bold');
482
483 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800484 if (!isBoldSafe) {
485 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700486 'from normal. Font family is: ' +
487 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800488 }
rginda9f5222b2012-03-05 11:53:28 -0800489
490 this.screen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800491};
492
493/**
rginda87b86462011-12-14 13:48:03 -0800494 * Return a copy of the current cursor position.
495 *
496 * @return {hterm.RowCol} The RowCol object representing the current position.
497 */
498hterm.Terminal.prototype.saveCursor = function() {
499 return this.screen_.cursorPosition.clone();
500};
501
rgindaa19afe22012-01-25 15:40:22 -0800502hterm.Terminal.prototype.getTextAttributes = function() {
503 return this.screen_.textAttributes;
504};
505
rginda87b86462011-12-14 13:48:03 -0800506/**
rgindaf522ce02012-04-17 17:49:17 -0700507 * Return the current browser zoom factor applied to the terminal.
508 *
509 * @return {number} The current browser zoom factor.
510 */
511hterm.Terminal.prototype.getZoomFactor = function() {
512 return this.scrollPort_.characterSize.zoomFactor;
513};
514
515/**
rginda9846e2f2012-01-27 13:53:33 -0800516 * Change the title of this terminal's window.
517 */
518hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800519 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800520};
521
522/**
rginda87b86462011-12-14 13:48:03 -0800523 * Restore a previously saved cursor position.
524 *
525 * @param {hterm.RowCol} cursor The position to restore.
526 */
527hterm.Terminal.prototype.restoreCursor = function(cursor) {
rginda35c456b2012-02-09 17:29:05 -0800528 var row = hterm.clamp(cursor.row, 0, this.screenSize.height - 1);
529 var column = hterm.clamp(cursor.column, 0, this.screenSize.width - 1);
530 this.screen_.setCursorPosition(row, column);
531 if (cursor.column > column ||
532 cursor.column == column && cursor.overflow) {
533 this.screen_.cursorPosition.overflow = true;
534 }
rginda87b86462011-12-14 13:48:03 -0800535};
536
537/**
538 * Set the width of the terminal, resizing the UI to match.
539 */
540hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800541 if (columnCount == null) {
542 this.div_.style.width = '100%';
543 return;
544 }
545
rginda35c456b2012-02-09 17:29:05 -0800546 this.div_.style.width = this.scrollPort_.characterSize.width *
547 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400548 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800549 this.scheduleSyncCursorPosition_();
550};
rginda87b86462011-12-14 13:48:03 -0800551
rgindac9bc5502012-01-18 11:48:44 -0800552/**
rginda35c456b2012-02-09 17:29:05 -0800553 * Set the height of the terminal, resizing the UI to match.
554 */
555hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800556 if (rowCount == null) {
557 this.div_.style.height = '100%';
558 return;
559 }
560
rginda35c456b2012-02-09 17:29:05 -0800561 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700562 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800563 this.realizeSize_(this.screenSize.width, rowCount);
564 this.scheduleSyncCursorPosition_();
565};
566
567/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400568 * Deal with terminal size changes.
569 *
570 */
571hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
572 if (columnCount != this.screenSize.width)
573 this.realizeWidth_(columnCount);
574
575 if (rowCount != this.screenSize.height)
576 this.realizeHeight_(rowCount);
577
578 // Send new terminal size to plugin.
579 this.io.onTerminalResize(columnCount, rowCount);
580};
581
582/**
rgindac9bc5502012-01-18 11:48:44 -0800583 * Deal with terminal width changes.
584 *
585 * This function does what needs to be done when the terminal width changes
586 * out from under us. It happens here rather than in onResize_() because this
587 * code may need to run synchronously to handle programmatic changes of
588 * terminal width.
589 *
590 * Relying on the browser to send us an async resize event means we may not be
591 * in the correct state yet when the next escape sequence hits.
592 */
593hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
594 var deltaColumns = columnCount - this.screen_.getWidth();
595
rginda87b86462011-12-14 13:48:03 -0800596 this.screenSize.width = columnCount;
597 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800598
599 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400600 if (this.defaultTabStops)
601 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800602 } else {
603 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400604 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800605 break;
606
607 this.tabStops_.pop();
608 }
609 }
610
611 this.screen_.setColumnCount(this.screenSize.width);
612};
613
614/**
615 * Deal with terminal height changes.
616 *
617 * This function does what needs to be done when the terminal height changes
618 * out from under us. It happens here rather than in onResize_() because this
619 * code may need to run synchronously to handle programmatic changes of
620 * terminal height.
621 *
622 * Relying on the browser to send us an async resize event means we may not be
623 * in the correct state yet when the next escape sequence hits.
624 */
625hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
626 var deltaRows = rowCount - this.screen_.getHeight();
627
628 this.screenSize.height = rowCount;
629
630 var cursor = this.saveCursor();
631
632 if (deltaRows < 0) {
633 // Screen got smaller.
634 deltaRows *= -1;
635 while (deltaRows) {
636 var lastRow = this.getRowCount() - 1;
637 if (lastRow - this.scrollbackRows_.length == cursor.row)
638 break;
639
640 if (this.getRowText(lastRow))
641 break;
642
643 this.screen_.popRow();
644 deltaRows--;
645 }
646
647 var ary = this.screen_.shiftRows(deltaRows);
648 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
649
650 // We just removed rows from the top of the screen, we need to update
651 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800652 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800653 } else if (deltaRows > 0) {
654 // Screen got larger.
655
656 if (deltaRows <= this.scrollbackRows_.length) {
657 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
658 var rows = this.scrollbackRows_.splice(
659 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
660 this.screen_.unshiftRows(rows);
661 deltaRows -= scrollbackCount;
662 cursor.row += scrollbackCount;
663 }
664
665 if (deltaRows)
666 this.appendRows_(deltaRows);
667 }
668
rginda35c456b2012-02-09 17:29:05 -0800669 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800670 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800671};
672
673/**
674 * Scroll the terminal to the top of the scrollback buffer.
675 */
676hterm.Terminal.prototype.scrollHome = function() {
677 this.scrollPort_.scrollRowToTop(0);
678};
679
680/**
681 * Scroll the terminal to the end.
682 */
683hterm.Terminal.prototype.scrollEnd = function() {
684 this.scrollPort_.scrollRowToBottom(this.getRowCount());
685};
686
687/**
688 * Scroll the terminal one page up (minus one line) relative to the current
689 * position.
690 */
691hterm.Terminal.prototype.scrollPageUp = function() {
692 var i = this.scrollPort_.getTopRowIndex();
693 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
694};
695
696/**
697 * Scroll the terminal one page down (minus one line) relative to the current
698 * position.
699 */
700hterm.Terminal.prototype.scrollPageDown = function() {
701 var i = this.scrollPort_.getTopRowIndex();
702 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800703};
704
rgindac9bc5502012-01-18 11:48:44 -0800705/**
706 * Full terminal reset.
707 */
rginda87b86462011-12-14 13:48:03 -0800708hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800709 this.clearAllTabStops();
710 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700711
712 this.clearHome(this.primaryScreen_);
713 this.primaryScreen_.textAttributes.reset();
714
715 this.clearHome(this.alternateScreen_);
716 this.alternateScreen_.textAttributes.reset();
717
rgindab8bc8932012-04-27 12:45:03 -0700718 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
719
rgindac9bc5502012-01-18 11:48:44 -0800720 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800721};
722
rgindac9bc5502012-01-18 11:48:44 -0800723/**
724 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700725 *
726 * Perform a soft reset to the default values listed in
727 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800728 */
rginda0f5c0292012-01-13 11:00:13 -0800729hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700730 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800731 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700732
rgindab8bc8932012-04-27 12:45:03 -0700733 // Xterm also resets the color palette on soft reset, even though it doesn't
734 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700735 this.primaryScreen_.textAttributes.resetColorPalette();
736 this.alternateScreen_.textAttributes.resetColorPalette();
737
rgindab8bc8932012-04-27 12:45:03 -0700738 // The xterm man page explicitly says this will happen on soft reset.
739 this.setVTScrollRegion(null, null);
740
741 // Xterm also shows the cursor on soft reset, but does not alter the blink
742 // state.
rgindaa19afe22012-01-25 15:40:22 -0800743 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800744};
745
rgindac9bc5502012-01-18 11:48:44 -0800746/**
747 * Move the cursor forward to the next tab stop, or to the last column
748 * if no more tab stops are set.
749 */
750hterm.Terminal.prototype.forwardTabStop = function() {
751 var column = this.screen_.cursorPosition.column;
752
753 for (var i = 0; i < this.tabStops_.length; i++) {
754 if (this.tabStops_[i] > column) {
755 this.setCursorColumn(this.tabStops_[i]);
756 return;
757 }
758 }
759
David Benjamin66e954d2012-05-05 21:08:12 -0400760 // xterm does not clear the overflow flag on HT or CHT.
761 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800762 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400763 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800764};
765
rgindac9bc5502012-01-18 11:48:44 -0800766/**
767 * Move the cursor backward to the previous tab stop, or to the first column
768 * if no previous tab stops are set.
769 */
770hterm.Terminal.prototype.backwardTabStop = function() {
771 var column = this.screen_.cursorPosition.column;
772
773 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
774 if (this.tabStops_[i] < column) {
775 this.setCursorColumn(this.tabStops_[i]);
776 return;
777 }
778 }
779
780 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800781};
782
rgindac9bc5502012-01-18 11:48:44 -0800783/**
784 * Set a tab stop at the given column.
785 *
786 * @param {int} column Zero based column.
787 */
788hterm.Terminal.prototype.setTabStop = function(column) {
789 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
790 if (this.tabStops_[i] == column)
791 return;
792
793 if (this.tabStops_[i] < column) {
794 this.tabStops_.splice(i + 1, 0, column);
795 return;
796 }
797 }
798
799 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800800};
801
rgindac9bc5502012-01-18 11:48:44 -0800802/**
803 * Clear the tab stop at the current cursor position.
804 *
805 * No effect if there is no tab stop at the current cursor position.
806 */
807hterm.Terminal.prototype.clearTabStopAtCursor = function() {
808 var column = this.screen_.cursorPosition.column;
809
810 var i = this.tabStops_.indexOf(column);
811 if (i == -1)
812 return;
813
814 this.tabStops_.splice(i, 1);
815};
816
817/**
818 * Clear all tab stops.
819 */
820hterm.Terminal.prototype.clearAllTabStops = function() {
821 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -0400822 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -0800823};
824
825/**
826 * Set up the default tab stops, starting from a given column.
827 *
828 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -0400829 * from the specified column, or 0 if no column is provided. It also flags
830 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -0800831 *
832 * This does not clear the existing tab stops first, use clearAllTabStops
833 * for that.
834 *
835 * @param {int} opt_start Optional starting zero based starting column, useful
836 * for filling out missing tab stops when the terminal is resized.
837 */
838hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
839 var start = opt_start || 0;
840 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -0400841 // Round start up to a default tab stop.
842 start = start - 1 - ((start - 1) % w) + w;
843 for (var i = start; i < this.screenSize.width; i += w) {
844 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -0800845 }
David Benjamin66e954d2012-05-05 21:08:12 -0400846
847 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -0800848};
849
rginda6d397402012-01-17 10:58:29 -0800850/**
851 * Save cursor position and attributes.
852 *
853 * TODO(rginda): Save attributes once we support them.
854 */
rginda87b86462011-12-14 13:48:03 -0800855hterm.Terminal.prototype.saveOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800856 this.savedOptions_.cursor = this.saveCursor();
rgindaa19afe22012-01-25 15:40:22 -0800857 this.savedOptions_.textAttributes = this.screen_.textAttributes.clone();
rginda87b86462011-12-14 13:48:03 -0800858};
859
rginda6d397402012-01-17 10:58:29 -0800860/**
861 * Restore cursor position and attributes.
862 *
863 * TODO(rginda): Restore attributes once we support them.
864 */
rginda87b86462011-12-14 13:48:03 -0800865hterm.Terminal.prototype.restoreOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800866 if (this.savedOptions_.cursor)
867 this.restoreCursor(this.savedOptions_.cursor);
rgindaa19afe22012-01-25 15:40:22 -0800868 if (this.savedOptions_.textAttributes)
869 this.screen_.textAttributes = this.savedOptions_.textAttributes;
rginda8ba33642011-12-14 12:31:31 -0800870};
871
872/**
873 * Interpret a sequence of characters.
874 *
875 * Incomplete escape sequences are buffered until the next call.
876 *
877 * @param {string} str Sequence of characters to interpret or pass through.
878 */
879hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800880 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800881 this.scheduleSyncCursorPosition_();
882};
883
884/**
885 * Take over the given DIV for use as the terminal display.
886 *
887 * @param {HTMLDivElement} div The div to use as the terminal display.
888 */
889hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800890 this.div_ = div;
891
rginda8ba33642011-12-14 12:31:31 -0800892 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -0700893 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -0400894 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
895 this.scrollPort_.setBackgroundPosition(
896 this.prefs_.get('background-position'));
rginda30f20f62012-04-05 16:36:19 -0700897
rginda0918b652012-04-04 11:26:24 -0700898 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -0800899
rginda9f5222b2012-03-05 11:53:28 -0800900 this.setFontSize(this.prefs_.get('font-size'));
901 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -0800902
David Reveman8f552492012-03-28 12:18:41 -0400903 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
904
rginda8ba33642011-12-14 12:31:31 -0800905 this.document_ = this.scrollPort_.getDocument();
906
rginda8ba33642011-12-14 12:31:31 -0800907 this.cursorNode_ = this.document_.createElement('div');
908 this.cursorNode_.style.cssText =
909 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800910 'top: -99px;' +
911 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -0800912 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
913 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda6d397402012-01-17 10:58:29 -0800914 '-webkit-transition: opacity, background-color 100ms linear;' +
rginda9f5222b2012-03-05 11:53:28 -0800915 'background-color: ' + this.prefs_.get('cursor-color'));
rginda8ba33642011-12-14 12:31:31 -0800916 this.document_.body.appendChild(this.cursorNode_);
917
rgindade84e382012-04-20 15:39:31 -0700918 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda8ba33642011-12-14 12:31:31 -0800919 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -0800920
rginda87b86462011-12-14 13:48:03 -0800921 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -0800922 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -0800923};
924
rginda0918b652012-04-04 11:26:24 -0700925/**
926 * Return the HTML document that contains the terminal DOM nodes.
927 */
rginda87b86462011-12-14 13:48:03 -0800928hterm.Terminal.prototype.getDocument = function() {
929 return this.document_;
rginda8ba33642011-12-14 12:31:31 -0800930};
931
932/**
rginda0918b652012-04-04 11:26:24 -0700933 * Focus the terminal.
934 */
935hterm.Terminal.prototype.focus = function() {
936 this.scrollPort_.focus();
937};
938
939/**
rginda8ba33642011-12-14 12:31:31 -0800940 * Return the HTML Element for a given row index.
941 *
942 * This is a method from the RowProvider interface. The ScrollPort uses
943 * it to fetch rows on demand as they are scrolled into view.
944 *
945 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
946 * pairs to conserve memory.
947 *
948 * @param {integer} index The zero-based row index, measured relative to the
949 * start of the scrollback buffer. On-screen rows will always have the
950 * largest indicies.
951 * @return {HTMLElement} The 'x-row' element containing for the requested row.
952 */
953hterm.Terminal.prototype.getRowNode = function(index) {
954 if (index < this.scrollbackRows_.length)
955 return this.scrollbackRows_[index];
956
957 var screenIndex = index - this.scrollbackRows_.length;
958 return this.screen_.rowsArray[screenIndex];
959};
960
961/**
962 * Return the text content for a given range of rows.
963 *
964 * This is a method from the RowProvider interface. The ScrollPort uses
965 * it to fetch text content on demand when the user attempts to copy their
966 * selection to the clipboard.
967 *
968 * @param {integer} start The zero-based row index to start from, measured
969 * relative to the start of the scrollback buffer. On-screen rows will
970 * always have the largest indicies.
971 * @param {integer} end The zero-based row index to end on, measured
972 * relative to the start of the scrollback buffer.
973 * @return {string} A single string containing the text value of the range of
974 * rows. Lines will be newline delimited, with no trailing newline.
975 */
976hterm.Terminal.prototype.getRowsText = function(start, end) {
977 var ary = [];
978 for (var i = start; i < end; i++) {
979 var node = this.getRowNode(i);
980 ary.push(node.textContent);
981 }
982
983 return ary.join('\n');
984};
985
986/**
987 * Return the text content for a given row.
988 *
989 * This is a method from the RowProvider interface. The ScrollPort uses
990 * it to fetch text content on demand when the user attempts to copy their
991 * selection to the clipboard.
992 *
993 * @param {integer} index The zero-based row index to return, measured
994 * relative to the start of the scrollback buffer. On-screen rows will
995 * always have the largest indicies.
996 * @return {string} A string containing the text value of the selected row.
997 */
998hterm.Terminal.prototype.getRowText = function(index) {
999 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001000 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001001};
1002
1003/**
1004 * Return the total number of rows in the addressable screen and in the
1005 * scrollback buffer of this terminal.
1006 *
1007 * This is a method from the RowProvider interface. The ScrollPort uses
1008 * it to compute the size of the scrollbar.
1009 *
1010 * @return {integer} The number of rows in this terminal.
1011 */
1012hterm.Terminal.prototype.getRowCount = function() {
1013 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1014};
1015
1016/**
1017 * Create DOM nodes for new rows and append them to the end of the terminal.
1018 *
1019 * This is the only correct way to add a new DOM node for a row. Notice that
1020 * the new row is appended to the bottom of the list of rows, and does not
1021 * require renumbering (of the rowIndex property) of previous rows.
1022 *
1023 * If you think you want a new blank row somewhere in the middle of the
1024 * terminal, look into moveRows_().
1025 *
1026 * This method does not pay attention to vtScrollTop/Bottom, since you should
1027 * be using moveRows() in cases where they would matter.
1028 *
1029 * The cursor will be positioned at column 0 of the first inserted line.
1030 */
1031hterm.Terminal.prototype.appendRows_ = function(count) {
1032 var cursorRow = this.screen_.rowsArray.length;
1033 var offset = this.scrollbackRows_.length + cursorRow;
1034 for (var i = 0; i < count; i++) {
1035 var row = this.document_.createElement('x-row');
1036 row.appendChild(this.document_.createTextNode(''));
1037 row.rowIndex = offset + i;
1038 this.screen_.pushRow(row);
1039 }
1040
1041 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1042 if (extraRows > 0) {
1043 var ary = this.screen_.shiftRows(extraRows);
1044 Array.prototype.push.apply(this.scrollbackRows_, ary);
1045 this.scheduleScrollDown_();
1046 }
1047
1048 if (cursorRow >= this.screen_.rowsArray.length)
1049 cursorRow = this.screen_.rowsArray.length - 1;
1050
rginda87b86462011-12-14 13:48:03 -08001051 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001052};
1053
1054/**
1055 * Relocate rows from one part of the addressable screen to another.
1056 *
1057 * This is used to recycle rows during VT scrolls (those which are driven
1058 * by VT commands, rather than by the user manipulating the scrollbar.)
1059 *
1060 * In this case, the blank lines scrolled into the scroll region are made of
1061 * the nodes we scrolled off. These have their rowIndex properties carefully
1062 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001063 */
1064hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1065 var ary = this.screen_.removeRows(fromIndex, count);
1066 this.screen_.insertRows(toIndex, ary);
1067
1068 var start, end;
1069 if (fromIndex < toIndex) {
1070 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001071 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001072 } else {
1073 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001074 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001075 }
1076
1077 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001078 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001079};
1080
1081/**
1082 * Renumber the rowIndex property of the given range of rows.
1083 *
1084 * The start and end indicies are relative to the screen, not the scrollback.
1085 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001086 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001087 * no need to renumber scrollback rows.
1088 */
1089hterm.Terminal.prototype.renumberRows_ = function(start, end) {
1090 var offset = this.scrollbackRows_.length;
1091 for (var i = start; i < end; i++) {
1092 this.screen_.rowsArray[i].rowIndex = offset + i;
1093 }
1094};
1095
1096/**
1097 * Print a string to the terminal.
1098 *
1099 * This respects the current insert and wraparound modes. It will add new lines
1100 * to the end of the terminal, scrolling off the top into the scrollback buffer
1101 * if necessary.
1102 *
1103 * The string is *not* parsed for escape codes. Use the interpret() method if
1104 * that's what you're after.
1105 *
1106 * @param{string} str The string to print.
1107 */
1108hterm.Terminal.prototype.print = function(str) {
rgindaa19afe22012-01-25 15:40:22 -08001109 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
1110 this.newLine();
rginda2312fff2012-01-05 16:20:52 -08001111
rgindaa19afe22012-01-25 15:40:22 -08001112 if (this.options_.insertMode) {
1113 this.screen_.insertString(str);
1114 } else {
1115 this.screen_.overwriteString(str);
1116 }
1117
1118 var overflow = this.screen_.maybeClipCurrentRow();
1119
1120 if (this.options_.wraparound && overflow) {
1121 var lastColumn;
1122
1123 do {
rginda35c456b2012-02-09 17:29:05 -08001124 this.newLine();
1125 lastColumn = overflow.characterLength;
rgindaa19afe22012-01-25 15:40:22 -08001126
1127 if (!this.options_.insertMode)
1128 this.screen_.deleteChars(overflow.characterLength);
1129
1130 this.screen_.prependNodes(overflow);
rgindaa19afe22012-01-25 15:40:22 -08001131
1132 overflow = this.screen_.maybeClipCurrentRow();
1133 } while (overflow);
1134
1135 this.setCursorColumn(lastColumn);
1136 }
rginda8ba33642011-12-14 12:31:31 -08001137
1138 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001139
rginda9f5222b2012-03-05 11:53:28 -08001140 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001141 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001142};
1143
1144/**
rginda87b86462011-12-14 13:48:03 -08001145 * Set the VT scroll region.
1146 *
rginda87b86462011-12-14 13:48:03 -08001147 * This also resets the cursor position to the absolute (0, 0) position, since
1148 * that's what xterm appears to do.
1149 *
1150 * @param {integer} scrollTop The zero-based top of the scroll region.
1151 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1152 * inclusive.
1153 */
1154hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1155 this.vtScrollTop_ = scrollTop;
1156 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001157};
1158
1159/**
rginda8ba33642011-12-14 12:31:31 -08001160 * Return the top row index according to the VT.
1161 *
1162 * This will return 0 unless the terminal has been told to restrict scrolling
1163 * to some lower row. It is used for some VT cursor positioning and scrolling
1164 * commands.
1165 *
1166 * @return {integer} The topmost row in the terminal's scroll region.
1167 */
1168hterm.Terminal.prototype.getVTScrollTop = function() {
1169 if (this.vtScrollTop_ != null)
1170 return this.vtScrollTop_;
1171
1172 return 0;
rginda87b86462011-12-14 13:48:03 -08001173};
rginda8ba33642011-12-14 12:31:31 -08001174
1175/**
1176 * Return the bottom row index according to the VT.
1177 *
1178 * This will return the height of the terminal unless the it has been told to
1179 * restrict scrolling to some higher row. It is used for some VT cursor
1180 * positioning and scrolling commands.
1181 *
1182 * @return {integer} The bottommost row in the terminal's scroll region.
1183 */
1184hterm.Terminal.prototype.getVTScrollBottom = function() {
1185 if (this.vtScrollBottom_ != null)
1186 return this.vtScrollBottom_;
1187
rginda87b86462011-12-14 13:48:03 -08001188 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001189}
1190
1191/**
1192 * Process a '\n' character.
1193 *
1194 * If the cursor is on the final row of the terminal this will append a new
1195 * blank row to the screen and scroll the topmost row into the scrollback
1196 * buffer.
1197 *
1198 * Otherwise, this moves the cursor to column zero of the next row.
1199 */
1200hterm.Terminal.prototype.newLine = function() {
1201 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001202 // If we're at the end of the screen we need to append a new line and
1203 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001204 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001205 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1206 // End of the scroll region does not affect the scrollback buffer.
1207 this.vtScrollUp(1);
1208 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001209 } else {
rginda87b86462011-12-14 13:48:03 -08001210 // Anywhere else in the screen just moves the cursor.
1211 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001212 }
1213};
1214
1215/**
1216 * Like newLine(), except maintain the cursor column.
1217 */
1218hterm.Terminal.prototype.lineFeed = function() {
1219 var column = this.screen_.cursorPosition.column;
1220 this.newLine();
1221 this.setCursorColumn(column);
1222};
1223
1224/**
rginda87b86462011-12-14 13:48:03 -08001225 * If autoCarriageReturn is set then newLine(), else lineFeed().
1226 */
1227hterm.Terminal.prototype.formFeed = function() {
1228 if (this.options_.autoCarriageReturn) {
1229 this.newLine();
1230 } else {
1231 this.lineFeed();
1232 }
1233};
1234
1235/**
1236 * Move the cursor up one row, possibly inserting a blank line.
1237 *
1238 * The cursor column is not changed.
1239 */
1240hterm.Terminal.prototype.reverseLineFeed = function() {
1241 var scrollTop = this.getVTScrollTop();
1242 var currentRow = this.screen_.cursorPosition.row;
1243
1244 if (currentRow == scrollTop) {
1245 this.insertLines(1);
1246 } else {
1247 this.setAbsoluteCursorRow(currentRow - 1);
1248 }
1249};
1250
1251/**
rginda8ba33642011-12-14 12:31:31 -08001252 * Replace all characters to the left of the current cursor with the space
1253 * character.
1254 *
1255 * TODO(rginda): This should probably *remove* the characters (not just replace
1256 * with a space) if there are no characters at or beyond the current cursor
1257 * position. Once it does that, it'll have the same text-attribute related
1258 * issues as hterm.Screen.prototype.clearCursorRow :/
1259 */
1260hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001261 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001262 this.setCursorColumn(0);
rginda87b86462011-12-14 13:48:03 -08001263 this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1));
1264 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001265};
1266
1267/**
David Benjamin684a9b72012-05-01 17:19:58 -04001268 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001269 *
1270 * The cursor position is unchanged.
1271 *
1272 * TODO(rginda): Test that this works even when the cursor is positioned beyond
1273 * the end of the text.
1274 *
1275 * TODO(rginda): This likely has text-attribute related troubles similar to the
1276 * todo on hterm.Screen.prototype.clearCursorRow.
David Benjamin684a9b72012-05-01 17:19:58 -04001277 *
1278 * TODO(davidben): Probably better to not add the whitespace to the clipboard
1279 * if erasing to the end of the drawn portion of the line. That said, xterm
1280 * behaves the same here.
rginda8ba33642011-12-14 12:31:31 -08001281 */
1282hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001283 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001284
rginda87b86462011-12-14 13:48:03 -08001285 var maxCount = this.screenSize.width - cursor.column;
David Benjamin684a9b72012-05-01 17:19:58 -04001286 if (opt_count === undefined || opt_count >= maxCount) {
1287 this.screen_.deleteChars(maxCount);
1288 } else {
1289 this.screen_.overwriteString(hterm.getWhitespace(opt_count));
1290 }
rginda87b86462011-12-14 13:48:03 -08001291 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001292};
1293
1294/**
1295 * Erase the current line.
1296 *
1297 * The cursor position is unchanged.
1298 *
1299 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1300 * has a text-attribute related TODO.
1301 */
1302hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001303 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001304 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001305 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001306};
1307
1308/**
David Benjamina08d78f2012-05-05 00:28:49 -04001309 * Erase all characters from the start of the screen to the current cursor
1310 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001311 *
1312 * The cursor position is unchanged.
1313 *
1314 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1315 * has a text-attribute related TODO.
1316 */
1317hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001318 var cursor = this.saveCursor();
1319
1320 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001321
David Benjamina08d78f2012-05-05 00:28:49 -04001322 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001323 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001324 this.screen_.clearCursorRow();
1325 }
1326
rginda87b86462011-12-14 13:48:03 -08001327 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001328};
1329
1330/**
1331 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001332 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001333 *
1334 * The cursor position is unchanged.
1335 *
1336 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1337 * has a text-attribute related TODO.
1338 */
1339hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001340 var cursor = this.saveCursor();
1341
1342 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001343
David Benjamina08d78f2012-05-05 00:28:49 -04001344 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001345 for (var i = cursor.row + 1; i <= bottom; i++) {
1346 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001347 this.screen_.clearCursorRow();
1348 }
1349
rginda87b86462011-12-14 13:48:03 -08001350 this.restoreCursor(cursor);
1351};
1352
1353/**
1354 * Fill the terminal with a given character.
1355 *
1356 * This methods does not respect the VT scroll region.
1357 *
1358 * @param {string} ch The character to use for the fill.
1359 */
1360hterm.Terminal.prototype.fill = function(ch) {
1361 var cursor = this.saveCursor();
1362
1363 this.setAbsoluteCursorPosition(0, 0);
1364 for (var row = 0; row < this.screenSize.height; row++) {
1365 for (var col = 0; col < this.screenSize.width; col++) {
1366 this.setAbsoluteCursorPosition(row, col);
1367 this.screen_.overwriteString(ch);
1368 }
1369 }
1370
1371 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001372};
1373
1374/**
rginda9ea433c2012-03-16 11:57:00 -07001375 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001376 *
rginda9ea433c2012-03-16 11:57:00 -07001377 * This does not respect the scroll region.
1378 *
1379 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1380 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001381 *
1382 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1383 * has a text-attribute related TODO.
1384 */
rginda9ea433c2012-03-16 11:57:00 -07001385hterm.Terminal.prototype.clearHome = function(opt_screen) {
1386 var screen = opt_screen || this.screen_;
1387 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001388
rginda11057d52012-04-25 12:29:56 -07001389 if (bottom == 0) {
1390 // Empty screen, nothing to do.
1391 return;
1392 }
1393
rgindae4d29232012-01-19 10:47:13 -08001394 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001395 screen.setCursorPosition(i, 0);
1396 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001397 }
1398
rginda9ea433c2012-03-16 11:57:00 -07001399 screen.setCursorPosition(0, 0);
1400};
1401
1402/**
1403 * Erase the entire display without changing the cursor position.
1404 *
1405 * The cursor position is unchanged. This does not respect the scroll
1406 * region.
1407 *
1408 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1409 * to the current screen.
1410 *
1411 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1412 * has a text-attribute related TODO.
1413 */
1414hterm.Terminal.prototype.clear = function(opt_screen) {
1415 var screen = opt_screen || this.screen_;
1416 var cursor = screen.cursorPosition.clone();
1417 this.clearHome(screen);
1418 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001419};
1420
1421/**
1422 * VT command to insert lines at the current cursor row.
1423 *
1424 * This respects the current scroll region. Rows pushed off the bottom are
1425 * lost (they won't show up in the scrollback buffer).
1426 *
1427 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1428 * has a text-attribute related TODO.
1429 *
1430 * @param {integer} count The number of lines to insert.
1431 */
1432hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001433 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001434
1435 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001436 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001437
rgindae4d29232012-01-19 10:47:13 -08001438 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001439 if (start != cursor.row)
1440 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001441
1442 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001443 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001444 this.screen_.clearCursorRow();
1445 }
1446
rginda87b86462011-12-14 13:48:03 -08001447 cursor.column = 0;
1448 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001449};
1450
1451/**
1452 * VT command to delete lines at the current cursor row.
1453 *
1454 * New rows are added to the bottom of scroll region to take their place. New
1455 * rows are strictly there to take up space and have no content or style.
1456 */
1457hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001458 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001459
rginda87b86462011-12-14 13:48:03 -08001460 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001461 var bottom = this.getVTScrollBottom();
1462
rginda87b86462011-12-14 13:48:03 -08001463 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001464 count = Math.min(count, maxCount);
1465
rginda87b86462011-12-14 13:48:03 -08001466 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001467 if (count != maxCount)
1468 this.moveRows_(top, count, moveStart);
1469
1470 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001471 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001472 this.screen_.clearCursorRow();
1473 }
1474
rginda87b86462011-12-14 13:48:03 -08001475 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001476};
1477
1478/**
1479 * Inserts the given number of spaces at the current cursor position.
1480 *
rginda87b86462011-12-14 13:48:03 -08001481 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001482 */
1483hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001484 var cursor = this.saveCursor();
1485
rginda0f5c0292012-01-13 11:00:13 -08001486 var ws = hterm.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001487 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001488 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001489
1490 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001491};
1492
1493/**
1494 * Forward-delete the specified number of characters starting at the cursor
1495 * position.
1496 *
1497 * @param {integer} count The number of characters to delete.
1498 */
1499hterm.Terminal.prototype.deleteChars = function(count) {
1500 this.screen_.deleteChars(count);
1501};
1502
1503/**
1504 * Shift rows in the scroll region upwards by a given number of lines.
1505 *
1506 * New rows are inserted at the bottom of the scroll region to fill the
1507 * vacated rows. The new rows not filled out with the current text attributes.
1508 *
1509 * This function does not affect the scrollback rows at all. Rows shifted
1510 * off the top are lost.
1511 *
rginda87b86462011-12-14 13:48:03 -08001512 * The cursor position is not altered.
1513 *
rginda8ba33642011-12-14 12:31:31 -08001514 * @param {integer} count The number of rows to scroll.
1515 */
1516hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001517 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001518
rginda87b86462011-12-14 13:48:03 -08001519 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001520 this.deleteLines(count);
1521
rginda87b86462011-12-14 13:48:03 -08001522 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001523};
1524
1525/**
1526 * Shift rows below the cursor down by a given number of lines.
1527 *
1528 * This function respects the current scroll region.
1529 *
1530 * New rows are inserted at the top of the scroll region to fill the
1531 * vacated rows. The new rows not filled out with the current text attributes.
1532 *
1533 * This function does not affect the scrollback rows at all. Rows shifted
1534 * off the bottom are lost.
1535 *
1536 * @param {integer} count The number of rows to scroll.
1537 */
1538hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001539 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001540
rginda87b86462011-12-14 13:48:03 -08001541 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001542 this.insertLines(opt_count);
1543
rginda87b86462011-12-14 13:48:03 -08001544 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001545};
1546
rginda87b86462011-12-14 13:48:03 -08001547
rginda8ba33642011-12-14 12:31:31 -08001548/**
1549 * Set the cursor position.
1550 *
1551 * The cursor row is relative to the scroll region if the terminal has
1552 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1553 *
1554 * @param {integer} row The new zero-based cursor row.
1555 * @param {integer} row The new zero-based cursor column.
1556 */
1557hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1558 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001559 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001560 } else {
rginda87b86462011-12-14 13:48:03 -08001561 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001562 }
rginda87b86462011-12-14 13:48:03 -08001563};
rginda8ba33642011-12-14 12:31:31 -08001564
rginda87b86462011-12-14 13:48:03 -08001565hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1566 var scrollTop = this.getVTScrollTop();
1567 row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
rginda2312fff2012-01-05 16:20:52 -08001568 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001569 this.screen_.setCursorPosition(row, column);
1570};
1571
1572hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rginda2312fff2012-01-05 16:20:52 -08001573 row = hterm.clamp(row, 0, this.screenSize.height - 1);
1574 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001575 this.screen_.setCursorPosition(row, column);
1576};
1577
1578/**
1579 * Set the cursor column.
1580 *
1581 * @param {integer} column The new zero-based cursor column.
1582 */
1583hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001584 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001585};
1586
1587/**
1588 * Return the cursor column.
1589 *
1590 * @return {integer} The zero-based cursor column.
1591 */
1592hterm.Terminal.prototype.getCursorColumn = function() {
1593 return this.screen_.cursorPosition.column;
1594};
1595
1596/**
1597 * Set the cursor row.
1598 *
1599 * The cursor row is relative to the scroll region if the terminal has
1600 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1601 *
1602 * @param {integer} row The new cursor row.
1603 */
rginda87b86462011-12-14 13:48:03 -08001604hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1605 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001606};
1607
1608/**
1609 * Return the cursor row.
1610 *
1611 * @return {integer} The zero-based cursor row.
1612 */
1613hterm.Terminal.prototype.getCursorRow = function(row) {
1614 return this.screen_.cursorPosition.row;
1615};
1616
1617/**
1618 * Request that the ScrollPort redraw itself soon.
1619 *
1620 * The redraw will happen asynchronously, soon after the call stack winds down.
1621 * Multiple calls will be coalesced into a single redraw.
1622 */
1623hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001624 if (this.timeouts_.redraw)
1625 return;
rginda8ba33642011-12-14 12:31:31 -08001626
1627 var self = this;
rginda87b86462011-12-14 13:48:03 -08001628 this.timeouts_.redraw = setTimeout(function() {
1629 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001630 self.scrollPort_.redraw_();
1631 }, 0);
1632};
1633
1634/**
1635 * Request that the ScrollPort be scrolled to the bottom.
1636 *
1637 * The scroll will happen asynchronously, soon after the call stack winds down.
1638 * Multiple calls will be coalesced into a single scroll.
1639 *
1640 * This affects the scrollbar position of the ScrollPort, and has nothing to
1641 * do with the VT scroll commands.
1642 */
1643hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1644 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001645 return;
rginda8ba33642011-12-14 12:31:31 -08001646
1647 var self = this;
1648 this.timeouts_.scrollDown = setTimeout(function() {
1649 delete self.timeouts_.scrollDown;
1650 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1651 }, 10);
1652};
1653
1654/**
1655 * Move the cursor up a specified number of rows.
1656 *
1657 * @param {integer} count The number of rows to move the cursor.
1658 */
1659hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001660 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001661};
1662
1663/**
1664 * Move the cursor down a specified number of rows.
1665 *
1666 * @param {integer} count The number of rows to move the cursor.
1667 */
1668hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001669 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001670 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1671 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1672 this.screenSize.height - 1);
1673
1674 var row = hterm.clamp(this.screen_.cursorPosition.row + count,
1675 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001676 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001677};
1678
1679/**
1680 * Move the cursor left a specified number of columns.
1681 *
1682 * @param {integer} count The number of columns to move the cursor.
1683 */
1684hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001685 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001686};
1687
1688/**
1689 * Move the cursor right a specified number of columns.
1690 *
1691 * @param {integer} count The number of columns to move the cursor.
1692 */
1693hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001694 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001695 var column = hterm.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001696 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001697 this.setCursorColumn(column);
1698};
1699
1700/**
1701 * Reverse the foreground and background colors of the terminal.
1702 *
1703 * This only affects text that was drawn with no attributes.
1704 *
1705 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1706 * been drawn with attributes that happen to coincide with the default
1707 * 'no-attribute' colors. My guess is probably not.
1708 */
1709hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001710 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001711 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001712 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1713 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001714 } else {
rginda9f5222b2012-03-05 11:53:28 -08001715 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1716 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001717 }
1718};
1719
1720/**
rginda87b86462011-12-14 13:48:03 -08001721 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001722 */
1723hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001724 if (this.bellAudio_.getAttribute('src'))
1725 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001726
rginda6d397402012-01-17 10:58:29 -08001727 this.cursorNode_.style.backgroundColor =
1728 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001729
1730 var self = this;
1731 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001732 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001733 }, 200);
rginda87b86462011-12-14 13:48:03 -08001734};
1735
1736/**
rginda8ba33642011-12-14 12:31:31 -08001737 * Set the origin mode bit.
1738 *
1739 * If origin mode is on, certain VT cursor and scrolling commands measure their
1740 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1741 * to the top of the addressable screen.
1742 *
1743 * Defaults to off.
1744 *
1745 * @param {boolean} state True to set origin mode, false to unset.
1746 */
1747hterm.Terminal.prototype.setOriginMode = function(state) {
1748 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001749 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001750};
1751
1752/**
1753 * Set the insert mode bit.
1754 *
1755 * If insert mode is on, existing text beyond the cursor position will be
1756 * shifted right to make room for new text. Otherwise, new text overwrites
1757 * any existing text.
1758 *
1759 * Defaults to off.
1760 *
1761 * @param {boolean} state True to set insert mode, false to unset.
1762 */
1763hterm.Terminal.prototype.setInsertMode = function(state) {
1764 this.options_.insertMode = state;
1765};
1766
1767/**
rginda87b86462011-12-14 13:48:03 -08001768 * Set the auto carriage return bit.
1769 *
1770 * If auto carriage return is on then a formfeed character is interpreted
1771 * as a newline, otherwise it's the same as a linefeed. The difference boils
1772 * down to whether or not the cursor column is reset.
1773 */
1774hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1775 this.options_.autoCarriageReturn = state;
1776};
1777
1778/**
rginda8ba33642011-12-14 12:31:31 -08001779 * Set the wraparound mode bit.
1780 *
1781 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1782 * to the start of the following row. Otherwise, the cursor is clamped to the
1783 * end of the screen and attempts to write past it are ignored.
1784 *
1785 * Defaults to on.
1786 *
1787 * @param {boolean} state True to set wraparound mode, false to unset.
1788 */
1789hterm.Terminal.prototype.setWraparound = function(state) {
1790 this.options_.wraparound = state;
1791};
1792
1793/**
1794 * Set the reverse-wraparound mode bit.
1795 *
1796 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1797 * to the end of the previous row. Otherwise, the cursor is clamped to column
1798 * 0.
1799 *
1800 * Defaults to off.
1801 *
1802 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1803 */
1804hterm.Terminal.prototype.setReverseWraparound = function(state) {
1805 this.options_.reverseWraparound = state;
1806};
1807
1808/**
1809 * Selects between the primary and alternate screens.
1810 *
1811 * If alternate mode is on, the alternate screen is active. Otherwise the
1812 * primary screen is active.
1813 *
1814 * Swapping screens has no effect on the scrollback buffer.
1815 *
1816 * Each screen maintains its own cursor position.
1817 *
1818 * Defaults to off.
1819 *
1820 * @param {boolean} state True to set alternate mode, false to unset.
1821 */
1822hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001823 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001824 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1825
rginda35c456b2012-02-09 17:29:05 -08001826 if (this.screen_.rowsArray.length &&
1827 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
1828 // If the screen changed sizes while we were away, our rowIndexes may
1829 // be incorrect.
1830 var offset = this.scrollbackRows_.length;
1831 var ary = this.screen_.rowsArray;
1832 for (i = 0; i < ary.length; i++) {
1833 ary[i].rowIndex = offset + i;
1834 }
1835 }
rginda8ba33642011-12-14 12:31:31 -08001836
rginda35c456b2012-02-09 17:29:05 -08001837 this.realizeWidth_(this.screenSize.width);
1838 this.realizeHeight_(this.screenSize.height);
1839 this.scrollPort_.syncScrollHeight();
1840 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001841
rginda6d397402012-01-17 10:58:29 -08001842 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08001843 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08001844};
1845
1846/**
1847 * Set the cursor-blink mode bit.
1848 *
1849 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1850 * a visible cursor does not blink.
1851 *
1852 * You should make sure to turn blinking off if you're going to dispose of a
1853 * terminal, otherwise you'll leak a timeout.
1854 *
1855 * Defaults to on.
1856 *
1857 * @param {boolean} state True to set cursor-blink mode, false to unset.
1858 */
1859hterm.Terminal.prototype.setCursorBlink = function(state) {
1860 this.options_.cursorBlink = state;
1861
1862 if (!state && this.timeouts_.cursorBlink) {
1863 clearTimeout(this.timeouts_.cursorBlink);
1864 delete this.timeouts_.cursorBlink;
1865 }
1866
1867 if (this.options_.cursorVisible)
1868 this.setCursorVisible(true);
1869};
1870
1871/**
1872 * Set the cursor-visible mode bit.
1873 *
1874 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1875 *
1876 * Defaults to on.
1877 *
1878 * @param {boolean} state True to set cursor-visible mode, false to unset.
1879 */
1880hterm.Terminal.prototype.setCursorVisible = function(state) {
1881 this.options_.cursorVisible = state;
1882
1883 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001884 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001885 return;
1886 }
1887
rginda87b86462011-12-14 13:48:03 -08001888 this.syncCursorPosition_();
1889
1890 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001891
1892 if (this.options_.cursorBlink) {
1893 if (this.timeouts_.cursorBlink)
1894 return;
1895
1896 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1897 500);
1898 } else {
1899 if (this.timeouts_.cursorBlink) {
1900 clearTimeout(this.timeouts_.cursorBlink);
1901 delete this.timeouts_.cursorBlink;
1902 }
1903 }
1904};
1905
1906/**
rginda87b86462011-12-14 13:48:03 -08001907 * Synchronizes the visible cursor and document selection with the current
1908 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08001909 */
1910hterm.Terminal.prototype.syncCursorPosition_ = function() {
1911 var topRowIndex = this.scrollPort_.getTopRowIndex();
1912 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
1913 var cursorRowIndex = this.scrollbackRows_.length +
1914 this.screen_.cursorPosition.row;
1915
1916 if (cursorRowIndex > bottomRowIndex) {
1917 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08001918 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08001919 return;
1920 }
1921
rginda35c456b2012-02-09 17:29:05 -08001922 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
1923 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
1924
rginda8ba33642011-12-14 12:31:31 -08001925 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08001926 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
1927 'px';
1928 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
1929 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08001930
1931 this.cursorNode_.setAttribute('title',
1932 '(' + this.screen_.cursorPosition.row +
1933 ', ' + this.screen_.cursorPosition.column +
1934 ')');
1935
1936 // Update the caret for a11y purposes.
1937 var selection = this.document_.getSelection();
1938 if (selection && selection.isCollapsed)
1939 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08001940};
1941
1942/**
1943 * Synchronizes the visible cursor with the current cursor coordinates.
1944 *
1945 * The sync will happen asynchronously, soon after the call stack winds down.
1946 * Multiple calls will be coalesced into a single sync.
1947 */
1948hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
1949 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08001950 return;
rginda8ba33642011-12-14 12:31:31 -08001951
1952 var self = this;
1953 this.timeouts_.syncCursor = setTimeout(function() {
1954 self.syncCursorPosition_();
1955 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08001956 }, 0);
1957};
1958
rgindacc2996c2012-02-24 14:59:31 -08001959/**
rgindaf522ce02012-04-17 17:49:17 -07001960 * Show or hide the zoom warning.
1961 *
1962 * The zoom warning is a message warning the user that their browser zoom must
1963 * be set to 100% in order for hterm to function properly.
1964 *
1965 * @param {boolean} state True to show the message, false to hide it.
1966 */
1967hterm.Terminal.prototype.showZoomWarning_ = function(state) {
1968 if (!this.zoomWarningNode_) {
1969 if (!state)
1970 return;
1971
1972 this.zoomWarningNode_ = this.document_.createElement('div');
1973 this.zoomWarningNode_.style.cssText = (
1974 'color: black;' +
1975 'background-color: #ff2222;' +
1976 'font-size: large;' +
1977 'border-radius: 8px;' +
1978 'opacity: 0.75;' +
1979 'padding: 0.2em 0.5em 0.2em 0.5em;' +
1980 'top: 0.5em;' +
1981 'right: 1.2em;' +
1982 'position: absolute;' +
1983 '-webkit-text-size-adjust: none;' +
1984 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07001985 }
1986
rgindade84e382012-04-20 15:39:31 -07001987 this.zoomWarningNode_.textContent = hterm.msg('ZOOM_WARNING') ||
1988 ('!! ' + parseInt(this.scrollPort_.characterSize.zoomFactor * 100) +
1989 '% !!');
rgindaf522ce02012-04-17 17:49:17 -07001990 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
1991
1992 if (state) {
1993 if (!this.zoomWarningNode_.parentNode)
1994 this.div_.parentNode.appendChild(this.zoomWarningNode_);
1995 } else if (this.zoomWarningNode_.parentNode) {
1996 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
1997 }
1998};
1999
2000/**
rgindacc2996c2012-02-24 14:59:31 -08002001 * Show the terminal overlay for a given amount of time.
2002 *
2003 * The terminal overlay appears in inverse video in a large font, centered
2004 * over the terminal. You should probably keep the overlay message brief,
2005 * since it's in a large font and you probably aren't going to check the size
2006 * of the terminal first.
2007 *
2008 * @param {string} msg The text (not HTML) message to display in the overlay.
2009 * @param {number} opt_timeout The amount of time to wait before fading out
2010 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2011 * stay up forever (or until the next overlay).
2012 */
2013hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002014 if (!this.overlayNode_) {
2015 if (!this.div_)
2016 return;
2017
2018 this.overlayNode_ = this.document_.createElement('div');
2019 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002020 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002021 'font-size: xx-large;' +
2022 'opacity: 0.75;' +
2023 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2024 'position: absolute;' +
2025 '-webkit-user-select: none;' +
2026 '-webkit-transition: opacity 180ms ease-in;');
2027 }
2028
rginda9f5222b2012-03-05 11:53:28 -08002029 this.overlayNode_.style.color = this.prefs_.get('background-color');
2030 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2031 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2032
rgindaf0090c92012-02-10 14:58:52 -08002033 this.overlayNode_.textContent = msg;
2034 this.overlayNode_.style.opacity = '0.75';
2035
2036 if (!this.overlayNode_.parentNode)
2037 this.div_.appendChild(this.overlayNode_);
2038
2039 this.overlayNode_.style.top = (
2040 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
2041 this.overlayNode_.style.left = (
2042 this.div_.clientWidth - this.overlayNode_.clientWidth -
2043 this.scrollbarWidthPx) / 2;
2044
2045 var self = this;
2046
2047 if (this.overlayTimeout_)
2048 clearTimeout(this.overlayTimeout_);
2049
rgindacc2996c2012-02-24 14:59:31 -08002050 if (opt_timeout === null)
2051 return;
2052
rgindaf0090c92012-02-10 14:58:52 -08002053 this.overlayTimeout_ = setTimeout(function() {
2054 self.overlayNode_.style.opacity = '0';
2055 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002056 if (self.overlayNode_.parentNode)
2057 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002058 self.overlayTimeout_ = null;
2059 self.overlayNode_.style.opacity = '0.75';
2060 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002061 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002062};
2063
2064hterm.Terminal.prototype.overlaySize = function() {
2065 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2066};
2067
rginda87b86462011-12-14 13:48:03 -08002068/**
2069 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2070 *
2071 * @param {string} string The VT string representing the keystroke.
2072 */
2073hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002074 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002075 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2076
2077 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08002078};
2079
2080/**
2081 * React when the ScrollPort is scrolled.
2082 */
2083hterm.Terminal.prototype.onScroll_ = function() {
2084 this.scheduleSyncCursorPosition_();
2085};
2086
2087/**
rginda9846e2f2012-01-27 13:53:33 -08002088 * React when text is pasted into the scrollPort.
2089 */
2090hterm.Terminal.prototype.onPaste_ = function(e) {
2091 this.io.onVTKeystroke(e.text);
2092};
2093
2094/**
rginda8ba33642011-12-14 12:31:31 -08002095 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002096 *
2097 * Note: This function should not directly contain code that alters the internal
2098 * state of the terminal. That kind of code belongs in realizeWidth or
2099 * realizeHeight, so that it can be executed synchronously in the case of a
2100 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002101 */
2102hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002103 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002104 this.scrollPort_.characterSize.width);
2105 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2106 this.scrollPort_.characterSize.height);
2107
2108 if (!(columnCount || rowCount)) {
2109 // We avoid these situations since they happen sometimes when the terminal
2110 // gets removed from the document, and we can't deal with that.
2111 return;
2112 }
2113
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002114 this.realizeSize_(columnCount, rowCount);
rgindac9bc5502012-01-18 11:48:44 -08002115 this.scheduleSyncCursorPosition_();
rgindaf522ce02012-04-17 17:49:17 -07002116 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaf0090c92012-02-10 14:58:52 -08002117 this.overlaySize();
rginda8ba33642011-12-14 12:31:31 -08002118};
2119
2120/**
2121 * Service the cursor blink timeout.
2122 */
2123hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002124 if (this.cursorNode_.style.opacity == '0') {
2125 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002126 } else {
rginda87b86462011-12-14 13:48:03 -08002127 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002128 }
2129};
David Reveman8f552492012-03-28 12:18:41 -04002130
2131/**
2132 * Set the scrollbar-visible mode bit.
2133 *
2134 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2135 * Otherwise it will not.
2136 *
2137 * Defaults to on.
2138 *
2139 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2140 */
2141hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2142 this.scrollPort_.setScrollbarVisible(state);
2143};