blob: 0a7414e56a54e769911bc95de14dccd526b6a6a8 [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
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
7lib.rtdep('lib.colors', 'lib.PreferenceManager',
8 'hterm.msg',
9 'hterm.Keyboard', 'hterm.Options', 'hterm.Screen',
10 'hterm.ScrollPort', 'hterm.Size', 'hterm.VT');
11
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
29 * @param {string} opt_profileName Optional preference profile name. If not
30 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
rginda9f5222b2012-03-05 11:53:28 -080032hterm.Terminal = function(opt_profileName) {
33 this.profileName_ = null;
34 this.setProfile(opt_profileName || 'default');
35
rginda8ba33642011-12-14 12:31:31 -080036 // Two screen instances.
37 this.primaryScreen_ = new hterm.Screen();
38 this.alternateScreen_ = new hterm.Screen();
39
40 // The "current" screen.
41 this.screen_ = this.primaryScreen_;
42
rginda8ba33642011-12-14 12:31:31 -080043 // The local notion of the screen size. ScreenBuffers also have a size which
44 // indicates their present size. During size changes, the two may disagree.
45 // Also, the inactive screen's size is not altered until it is made the active
46 // screen.
47 this.screenSize = new hterm.Size(0, 0);
48
rginda8ba33642011-12-14 12:31:31 -080049 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080050 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080051 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
52 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080053 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070054 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080055
rginda87b86462011-12-14 13:48:03 -080056 // The div that contains this terminal.
57 this.div_ = null;
58
rgindac9bc5502012-01-18 11:48:44 -080059 // The document that contains the scrollPort. Defaulted to the global
60 // document here so that the terminal is functional even if it hasn't been
61 // inserted into a document yet, but re-set in decorate().
62 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080063
rginda8ba33642011-12-14 12:31:31 -080064 // The rows that have scrolled off screen and are no longer addressable.
65 this.scrollbackRows_ = [];
66
rgindac9bc5502012-01-18 11:48:44 -080067 // Saved tab stops.
68 this.tabStops_ = [];
69
David Benjamin66e954d2012-05-05 21:08:12 -040070 // Keep track of whether default tab stops have been erased; after a TBC
71 // clears all tab stops, defaults aren't restored on resize until a reset.
72 this.defaultTabStops = true;
73
rginda8ba33642011-12-14 12:31:31 -080074 // The VT's notion of the top and bottom rows. Used during some VT
75 // cursor positioning and scrolling commands.
76 this.vtScrollTop_ = null;
77 this.vtScrollBottom_ = null;
78
79 // The DIV element for the visible cursor.
80 this.cursorNode_ = null;
81
rginda9f5222b2012-03-05 11:53:28 -080082 // These prefs are cached so we don't have to read from local storage with
83 // each output and keystroke.
84 this.scrollOnOutput_ = this.prefs_.get('scroll-on-output');
85 this.scrollOnKeystroke_ = this.prefs_.get('scroll-on-keystroke');
rginda8e92a692012-05-20 19:37:20 -070086 this.foregroundColor_ = this.prefs_.get('foreground-color');
87 this.backgroundColor_ = this.prefs_.get('background-color');
rginda9f5222b2012-03-05 11:53:28 -080088
rgindaf0090c92012-02-10 14:58:52 -080089 // Terminal bell sound.
90 this.bellAudio_ = this.document_.createElement('audio');
rginda9f5222b2012-03-05 11:53:28 -080091 this.bellAudio_.setAttribute('src', this.prefs_.get('audible-bell-sound'));
rgindaf0090c92012-02-10 14:58:52 -080092 this.bellAudio_.setAttribute('preload', 'auto');
93
rginda6d397402012-01-17 10:58:29 -080094 // Cursor position and attributes saved with DECSC.
95 this.savedOptions_ = {};
96
rginda8ba33642011-12-14 12:31:31 -080097 // The current mode bits for the terminal.
98 this.options_ = new hterm.Options();
99
100 // Timeouts we might need to clear.
101 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800102
103 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800104 this.vt = new hterm.VT(this);
rginda11057d52012-04-25 12:29:56 -0700105 this.vt.enable8BitControl = this.prefs_.get('enable-8-bit-control');
106 this.vt.maxStringSequence = this.prefs_.get('max-string-sequence');
rgindaa8ba17d2012-08-15 14:41:10 -0700107 this.vt.enableClipboardWrite = this.prefs_.get('enable-clipboard-write');
rginda87b86462011-12-14 13:48:03 -0800108
rgindafeaf3142012-01-31 15:14:20 -0800109 // The keyboard hander.
110 this.keyboard = new hterm.Keyboard(this);
111
rginda87b86462011-12-14 13:48:03 -0800112 // General IO interface that can be given to third parties without exposing
113 // the entire terminal object.
114 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800115
rgindad5613292012-06-19 15:40:37 -0700116 // True if mouse-click-drag should scroll the terminal.
117 this.enableMouseDragScroll = true;
118
rginda4bba5e12012-06-20 16:15:30 -0700119 this.copyOnSelect = this.prefs_.get('copy-on-select');
120 this.mousePasteButton = null;
121 this.syncMousePasteButton();
122
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400123 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800124 this.setDefaultTabStops();
rginda87b86462011-12-14 13:48:03 -0800125};
126
127/**
rginda35c456b2012-02-09 17:29:05 -0800128 * Default tab with of 8 to match xterm.
129 */
130hterm.Terminal.prototype.tabWidth = 8;
131
132/**
rginda35c456b2012-02-09 17:29:05 -0800133 * The assumed width of a scrollbar.
134 */
135hterm.Terminal.prototype.scrollbarWidthPx = 16;
136
137/**
rginda9f5222b2012-03-05 11:53:28 -0800138 * Select a preference profile.
139 *
140 * This will load the terminal preferences for the given profile name and
141 * associate subsequent preference changes with the new preference profile.
142 *
143 * @param {string} newName The name of the preference profile. Forward slash
144 * characters will be removed from the name.
145 */
146hterm.Terminal.prototype.setProfile = function(profileName) {
147 // If we already have a profile selected, we're going to need to re-sync
148 // with the new profile.
149 var needSync = !!this.profileName_;
150
151 this.profileName_ = profileName.replace(/\//g, '');
152
rgindacbbd7482012-06-13 15:06:16 -0700153 this.prefs_ = new lib.PreferenceManager(
rginda9f5222b2012-03-05 11:53:28 -0800154 '/hterm/prefs/profiles/' + this.profileName_);
155
156 var self = this;
157 this.prefs_.definePreferences
rginda30f20f62012-04-05 16:36:19 -0700158 ([
159 /**
160 * Set whether the alt key acts as a meta key or as a distinct alt key.
rginda9f5222b2012-03-05 11:53:28 -0800161 */
rginda30f20f62012-04-05 16:36:19 -0700162 ['alt-is-meta', false, function(v) {
rgindaf9c36852012-05-09 11:08:39 -0700163 self.keyboard.altIsMeta = v;
rginda9f5222b2012-03-05 11:53:28 -0800164 }
165 ],
166
rginda30f20f62012-04-05 16:36:19 -0700167 /**
rginda39bdf6f2012-04-10 16:50:55 -0700168 * Controls how the alt key is handled.
169 *
170 * escape....... Send an ESC prefix.
171 * 8-bit........ Add 128 to the unshifted character as in xterm.
172 * browser-key.. Wait for the keypress event and see what the browser says.
173 * (This won't work well on platforms where the browser
174 * performs a default action for some alt sequences.)
rginda30f20f62012-04-05 16:36:19 -0700175 */
rginda39bdf6f2012-04-10 16:50:55 -0700176 ['alt-sends-what', 'escape', function(v) {
177 if (!/^(escape|8-bit|browser-key)$/.test(v))
178 v = 'escape';
179
rgindaf9c36852012-05-09 11:08:39 -0700180 self.keyboard.altSendsWhat = v;
rginda30f20f62012-04-05 16:36:19 -0700181 }
182 ],
183
184 /**
185 * Terminal bell sound. Empty string for no audible bell.
186 */
187 ['audible-bell-sound', '../audio/bell.ogg', function(v) {
188 self.bellAudio_.setAttribute('src', v);
189 }
190 ],
191
192 /**
193 * The background color for text with no other color attributes.
194 */
195 ['background-color', 'rgb(16, 16, 16)', function(v) {
rginda8e92a692012-05-20 19:37:20 -0700196 self.setBackgroundColor(v);
rginda9f5222b2012-03-05 11:53:28 -0800197 }
198 ],
199
200 /**
rginda30f20f62012-04-05 16:36:19 -0700201 * The background image.
rginda30f20f62012-04-05 16:36:19 -0700202 */
rginda8e92a692012-05-20 19:37:20 -0700203 ['background-image', '',
rginda30f20f62012-04-05 16:36:19 -0700204 function(v) {
205 self.scrollPort_.setBackgroundImage(v);
206 }
207 ],
208
209 /**
Philip Douglass959b49d2012-05-30 13:29:29 -0400210 * The background image size,
211 *
212 * Defaults to none.
213 */
214 ['background-size', '', function(v) {
215 self.scrollPort_.setBackgroundSize(v);
216 }
217 ],
218
219 /**
220 * The background image position,
221 *
222 * Defaults to none.
223 */
224 ['background-position', '', function(v) {
225 self.scrollPort_.setBackgroundPosition(v);
226 }
227 ],
228
229 /**
rginda30f20f62012-04-05 16:36:19 -0700230 * If true, the backspace should send BS ('\x08', aka ^H). Otherwise
231 * the backspace key should send '\x7f'.
232 */
233 ['backspace-sends-backspace', false, function(v) {
234 self.keyboard.backspaceSendsBackspace = v;
235 }
236 ],
237
238 /**
rginda9875d902012-08-20 16:21:57 -0700239 * Whether or not to close the window when the command exits.
240 */
241 ['close-on-exit', true, null],
242
243 /**
rgindade84e382012-04-20 15:39:31 -0700244 * Whether or not to blink the cursor by default.
245 */
246 ['cursor-blink', false, function(v) {
247 self.setCursorBlink(!!v);
248 }
249 ],
250
251 /**
rginda30f20f62012-04-05 16:36:19 -0700252 * The color of the visible cursor.
253 */
254 ['cursor-color', 'rgba(255,0,0,0.5)', function(v) {
rginda8e92a692012-05-20 19:37:20 -0700255 self.setCursorColor(v);
rginda30f20f62012-04-05 16:36:19 -0700256 }
257 ],
258
259 /**
rginda4bba5e12012-06-20 16:15:30 -0700260 * Automatically copy mouse selection to the clipboard.
261 */
262 ['copy-on-select', true, function(v) {
263 self.copyOnSelect = !!v;
264 }
265 ],
266
267 /**
rginda11057d52012-04-25 12:29:56 -0700268 * True to enable 8-bit control characters, false to ignore them.
269 *
270 * We'll respect the two-byte versions of these control characters
271 * regardless of this setting.
272 */
273 ['enable-8-bit-control', false, function(v) {
274 self.vt.enable8BitControl = !!v;
275 }
276 ],
277
278 /**
rginda30f20f62012-04-05 16:36:19 -0700279 * True if we should use bold weight font for text with the bold/bright
280 * attribute. False to use bright colors only. Null to autodetect.
281 */
282 ['enable-bold', null, function(v) {
283 self.syncBoldSafeState();
284 }
285 ],
286
287 /**
rgindaa8ba17d2012-08-15 14:41:10 -0700288 * Allow the host to write directly to the system clipboard.
289 */
290 ['enable-clipboard-write', true, function(v) {
291 self.vt.enableClipboardWrite = !!v;
292 }
293 ],
294
295 /**
rginda9f5222b2012-03-05 11:53:28 -0800296 * Default font family for the terminal text.
297 */
298 ['font-family', ('"DejaVu Sans Mono", "Everson Mono", ' +
rgindaa8ba17d2012-08-15 14:41:10 -0700299 'FreeMono, "Menlo", "Terminal", ' +
rginda9f5222b2012-03-05 11:53:28 -0800300 'monospace'),
301 function(v) { self.syncFontFamily() }
302 ],
303
304 /**
rginda30f20f62012-04-05 16:36:19 -0700305 * The default font size in pixels.
306 */
307 ['font-size', 15, function(v) {
308 self.setFontSize(v);
309 }
310 ],
311
312 /**
rginda9f5222b2012-03-05 11:53:28 -0800313 * Anti-aliasing.
314 */
315 ['font-smoothing', 'antialiased',
316 function(v) { self.syncFontFamily() }
317 ],
318
319 /**
rginda30f20f62012-04-05 16:36:19 -0700320 * The foreground color for text with no other color attributes.
rginda9f5222b2012-03-05 11:53:28 -0800321 */
rginda30f20f62012-04-05 16:36:19 -0700322 ['foreground-color', 'rgb(240, 240, 240)', function(v) {
rginda8e92a692012-05-20 19:37:20 -0700323 self.setForegroundColor(v);
rginda9f5222b2012-03-05 11:53:28 -0800324 }
325 ],
326
327 /**
rginda30f20f62012-04-05 16:36:19 -0700328 * If true, home/end will control the terminal scrollbar and shift home/end
329 * will send the VT keycodes. If false then home/end sends VT codes and
330 * shift home/end scrolls.
rginda9f5222b2012-03-05 11:53:28 -0800331 */
rginda30f20f62012-04-05 16:36:19 -0700332 ['home-keys-scroll', false, function(v) {
333 self.keyboard.homeKeysScroll = v;
334 }
335 ],
336
337 /**
rginda11057d52012-04-25 12:29:56 -0700338 * Max length of a DCS, OSC, PM, or APS sequence before we give up and
339 * ignore the code.
340 */
341 ['max-string-sequence', 1024, function(v) {
342 self.vt.maxStringSequence = v;
343 }
344 ],
345
346 /**
rginda30f20f62012-04-05 16:36:19 -0700347 * Set whether the meta key sends a leading escape or not.
348 */
349 ['meta-sends-escape', true, function(v) {
350 self.keyboard.metaSendsEscape = v;
rginda9f5222b2012-03-05 11:53:28 -0800351 }
352 ],
353
354 /**
rgindad5613292012-06-19 15:40:37 -0700355 * Set whether we should treat DEC mode 1002 (mouse cell motion tracking)
356 * as if it were 1000 (mouse click tracking).
357 *
358 * This makes it possible to use vi's ":set mouse=a" mode without losing
359 * access to the system text selection mechanism.
360 */
361 ['mouse-cell-motion-trick', false, function(v) {
362 self.vt.setMouseCellMotionTrick(v);
363 }
364 ],
365
366 /**
rginda4bba5e12012-06-20 16:15:30 -0700367 * Mouse paste button, or null to autodetect.
368 *
369 * For autodetect, we'll try to enable middle button paste for non-X11
370 * platforms.
371 *
372 * On X11 we move it to button 3, but that'll probably be a context menu
373 * in the future.
374 */
375 ['mouse-paste-button', null, function(v) {
376 self.syncMousePasteButton();
377 }
378 ],
379
380 /**
rginda9f5222b2012-03-05 11:53:28 -0800381 * If true, scroll to the bottom on any keystroke.
382 */
383 ['scroll-on-keystroke', true, function(v) {
384 self.scrollOnKeystroke_ = v;
385 }
386 ],
387
388 /**
389 * If true, scroll to the bottom on terminal output.
390 */
391 ['scroll-on-output', false, function(v) {
392 self.scrollOnOutput_ = v;
393 }
394 ],
395
396 /**
David Reveman8f552492012-03-28 12:18:41 -0400397 * The vertical scrollbar mode.
398 */
399 ['scrollbar-visible', true, function(v) {
400 self.setScrollbarVisible(v);
401 }
402 ],
rginda30f20f62012-04-05 16:36:19 -0700403
404 /**
rginda4bba5e12012-06-20 16:15:30 -0700405 * Shift + Insert pastes if true, sent to host if false.
406 */
407 ['shift-insert-paste', true, function(v) {
408 self.keyboard.shiftInsertPaste = v;
409 }
410 ],
411
412 /**
rgindaf522ce02012-04-17 17:49:17 -0700413 * The default environment variables.
414 */
415 ['environment', {TERM: 'xterm-256color'}, null],
416
417 /**
rginda30f20f62012-04-05 16:36:19 -0700418 * If true, page up/down will control the terminal scrollbar and shift
419 * page up/down will send the VT keycodes. If false then page up/down
420 * sends VT codes and shift page up/down scrolls.
421 */
422 ['page-keys-scroll', false, function(v) {
423 self.keyboard.pageKeysScroll = v;
424 }
425 ],
426
rginda9f5222b2012-03-05 11:53:28 -0800427 ]);
428
429 if (needSync)
430 this.prefs_.notifyAll();
431};
432
rginda8e92a692012-05-20 19:37:20 -0700433
434/**
435 * Set the color for the cursor.
436 *
437 * If you want this setting to persist, set it through prefs_, rather than
438 * with this method.
439 */
440hterm.Terminal.prototype.setCursorColor = function(color) {
441 this.cursorNode_.style.backgroundColor = color;
442 this.cursorNode_.style.borderColor = color;
443};
444
445/**
446 * Return the current cursor color as a string.
447 */
448hterm.Terminal.prototype.getCursorColor = function() {
449 return this.cursorNode_.style.backgroundColor;
450};
451
452/**
rgindad5613292012-06-19 15:40:37 -0700453 * Enable or disable mouse based text selection in the terminal.
454 */
455hterm.Terminal.prototype.setSelectionEnabled = function(state) {
456 this.enableMouseDragScroll = state;
457 this.scrollPort_.setSelectionEnabled(state);
458};
459
460/**
rginda8e92a692012-05-20 19:37:20 -0700461 * Set the background color.
462 *
463 * If you want this setting to persist, set it through prefs_, rather than
464 * with this method.
465 */
466hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700467 this.backgroundColor_ = lib.colors.normalizeCSS(color);
rginda8e92a692012-05-20 19:37:20 -0700468 this.scrollPort_.setBackgroundColor(color);
469};
470
rginda9f5222b2012-03-05 11:53:28 -0800471/**
472 * Return the current terminal background color.
473 *
474 * Intended for use by other classes, so we don't have to expose the entire
475 * prefs_ object.
476 */
477hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700478 return this.backgroundColor_;
479};
480
481/**
482 * Set the foreground color.
483 *
484 * If you want this setting to persist, set it through prefs_, rather than
485 * with this method.
486 */
487hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700488 this.foregroundColor_ = lib.colors.normalizeCSS(color);
rginda8e92a692012-05-20 19:37:20 -0700489 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800490};
491
492/**
493 * Return the current terminal foreground color.
494 *
495 * Intended for use by other classes, so we don't have to expose the entire
496 * prefs_ object.
497 */
498hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700499 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800500};
501
502/**
rginda87b86462011-12-14 13:48:03 -0800503 * Create a new instance of a terminal command and run it with a given
504 * argument string.
505 *
506 * @param {function} commandClass The constructor for a terminal command.
507 * @param {string} argString The argument string to pass to the command.
508 */
509hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700510 var environment = this.prefs_.get('environment');
511 if (typeof environment != 'object' || environment == null)
512 environment = {};
513
rginda87b86462011-12-14 13:48:03 -0800514 var self = this;
515 this.command = new commandClass(
516 { argString: argString || '',
517 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700518 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800519 onExit: function(code) {
520 self.io.pop();
521 self.io.println(hterm.msg('COMMAND_COMPLETE',
522 [self.command.commandName, code]));
rgindafeaf3142012-01-31 15:14:20 -0800523 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700524 if (self.prefs_.get('close-on-exit'))
525 window.close();
rginda87b86462011-12-14 13:48:03 -0800526 }
527 });
528
rgindafeaf3142012-01-31 15:14:20 -0800529 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800530 this.command.run();
531};
532
533/**
rgindafeaf3142012-01-31 15:14:20 -0800534 * Returns true if the current screen is the primary screen, false otherwise.
535 */
536hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700537 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800538};
539
540/**
541 * Install the keyboard handler for this terminal.
542 *
543 * This will prevent the browser from seeing any keystrokes sent to the
544 * terminal.
545 */
546hterm.Terminal.prototype.installKeyboard = function() {
547 this.keyboard.installKeyboard(this.document_.body.firstChild);
548}
549
550/**
551 * Uninstall the keyboard handler for this terminal.
552 */
553hterm.Terminal.prototype.uninstallKeyboard = function() {
554 this.keyboard.installKeyboard(null);
555}
556
557/**
rginda35c456b2012-02-09 17:29:05 -0800558 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800559 *
560 * Call setFontSize(0) to reset to the default font size.
561 *
562 * This function does not modify the font-size preference.
563 *
564 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800565 */
566hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800567 if (px === 0)
568 px = this.prefs_.get('font-size');
569
rginda35c456b2012-02-09 17:29:05 -0800570 this.scrollPort_.setFontSize(px);
571};
572
573/**
574 * Get the current font size.
575 */
576hterm.Terminal.prototype.getFontSize = function() {
577 return this.scrollPort_.getFontSize();
578};
579
580/**
rginda8e92a692012-05-20 19:37:20 -0700581 * Get the current font family.
582 */
583hterm.Terminal.prototype.getFontFamily = function() {
584 return this.scrollPort_.getFontFamily();
585};
586
587/**
rginda35c456b2012-02-09 17:29:05 -0800588 * Set the CSS "font-family" for this terminal.
589 */
rginda9f5222b2012-03-05 11:53:28 -0800590hterm.Terminal.prototype.syncFontFamily = function() {
591 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
592 this.prefs_.get('font-smoothing'));
593 this.syncBoldSafeState();
594};
595
rginda4bba5e12012-06-20 16:15:30 -0700596/**
597 * Set this.mousePasteButton based on the mouse-paste-button pref,
598 * autodetecting if necessary.
599 */
600hterm.Terminal.prototype.syncMousePasteButton = function() {
601 var button = this.prefs_.get('mouse-paste-button');
602 if (typeof button == 'number') {
603 this.mousePasteButton = button;
604 return;
605 }
606
607 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
608 if (!ary || ary[2] == 'CrOS') {
609 this.mousePasteButton = 2;
610 } else {
611 this.mousePasteButton = 3;
612 }
613};
614
615/**
616 * Enable or disable bold based on the enable-bold pref, autodetecting if
617 * necessary.
618 */
rginda9f5222b2012-03-05 11:53:28 -0800619hterm.Terminal.prototype.syncBoldSafeState = function() {
620 var enableBold = this.prefs_.get('enable-bold');
621 if (enableBold !== null) {
622 this.screen_.textAttributes.enableBold = enableBold;
623 return;
624 }
625
rgindaf7521392012-02-28 17:20:34 -0800626 var normalSize = this.scrollPort_.measureCharacterSize();
627 var boldSize = this.scrollPort_.measureCharacterSize('bold');
628
629 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800630 if (!isBoldSafe) {
631 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700632 'from normal. Font family is: ' +
633 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800634 }
rginda9f5222b2012-03-05 11:53:28 -0800635
636 this.screen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800637};
638
639/**
rginda87b86462011-12-14 13:48:03 -0800640 * Return a copy of the current cursor position.
641 *
642 * @return {hterm.RowCol} The RowCol object representing the current position.
643 */
644hterm.Terminal.prototype.saveCursor = function() {
645 return this.screen_.cursorPosition.clone();
646};
647
rgindaa19afe22012-01-25 15:40:22 -0800648hterm.Terminal.prototype.getTextAttributes = function() {
649 return this.screen_.textAttributes;
650};
651
rginda1a09aa02012-06-18 21:11:25 -0700652hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
653 this.screen_.textAttributes = textAttributes;
654};
655
rginda87b86462011-12-14 13:48:03 -0800656/**
rgindaf522ce02012-04-17 17:49:17 -0700657 * Return the current browser zoom factor applied to the terminal.
658 *
659 * @return {number} The current browser zoom factor.
660 */
661hterm.Terminal.prototype.getZoomFactor = function() {
662 return this.scrollPort_.characterSize.zoomFactor;
663};
664
665/**
rginda9846e2f2012-01-27 13:53:33 -0800666 * Change the title of this terminal's window.
667 */
668hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800669 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800670};
671
672/**
rginda87b86462011-12-14 13:48:03 -0800673 * Restore a previously saved cursor position.
674 *
675 * @param {hterm.RowCol} cursor The position to restore.
676 */
677hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700678 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
679 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800680 this.screen_.setCursorPosition(row, column);
681 if (cursor.column > column ||
682 cursor.column == column && cursor.overflow) {
683 this.screen_.cursorPosition.overflow = true;
684 }
rginda87b86462011-12-14 13:48:03 -0800685};
686
687/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400688 * Clear the cursor's overflow flag.
689 */
690hterm.Terminal.prototype.clearCursorOverflow = function() {
691 this.screen_.cursorPosition.overflow = false;
692};
693
694/**
rginda87b86462011-12-14 13:48:03 -0800695 * Set the width of the terminal, resizing the UI to match.
696 */
697hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800698 if (columnCount == null) {
699 this.div_.style.width = '100%';
700 return;
701 }
702
rginda35c456b2012-02-09 17:29:05 -0800703 this.div_.style.width = this.scrollPort_.characterSize.width *
704 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400705 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800706 this.scheduleSyncCursorPosition_();
707};
rginda87b86462011-12-14 13:48:03 -0800708
rgindac9bc5502012-01-18 11:48:44 -0800709/**
rginda35c456b2012-02-09 17:29:05 -0800710 * Set the height of the terminal, resizing the UI to match.
711 */
712hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800713 if (rowCount == null) {
714 this.div_.style.height = '100%';
715 return;
716 }
717
rginda35c456b2012-02-09 17:29:05 -0800718 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700719 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800720 this.realizeSize_(this.screenSize.width, rowCount);
721 this.scheduleSyncCursorPosition_();
722};
723
724/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400725 * Deal with terminal size changes.
726 *
727 */
728hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
729 if (columnCount != this.screenSize.width)
730 this.realizeWidth_(columnCount);
731
732 if (rowCount != this.screenSize.height)
733 this.realizeHeight_(rowCount);
734
735 // Send new terminal size to plugin.
736 this.io.onTerminalResize(columnCount, rowCount);
737};
738
739/**
rgindac9bc5502012-01-18 11:48:44 -0800740 * Deal with terminal width changes.
741 *
742 * This function does what needs to be done when the terminal width changes
743 * out from under us. It happens here rather than in onResize_() because this
744 * code may need to run synchronously to handle programmatic changes of
745 * terminal width.
746 *
747 * Relying on the browser to send us an async resize event means we may not be
748 * in the correct state yet when the next escape sequence hits.
749 */
750hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700751 if (columnCount <= 0)
752 throw new Error('Attempt to realize bad width: ' + columnCount);
753
rgindac9bc5502012-01-18 11:48:44 -0800754 var deltaColumns = columnCount - this.screen_.getWidth();
755
rginda87b86462011-12-14 13:48:03 -0800756 this.screenSize.width = columnCount;
757 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800758
759 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400760 if (this.defaultTabStops)
761 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800762 } else {
763 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400764 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800765 break;
766
767 this.tabStops_.pop();
768 }
769 }
770
771 this.screen_.setColumnCount(this.screenSize.width);
772};
773
774/**
775 * Deal with terminal height changes.
776 *
777 * This function does what needs to be done when the terminal height changes
778 * out from under us. It happens here rather than in onResize_() because this
779 * code may need to run synchronously to handle programmatic changes of
780 * terminal height.
781 *
782 * Relying on the browser to send us an async resize event means we may not be
783 * in the correct state yet when the next escape sequence hits.
784 */
785hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700786 if (rowCount <= 0)
787 throw new Error('Attempt to realize bad height: ' + rowCount);
788
rgindac9bc5502012-01-18 11:48:44 -0800789 var deltaRows = rowCount - this.screen_.getHeight();
790
791 this.screenSize.height = rowCount;
792
793 var cursor = this.saveCursor();
794
795 if (deltaRows < 0) {
796 // Screen got smaller.
797 deltaRows *= -1;
798 while (deltaRows) {
799 var lastRow = this.getRowCount() - 1;
800 if (lastRow - this.scrollbackRows_.length == cursor.row)
801 break;
802
803 if (this.getRowText(lastRow))
804 break;
805
806 this.screen_.popRow();
807 deltaRows--;
808 }
809
810 var ary = this.screen_.shiftRows(deltaRows);
811 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
812
813 // We just removed rows from the top of the screen, we need to update
814 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800815 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800816 } else if (deltaRows > 0) {
817 // Screen got larger.
818
819 if (deltaRows <= this.scrollbackRows_.length) {
820 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
821 var rows = this.scrollbackRows_.splice(
822 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
823 this.screen_.unshiftRows(rows);
824 deltaRows -= scrollbackCount;
825 cursor.row += scrollbackCount;
826 }
827
828 if (deltaRows)
829 this.appendRows_(deltaRows);
830 }
831
rginda35c456b2012-02-09 17:29:05 -0800832 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800833 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800834};
835
836/**
837 * Scroll the terminal to the top of the scrollback buffer.
838 */
839hterm.Terminal.prototype.scrollHome = function() {
840 this.scrollPort_.scrollRowToTop(0);
841};
842
843/**
844 * Scroll the terminal to the end.
845 */
846hterm.Terminal.prototype.scrollEnd = function() {
847 this.scrollPort_.scrollRowToBottom(this.getRowCount());
848};
849
850/**
851 * Scroll the terminal one page up (minus one line) relative to the current
852 * position.
853 */
854hterm.Terminal.prototype.scrollPageUp = function() {
855 var i = this.scrollPort_.getTopRowIndex();
856 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
857};
858
859/**
860 * Scroll the terminal one page down (minus one line) relative to the current
861 * position.
862 */
863hterm.Terminal.prototype.scrollPageDown = function() {
864 var i = this.scrollPort_.getTopRowIndex();
865 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800866};
867
rgindac9bc5502012-01-18 11:48:44 -0800868/**
869 * Full terminal reset.
870 */
rginda87b86462011-12-14 13:48:03 -0800871hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800872 this.clearAllTabStops();
873 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700874
875 this.clearHome(this.primaryScreen_);
876 this.primaryScreen_.textAttributes.reset();
877
878 this.clearHome(this.alternateScreen_);
879 this.alternateScreen_.textAttributes.reset();
880
rgindab8bc8932012-04-27 12:45:03 -0700881 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
882
rgindac9bc5502012-01-18 11:48:44 -0800883 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800884};
885
rgindac9bc5502012-01-18 11:48:44 -0800886/**
887 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700888 *
889 * Perform a soft reset to the default values listed in
890 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800891 */
rginda0f5c0292012-01-13 11:00:13 -0800892hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700893 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800894 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700895
rgindab8bc8932012-04-27 12:45:03 -0700896 // Xterm also resets the color palette on soft reset, even though it doesn't
897 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700898 this.primaryScreen_.textAttributes.resetColorPalette();
899 this.alternateScreen_.textAttributes.resetColorPalette();
900
rgindab8bc8932012-04-27 12:45:03 -0700901 // The xterm man page explicitly says this will happen on soft reset.
902 this.setVTScrollRegion(null, null);
903
904 // Xterm also shows the cursor on soft reset, but does not alter the blink
905 // state.
rgindaa19afe22012-01-25 15:40:22 -0800906 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800907};
908
rgindac9bc5502012-01-18 11:48:44 -0800909/**
910 * Move the cursor forward to the next tab stop, or to the last column
911 * if no more tab stops are set.
912 */
913hterm.Terminal.prototype.forwardTabStop = function() {
914 var column = this.screen_.cursorPosition.column;
915
916 for (var i = 0; i < this.tabStops_.length; i++) {
917 if (this.tabStops_[i] > column) {
918 this.setCursorColumn(this.tabStops_[i]);
919 return;
920 }
921 }
922
David Benjamin66e954d2012-05-05 21:08:12 -0400923 // xterm does not clear the overflow flag on HT or CHT.
924 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800925 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400926 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800927};
928
rgindac9bc5502012-01-18 11:48:44 -0800929/**
930 * Move the cursor backward to the previous tab stop, or to the first column
931 * if no previous tab stops are set.
932 */
933hterm.Terminal.prototype.backwardTabStop = function() {
934 var column = this.screen_.cursorPosition.column;
935
936 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
937 if (this.tabStops_[i] < column) {
938 this.setCursorColumn(this.tabStops_[i]);
939 return;
940 }
941 }
942
943 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800944};
945
rgindac9bc5502012-01-18 11:48:44 -0800946/**
947 * Set a tab stop at the given column.
948 *
949 * @param {int} column Zero based column.
950 */
951hterm.Terminal.prototype.setTabStop = function(column) {
952 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
953 if (this.tabStops_[i] == column)
954 return;
955
956 if (this.tabStops_[i] < column) {
957 this.tabStops_.splice(i + 1, 0, column);
958 return;
959 }
960 }
961
962 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800963};
964
rgindac9bc5502012-01-18 11:48:44 -0800965/**
966 * Clear the tab stop at the current cursor position.
967 *
968 * No effect if there is no tab stop at the current cursor position.
969 */
970hterm.Terminal.prototype.clearTabStopAtCursor = function() {
971 var column = this.screen_.cursorPosition.column;
972
973 var i = this.tabStops_.indexOf(column);
974 if (i == -1)
975 return;
976
977 this.tabStops_.splice(i, 1);
978};
979
980/**
981 * Clear all tab stops.
982 */
983hterm.Terminal.prototype.clearAllTabStops = function() {
984 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -0400985 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -0800986};
987
988/**
989 * Set up the default tab stops, starting from a given column.
990 *
991 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -0400992 * from the specified column, or 0 if no column is provided. It also flags
993 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -0800994 *
995 * This does not clear the existing tab stops first, use clearAllTabStops
996 * for that.
997 *
998 * @param {int} opt_start Optional starting zero based starting column, useful
999 * for filling out missing tab stops when the terminal is resized.
1000 */
1001hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1002 var start = opt_start || 0;
1003 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001004 // Round start up to a default tab stop.
1005 start = start - 1 - ((start - 1) % w) + w;
1006 for (var i = start; i < this.screenSize.width; i += w) {
1007 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001008 }
David Benjamin66e954d2012-05-05 21:08:12 -04001009
1010 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001011};
1012
rginda6d397402012-01-17 10:58:29 -08001013/**
rginda8ba33642011-12-14 12:31:31 -08001014 * Interpret a sequence of characters.
1015 *
1016 * Incomplete escape sequences are buffered until the next call.
1017 *
1018 * @param {string} str Sequence of characters to interpret or pass through.
1019 */
1020hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001021 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001022 this.scheduleSyncCursorPosition_();
1023};
1024
1025/**
1026 * Take over the given DIV for use as the terminal display.
1027 *
1028 * @param {HTMLDivElement} div The div to use as the terminal display.
1029 */
1030hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001031 this.div_ = div;
1032
rginda8ba33642011-12-14 12:31:31 -08001033 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001034 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001035 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1036 this.scrollPort_.setBackgroundPosition(
1037 this.prefs_.get('background-position'));
rginda30f20f62012-04-05 16:36:19 -07001038
rginda0918b652012-04-04 11:26:24 -07001039 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001040
rginda9f5222b2012-03-05 11:53:28 -08001041 this.setFontSize(this.prefs_.get('font-size'));
1042 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001043
David Reveman8f552492012-03-28 12:18:41 -04001044 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1045
rginda8ba33642011-12-14 12:31:31 -08001046 this.document_ = this.scrollPort_.getDocument();
1047
rginda4bba5e12012-06-20 16:15:30 -07001048 this.document_.body.oncontextmenu = function() { return false };
1049
1050 var onMouse = this.onMouse_.bind(this);
1051 this.document_.body.firstChild.addEventListener('mousedown', onMouse);
1052 this.document_.body.firstChild.addEventListener('mouseup', onMouse);
1053 this.document_.body.firstChild.addEventListener('mousemove', onMouse);
1054 this.scrollPort_.onScrollWheel = onMouse;
1055
rginda8e92a692012-05-20 19:37:20 -07001056 this.document_.body.firstChild.addEventListener(
1057 'focus', this.onFocusChange_.bind(this, true));
1058 this.document_.body.firstChild.addEventListener(
1059 'blur', this.onFocusChange_.bind(this, false));
1060
1061 var style = this.document_.createElement('style');
1062 style.textContent =
1063 ('.cursor-node[focus="false"] {' +
1064 ' box-sizing: border-box;' +
1065 ' background-color: transparent !important;' +
1066 ' border-width: 2px;' +
1067 ' border-style: solid;' +
1068 '}');
1069 this.document_.head.appendChild(style);
1070
rginda8ba33642011-12-14 12:31:31 -08001071 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001072 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001073 this.cursorNode_.style.cssText =
1074 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001075 'top: -99px;' +
1076 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001077 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1078 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001079 '-webkit-transition: opacity, background-color 100ms linear;');
1080 this.setCursorColor(this.prefs_.get('cursor-color'));
rgindad5613292012-06-19 15:40:37 -07001081
rginda8ba33642011-12-14 12:31:31 -08001082 this.document_.body.appendChild(this.cursorNode_);
1083
rgindad5613292012-06-19 15:40:37 -07001084 // When 'enableMouseDragScroll' is off we reposition this element directly
1085 // under the mouse cursor after a click. This makes Chrome associate
1086 // subsequent mousemove events with the scroll-blocker. Since the
1087 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1088 // events do not cause the scrollport to scroll.
1089 //
1090 // It's a hack, but it's the cleanest way I could find.
1091 this.scrollBlockerNode_ = this.document_.createElement('div');
1092 this.scrollBlockerNode_.style.cssText =
1093 ('position: absolute;' +
1094 'top: -99px;' +
1095 'display: block;' +
1096 'width: 10px;' +
1097 'height: 10px;');
1098 this.document_.body.appendChild(this.scrollBlockerNode_);
1099
1100 var onMouse = this.onMouse_.bind(this);
1101 this.scrollPort_.onScrollWheel = onMouse;
1102 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1103 ].forEach(function(event) {
1104 this.scrollBlockerNode_.addEventListener(event, onMouse);
1105 this.cursorNode_.addEventListener(event, onMouse);
1106 this.document_.addEventListener(event, onMouse);
1107 }.bind(this));
1108
1109 this.cursorNode_.addEventListener('mousedown', function() {
1110 setTimeout(this.focus.bind(this));
1111 }.bind(this));
1112
rgindade84e382012-04-20 15:39:31 -07001113 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda8ba33642011-12-14 12:31:31 -08001114 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001115
rginda87b86462011-12-14 13:48:03 -08001116 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001117 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001118};
1119
rginda0918b652012-04-04 11:26:24 -07001120/**
1121 * Return the HTML document that contains the terminal DOM nodes.
1122 */
rginda87b86462011-12-14 13:48:03 -08001123hterm.Terminal.prototype.getDocument = function() {
1124 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001125};
1126
1127/**
rginda0918b652012-04-04 11:26:24 -07001128 * Focus the terminal.
1129 */
1130hterm.Terminal.prototype.focus = function() {
1131 this.scrollPort_.focus();
1132};
1133
1134/**
rginda8ba33642011-12-14 12:31:31 -08001135 * Return the HTML Element for a given row index.
1136 *
1137 * This is a method from the RowProvider interface. The ScrollPort uses
1138 * it to fetch rows on demand as they are scrolled into view.
1139 *
1140 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1141 * pairs to conserve memory.
1142 *
1143 * @param {integer} index The zero-based row index, measured relative to the
1144 * start of the scrollback buffer. On-screen rows will always have the
1145 * largest indicies.
1146 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1147 */
1148hterm.Terminal.prototype.getRowNode = function(index) {
1149 if (index < this.scrollbackRows_.length)
1150 return this.scrollbackRows_[index];
1151
1152 var screenIndex = index - this.scrollbackRows_.length;
1153 return this.screen_.rowsArray[screenIndex];
1154};
1155
1156/**
1157 * Return the text content for a given range of rows.
1158 *
1159 * This is a method from the RowProvider interface. The ScrollPort uses
1160 * it to fetch text content on demand when the user attempts to copy their
1161 * selection to the clipboard.
1162 *
1163 * @param {integer} start The zero-based row index to start from, measured
1164 * relative to the start of the scrollback buffer. On-screen rows will
1165 * always have the largest indicies.
1166 * @param {integer} end The zero-based row index to end on, measured
1167 * relative to the start of the scrollback buffer.
1168 * @return {string} A single string containing the text value of the range of
1169 * rows. Lines will be newline delimited, with no trailing newline.
1170 */
1171hterm.Terminal.prototype.getRowsText = function(start, end) {
1172 var ary = [];
1173 for (var i = start; i < end; i++) {
1174 var node = this.getRowNode(i);
1175 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001176 if (i < end - 1 && !node.getAttribute('line-overflow'))
1177 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001178 }
1179
rgindaa09e7332012-08-17 12:49:51 -07001180 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001181};
1182
1183/**
1184 * Return the text content for a given row.
1185 *
1186 * This is a method from the RowProvider interface. The ScrollPort uses
1187 * it to fetch text content on demand when the user attempts to copy their
1188 * selection to the clipboard.
1189 *
1190 * @param {integer} index The zero-based row index to return, measured
1191 * relative to the start of the scrollback buffer. On-screen rows will
1192 * always have the largest indicies.
1193 * @return {string} A string containing the text value of the selected row.
1194 */
1195hterm.Terminal.prototype.getRowText = function(index) {
1196 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001197 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001198};
1199
1200/**
1201 * Return the total number of rows in the addressable screen and in the
1202 * scrollback buffer of this terminal.
1203 *
1204 * This is a method from the RowProvider interface. The ScrollPort uses
1205 * it to compute the size of the scrollbar.
1206 *
1207 * @return {integer} The number of rows in this terminal.
1208 */
1209hterm.Terminal.prototype.getRowCount = function() {
1210 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1211};
1212
1213/**
1214 * Create DOM nodes for new rows and append them to the end of the terminal.
1215 *
1216 * This is the only correct way to add a new DOM node for a row. Notice that
1217 * the new row is appended to the bottom of the list of rows, and does not
1218 * require renumbering (of the rowIndex property) of previous rows.
1219 *
1220 * If you think you want a new blank row somewhere in the middle of the
1221 * terminal, look into moveRows_().
1222 *
1223 * This method does not pay attention to vtScrollTop/Bottom, since you should
1224 * be using moveRows() in cases where they would matter.
1225 *
1226 * The cursor will be positioned at column 0 of the first inserted line.
1227 */
1228hterm.Terminal.prototype.appendRows_ = function(count) {
1229 var cursorRow = this.screen_.rowsArray.length;
1230 var offset = this.scrollbackRows_.length + cursorRow;
1231 for (var i = 0; i < count; i++) {
1232 var row = this.document_.createElement('x-row');
1233 row.appendChild(this.document_.createTextNode(''));
1234 row.rowIndex = offset + i;
1235 this.screen_.pushRow(row);
1236 }
1237
1238 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1239 if (extraRows > 0) {
1240 var ary = this.screen_.shiftRows(extraRows);
1241 Array.prototype.push.apply(this.scrollbackRows_, ary);
1242 this.scheduleScrollDown_();
1243 }
1244
1245 if (cursorRow >= this.screen_.rowsArray.length)
1246 cursorRow = this.screen_.rowsArray.length - 1;
1247
rginda87b86462011-12-14 13:48:03 -08001248 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001249};
1250
1251/**
1252 * Relocate rows from one part of the addressable screen to another.
1253 *
1254 * This is used to recycle rows during VT scrolls (those which are driven
1255 * by VT commands, rather than by the user manipulating the scrollbar.)
1256 *
1257 * In this case, the blank lines scrolled into the scroll region are made of
1258 * the nodes we scrolled off. These have their rowIndex properties carefully
1259 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001260 */
1261hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1262 var ary = this.screen_.removeRows(fromIndex, count);
1263 this.screen_.insertRows(toIndex, ary);
1264
1265 var start, end;
1266 if (fromIndex < toIndex) {
1267 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001268 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001269 } else {
1270 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001271 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001272 }
1273
1274 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001275 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001276};
1277
1278/**
1279 * Renumber the rowIndex property of the given range of rows.
1280 *
1281 * The start and end indicies are relative to the screen, not the scrollback.
1282 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001283 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001284 * no need to renumber scrollback rows.
1285 */
1286hterm.Terminal.prototype.renumberRows_ = function(start, end) {
1287 var offset = this.scrollbackRows_.length;
1288 for (var i = start; i < end; i++) {
1289 this.screen_.rowsArray[i].rowIndex = offset + i;
1290 }
1291};
1292
1293/**
1294 * Print a string to the terminal.
1295 *
1296 * This respects the current insert and wraparound modes. It will add new lines
1297 * to the end of the terminal, scrolling off the top into the scrollback buffer
1298 * if necessary.
1299 *
1300 * The string is *not* parsed for escape codes. Use the interpret() method if
1301 * that's what you're after.
1302 *
1303 * @param{string} str The string to print.
1304 */
1305hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001306 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001307
rgindaa9abdd82012-08-06 18:05:09 -07001308 while (startOffset < str.length) {
rgindaa09e7332012-08-17 12:49:51 -07001309 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1310 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001311 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001312 }
rgindaa19afe22012-01-25 15:40:22 -08001313
rgindaa9abdd82012-08-06 18:05:09 -07001314 var count = str.length - startOffset;
1315 var didOverflow = false;
1316 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001317
rgindaa9abdd82012-08-06 18:05:09 -07001318 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1319 didOverflow = true;
1320 count = this.screenSize.width - this.screen_.cursorPosition.column;
1321 }
rgindaa19afe22012-01-25 15:40:22 -08001322
rgindaa9abdd82012-08-06 18:05:09 -07001323 if (didOverflow && !this.options_.wraparound) {
1324 // If the string overflowed the line but wraparound is off, then the
1325 // last printed character should be the last of the string.
1326 // TODO: This will add to our problems with multibyte UTF-16 characters.
1327 substr = str.substr(startOffset, count - 1) +
1328 str.substr(str.length - 1);
1329 count = str.length;
1330 } else {
1331 substr = str.substr(startOffset, count);
1332 }
rgindaa19afe22012-01-25 15:40:22 -08001333
rgindaa9abdd82012-08-06 18:05:09 -07001334 if (this.options_.insertMode) {
1335 this.screen_.insertString(substr);
1336 } else {
1337 this.screen_.overwriteString(substr);
1338 }
1339
1340 this.screen_.maybeClipCurrentRow();
1341 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001342 }
rginda8ba33642011-12-14 12:31:31 -08001343
1344 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001345
rginda9f5222b2012-03-05 11:53:28 -08001346 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001347 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001348};
1349
1350/**
rginda87b86462011-12-14 13:48:03 -08001351 * Set the VT scroll region.
1352 *
rginda87b86462011-12-14 13:48:03 -08001353 * This also resets the cursor position to the absolute (0, 0) position, since
1354 * that's what xterm appears to do.
1355 *
1356 * @param {integer} scrollTop The zero-based top of the scroll region.
1357 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1358 * inclusive.
1359 */
1360hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1361 this.vtScrollTop_ = scrollTop;
1362 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001363};
1364
1365/**
rginda8ba33642011-12-14 12:31:31 -08001366 * Return the top row index according to the VT.
1367 *
1368 * This will return 0 unless the terminal has been told to restrict scrolling
1369 * to some lower row. It is used for some VT cursor positioning and scrolling
1370 * commands.
1371 *
1372 * @return {integer} The topmost row in the terminal's scroll region.
1373 */
1374hterm.Terminal.prototype.getVTScrollTop = function() {
1375 if (this.vtScrollTop_ != null)
1376 return this.vtScrollTop_;
1377
1378 return 0;
rginda87b86462011-12-14 13:48:03 -08001379};
rginda8ba33642011-12-14 12:31:31 -08001380
1381/**
1382 * Return the bottom row index according to the VT.
1383 *
1384 * This will return the height of the terminal unless the it has been told to
1385 * restrict scrolling to some higher row. It is used for some VT cursor
1386 * positioning and scrolling commands.
1387 *
1388 * @return {integer} The bottommost row in the terminal's scroll region.
1389 */
1390hterm.Terminal.prototype.getVTScrollBottom = function() {
1391 if (this.vtScrollBottom_ != null)
1392 return this.vtScrollBottom_;
1393
rginda87b86462011-12-14 13:48:03 -08001394 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001395}
1396
1397/**
1398 * Process a '\n' character.
1399 *
1400 * If the cursor is on the final row of the terminal this will append a new
1401 * blank row to the screen and scroll the topmost row into the scrollback
1402 * buffer.
1403 *
1404 * Otherwise, this moves the cursor to column zero of the next row.
1405 */
1406hterm.Terminal.prototype.newLine = function() {
1407 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001408 // If we're at the end of the screen we need to append a new line and
1409 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001410 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001411 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1412 // End of the scroll region does not affect the scrollback buffer.
1413 this.vtScrollUp(1);
1414 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001415 } else {
rginda87b86462011-12-14 13:48:03 -08001416 // Anywhere else in the screen just moves the cursor.
1417 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001418 }
1419};
1420
1421/**
1422 * Like newLine(), except maintain the cursor column.
1423 */
1424hterm.Terminal.prototype.lineFeed = function() {
1425 var column = this.screen_.cursorPosition.column;
1426 this.newLine();
1427 this.setCursorColumn(column);
1428};
1429
1430/**
rginda87b86462011-12-14 13:48:03 -08001431 * If autoCarriageReturn is set then newLine(), else lineFeed().
1432 */
1433hterm.Terminal.prototype.formFeed = function() {
1434 if (this.options_.autoCarriageReturn) {
1435 this.newLine();
1436 } else {
1437 this.lineFeed();
1438 }
1439};
1440
1441/**
1442 * Move the cursor up one row, possibly inserting a blank line.
1443 *
1444 * The cursor column is not changed.
1445 */
1446hterm.Terminal.prototype.reverseLineFeed = function() {
1447 var scrollTop = this.getVTScrollTop();
1448 var currentRow = this.screen_.cursorPosition.row;
1449
1450 if (currentRow == scrollTop) {
1451 this.insertLines(1);
1452 } else {
1453 this.setAbsoluteCursorRow(currentRow - 1);
1454 }
1455};
1456
1457/**
rginda8ba33642011-12-14 12:31:31 -08001458 * Replace all characters to the left of the current cursor with the space
1459 * character.
1460 *
1461 * TODO(rginda): This should probably *remove* the characters (not just replace
1462 * with a space) if there are no characters at or beyond the current cursor
1463 * position. Once it does that, it'll have the same text-attribute related
1464 * issues as hterm.Screen.prototype.clearCursorRow :/
1465 */
1466hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001467 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001468 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001469 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001470 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001471};
1472
1473/**
David Benjamin684a9b72012-05-01 17:19:58 -04001474 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001475 *
1476 * The cursor position is unchanged.
1477 *
1478 * TODO(rginda): Test that this works even when the cursor is positioned beyond
1479 * the end of the text.
1480 *
1481 * TODO(rginda): This likely has text-attribute related troubles similar to the
1482 * todo on hterm.Screen.prototype.clearCursorRow.
David Benjamin684a9b72012-05-01 17:19:58 -04001483 *
1484 * TODO(davidben): Probably better to not add the whitespace to the clipboard
1485 * if erasing to the end of the drawn portion of the line. That said, xterm
1486 * behaves the same here.
rginda8ba33642011-12-14 12:31:31 -08001487 */
1488hterm.Terminal.prototype.eraseToRight = function(opt_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 var maxCount = this.screenSize.width - cursor.column;
David Benjamin684a9b72012-05-01 17:19:58 -04001492 if (opt_count === undefined || opt_count >= maxCount) {
1493 this.screen_.deleteChars(maxCount);
1494 } else {
rgindacbbd7482012-06-13 15:06:16 -07001495 this.screen_.overwriteString(lib.f.getWhitespace(opt_count));
David Benjamin684a9b72012-05-01 17:19:58 -04001496 }
rginda87b86462011-12-14 13:48:03 -08001497 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001498 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001499};
1500
1501/**
1502 * Erase the current line.
1503 *
1504 * The cursor position is unchanged.
1505 *
1506 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1507 * has a text-attribute related TODO.
1508 */
1509hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001510 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001511 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001512 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001513 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001514};
1515
1516/**
David Benjamina08d78f2012-05-05 00:28:49 -04001517 * Erase all characters from the start of the screen to the current cursor
1518 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001519 *
1520 * The cursor position is unchanged.
1521 *
1522 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1523 * has a text-attribute related TODO.
1524 */
1525hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001526 var cursor = this.saveCursor();
1527
1528 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001529
David Benjamina08d78f2012-05-05 00:28:49 -04001530 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001531 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001532 this.screen_.clearCursorRow();
1533 }
1534
rginda87b86462011-12-14 13:48:03 -08001535 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001536 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001537};
1538
1539/**
1540 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001541 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001542 *
1543 * The cursor position is unchanged.
1544 *
1545 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1546 * has a text-attribute related TODO.
1547 */
1548hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001549 var cursor = this.saveCursor();
1550
1551 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001552
David Benjamina08d78f2012-05-05 00:28:49 -04001553 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001554 for (var i = cursor.row + 1; i <= bottom; i++) {
1555 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001556 this.screen_.clearCursorRow();
1557 }
1558
rginda87b86462011-12-14 13:48:03 -08001559 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001560 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001561};
1562
1563/**
1564 * Fill the terminal with a given character.
1565 *
1566 * This methods does not respect the VT scroll region.
1567 *
1568 * @param {string} ch The character to use for the fill.
1569 */
1570hterm.Terminal.prototype.fill = function(ch) {
1571 var cursor = this.saveCursor();
1572
1573 this.setAbsoluteCursorPosition(0, 0);
1574 for (var row = 0; row < this.screenSize.height; row++) {
1575 for (var col = 0; col < this.screenSize.width; col++) {
1576 this.setAbsoluteCursorPosition(row, col);
1577 this.screen_.overwriteString(ch);
1578 }
1579 }
1580
1581 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001582};
1583
1584/**
rginda9ea433c2012-03-16 11:57:00 -07001585 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001586 *
rginda9ea433c2012-03-16 11:57:00 -07001587 * This does not respect the scroll region.
1588 *
1589 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1590 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001591 *
1592 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1593 * has a text-attribute related TODO.
1594 */
rginda9ea433c2012-03-16 11:57:00 -07001595hterm.Terminal.prototype.clearHome = function(opt_screen) {
1596 var screen = opt_screen || this.screen_;
1597 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001598
rginda11057d52012-04-25 12:29:56 -07001599 if (bottom == 0) {
1600 // Empty screen, nothing to do.
1601 return;
1602 }
1603
rgindae4d29232012-01-19 10:47:13 -08001604 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001605 screen.setCursorPosition(i, 0);
1606 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001607 }
1608
rginda9ea433c2012-03-16 11:57:00 -07001609 screen.setCursorPosition(0, 0);
1610};
1611
1612/**
1613 * Erase the entire display without changing the cursor position.
1614 *
1615 * The cursor position is unchanged. This does not respect the scroll
1616 * region.
1617 *
1618 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1619 * to the current screen.
1620 *
1621 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1622 * has a text-attribute related TODO.
1623 */
1624hterm.Terminal.prototype.clear = function(opt_screen) {
1625 var screen = opt_screen || this.screen_;
1626 var cursor = screen.cursorPosition.clone();
1627 this.clearHome(screen);
1628 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001629};
1630
1631/**
1632 * VT command to insert lines at the current cursor row.
1633 *
1634 * This respects the current scroll region. Rows pushed off the bottom are
1635 * lost (they won't show up in the scrollback buffer).
1636 *
1637 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1638 * has a text-attribute related TODO.
1639 *
1640 * @param {integer} count The number of lines to insert.
1641 */
1642hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001643 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001644
1645 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001646 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001647
rgindae4d29232012-01-19 10:47:13 -08001648 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001649 if (start != cursor.row)
1650 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001651
1652 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001653 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001654 this.screen_.clearCursorRow();
1655 }
1656
rginda87b86462011-12-14 13:48:03 -08001657 cursor.column = 0;
1658 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001659};
1660
1661/**
1662 * VT command to delete lines at the current cursor row.
1663 *
1664 * New rows are added to the bottom of scroll region to take their place. New
1665 * rows are strictly there to take up space and have no content or style.
1666 */
1667hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001668 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001669
rginda87b86462011-12-14 13:48:03 -08001670 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001671 var bottom = this.getVTScrollBottom();
1672
rginda87b86462011-12-14 13:48:03 -08001673 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001674 count = Math.min(count, maxCount);
1675
rginda87b86462011-12-14 13:48:03 -08001676 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001677 if (count != maxCount)
1678 this.moveRows_(top, count, moveStart);
1679
1680 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001681 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001682 this.screen_.clearCursorRow();
1683 }
1684
rginda87b86462011-12-14 13:48:03 -08001685 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001686 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001687};
1688
1689/**
1690 * Inserts the given number of spaces at the current cursor position.
1691 *
rginda87b86462011-12-14 13:48:03 -08001692 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001693 */
1694hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001695 var cursor = this.saveCursor();
1696
rgindacbbd7482012-06-13 15:06:16 -07001697 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001698 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001699 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001700
1701 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001702 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001703};
1704
1705/**
1706 * Forward-delete the specified number of characters starting at the cursor
1707 * position.
1708 *
1709 * @param {integer} count The number of characters to delete.
1710 */
1711hterm.Terminal.prototype.deleteChars = function(count) {
1712 this.screen_.deleteChars(count);
David Benjamin54e8bf62012-06-01 22:31:40 -04001713 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001714};
1715
1716/**
1717 * Shift rows in the scroll region upwards by a given number of lines.
1718 *
1719 * New rows are inserted at the bottom of the scroll region to fill the
1720 * vacated rows. The new rows not filled out with the current text attributes.
1721 *
1722 * This function does not affect the scrollback rows at all. Rows shifted
1723 * off the top are lost.
1724 *
rginda87b86462011-12-14 13:48:03 -08001725 * The cursor position is not altered.
1726 *
rginda8ba33642011-12-14 12:31:31 -08001727 * @param {integer} count The number of rows to scroll.
1728 */
1729hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001730 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001731
rginda87b86462011-12-14 13:48:03 -08001732 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001733 this.deleteLines(count);
1734
rginda87b86462011-12-14 13:48:03 -08001735 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001736};
1737
1738/**
1739 * Shift rows below the cursor down by a given number of lines.
1740 *
1741 * This function respects the current scroll region.
1742 *
1743 * New rows are inserted at the top of the scroll region to fill the
1744 * vacated rows. The new rows not filled out with the current text attributes.
1745 *
1746 * This function does not affect the scrollback rows at all. Rows shifted
1747 * off the bottom are lost.
1748 *
1749 * @param {integer} count The number of rows to scroll.
1750 */
1751hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001752 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001753
rginda87b86462011-12-14 13:48:03 -08001754 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001755 this.insertLines(opt_count);
1756
rginda87b86462011-12-14 13:48:03 -08001757 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001758};
1759
rginda87b86462011-12-14 13:48:03 -08001760
rginda8ba33642011-12-14 12:31:31 -08001761/**
1762 * Set the cursor position.
1763 *
1764 * The cursor row is relative to the scroll region if the terminal has
1765 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1766 *
1767 * @param {integer} row The new zero-based cursor row.
1768 * @param {integer} row The new zero-based cursor column.
1769 */
1770hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1771 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001772 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001773 } else {
rginda87b86462011-12-14 13:48:03 -08001774 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001775 }
rginda87b86462011-12-14 13:48:03 -08001776};
rginda8ba33642011-12-14 12:31:31 -08001777
rginda87b86462011-12-14 13:48:03 -08001778hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1779 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001780 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1781 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001782 this.screen_.setCursorPosition(row, column);
1783};
1784
1785hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001786 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1787 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001788 this.screen_.setCursorPosition(row, column);
1789};
1790
1791/**
1792 * Set the cursor column.
1793 *
1794 * @param {integer} column The new zero-based cursor column.
1795 */
1796hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001797 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001798};
1799
1800/**
1801 * Return the cursor column.
1802 *
1803 * @return {integer} The zero-based cursor column.
1804 */
1805hterm.Terminal.prototype.getCursorColumn = function() {
1806 return this.screen_.cursorPosition.column;
1807};
1808
1809/**
1810 * Set the cursor row.
1811 *
1812 * The cursor row is relative to the scroll region if the terminal has
1813 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1814 *
1815 * @param {integer} row The new cursor row.
1816 */
rginda87b86462011-12-14 13:48:03 -08001817hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1818 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001819};
1820
1821/**
1822 * Return the cursor row.
1823 *
1824 * @return {integer} The zero-based cursor row.
1825 */
1826hterm.Terminal.prototype.getCursorRow = function(row) {
1827 return this.screen_.cursorPosition.row;
1828};
1829
1830/**
1831 * Request that the ScrollPort redraw itself soon.
1832 *
1833 * The redraw will happen asynchronously, soon after the call stack winds down.
1834 * Multiple calls will be coalesced into a single redraw.
1835 */
1836hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001837 if (this.timeouts_.redraw)
1838 return;
rginda8ba33642011-12-14 12:31:31 -08001839
1840 var self = this;
rginda87b86462011-12-14 13:48:03 -08001841 this.timeouts_.redraw = setTimeout(function() {
1842 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001843 self.scrollPort_.redraw_();
1844 }, 0);
1845};
1846
1847/**
1848 * Request that the ScrollPort be scrolled to the bottom.
1849 *
1850 * The scroll will happen asynchronously, soon after the call stack winds down.
1851 * Multiple calls will be coalesced into a single scroll.
1852 *
1853 * This affects the scrollbar position of the ScrollPort, and has nothing to
1854 * do with the VT scroll commands.
1855 */
1856hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1857 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001858 return;
rginda8ba33642011-12-14 12:31:31 -08001859
1860 var self = this;
1861 this.timeouts_.scrollDown = setTimeout(function() {
1862 delete self.timeouts_.scrollDown;
1863 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1864 }, 10);
1865};
1866
1867/**
1868 * Move the cursor up a specified number of rows.
1869 *
1870 * @param {integer} count The number of rows to move the cursor.
1871 */
1872hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001873 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001874};
1875
1876/**
1877 * Move the cursor down a specified number of rows.
1878 *
1879 * @param {integer} count The number of rows to move the cursor.
1880 */
1881hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001882 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001883 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1884 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1885 this.screenSize.height - 1);
1886
rgindacbbd7482012-06-13 15:06:16 -07001887 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001888 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001889 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001890};
1891
1892/**
1893 * Move the cursor left a specified number of columns.
1894 *
1895 * @param {integer} count The number of columns to move the cursor.
1896 */
1897hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001898 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001899};
1900
1901/**
1902 * Move the cursor right a specified number of columns.
1903 *
1904 * @param {integer} count The number of columns to move the cursor.
1905 */
1906hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001907 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001908 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001909 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001910 this.setCursorColumn(column);
1911};
1912
1913/**
1914 * Reverse the foreground and background colors of the terminal.
1915 *
1916 * This only affects text that was drawn with no attributes.
1917 *
1918 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1919 * been drawn with attributes that happen to coincide with the default
1920 * 'no-attribute' colors. My guess is probably not.
1921 */
1922hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001923 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001924 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001925 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1926 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001927 } else {
rginda9f5222b2012-03-05 11:53:28 -08001928 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1929 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001930 }
1931};
1932
1933/**
rginda87b86462011-12-14 13:48:03 -08001934 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001935 */
1936hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001937 if (this.bellAudio_.getAttribute('src'))
1938 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001939
rginda6d397402012-01-17 10:58:29 -08001940 this.cursorNode_.style.backgroundColor =
1941 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001942
1943 var self = this;
1944 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001945 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001946 }, 200);
rginda87b86462011-12-14 13:48:03 -08001947};
1948
1949/**
rginda8ba33642011-12-14 12:31:31 -08001950 * Set the origin mode bit.
1951 *
1952 * If origin mode is on, certain VT cursor and scrolling commands measure their
1953 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1954 * to the top of the addressable screen.
1955 *
1956 * Defaults to off.
1957 *
1958 * @param {boolean} state True to set origin mode, false to unset.
1959 */
1960hterm.Terminal.prototype.setOriginMode = function(state) {
1961 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001962 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001963};
1964
1965/**
1966 * Set the insert mode bit.
1967 *
1968 * If insert mode is on, existing text beyond the cursor position will be
1969 * shifted right to make room for new text. Otherwise, new text overwrites
1970 * any existing text.
1971 *
1972 * Defaults to off.
1973 *
1974 * @param {boolean} state True to set insert mode, false to unset.
1975 */
1976hterm.Terminal.prototype.setInsertMode = function(state) {
1977 this.options_.insertMode = state;
1978};
1979
1980/**
rginda87b86462011-12-14 13:48:03 -08001981 * Set the auto carriage return bit.
1982 *
1983 * If auto carriage return is on then a formfeed character is interpreted
1984 * as a newline, otherwise it's the same as a linefeed. The difference boils
1985 * down to whether or not the cursor column is reset.
1986 */
1987hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1988 this.options_.autoCarriageReturn = state;
1989};
1990
1991/**
rginda8ba33642011-12-14 12:31:31 -08001992 * Set the wraparound mode bit.
1993 *
1994 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1995 * to the start of the following row. Otherwise, the cursor is clamped to the
1996 * end of the screen and attempts to write past it are ignored.
1997 *
1998 * Defaults to on.
1999 *
2000 * @param {boolean} state True to set wraparound mode, false to unset.
2001 */
2002hterm.Terminal.prototype.setWraparound = function(state) {
2003 this.options_.wraparound = state;
2004};
2005
2006/**
2007 * Set the reverse-wraparound mode bit.
2008 *
2009 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2010 * to the end of the previous row. Otherwise, the cursor is clamped to column
2011 * 0.
2012 *
2013 * Defaults to off.
2014 *
2015 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2016 */
2017hterm.Terminal.prototype.setReverseWraparound = function(state) {
2018 this.options_.reverseWraparound = state;
2019};
2020
2021/**
2022 * Selects between the primary and alternate screens.
2023 *
2024 * If alternate mode is on, the alternate screen is active. Otherwise the
2025 * primary screen is active.
2026 *
2027 * Swapping screens has no effect on the scrollback buffer.
2028 *
2029 * Each screen maintains its own cursor position.
2030 *
2031 * Defaults to off.
2032 *
2033 * @param {boolean} state True to set alternate mode, false to unset.
2034 */
2035hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002036 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002037 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2038
rginda35c456b2012-02-09 17:29:05 -08002039 if (this.screen_.rowsArray.length &&
2040 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2041 // If the screen changed sizes while we were away, our rowIndexes may
2042 // be incorrect.
2043 var offset = this.scrollbackRows_.length;
2044 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002045 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002046 ary[i].rowIndex = offset + i;
2047 }
2048 }
rginda8ba33642011-12-14 12:31:31 -08002049
rginda35c456b2012-02-09 17:29:05 -08002050 this.realizeWidth_(this.screenSize.width);
2051 this.realizeHeight_(this.screenSize.height);
2052 this.scrollPort_.syncScrollHeight();
2053 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002054
rginda6d397402012-01-17 10:58:29 -08002055 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002056 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002057};
2058
2059/**
2060 * Set the cursor-blink mode bit.
2061 *
2062 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2063 * a visible cursor does not blink.
2064 *
2065 * You should make sure to turn blinking off if you're going to dispose of a
2066 * terminal, otherwise you'll leak a timeout.
2067 *
2068 * Defaults to on.
2069 *
2070 * @param {boolean} state True to set cursor-blink mode, false to unset.
2071 */
2072hterm.Terminal.prototype.setCursorBlink = function(state) {
2073 this.options_.cursorBlink = state;
2074
2075 if (!state && this.timeouts_.cursorBlink) {
2076 clearTimeout(this.timeouts_.cursorBlink);
2077 delete this.timeouts_.cursorBlink;
2078 }
2079
2080 if (this.options_.cursorVisible)
2081 this.setCursorVisible(true);
2082};
2083
2084/**
2085 * Set the cursor-visible mode bit.
2086 *
2087 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2088 *
2089 * Defaults to on.
2090 *
2091 * @param {boolean} state True to set cursor-visible mode, false to unset.
2092 */
2093hterm.Terminal.prototype.setCursorVisible = function(state) {
2094 this.options_.cursorVisible = state;
2095
2096 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002097 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002098 return;
2099 }
2100
rginda87b86462011-12-14 13:48:03 -08002101 this.syncCursorPosition_();
2102
2103 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002104
2105 if (this.options_.cursorBlink) {
2106 if (this.timeouts_.cursorBlink)
2107 return;
2108
2109 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2110 500);
2111 } else {
2112 if (this.timeouts_.cursorBlink) {
2113 clearTimeout(this.timeouts_.cursorBlink);
2114 delete this.timeouts_.cursorBlink;
2115 }
2116 }
2117};
2118
2119/**
rginda87b86462011-12-14 13:48:03 -08002120 * Synchronizes the visible cursor and document selection with the current
2121 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002122 */
2123hterm.Terminal.prototype.syncCursorPosition_ = function() {
2124 var topRowIndex = this.scrollPort_.getTopRowIndex();
2125 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2126 var cursorRowIndex = this.scrollbackRows_.length +
2127 this.screen_.cursorPosition.row;
2128
2129 if (cursorRowIndex > bottomRowIndex) {
2130 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002131 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002132 return;
2133 }
2134
rginda35c456b2012-02-09 17:29:05 -08002135 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
2136 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
2137
rginda8ba33642011-12-14 12:31:31 -08002138 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002139 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2140 'px';
2141 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2142 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002143
2144 this.cursorNode_.setAttribute('title',
2145 '(' + this.screen_.cursorPosition.row +
2146 ', ' + this.screen_.cursorPosition.column +
2147 ')');
2148
2149 // Update the caret for a11y purposes.
2150 var selection = this.document_.getSelection();
2151 if (selection && selection.isCollapsed)
2152 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002153};
2154
2155/**
2156 * Synchronizes the visible cursor with the current cursor coordinates.
2157 *
2158 * The sync will happen asynchronously, soon after the call stack winds down.
2159 * Multiple calls will be coalesced into a single sync.
2160 */
2161hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2162 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002163 return;
rginda8ba33642011-12-14 12:31:31 -08002164
2165 var self = this;
2166 this.timeouts_.syncCursor = setTimeout(function() {
2167 self.syncCursorPosition_();
2168 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002169 }, 0);
2170};
2171
rgindacc2996c2012-02-24 14:59:31 -08002172/**
rgindaf522ce02012-04-17 17:49:17 -07002173 * Show or hide the zoom warning.
2174 *
2175 * The zoom warning is a message warning the user that their browser zoom must
2176 * be set to 100% in order for hterm to function properly.
2177 *
2178 * @param {boolean} state True to show the message, false to hide it.
2179 */
2180hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2181 if (!this.zoomWarningNode_) {
2182 if (!state)
2183 return;
2184
2185 this.zoomWarningNode_ = this.document_.createElement('div');
2186 this.zoomWarningNode_.style.cssText = (
2187 'color: black;' +
2188 'background-color: #ff2222;' +
2189 'font-size: large;' +
2190 'border-radius: 8px;' +
2191 'opacity: 0.75;' +
2192 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2193 'top: 0.5em;' +
2194 'right: 1.2em;' +
2195 'position: absolute;' +
2196 '-webkit-text-size-adjust: none;' +
2197 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002198 }
2199
rgindade84e382012-04-20 15:39:31 -07002200 this.zoomWarningNode_.textContent = hterm.msg('ZOOM_WARNING') ||
2201 ('!! ' + parseInt(this.scrollPort_.characterSize.zoomFactor * 100) +
2202 '% !!');
rgindaf522ce02012-04-17 17:49:17 -07002203 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2204
2205 if (state) {
2206 if (!this.zoomWarningNode_.parentNode)
2207 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2208 } else if (this.zoomWarningNode_.parentNode) {
2209 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2210 }
2211};
2212
2213/**
rgindacc2996c2012-02-24 14:59:31 -08002214 * Show the terminal overlay for a given amount of time.
2215 *
2216 * The terminal overlay appears in inverse video in a large font, centered
2217 * over the terminal. You should probably keep the overlay message brief,
2218 * since it's in a large font and you probably aren't going to check the size
2219 * of the terminal first.
2220 *
2221 * @param {string} msg The text (not HTML) message to display in the overlay.
2222 * @param {number} opt_timeout The amount of time to wait before fading out
2223 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2224 * stay up forever (or until the next overlay).
2225 */
2226hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002227 if (!this.overlayNode_) {
2228 if (!this.div_)
2229 return;
2230
2231 this.overlayNode_ = this.document_.createElement('div');
2232 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002233 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002234 'font-size: xx-large;' +
2235 'opacity: 0.75;' +
2236 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2237 'position: absolute;' +
2238 '-webkit-user-select: none;' +
2239 '-webkit-transition: opacity 180ms ease-in;');
2240 }
2241
rginda9f5222b2012-03-05 11:53:28 -08002242 this.overlayNode_.style.color = this.prefs_.get('background-color');
2243 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2244 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2245
rgindaf0090c92012-02-10 14:58:52 -08002246 this.overlayNode_.textContent = msg;
2247 this.overlayNode_.style.opacity = '0.75';
2248
2249 if (!this.overlayNode_.parentNode)
2250 this.div_.appendChild(this.overlayNode_);
2251
2252 this.overlayNode_.style.top = (
2253 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
2254 this.overlayNode_.style.left = (
2255 this.div_.clientWidth - this.overlayNode_.clientWidth -
2256 this.scrollbarWidthPx) / 2;
2257
2258 var self = this;
2259
2260 if (this.overlayTimeout_)
2261 clearTimeout(this.overlayTimeout_);
2262
rgindacc2996c2012-02-24 14:59:31 -08002263 if (opt_timeout === null)
2264 return;
2265
rgindaf0090c92012-02-10 14:58:52 -08002266 this.overlayTimeout_ = setTimeout(function() {
2267 self.overlayNode_.style.opacity = '0';
2268 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002269 if (self.overlayNode_.parentNode)
2270 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002271 self.overlayTimeout_ = null;
2272 self.overlayNode_.style.opacity = '0.75';
2273 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002274 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002275};
2276
rginda4bba5e12012-06-20 16:15:30 -07002277/**
2278 * Paste from the system clipboard to the terminal.
2279 */
2280hterm.Terminal.prototype.paste = function() {
2281 hterm.pasteFromClipboard(this.document_);
2282};
2283
2284/**
2285 * Copy a string to the system clipboard.
2286 *
2287 * Note: If there is a selected range in the terminal, it'll be cleared.
2288 */
2289hterm.Terminal.prototype.copyStringToClipboard = function(str) {
rgindafaa74742012-08-21 13:34:03 -07002290 setTimeout(this.showOverlay.bind(this, hterm.msg('NOTIFY_COPY'), 500), 200);
rgindaa09e7332012-08-17 12:49:51 -07002291
2292 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002293 copySource.textContent = str;
2294 copySource.style.cssText = (
2295 '-webkit-user-select: text;' +
2296 'position: absolute;' +
2297 'top: -99px');
2298
2299 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002300
rginda4bba5e12012-06-20 16:15:30 -07002301 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002302 var anchorNode = selection.anchorNode;
2303 var anchorOffset = selection.anchorOffset;
2304 var focusNode = selection.focusNode;
2305 var focusOffset = selection.focusOffset;
2306
rginda4bba5e12012-06-20 16:15:30 -07002307 selection.selectAllChildren(copySource);
2308
rgindaa09e7332012-08-17 12:49:51 -07002309 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002310
rgindafaa74742012-08-21 13:34:03 -07002311 selection.collapse(anchorNode, anchorOffset);
2312 selection.extend(focusNode, focusOffset);
2313
rginda4bba5e12012-06-20 16:15:30 -07002314 copySource.parentNode.removeChild(copySource);
2315};
2316
rgindaa09e7332012-08-17 12:49:51 -07002317hterm.Terminal.prototype.getSelectionText = function() {
2318 var selection = this.scrollPort_.selection;
2319 selection.sync();
2320
2321 if (selection.isCollapsed)
2322 return null;
2323
2324
2325 // Start offset measures from the beginning of the line.
2326 var startOffset = selection.startOffset;
2327 var node = selection.startNode;
2328 while (node.previousSibling) {
2329 node = node.previousSibling;
2330 startOffset += node.textContent.length;
2331 }
2332
2333 // End offset measures from the end of the line.
2334 var endOffset = selection.endNode.textContent.length - selection.endOffset;
2335 var node = selection.endNode;
2336 while (node.nextSibling) {
2337 node = node.nextSibling;
2338 endOffset += node.textContent.length;
2339 }
2340
2341 var rv = this.getRowsText(selection.startRow.rowIndex,
2342 selection.endRow.rowIndex + 1);
2343 return rv.substring(startOffset, rv.length - endOffset);
2344};
2345
rginda4bba5e12012-06-20 16:15:30 -07002346/**
2347 * Copy the current selection to the system clipboard, then clear it after a
2348 * short delay.
2349 */
2350hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002351 var text = this.getSelectionText();
2352 if (text != null)
2353 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002354};
2355
rgindaf0090c92012-02-10 14:58:52 -08002356hterm.Terminal.prototype.overlaySize = function() {
2357 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2358};
2359
rginda87b86462011-12-14 13:48:03 -08002360/**
2361 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2362 *
2363 * @param {string} string The VT string representing the keystroke.
2364 */
2365hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002366 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002367 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2368
2369 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08002370};
2371
2372/**
rgindad5613292012-06-19 15:40:37 -07002373 * Add the terminalRow and terminalColumn properties to mouse events and
2374 * then forward on to onMouse().
2375 *
2376 * The terminalRow and terminalColumn properties contain the (row, column)
2377 * coordinates for the mouse event.
2378 */
2379hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002380 if (e.processedByTerminalHandler_) {
2381 // We register our event handlers on the document, as well as the cursor
2382 // and the scroll blocker. Mouse events that occur on the cursor or
2383 // scroll blocker will also appear on the document, but we don't want to
2384 // process them twice.
2385 //
2386 // We can't just prevent bubbling because that has other side effects, so
2387 // we decorate the event object with this property instead.
2388 return;
2389 }
2390
2391 e.processedByTerminalHandler_ = true;
2392
rginda4bba5e12012-06-20 16:15:30 -07002393 if (e.type == 'mousedown' && e.which == this.mousePasteButton) {
2394 this.paste();
2395 return;
2396 }
2397
2398 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2399 !this.document_.getSelection().isCollapsed) {
rgindafaa74742012-08-21 13:34:03 -07002400 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002401 return;
2402 }
2403
rgindad5613292012-06-19 15:40:37 -07002404 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2405 this.scrollPort_.characterSize.height) + 1;
2406 e.terminalColumn = parseInt(e.clientX /
2407 this.scrollPort_.characterSize.width) + 1;
2408
2409 if (e.type == 'mousedown') {
2410 if (e.terminalColumn > this.screenSize.width) {
2411 // Mousedown in the scrollbar area.
2412 return;
2413 }
2414
2415 if (!this.enableMouseDragScroll) {
2416 // Move the scroll-blocker into place if we want to keep the scrollport
2417 // from scrolling.
2418 this.scrollBlockerNode_.engaged = true;
2419 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2420 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2421 }
2422 } else if (this.scrollBlockerNode_.engaged &&
2423 (e.type == 'mousemove' || e.type == 'mouseup')) {
2424 // Disengage the scroll-blocker after one of these events.
2425 this.scrollBlockerNode_.engaged = false;
2426 this.scrollBlockerNode_.style.top = '-99px';
2427 }
2428
rgindafaa74742012-08-21 13:34:03 -07002429 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002430};
2431
2432/**
2433 * Clients should override this if they care to know about mouse events.
2434 *
2435 * The event parameter will be a normal DOM mouse click event with additional
2436 * 'terminalRow' and 'terminalColumn' properties.
2437 */
2438hterm.Terminal.prototype.onMouse = function(e) { };
2439
2440/**
rginda8e92a692012-05-20 19:37:20 -07002441 * React when focus changes.
2442 */
2443hterm.Terminal.prototype.onFocusChange_ = function(state) {
2444 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
2445};
2446
2447/**
rginda8ba33642011-12-14 12:31:31 -08002448 * React when the ScrollPort is scrolled.
2449 */
2450hterm.Terminal.prototype.onScroll_ = function() {
2451 this.scheduleSyncCursorPosition_();
2452};
2453
2454/**
rginda9846e2f2012-01-27 13:53:33 -08002455 * React when text is pasted into the scrollPort.
2456 */
2457hterm.Terminal.prototype.onPaste_ = function(e) {
David Benjamin8f962172012-07-17 07:38:43 -04002458 this.io.onVTKeystroke(this.vt.encodeUTF8(e.text));
rginda9846e2f2012-01-27 13:53:33 -08002459};
2460
2461/**
rgindaa09e7332012-08-17 12:49:51 -07002462 * React when the user tries to copy from the scrollPort.
2463 */
2464hterm.Terminal.prototype.onCopy_ = function(e) {
2465 e.preventDefault();
rgindafaa74742012-08-21 13:34:03 -07002466 this.copySelectionToClipboard();
rgindaa09e7332012-08-17 12:49:51 -07002467};
2468
2469/**
rginda8ba33642011-12-14 12:31:31 -08002470 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002471 *
2472 * Note: This function should not directly contain code that alters the internal
2473 * state of the terminal. That kind of code belongs in realizeWidth or
2474 * realizeHeight, so that it can be executed synchronously in the case of a
2475 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002476 */
2477hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002478 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002479 this.scrollPort_.characterSize.width);
2480 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2481 this.scrollPort_.characterSize.height);
2482
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002483 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002484 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002485 // gets removed from the document or during the initial load, and we can't
2486 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002487 return;
2488 }
2489
rgindaa8ba17d2012-08-15 14:41:10 -07002490 var isNewSize = (columnCount != this.screenSize.width ||
2491 rowCount != this.screenSize.height);
2492
2493 // We do this even if the size didn't change, just to be sure everything is
2494 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002495 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002496 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002497
2498 if (isNewSize)
2499 this.overlaySize();
2500
2501 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002502};
2503
2504/**
2505 * Service the cursor blink timeout.
2506 */
2507hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002508 if (this.cursorNode_.style.opacity == '0') {
2509 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002510 } else {
rginda87b86462011-12-14 13:48:03 -08002511 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002512 }
2513};
David Reveman8f552492012-03-28 12:18:41 -04002514
2515/**
2516 * Set the scrollbar-visible mode bit.
2517 *
2518 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2519 * Otherwise it will not.
2520 *
2521 * Defaults to on.
2522 *
2523 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2524 */
2525hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2526 this.scrollPort_.setScrollbarVisible(state);
2527};