blob: 85833143620fd852ee45834b913686c3ffae0573 [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) {
145 self.vt.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
162 self.vt.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 /**
200 * If true, the backspace should send BS ('\x08', aka ^H). Otherwise
201 * the backspace key should send '\x7f'.
202 */
203 ['backspace-sends-backspace', false, function(v) {
204 self.keyboard.backspaceSendsBackspace = v;
205 }
206 ],
207
208 /**
rgindade84e382012-04-20 15:39:31 -0700209 * Whether or not to blink the cursor by default.
210 */
211 ['cursor-blink', false, function(v) {
212 self.setCursorBlink(!!v);
213 }
214 ],
215
216 /**
rginda30f20f62012-04-05 16:36:19 -0700217 * The color of the visible cursor.
218 */
219 ['cursor-color', 'rgba(255,0,0,0.5)', function(v) {
220 self.cursorNode_.style.backgroundColor = v;
221 }
222 ],
223
224 /**
rginda11057d52012-04-25 12:29:56 -0700225 * True to enable 8-bit control characters, false to ignore them.
226 *
227 * We'll respect the two-byte versions of these control characters
228 * regardless of this setting.
229 */
230 ['enable-8-bit-control', false, function(v) {
231 self.vt.enable8BitControl = !!v;
232 }
233 ],
234
235 /**
rginda30f20f62012-04-05 16:36:19 -0700236 * True if we should use bold weight font for text with the bold/bright
237 * attribute. False to use bright colors only. Null to autodetect.
238 */
239 ['enable-bold', null, function(v) {
240 self.syncBoldSafeState();
241 }
242 ],
243
244 /**
rginda9f5222b2012-03-05 11:53:28 -0800245 * Default font family for the terminal text.
246 */
247 ['font-family', ('"DejaVu Sans Mono", "Everson Mono", ' +
248 'FreeMono, "Menlo", "Lucida Console", ' +
249 'monospace'),
250 function(v) { self.syncFontFamily() }
251 ],
252
253 /**
rginda30f20f62012-04-05 16:36:19 -0700254 * The default font size in pixels.
255 */
256 ['font-size', 15, function(v) {
257 self.setFontSize(v);
258 }
259 ],
260
261 /**
rginda9f5222b2012-03-05 11:53:28 -0800262 * Anti-aliasing.
263 */
264 ['font-smoothing', 'antialiased',
265 function(v) { self.syncFontFamily() }
266 ],
267
268 /**
rginda30f20f62012-04-05 16:36:19 -0700269 * The foreground color for text with no other color attributes.
rginda9f5222b2012-03-05 11:53:28 -0800270 */
rginda30f20f62012-04-05 16:36:19 -0700271 ['foreground-color', 'rgb(240, 240, 240)', function(v) {
272 self.scrollPort_.setForegroundColor(v);
rginda9f5222b2012-03-05 11:53:28 -0800273 }
274 ],
275
276 /**
rginda30f20f62012-04-05 16:36:19 -0700277 * If true, home/end will control the terminal scrollbar and shift home/end
278 * will send the VT keycodes. If false then home/end sends VT codes and
279 * shift home/end scrolls.
rginda9f5222b2012-03-05 11:53:28 -0800280 */
rginda30f20f62012-04-05 16:36:19 -0700281 ['home-keys-scroll', false, function(v) {
282 self.keyboard.homeKeysScroll = v;
283 }
284 ],
285
286 /**
rginda11057d52012-04-25 12:29:56 -0700287 * Max length of a DCS, OSC, PM, or APS sequence before we give up and
288 * ignore the code.
289 */
290 ['max-string-sequence', 1024, function(v) {
291 self.vt.maxStringSequence = v;
292 }
293 ],
294
295 /**
rginda30f20f62012-04-05 16:36:19 -0700296 * Set whether the meta key sends a leading escape or not.
297 */
298 ['meta-sends-escape', true, function(v) {
299 self.keyboard.metaSendsEscape = v;
rginda9f5222b2012-03-05 11:53:28 -0800300 }
301 ],
302
303 /**
304 * If true, scroll to the bottom on any keystroke.
305 */
306 ['scroll-on-keystroke', true, function(v) {
307 self.scrollOnKeystroke_ = v;
308 }
309 ],
310
311 /**
312 * If true, scroll to the bottom on terminal output.
313 */
314 ['scroll-on-output', false, function(v) {
315 self.scrollOnOutput_ = v;
316 }
317 ],
318
319 /**
David Reveman8f552492012-03-28 12:18:41 -0400320 * The vertical scrollbar mode.
321 */
322 ['scrollbar-visible', true, function(v) {
323 self.setScrollbarVisible(v);
324 }
325 ],
rginda30f20f62012-04-05 16:36:19 -0700326
327 /**
rgindaf522ce02012-04-17 17:49:17 -0700328 * The default environment variables.
329 */
330 ['environment', {TERM: 'xterm-256color'}, null],
331
332 /**
rginda30f20f62012-04-05 16:36:19 -0700333 * If true, page up/down will control the terminal scrollbar and shift
334 * page up/down will send the VT keycodes. If false then page up/down
335 * sends VT codes and shift page up/down scrolls.
336 */
337 ['page-keys-scroll', false, function(v) {
338 self.keyboard.pageKeysScroll = v;
339 }
340 ],
341
rginda9f5222b2012-03-05 11:53:28 -0800342 ]);
343
344 if (needSync)
345 this.prefs_.notifyAll();
346};
347
348/**
349 * Return the current terminal background color.
350 *
351 * Intended for use by other classes, so we don't have to expose the entire
352 * prefs_ object.
353 */
354hterm.Terminal.prototype.getBackgroundColor = function() {
355 return this.prefs_.get('background-color');
356};
357
358/**
359 * Return the current terminal foreground color.
360 *
361 * Intended for use by other classes, so we don't have to expose the entire
362 * prefs_ object.
363 */
364hterm.Terminal.prototype.getForegroundColor = function() {
365 return this.prefs_.get('foreground-color');
366};
367
368/**
rginda87b86462011-12-14 13:48:03 -0800369 * Create a new instance of a terminal command and run it with a given
370 * argument string.
371 *
372 * @param {function} commandClass The constructor for a terminal command.
373 * @param {string} argString The argument string to pass to the command.
374 */
375hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700376 var environment = this.prefs_.get('environment');
377 if (typeof environment != 'object' || environment == null)
378 environment = {};
379
rginda87b86462011-12-14 13:48:03 -0800380 var self = this;
381 this.command = new commandClass(
382 { argString: argString || '',
383 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700384 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800385 onExit: function(code) {
386 self.io.pop();
387 self.io.println(hterm.msg('COMMAND_COMPLETE',
388 [self.command.commandName, code]));
rgindafeaf3142012-01-31 15:14:20 -0800389 self.uninstallKeyboard();
rginda87b86462011-12-14 13:48:03 -0800390 }
391 });
392
rgindafeaf3142012-01-31 15:14:20 -0800393 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800394 this.command.run();
395};
396
397/**
rgindafeaf3142012-01-31 15:14:20 -0800398 * Returns true if the current screen is the primary screen, false otherwise.
399 */
400hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700401 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800402};
403
404/**
405 * Install the keyboard handler for this terminal.
406 *
407 * This will prevent the browser from seeing any keystrokes sent to the
408 * terminal.
409 */
410hterm.Terminal.prototype.installKeyboard = function() {
411 this.keyboard.installKeyboard(this.document_.body.firstChild);
412}
413
414/**
415 * Uninstall the keyboard handler for this terminal.
416 */
417hterm.Terminal.prototype.uninstallKeyboard = function() {
418 this.keyboard.installKeyboard(null);
419}
420
421/**
rginda35c456b2012-02-09 17:29:05 -0800422 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800423 *
424 * Call setFontSize(0) to reset to the default font size.
425 *
426 * This function does not modify the font-size preference.
427 *
428 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800429 */
430hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800431 if (px === 0)
432 px = this.prefs_.get('font-size');
433
rginda35c456b2012-02-09 17:29:05 -0800434 this.scrollPort_.setFontSize(px);
435};
436
437/**
438 * Get the current font size.
439 */
440hterm.Terminal.prototype.getFontSize = function() {
441 return this.scrollPort_.getFontSize();
442};
443
444/**
445 * Set the CSS "font-family" for this terminal.
446 */
rginda9f5222b2012-03-05 11:53:28 -0800447hterm.Terminal.prototype.syncFontFamily = function() {
448 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
449 this.prefs_.get('font-smoothing'));
450 this.syncBoldSafeState();
451};
452
453hterm.Terminal.prototype.syncBoldSafeState = function() {
454 var enableBold = this.prefs_.get('enable-bold');
455 if (enableBold !== null) {
456 this.screen_.textAttributes.enableBold = enableBold;
457 return;
458 }
459
rgindaf7521392012-02-28 17:20:34 -0800460 var normalSize = this.scrollPort_.measureCharacterSize();
461 var boldSize = this.scrollPort_.measureCharacterSize('bold');
462
463 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800464 if (!isBoldSafe) {
465 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700466 'from normal. Font family is: ' +
467 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800468 }
rginda9f5222b2012-03-05 11:53:28 -0800469
470 this.screen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800471};
472
473/**
rginda87b86462011-12-14 13:48:03 -0800474 * Return a copy of the current cursor position.
475 *
476 * @return {hterm.RowCol} The RowCol object representing the current position.
477 */
478hterm.Terminal.prototype.saveCursor = function() {
479 return this.screen_.cursorPosition.clone();
480};
481
rgindaa19afe22012-01-25 15:40:22 -0800482hterm.Terminal.prototype.getTextAttributes = function() {
483 return this.screen_.textAttributes;
484};
485
rginda87b86462011-12-14 13:48:03 -0800486/**
rgindaf522ce02012-04-17 17:49:17 -0700487 * Return the current browser zoom factor applied to the terminal.
488 *
489 * @return {number} The current browser zoom factor.
490 */
491hterm.Terminal.prototype.getZoomFactor = function() {
492 return this.scrollPort_.characterSize.zoomFactor;
493};
494
495/**
rginda9846e2f2012-01-27 13:53:33 -0800496 * Change the title of this terminal's window.
497 */
498hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800499 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800500};
501
502/**
rginda87b86462011-12-14 13:48:03 -0800503 * Restore a previously saved cursor position.
504 *
505 * @param {hterm.RowCol} cursor The position to restore.
506 */
507hterm.Terminal.prototype.restoreCursor = function(cursor) {
rginda35c456b2012-02-09 17:29:05 -0800508 var row = hterm.clamp(cursor.row, 0, this.screenSize.height - 1);
509 var column = hterm.clamp(cursor.column, 0, this.screenSize.width - 1);
510 this.screen_.setCursorPosition(row, column);
511 if (cursor.column > column ||
512 cursor.column == column && cursor.overflow) {
513 this.screen_.cursorPosition.overflow = true;
514 }
rginda87b86462011-12-14 13:48:03 -0800515};
516
517/**
518 * Set the width of the terminal, resizing the UI to match.
519 */
520hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800521 if (columnCount == null) {
522 this.div_.style.width = '100%';
523 return;
524 }
525
rginda35c456b2012-02-09 17:29:05 -0800526 this.div_.style.width = this.scrollPort_.characterSize.width *
527 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400528 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800529 this.scheduleSyncCursorPosition_();
530};
rginda87b86462011-12-14 13:48:03 -0800531
rgindac9bc5502012-01-18 11:48:44 -0800532/**
rginda35c456b2012-02-09 17:29:05 -0800533 * Set the height of the terminal, resizing the UI to match.
534 */
535hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800536 if (rowCount == null) {
537 this.div_.style.height = '100%';
538 return;
539 }
540
rginda35c456b2012-02-09 17:29:05 -0800541 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700542 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800543 this.realizeSize_(this.screenSize.width, rowCount);
544 this.scheduleSyncCursorPosition_();
545};
546
547/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400548 * Deal with terminal size changes.
549 *
550 */
551hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
552 if (columnCount != this.screenSize.width)
553 this.realizeWidth_(columnCount);
554
555 if (rowCount != this.screenSize.height)
556 this.realizeHeight_(rowCount);
557
558 // Send new terminal size to plugin.
559 this.io.onTerminalResize(columnCount, rowCount);
560};
561
562/**
rgindac9bc5502012-01-18 11:48:44 -0800563 * Deal with terminal width changes.
564 *
565 * This function does what needs to be done when the terminal width changes
566 * out from under us. It happens here rather than in onResize_() because this
567 * code may need to run synchronously to handle programmatic changes of
568 * terminal width.
569 *
570 * Relying on the browser to send us an async resize event means we may not be
571 * in the correct state yet when the next escape sequence hits.
572 */
573hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
574 var deltaColumns = columnCount - this.screen_.getWidth();
575
rginda87b86462011-12-14 13:48:03 -0800576 this.screenSize.width = columnCount;
577 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800578
579 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400580 if (this.defaultTabStops)
581 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800582 } else {
583 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400584 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800585 break;
586
587 this.tabStops_.pop();
588 }
589 }
590
591 this.screen_.setColumnCount(this.screenSize.width);
592};
593
594/**
595 * Deal with terminal height changes.
596 *
597 * This function does what needs to be done when the terminal height changes
598 * out from under us. It happens here rather than in onResize_() because this
599 * code may need to run synchronously to handle programmatic changes of
600 * terminal height.
601 *
602 * Relying on the browser to send us an async resize event means we may not be
603 * in the correct state yet when the next escape sequence hits.
604 */
605hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
606 var deltaRows = rowCount - this.screen_.getHeight();
607
608 this.screenSize.height = rowCount;
609
610 var cursor = this.saveCursor();
611
612 if (deltaRows < 0) {
613 // Screen got smaller.
614 deltaRows *= -1;
615 while (deltaRows) {
616 var lastRow = this.getRowCount() - 1;
617 if (lastRow - this.scrollbackRows_.length == cursor.row)
618 break;
619
620 if (this.getRowText(lastRow))
621 break;
622
623 this.screen_.popRow();
624 deltaRows--;
625 }
626
627 var ary = this.screen_.shiftRows(deltaRows);
628 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
629
630 // We just removed rows from the top of the screen, we need to update
631 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800632 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800633 } else if (deltaRows > 0) {
634 // Screen got larger.
635
636 if (deltaRows <= this.scrollbackRows_.length) {
637 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
638 var rows = this.scrollbackRows_.splice(
639 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
640 this.screen_.unshiftRows(rows);
641 deltaRows -= scrollbackCount;
642 cursor.row += scrollbackCount;
643 }
644
645 if (deltaRows)
646 this.appendRows_(deltaRows);
647 }
648
rginda35c456b2012-02-09 17:29:05 -0800649 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800650 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800651};
652
653/**
654 * Scroll the terminal to the top of the scrollback buffer.
655 */
656hterm.Terminal.prototype.scrollHome = function() {
657 this.scrollPort_.scrollRowToTop(0);
658};
659
660/**
661 * Scroll the terminal to the end.
662 */
663hterm.Terminal.prototype.scrollEnd = function() {
664 this.scrollPort_.scrollRowToBottom(this.getRowCount());
665};
666
667/**
668 * Scroll the terminal one page up (minus one line) relative to the current
669 * position.
670 */
671hterm.Terminal.prototype.scrollPageUp = function() {
672 var i = this.scrollPort_.getTopRowIndex();
673 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
674};
675
676/**
677 * Scroll the terminal one page down (minus one line) relative to the current
678 * position.
679 */
680hterm.Terminal.prototype.scrollPageDown = function() {
681 var i = this.scrollPort_.getTopRowIndex();
682 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800683};
684
rgindac9bc5502012-01-18 11:48:44 -0800685/**
686 * Full terminal reset.
687 */
rginda87b86462011-12-14 13:48:03 -0800688hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800689 this.clearAllTabStops();
690 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700691
692 this.clearHome(this.primaryScreen_);
693 this.primaryScreen_.textAttributes.reset();
694
695 this.clearHome(this.alternateScreen_);
696 this.alternateScreen_.textAttributes.reset();
697
rgindab8bc8932012-04-27 12:45:03 -0700698 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
699
rgindac9bc5502012-01-18 11:48:44 -0800700 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800701};
702
rgindac9bc5502012-01-18 11:48:44 -0800703/**
704 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700705 *
706 * Perform a soft reset to the default values listed in
707 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800708 */
rginda0f5c0292012-01-13 11:00:13 -0800709hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700710 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800711 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700712
rgindab8bc8932012-04-27 12:45:03 -0700713 // Xterm also resets the color palette on soft reset, even though it doesn't
714 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700715 this.primaryScreen_.textAttributes.resetColorPalette();
716 this.alternateScreen_.textAttributes.resetColorPalette();
717
rgindab8bc8932012-04-27 12:45:03 -0700718 // The xterm man page explicitly says this will happen on soft reset.
719 this.setVTScrollRegion(null, null);
720
721 // Xterm also shows the cursor on soft reset, but does not alter the blink
722 // state.
rgindaa19afe22012-01-25 15:40:22 -0800723 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800724};
725
rgindac9bc5502012-01-18 11:48:44 -0800726/**
727 * Move the cursor forward to the next tab stop, or to the last column
728 * if no more tab stops are set.
729 */
730hterm.Terminal.prototype.forwardTabStop = function() {
731 var column = this.screen_.cursorPosition.column;
732
733 for (var i = 0; i < this.tabStops_.length; i++) {
734 if (this.tabStops_[i] > column) {
735 this.setCursorColumn(this.tabStops_[i]);
736 return;
737 }
738 }
739
David Benjamin66e954d2012-05-05 21:08:12 -0400740 // xterm does not clear the overflow flag on HT or CHT.
741 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800742 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400743 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800744};
745
rgindac9bc5502012-01-18 11:48:44 -0800746/**
747 * Move the cursor backward to the previous tab stop, or to the first column
748 * if no previous tab stops are set.
749 */
750hterm.Terminal.prototype.backwardTabStop = function() {
751 var column = this.screen_.cursorPosition.column;
752
753 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
754 if (this.tabStops_[i] < column) {
755 this.setCursorColumn(this.tabStops_[i]);
756 return;
757 }
758 }
759
760 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800761};
762
rgindac9bc5502012-01-18 11:48:44 -0800763/**
764 * Set a tab stop at the given column.
765 *
766 * @param {int} column Zero based column.
767 */
768hterm.Terminal.prototype.setTabStop = function(column) {
769 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
770 if (this.tabStops_[i] == column)
771 return;
772
773 if (this.tabStops_[i] < column) {
774 this.tabStops_.splice(i + 1, 0, column);
775 return;
776 }
777 }
778
779 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800780};
781
rgindac9bc5502012-01-18 11:48:44 -0800782/**
783 * Clear the tab stop at the current cursor position.
784 *
785 * No effect if there is no tab stop at the current cursor position.
786 */
787hterm.Terminal.prototype.clearTabStopAtCursor = function() {
788 var column = this.screen_.cursorPosition.column;
789
790 var i = this.tabStops_.indexOf(column);
791 if (i == -1)
792 return;
793
794 this.tabStops_.splice(i, 1);
795};
796
797/**
798 * Clear all tab stops.
799 */
800hterm.Terminal.prototype.clearAllTabStops = function() {
801 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -0400802 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -0800803};
804
805/**
806 * Set up the default tab stops, starting from a given column.
807 *
808 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -0400809 * from the specified column, or 0 if no column is provided. It also flags
810 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -0800811 *
812 * This does not clear the existing tab stops first, use clearAllTabStops
813 * for that.
814 *
815 * @param {int} opt_start Optional starting zero based starting column, useful
816 * for filling out missing tab stops when the terminal is resized.
817 */
818hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
819 var start = opt_start || 0;
820 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -0400821 // Round start up to a default tab stop.
822 start = start - 1 - ((start - 1) % w) + w;
823 for (var i = start; i < this.screenSize.width; i += w) {
824 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -0800825 }
David Benjamin66e954d2012-05-05 21:08:12 -0400826
827 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -0800828};
829
rginda6d397402012-01-17 10:58:29 -0800830/**
831 * Save cursor position and attributes.
832 *
833 * TODO(rginda): Save attributes once we support them.
834 */
rginda87b86462011-12-14 13:48:03 -0800835hterm.Terminal.prototype.saveOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800836 this.savedOptions_.cursor = this.saveCursor();
rgindaa19afe22012-01-25 15:40:22 -0800837 this.savedOptions_.textAttributes = this.screen_.textAttributes.clone();
rginda87b86462011-12-14 13:48:03 -0800838};
839
rginda6d397402012-01-17 10:58:29 -0800840/**
841 * Restore cursor position and attributes.
842 *
843 * TODO(rginda): Restore attributes once we support them.
844 */
rginda87b86462011-12-14 13:48:03 -0800845hterm.Terminal.prototype.restoreOptions = function() {
rginda6d397402012-01-17 10:58:29 -0800846 if (this.savedOptions_.cursor)
847 this.restoreCursor(this.savedOptions_.cursor);
rgindaa19afe22012-01-25 15:40:22 -0800848 if (this.savedOptions_.textAttributes)
849 this.screen_.textAttributes = this.savedOptions_.textAttributes;
rginda8ba33642011-12-14 12:31:31 -0800850};
851
852/**
853 * Interpret a sequence of characters.
854 *
855 * Incomplete escape sequences are buffered until the next call.
856 *
857 * @param {string} str Sequence of characters to interpret or pass through.
858 */
859hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800860 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800861 this.scheduleSyncCursorPosition_();
862};
863
864/**
865 * Take over the given DIV for use as the terminal display.
866 *
867 * @param {HTMLDivElement} div The div to use as the terminal display.
868 */
869hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800870 this.div_ = div;
871
rginda8ba33642011-12-14 12:31:31 -0800872 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -0700873 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
874
rginda0918b652012-04-04 11:26:24 -0700875 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -0800876
rginda9f5222b2012-03-05 11:53:28 -0800877 this.setFontSize(this.prefs_.get('font-size'));
878 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -0800879
David Reveman8f552492012-03-28 12:18:41 -0400880 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
881
rginda8ba33642011-12-14 12:31:31 -0800882 this.document_ = this.scrollPort_.getDocument();
883
rginda8ba33642011-12-14 12:31:31 -0800884 this.cursorNode_ = this.document_.createElement('div');
885 this.cursorNode_.style.cssText =
886 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800887 'top: -99px;' +
888 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -0800889 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
890 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda6d397402012-01-17 10:58:29 -0800891 '-webkit-transition: opacity, background-color 100ms linear;' +
rginda9f5222b2012-03-05 11:53:28 -0800892 'background-color: ' + this.prefs_.get('cursor-color'));
rginda8ba33642011-12-14 12:31:31 -0800893 this.document_.body.appendChild(this.cursorNode_);
894
rgindade84e382012-04-20 15:39:31 -0700895 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda8ba33642011-12-14 12:31:31 -0800896 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -0800897
rginda87b86462011-12-14 13:48:03 -0800898 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -0800899 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -0800900};
901
rginda0918b652012-04-04 11:26:24 -0700902/**
903 * Return the HTML document that contains the terminal DOM nodes.
904 */
rginda87b86462011-12-14 13:48:03 -0800905hterm.Terminal.prototype.getDocument = function() {
906 return this.document_;
rginda8ba33642011-12-14 12:31:31 -0800907};
908
909/**
rginda0918b652012-04-04 11:26:24 -0700910 * Focus the terminal.
911 */
912hterm.Terminal.prototype.focus = function() {
913 this.scrollPort_.focus();
914};
915
916/**
rginda8ba33642011-12-14 12:31:31 -0800917 * Return the HTML Element for a given row index.
918 *
919 * This is a method from the RowProvider interface. The ScrollPort uses
920 * it to fetch rows on demand as they are scrolled into view.
921 *
922 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
923 * pairs to conserve memory.
924 *
925 * @param {integer} index The zero-based row index, measured relative to the
926 * start of the scrollback buffer. On-screen rows will always have the
927 * largest indicies.
928 * @return {HTMLElement} The 'x-row' element containing for the requested row.
929 */
930hterm.Terminal.prototype.getRowNode = function(index) {
931 if (index < this.scrollbackRows_.length)
932 return this.scrollbackRows_[index];
933
934 var screenIndex = index - this.scrollbackRows_.length;
935 return this.screen_.rowsArray[screenIndex];
936};
937
938/**
939 * Return the text content for a given range of rows.
940 *
941 * This is a method from the RowProvider interface. The ScrollPort uses
942 * it to fetch text content on demand when the user attempts to copy their
943 * selection to the clipboard.
944 *
945 * @param {integer} start The zero-based row index to start from, measured
946 * relative to the start of the scrollback buffer. On-screen rows will
947 * always have the largest indicies.
948 * @param {integer} end The zero-based row index to end on, measured
949 * relative to the start of the scrollback buffer.
950 * @return {string} A single string containing the text value of the range of
951 * rows. Lines will be newline delimited, with no trailing newline.
952 */
953hterm.Terminal.prototype.getRowsText = function(start, end) {
954 var ary = [];
955 for (var i = start; i < end; i++) {
956 var node = this.getRowNode(i);
957 ary.push(node.textContent);
958 }
959
960 return ary.join('\n');
961};
962
963/**
964 * Return the text content for a given row.
965 *
966 * This is a method from the RowProvider interface. The ScrollPort uses
967 * it to fetch text content on demand when the user attempts to copy their
968 * selection to the clipboard.
969 *
970 * @param {integer} index The zero-based row index to return, measured
971 * relative to the start of the scrollback buffer. On-screen rows will
972 * always have the largest indicies.
973 * @return {string} A string containing the text value of the selected row.
974 */
975hterm.Terminal.prototype.getRowText = function(index) {
976 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -0800977 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -0800978};
979
980/**
981 * Return the total number of rows in the addressable screen and in the
982 * scrollback buffer of this terminal.
983 *
984 * This is a method from the RowProvider interface. The ScrollPort uses
985 * it to compute the size of the scrollbar.
986 *
987 * @return {integer} The number of rows in this terminal.
988 */
989hterm.Terminal.prototype.getRowCount = function() {
990 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
991};
992
993/**
994 * Create DOM nodes for new rows and append them to the end of the terminal.
995 *
996 * This is the only correct way to add a new DOM node for a row. Notice that
997 * the new row is appended to the bottom of the list of rows, and does not
998 * require renumbering (of the rowIndex property) of previous rows.
999 *
1000 * If you think you want a new blank row somewhere in the middle of the
1001 * terminal, look into moveRows_().
1002 *
1003 * This method does not pay attention to vtScrollTop/Bottom, since you should
1004 * be using moveRows() in cases where they would matter.
1005 *
1006 * The cursor will be positioned at column 0 of the first inserted line.
1007 */
1008hterm.Terminal.prototype.appendRows_ = function(count) {
1009 var cursorRow = this.screen_.rowsArray.length;
1010 var offset = this.scrollbackRows_.length + cursorRow;
1011 for (var i = 0; i < count; i++) {
1012 var row = this.document_.createElement('x-row');
1013 row.appendChild(this.document_.createTextNode(''));
1014 row.rowIndex = offset + i;
1015 this.screen_.pushRow(row);
1016 }
1017
1018 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1019 if (extraRows > 0) {
1020 var ary = this.screen_.shiftRows(extraRows);
1021 Array.prototype.push.apply(this.scrollbackRows_, ary);
1022 this.scheduleScrollDown_();
1023 }
1024
1025 if (cursorRow >= this.screen_.rowsArray.length)
1026 cursorRow = this.screen_.rowsArray.length - 1;
1027
rginda87b86462011-12-14 13:48:03 -08001028 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001029};
1030
1031/**
1032 * Relocate rows from one part of the addressable screen to another.
1033 *
1034 * This is used to recycle rows during VT scrolls (those which are driven
1035 * by VT commands, rather than by the user manipulating the scrollbar.)
1036 *
1037 * In this case, the blank lines scrolled into the scroll region are made of
1038 * the nodes we scrolled off. These have their rowIndex properties carefully
1039 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001040 */
1041hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1042 var ary = this.screen_.removeRows(fromIndex, count);
1043 this.screen_.insertRows(toIndex, ary);
1044
1045 var start, end;
1046 if (fromIndex < toIndex) {
1047 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001048 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001049 } else {
1050 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001051 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001052 }
1053
1054 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001055 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001056};
1057
1058/**
1059 * Renumber the rowIndex property of the given range of rows.
1060 *
1061 * The start and end indicies are relative to the screen, not the scrollback.
1062 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001063 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001064 * no need to renumber scrollback rows.
1065 */
1066hterm.Terminal.prototype.renumberRows_ = function(start, end) {
1067 var offset = this.scrollbackRows_.length;
1068 for (var i = start; i < end; i++) {
1069 this.screen_.rowsArray[i].rowIndex = offset + i;
1070 }
1071};
1072
1073/**
1074 * Print a string to the terminal.
1075 *
1076 * This respects the current insert and wraparound modes. It will add new lines
1077 * to the end of the terminal, scrolling off the top into the scrollback buffer
1078 * if necessary.
1079 *
1080 * The string is *not* parsed for escape codes. Use the interpret() method if
1081 * that's what you're after.
1082 *
1083 * @param{string} str The string to print.
1084 */
1085hterm.Terminal.prototype.print = function(str) {
rgindaa19afe22012-01-25 15:40:22 -08001086 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
1087 this.newLine();
rginda2312fff2012-01-05 16:20:52 -08001088
rgindaa19afe22012-01-25 15:40:22 -08001089 if (this.options_.insertMode) {
1090 this.screen_.insertString(str);
1091 } else {
1092 this.screen_.overwriteString(str);
1093 }
1094
1095 var overflow = this.screen_.maybeClipCurrentRow();
1096
1097 if (this.options_.wraparound && overflow) {
1098 var lastColumn;
1099
1100 do {
rginda35c456b2012-02-09 17:29:05 -08001101 this.newLine();
1102 lastColumn = overflow.characterLength;
rgindaa19afe22012-01-25 15:40:22 -08001103
1104 if (!this.options_.insertMode)
1105 this.screen_.deleteChars(overflow.characterLength);
1106
1107 this.screen_.prependNodes(overflow);
rgindaa19afe22012-01-25 15:40:22 -08001108
1109 overflow = this.screen_.maybeClipCurrentRow();
1110 } while (overflow);
1111
1112 this.setCursorColumn(lastColumn);
1113 }
rginda8ba33642011-12-14 12:31:31 -08001114
1115 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001116
rginda9f5222b2012-03-05 11:53:28 -08001117 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001118 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001119};
1120
1121/**
rginda87b86462011-12-14 13:48:03 -08001122 * Set the VT scroll region.
1123 *
rginda87b86462011-12-14 13:48:03 -08001124 * This also resets the cursor position to the absolute (0, 0) position, since
1125 * that's what xterm appears to do.
1126 *
1127 * @param {integer} scrollTop The zero-based top of the scroll region.
1128 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1129 * inclusive.
1130 */
1131hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1132 this.vtScrollTop_ = scrollTop;
1133 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001134};
1135
1136/**
rginda8ba33642011-12-14 12:31:31 -08001137 * Return the top row index according to the VT.
1138 *
1139 * This will return 0 unless the terminal has been told to restrict scrolling
1140 * to some lower row. It is used for some VT cursor positioning and scrolling
1141 * commands.
1142 *
1143 * @return {integer} The topmost row in the terminal's scroll region.
1144 */
1145hterm.Terminal.prototype.getVTScrollTop = function() {
1146 if (this.vtScrollTop_ != null)
1147 return this.vtScrollTop_;
1148
1149 return 0;
rginda87b86462011-12-14 13:48:03 -08001150};
rginda8ba33642011-12-14 12:31:31 -08001151
1152/**
1153 * Return the bottom row index according to the VT.
1154 *
1155 * This will return the height of the terminal unless the it has been told to
1156 * restrict scrolling to some higher row. It is used for some VT cursor
1157 * positioning and scrolling commands.
1158 *
1159 * @return {integer} The bottommost row in the terminal's scroll region.
1160 */
1161hterm.Terminal.prototype.getVTScrollBottom = function() {
1162 if (this.vtScrollBottom_ != null)
1163 return this.vtScrollBottom_;
1164
rginda87b86462011-12-14 13:48:03 -08001165 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001166}
1167
1168/**
1169 * Process a '\n' character.
1170 *
1171 * If the cursor is on the final row of the terminal this will append a new
1172 * blank row to the screen and scroll the topmost row into the scrollback
1173 * buffer.
1174 *
1175 * Otherwise, this moves the cursor to column zero of the next row.
1176 */
1177hterm.Terminal.prototype.newLine = function() {
1178 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001179 // If we're at the end of the screen we need to append a new line and
1180 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001181 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001182 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1183 // End of the scroll region does not affect the scrollback buffer.
1184 this.vtScrollUp(1);
1185 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001186 } else {
rginda87b86462011-12-14 13:48:03 -08001187 // Anywhere else in the screen just moves the cursor.
1188 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001189 }
1190};
1191
1192/**
1193 * Like newLine(), except maintain the cursor column.
1194 */
1195hterm.Terminal.prototype.lineFeed = function() {
1196 var column = this.screen_.cursorPosition.column;
1197 this.newLine();
1198 this.setCursorColumn(column);
1199};
1200
1201/**
rginda87b86462011-12-14 13:48:03 -08001202 * If autoCarriageReturn is set then newLine(), else lineFeed().
1203 */
1204hterm.Terminal.prototype.formFeed = function() {
1205 if (this.options_.autoCarriageReturn) {
1206 this.newLine();
1207 } else {
1208 this.lineFeed();
1209 }
1210};
1211
1212/**
1213 * Move the cursor up one row, possibly inserting a blank line.
1214 *
1215 * The cursor column is not changed.
1216 */
1217hterm.Terminal.prototype.reverseLineFeed = function() {
1218 var scrollTop = this.getVTScrollTop();
1219 var currentRow = this.screen_.cursorPosition.row;
1220
1221 if (currentRow == scrollTop) {
1222 this.insertLines(1);
1223 } else {
1224 this.setAbsoluteCursorRow(currentRow - 1);
1225 }
1226};
1227
1228/**
rginda8ba33642011-12-14 12:31:31 -08001229 * Replace all characters to the left of the current cursor with the space
1230 * character.
1231 *
1232 * TODO(rginda): This should probably *remove* the characters (not just replace
1233 * with a space) if there are no characters at or beyond the current cursor
1234 * position. Once it does that, it'll have the same text-attribute related
1235 * issues as hterm.Screen.prototype.clearCursorRow :/
1236 */
1237hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001238 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001239 this.setCursorColumn(0);
rginda87b86462011-12-14 13:48:03 -08001240 this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1));
1241 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001242};
1243
1244/**
1245 * Erase a given number of characters to the right of the cursor, shifting
1246 * remaining characters to the left.
1247 *
1248 * The cursor position is unchanged.
1249 *
1250 * TODO(rginda): Test that this works even when the cursor is positioned beyond
1251 * the end of the text.
1252 *
1253 * TODO(rginda): This likely has text-attribute related troubles similar to the
1254 * todo on hterm.Screen.prototype.clearCursorRow.
1255 */
1256hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001257 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001258
rginda87b86462011-12-14 13:48:03 -08001259 var maxCount = this.screenSize.width - cursor.column;
rginda8ba33642011-12-14 12:31:31 -08001260 var count = (opt_count && opt_count < maxCount) ? opt_count : maxCount;
1261 this.screen_.deleteChars(count);
rginda87b86462011-12-14 13:48:03 -08001262 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001263};
1264
1265/**
1266 * Erase the current line.
1267 *
1268 * The cursor position is unchanged.
1269 *
1270 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1271 * has a text-attribute related TODO.
1272 */
1273hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001274 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001275 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001276 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001277};
1278
1279/**
1280 * Erase all characters from the start of the scroll region to the current
1281 * cursor position.
1282 *
1283 * The cursor position is unchanged.
1284 *
1285 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1286 * has a text-attribute related TODO.
1287 */
1288hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001289 var cursor = this.saveCursor();
1290
1291 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001292
1293 var top = this.getVTScrollTop();
rginda87b86462011-12-14 13:48:03 -08001294 for (var i = top; i < cursor.row; i++) {
1295 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001296 this.screen_.clearCursorRow();
1297 }
1298
rginda87b86462011-12-14 13:48:03 -08001299 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001300};
1301
1302/**
1303 * Erase all characters from the current cursor position to the end of the
1304 * scroll region.
1305 *
1306 * The cursor position is unchanged.
1307 *
1308 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1309 * has a text-attribute related TODO.
1310 */
1311hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001312 var cursor = this.saveCursor();
1313
1314 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001315
1316 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001317 for (var i = cursor.row + 1; i <= bottom; i++) {
1318 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001319 this.screen_.clearCursorRow();
1320 }
1321
rginda87b86462011-12-14 13:48:03 -08001322 this.restoreCursor(cursor);
1323};
1324
1325/**
1326 * Fill the terminal with a given character.
1327 *
1328 * This methods does not respect the VT scroll region.
1329 *
1330 * @param {string} ch The character to use for the fill.
1331 */
1332hterm.Terminal.prototype.fill = function(ch) {
1333 var cursor = this.saveCursor();
1334
1335 this.setAbsoluteCursorPosition(0, 0);
1336 for (var row = 0; row < this.screenSize.height; row++) {
1337 for (var col = 0; col < this.screenSize.width; col++) {
1338 this.setAbsoluteCursorPosition(row, col);
1339 this.screen_.overwriteString(ch);
1340 }
1341 }
1342
1343 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001344};
1345
1346/**
rginda9ea433c2012-03-16 11:57:00 -07001347 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001348 *
rginda9ea433c2012-03-16 11:57:00 -07001349 * This does not respect the scroll region.
1350 *
1351 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1352 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001353 *
1354 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1355 * has a text-attribute related TODO.
1356 */
rginda9ea433c2012-03-16 11:57:00 -07001357hterm.Terminal.prototype.clearHome = function(opt_screen) {
1358 var screen = opt_screen || this.screen_;
1359 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001360
rginda11057d52012-04-25 12:29:56 -07001361 if (bottom == 0) {
1362 // Empty screen, nothing to do.
1363 return;
1364 }
1365
rgindae4d29232012-01-19 10:47:13 -08001366 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001367 screen.setCursorPosition(i, 0);
1368 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001369 }
1370
rginda9ea433c2012-03-16 11:57:00 -07001371 screen.setCursorPosition(0, 0);
1372};
1373
1374/**
1375 * Erase the entire display without changing the cursor position.
1376 *
1377 * The cursor position is unchanged. This does not respect the scroll
1378 * region.
1379 *
1380 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1381 * to the current screen.
1382 *
1383 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1384 * has a text-attribute related TODO.
1385 */
1386hterm.Terminal.prototype.clear = function(opt_screen) {
1387 var screen = opt_screen || this.screen_;
1388 var cursor = screen.cursorPosition.clone();
1389 this.clearHome(screen);
1390 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001391};
1392
1393/**
1394 * VT command to insert lines at the current cursor row.
1395 *
1396 * This respects the current scroll region. Rows pushed off the bottom are
1397 * lost (they won't show up in the scrollback buffer).
1398 *
1399 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1400 * has a text-attribute related TODO.
1401 *
1402 * @param {integer} count The number of lines to insert.
1403 */
1404hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001405 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001406
1407 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001408 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001409
rgindae4d29232012-01-19 10:47:13 -08001410 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001411 if (start != cursor.row)
1412 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001413
1414 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001415 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001416 this.screen_.clearCursorRow();
1417 }
1418
rginda87b86462011-12-14 13:48:03 -08001419 cursor.column = 0;
1420 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001421};
1422
1423/**
1424 * VT command to delete lines at the current cursor row.
1425 *
1426 * New rows are added to the bottom of scroll region to take their place. New
1427 * rows are strictly there to take up space and have no content or style.
1428 */
1429hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001430 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001431
rginda87b86462011-12-14 13:48:03 -08001432 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001433 var bottom = this.getVTScrollBottom();
1434
rginda87b86462011-12-14 13:48:03 -08001435 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001436 count = Math.min(count, maxCount);
1437
rginda87b86462011-12-14 13:48:03 -08001438 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001439 if (count != maxCount)
1440 this.moveRows_(top, count, moveStart);
1441
1442 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001443 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001444 this.screen_.clearCursorRow();
1445 }
1446
rginda87b86462011-12-14 13:48:03 -08001447 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001448};
1449
1450/**
1451 * Inserts the given number of spaces at the current cursor position.
1452 *
rginda87b86462011-12-14 13:48:03 -08001453 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001454 */
1455hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001456 var cursor = this.saveCursor();
1457
rginda0f5c0292012-01-13 11:00:13 -08001458 var ws = hterm.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001459 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001460 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001461
1462 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001463};
1464
1465/**
1466 * Forward-delete the specified number of characters starting at the cursor
1467 * position.
1468 *
1469 * @param {integer} count The number of characters to delete.
1470 */
1471hterm.Terminal.prototype.deleteChars = function(count) {
1472 this.screen_.deleteChars(count);
1473};
1474
1475/**
1476 * Shift rows in the scroll region upwards by a given number of lines.
1477 *
1478 * New rows are inserted at the bottom of the scroll region to fill the
1479 * vacated rows. The new rows not filled out with the current text attributes.
1480 *
1481 * This function does not affect the scrollback rows at all. Rows shifted
1482 * off the top are lost.
1483 *
rginda87b86462011-12-14 13:48:03 -08001484 * The cursor position is not altered.
1485 *
rginda8ba33642011-12-14 12:31:31 -08001486 * @param {integer} count The number of rows to scroll.
1487 */
1488hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001489 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001490
rginda87b86462011-12-14 13:48:03 -08001491 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001492 this.deleteLines(count);
1493
rginda87b86462011-12-14 13:48:03 -08001494 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001495};
1496
1497/**
1498 * Shift rows below the cursor down by a given number of lines.
1499 *
1500 * This function respects the current scroll region.
1501 *
1502 * New rows are inserted at the top of the scroll region to fill the
1503 * vacated rows. The new rows not filled out with the current text attributes.
1504 *
1505 * This function does not affect the scrollback rows at all. Rows shifted
1506 * off the bottom are lost.
1507 *
1508 * @param {integer} count The number of rows to scroll.
1509 */
1510hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001511 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001512
rginda87b86462011-12-14 13:48:03 -08001513 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001514 this.insertLines(opt_count);
1515
rginda87b86462011-12-14 13:48:03 -08001516 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001517};
1518
rginda87b86462011-12-14 13:48:03 -08001519
rginda8ba33642011-12-14 12:31:31 -08001520/**
1521 * Set the cursor position.
1522 *
1523 * The cursor row is relative to the scroll region if the terminal has
1524 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1525 *
1526 * @param {integer} row The new zero-based cursor row.
1527 * @param {integer} row The new zero-based cursor column.
1528 */
1529hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1530 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001531 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001532 } else {
rginda87b86462011-12-14 13:48:03 -08001533 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001534 }
rginda87b86462011-12-14 13:48:03 -08001535};
rginda8ba33642011-12-14 12:31:31 -08001536
rginda87b86462011-12-14 13:48:03 -08001537hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1538 var scrollTop = this.getVTScrollTop();
1539 row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
rginda2312fff2012-01-05 16:20:52 -08001540 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001541 this.screen_.setCursorPosition(row, column);
1542};
1543
1544hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rginda2312fff2012-01-05 16:20:52 -08001545 row = hterm.clamp(row, 0, this.screenSize.height - 1);
1546 column = hterm.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001547 this.screen_.setCursorPosition(row, column);
1548};
1549
1550/**
1551 * Set the cursor column.
1552 *
1553 * @param {integer} column The new zero-based cursor column.
1554 */
1555hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001556 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001557};
1558
1559/**
1560 * Return the cursor column.
1561 *
1562 * @return {integer} The zero-based cursor column.
1563 */
1564hterm.Terminal.prototype.getCursorColumn = function() {
1565 return this.screen_.cursorPosition.column;
1566};
1567
1568/**
1569 * Set the cursor row.
1570 *
1571 * The cursor row is relative to the scroll region if the terminal has
1572 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1573 *
1574 * @param {integer} row The new cursor row.
1575 */
rginda87b86462011-12-14 13:48:03 -08001576hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1577 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001578};
1579
1580/**
1581 * Return the cursor row.
1582 *
1583 * @return {integer} The zero-based cursor row.
1584 */
1585hterm.Terminal.prototype.getCursorRow = function(row) {
1586 return this.screen_.cursorPosition.row;
1587};
1588
1589/**
1590 * Request that the ScrollPort redraw itself soon.
1591 *
1592 * The redraw will happen asynchronously, soon after the call stack winds down.
1593 * Multiple calls will be coalesced into a single redraw.
1594 */
1595hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001596 if (this.timeouts_.redraw)
1597 return;
rginda8ba33642011-12-14 12:31:31 -08001598
1599 var self = this;
rginda87b86462011-12-14 13:48:03 -08001600 this.timeouts_.redraw = setTimeout(function() {
1601 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001602 self.scrollPort_.redraw_();
1603 }, 0);
1604};
1605
1606/**
1607 * Request that the ScrollPort be scrolled to the bottom.
1608 *
1609 * The scroll will happen asynchronously, soon after the call stack winds down.
1610 * Multiple calls will be coalesced into a single scroll.
1611 *
1612 * This affects the scrollbar position of the ScrollPort, and has nothing to
1613 * do with the VT scroll commands.
1614 */
1615hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1616 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001617 return;
rginda8ba33642011-12-14 12:31:31 -08001618
1619 var self = this;
1620 this.timeouts_.scrollDown = setTimeout(function() {
1621 delete self.timeouts_.scrollDown;
1622 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1623 }, 10);
1624};
1625
1626/**
1627 * Move the cursor up a specified number of rows.
1628 *
1629 * @param {integer} count The number of rows to move the cursor.
1630 */
1631hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001632 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001633};
1634
1635/**
1636 * Move the cursor down a specified number of rows.
1637 *
1638 * @param {integer} count The number of rows to move the cursor.
1639 */
1640hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001641 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001642 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1643 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1644 this.screenSize.height - 1);
1645
1646 var row = hterm.clamp(this.screen_.cursorPosition.row + count,
1647 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001648 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001649};
1650
1651/**
1652 * Move the cursor left a specified number of columns.
1653 *
1654 * @param {integer} count The number of columns to move the cursor.
1655 */
1656hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001657 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001658};
1659
1660/**
1661 * Move the cursor right a specified number of columns.
1662 *
1663 * @param {integer} count The number of columns to move the cursor.
1664 */
1665hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001666 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001667 var column = hterm.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001668 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001669 this.setCursorColumn(column);
1670};
1671
1672/**
1673 * Reverse the foreground and background colors of the terminal.
1674 *
1675 * This only affects text that was drawn with no attributes.
1676 *
1677 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1678 * been drawn with attributes that happen to coincide with the default
1679 * 'no-attribute' colors. My guess is probably not.
1680 */
1681hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001682 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001683 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001684 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1685 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001686 } else {
rginda9f5222b2012-03-05 11:53:28 -08001687 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1688 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001689 }
1690};
1691
1692/**
rginda87b86462011-12-14 13:48:03 -08001693 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001694 */
1695hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001696 if (this.bellAudio_.getAttribute('src'))
1697 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001698
rginda6d397402012-01-17 10:58:29 -08001699 this.cursorNode_.style.backgroundColor =
1700 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001701
1702 var self = this;
1703 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001704 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001705 }, 200);
rginda87b86462011-12-14 13:48:03 -08001706};
1707
1708/**
rginda8ba33642011-12-14 12:31:31 -08001709 * Set the origin mode bit.
1710 *
1711 * If origin mode is on, certain VT cursor and scrolling commands measure their
1712 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1713 * to the top of the addressable screen.
1714 *
1715 * Defaults to off.
1716 *
1717 * @param {boolean} state True to set origin mode, false to unset.
1718 */
1719hterm.Terminal.prototype.setOriginMode = function(state) {
1720 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001721 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001722};
1723
1724/**
1725 * Set the insert mode bit.
1726 *
1727 * If insert mode is on, existing text beyond the cursor position will be
1728 * shifted right to make room for new text. Otherwise, new text overwrites
1729 * any existing text.
1730 *
1731 * Defaults to off.
1732 *
1733 * @param {boolean} state True to set insert mode, false to unset.
1734 */
1735hterm.Terminal.prototype.setInsertMode = function(state) {
1736 this.options_.insertMode = state;
1737};
1738
1739/**
rginda87b86462011-12-14 13:48:03 -08001740 * Set the auto carriage return bit.
1741 *
1742 * If auto carriage return is on then a formfeed character is interpreted
1743 * as a newline, otherwise it's the same as a linefeed. The difference boils
1744 * down to whether or not the cursor column is reset.
1745 */
1746hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1747 this.options_.autoCarriageReturn = state;
1748};
1749
1750/**
rginda8ba33642011-12-14 12:31:31 -08001751 * Set the wraparound mode bit.
1752 *
1753 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1754 * to the start of the following row. Otherwise, the cursor is clamped to the
1755 * end of the screen and attempts to write past it are ignored.
1756 *
1757 * Defaults to on.
1758 *
1759 * @param {boolean} state True to set wraparound mode, false to unset.
1760 */
1761hterm.Terminal.prototype.setWraparound = function(state) {
1762 this.options_.wraparound = state;
1763};
1764
1765/**
1766 * Set the reverse-wraparound mode bit.
1767 *
1768 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1769 * to the end of the previous row. Otherwise, the cursor is clamped to column
1770 * 0.
1771 *
1772 * Defaults to off.
1773 *
1774 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1775 */
1776hterm.Terminal.prototype.setReverseWraparound = function(state) {
1777 this.options_.reverseWraparound = state;
1778};
1779
1780/**
1781 * Selects between the primary and alternate screens.
1782 *
1783 * If alternate mode is on, the alternate screen is active. Otherwise the
1784 * primary screen is active.
1785 *
1786 * Swapping screens has no effect on the scrollback buffer.
1787 *
1788 * Each screen maintains its own cursor position.
1789 *
1790 * Defaults to off.
1791 *
1792 * @param {boolean} state True to set alternate mode, false to unset.
1793 */
1794hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001795 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001796 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1797
rginda35c456b2012-02-09 17:29:05 -08001798 if (this.screen_.rowsArray.length &&
1799 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
1800 // If the screen changed sizes while we were away, our rowIndexes may
1801 // be incorrect.
1802 var offset = this.scrollbackRows_.length;
1803 var ary = this.screen_.rowsArray;
1804 for (i = 0; i < ary.length; i++) {
1805 ary[i].rowIndex = offset + i;
1806 }
1807 }
rginda8ba33642011-12-14 12:31:31 -08001808
rginda35c456b2012-02-09 17:29:05 -08001809 this.realizeWidth_(this.screenSize.width);
1810 this.realizeHeight_(this.screenSize.height);
1811 this.scrollPort_.syncScrollHeight();
1812 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001813
rginda6d397402012-01-17 10:58:29 -08001814 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08001815 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08001816};
1817
1818/**
1819 * Set the cursor-blink mode bit.
1820 *
1821 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1822 * a visible cursor does not blink.
1823 *
1824 * You should make sure to turn blinking off if you're going to dispose of a
1825 * terminal, otherwise you'll leak a timeout.
1826 *
1827 * Defaults to on.
1828 *
1829 * @param {boolean} state True to set cursor-blink mode, false to unset.
1830 */
1831hterm.Terminal.prototype.setCursorBlink = function(state) {
1832 this.options_.cursorBlink = state;
1833
1834 if (!state && this.timeouts_.cursorBlink) {
1835 clearTimeout(this.timeouts_.cursorBlink);
1836 delete this.timeouts_.cursorBlink;
1837 }
1838
1839 if (this.options_.cursorVisible)
1840 this.setCursorVisible(true);
1841};
1842
1843/**
1844 * Set the cursor-visible mode bit.
1845 *
1846 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1847 *
1848 * Defaults to on.
1849 *
1850 * @param {boolean} state True to set cursor-visible mode, false to unset.
1851 */
1852hterm.Terminal.prototype.setCursorVisible = function(state) {
1853 this.options_.cursorVisible = state;
1854
1855 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001856 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001857 return;
1858 }
1859
rginda87b86462011-12-14 13:48:03 -08001860 this.syncCursorPosition_();
1861
1862 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001863
1864 if (this.options_.cursorBlink) {
1865 if (this.timeouts_.cursorBlink)
1866 return;
1867
1868 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1869 500);
1870 } else {
1871 if (this.timeouts_.cursorBlink) {
1872 clearTimeout(this.timeouts_.cursorBlink);
1873 delete this.timeouts_.cursorBlink;
1874 }
1875 }
1876};
1877
1878/**
rginda87b86462011-12-14 13:48:03 -08001879 * Synchronizes the visible cursor and document selection with the current
1880 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08001881 */
1882hterm.Terminal.prototype.syncCursorPosition_ = function() {
1883 var topRowIndex = this.scrollPort_.getTopRowIndex();
1884 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
1885 var cursorRowIndex = this.scrollbackRows_.length +
1886 this.screen_.cursorPosition.row;
1887
1888 if (cursorRowIndex > bottomRowIndex) {
1889 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08001890 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08001891 return;
1892 }
1893
rginda35c456b2012-02-09 17:29:05 -08001894 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
1895 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
1896
rginda8ba33642011-12-14 12:31:31 -08001897 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08001898 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
1899 'px';
1900 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
1901 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08001902
1903 this.cursorNode_.setAttribute('title',
1904 '(' + this.screen_.cursorPosition.row +
1905 ', ' + this.screen_.cursorPosition.column +
1906 ')');
1907
1908 // Update the caret for a11y purposes.
1909 var selection = this.document_.getSelection();
1910 if (selection && selection.isCollapsed)
1911 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08001912};
1913
1914/**
1915 * Synchronizes the visible cursor with the current cursor coordinates.
1916 *
1917 * The sync will happen asynchronously, soon after the call stack winds down.
1918 * Multiple calls will be coalesced into a single sync.
1919 */
1920hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
1921 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08001922 return;
rginda8ba33642011-12-14 12:31:31 -08001923
1924 var self = this;
1925 this.timeouts_.syncCursor = setTimeout(function() {
1926 self.syncCursorPosition_();
1927 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08001928 }, 0);
1929};
1930
rgindacc2996c2012-02-24 14:59:31 -08001931/**
rgindaf522ce02012-04-17 17:49:17 -07001932 * Show or hide the zoom warning.
1933 *
1934 * The zoom warning is a message warning the user that their browser zoom must
1935 * be set to 100% in order for hterm to function properly.
1936 *
1937 * @param {boolean} state True to show the message, false to hide it.
1938 */
1939hterm.Terminal.prototype.showZoomWarning_ = function(state) {
1940 if (!this.zoomWarningNode_) {
1941 if (!state)
1942 return;
1943
1944 this.zoomWarningNode_ = this.document_.createElement('div');
1945 this.zoomWarningNode_.style.cssText = (
1946 'color: black;' +
1947 'background-color: #ff2222;' +
1948 'font-size: large;' +
1949 'border-radius: 8px;' +
1950 'opacity: 0.75;' +
1951 'padding: 0.2em 0.5em 0.2em 0.5em;' +
1952 'top: 0.5em;' +
1953 'right: 1.2em;' +
1954 'position: absolute;' +
1955 '-webkit-text-size-adjust: none;' +
1956 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07001957 }
1958
rgindade84e382012-04-20 15:39:31 -07001959 this.zoomWarningNode_.textContent = hterm.msg('ZOOM_WARNING') ||
1960 ('!! ' + parseInt(this.scrollPort_.characterSize.zoomFactor * 100) +
1961 '% !!');
rgindaf522ce02012-04-17 17:49:17 -07001962 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
1963
1964 if (state) {
1965 if (!this.zoomWarningNode_.parentNode)
1966 this.div_.parentNode.appendChild(this.zoomWarningNode_);
1967 } else if (this.zoomWarningNode_.parentNode) {
1968 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
1969 }
1970};
1971
1972/**
rgindacc2996c2012-02-24 14:59:31 -08001973 * Show the terminal overlay for a given amount of time.
1974 *
1975 * The terminal overlay appears in inverse video in a large font, centered
1976 * over the terminal. You should probably keep the overlay message brief,
1977 * since it's in a large font and you probably aren't going to check the size
1978 * of the terminal first.
1979 *
1980 * @param {string} msg The text (not HTML) message to display in the overlay.
1981 * @param {number} opt_timeout The amount of time to wait before fading out
1982 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
1983 * stay up forever (or until the next overlay).
1984 */
1985hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08001986 if (!this.overlayNode_) {
1987 if (!this.div_)
1988 return;
1989
1990 this.overlayNode_ = this.document_.createElement('div');
1991 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08001992 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08001993 'font-size: xx-large;' +
1994 'opacity: 0.75;' +
1995 'padding: 0.2em 0.5em 0.2em 0.5em;' +
1996 'position: absolute;' +
1997 '-webkit-user-select: none;' +
1998 '-webkit-transition: opacity 180ms ease-in;');
1999 }
2000
rginda9f5222b2012-03-05 11:53:28 -08002001 this.overlayNode_.style.color = this.prefs_.get('background-color');
2002 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2003 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2004
rgindaf0090c92012-02-10 14:58:52 -08002005 this.overlayNode_.textContent = msg;
2006 this.overlayNode_.style.opacity = '0.75';
2007
2008 if (!this.overlayNode_.parentNode)
2009 this.div_.appendChild(this.overlayNode_);
2010
2011 this.overlayNode_.style.top = (
2012 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
2013 this.overlayNode_.style.left = (
2014 this.div_.clientWidth - this.overlayNode_.clientWidth -
2015 this.scrollbarWidthPx) / 2;
2016
2017 var self = this;
2018
2019 if (this.overlayTimeout_)
2020 clearTimeout(this.overlayTimeout_);
2021
rgindacc2996c2012-02-24 14:59:31 -08002022 if (opt_timeout === null)
2023 return;
2024
rgindaf0090c92012-02-10 14:58:52 -08002025 this.overlayTimeout_ = setTimeout(function() {
2026 self.overlayNode_.style.opacity = '0';
2027 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002028 if (self.overlayNode_.parentNode)
2029 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002030 self.overlayTimeout_ = null;
2031 self.overlayNode_.style.opacity = '0.75';
2032 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002033 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002034};
2035
2036hterm.Terminal.prototype.overlaySize = function() {
2037 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2038};
2039
rginda87b86462011-12-14 13:48:03 -08002040/**
2041 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2042 *
2043 * @param {string} string The VT string representing the keystroke.
2044 */
2045hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002046 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002047 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2048
2049 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08002050};
2051
2052/**
2053 * React when the ScrollPort is scrolled.
2054 */
2055hterm.Terminal.prototype.onScroll_ = function() {
2056 this.scheduleSyncCursorPosition_();
2057};
2058
2059/**
rginda9846e2f2012-01-27 13:53:33 -08002060 * React when text is pasted into the scrollPort.
2061 */
2062hterm.Terminal.prototype.onPaste_ = function(e) {
2063 this.io.onVTKeystroke(e.text);
2064};
2065
2066/**
rginda8ba33642011-12-14 12:31:31 -08002067 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002068 *
2069 * Note: This function should not directly contain code that alters the internal
2070 * state of the terminal. That kind of code belongs in realizeWidth or
2071 * realizeHeight, so that it can be executed synchronously in the case of a
2072 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002073 */
2074hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002075 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002076 this.scrollPort_.characterSize.width);
2077 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2078 this.scrollPort_.characterSize.height);
2079
2080 if (!(columnCount || rowCount)) {
2081 // We avoid these situations since they happen sometimes when the terminal
2082 // gets removed from the document, and we can't deal with that.
2083 return;
2084 }
2085
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002086 this.realizeSize_(columnCount, rowCount);
rgindac9bc5502012-01-18 11:48:44 -08002087 this.scheduleSyncCursorPosition_();
rgindaf522ce02012-04-17 17:49:17 -07002088 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaf0090c92012-02-10 14:58:52 -08002089 this.overlaySize();
rginda8ba33642011-12-14 12:31:31 -08002090};
2091
2092/**
2093 * Service the cursor blink timeout.
2094 */
2095hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002096 if (this.cursorNode_.style.opacity == '0') {
2097 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002098 } else {
rginda87b86462011-12-14 13:48:03 -08002099 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002100 }
2101};
David Reveman8f552492012-03-28 12:18:41 -04002102
2103/**
2104 * Set the scrollbar-visible mode bit.
2105 *
2106 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2107 * Otherwise it will not.
2108 *
2109 * Defaults to on.
2110 *
2111 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2112 */
2113hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2114 this.scrollPort_.setScrollbarVisible(state);
2115};