blob: 9ae7b49cc4a1aff2d5d0b7760a1a3ef5f01d1ab7 [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 /**
rgindade84e382012-04-20 15:39:31 -0700239 * Whether or not to blink the cursor by default.
240 */
241 ['cursor-blink', false, function(v) {
242 self.setCursorBlink(!!v);
243 }
244 ],
245
246 /**
rginda30f20f62012-04-05 16:36:19 -0700247 * The color of the visible cursor.
248 */
249 ['cursor-color', 'rgba(255,0,0,0.5)', function(v) {
rginda8e92a692012-05-20 19:37:20 -0700250 self.setCursorColor(v);
rginda30f20f62012-04-05 16:36:19 -0700251 }
252 ],
253
254 /**
rginda4bba5e12012-06-20 16:15:30 -0700255 * Automatically copy mouse selection to the clipboard.
256 */
257 ['copy-on-select', true, function(v) {
258 self.copyOnSelect = !!v;
259 }
260 ],
261
262 /**
rginda11057d52012-04-25 12:29:56 -0700263 * True to enable 8-bit control characters, false to ignore them.
264 *
265 * We'll respect the two-byte versions of these control characters
266 * regardless of this setting.
267 */
268 ['enable-8-bit-control', false, function(v) {
269 self.vt.enable8BitControl = !!v;
270 }
271 ],
272
273 /**
rginda30f20f62012-04-05 16:36:19 -0700274 * True if we should use bold weight font for text with the bold/bright
275 * attribute. False to use bright colors only. Null to autodetect.
276 */
277 ['enable-bold', null, function(v) {
278 self.syncBoldSafeState();
279 }
280 ],
281
282 /**
rgindaa8ba17d2012-08-15 14:41:10 -0700283 * Allow the host to write directly to the system clipboard.
284 */
285 ['enable-clipboard-write', true, function(v) {
286 self.vt.enableClipboardWrite = !!v;
287 }
288 ],
289
290 /**
rginda9f5222b2012-03-05 11:53:28 -0800291 * Default font family for the terminal text.
292 */
293 ['font-family', ('"DejaVu Sans Mono", "Everson Mono", ' +
rgindaa8ba17d2012-08-15 14:41:10 -0700294 'FreeMono, "Menlo", "Terminal", ' +
rginda9f5222b2012-03-05 11:53:28 -0800295 'monospace'),
296 function(v) { self.syncFontFamily() }
297 ],
298
299 /**
rginda30f20f62012-04-05 16:36:19 -0700300 * The default font size in pixels.
301 */
302 ['font-size', 15, function(v) {
303 self.setFontSize(v);
304 }
305 ],
306
307 /**
rginda9f5222b2012-03-05 11:53:28 -0800308 * Anti-aliasing.
309 */
310 ['font-smoothing', 'antialiased',
311 function(v) { self.syncFontFamily() }
312 ],
313
314 /**
rginda30f20f62012-04-05 16:36:19 -0700315 * The foreground color for text with no other color attributes.
rginda9f5222b2012-03-05 11:53:28 -0800316 */
rginda30f20f62012-04-05 16:36:19 -0700317 ['foreground-color', 'rgb(240, 240, 240)', function(v) {
rginda8e92a692012-05-20 19:37:20 -0700318 self.setForegroundColor(v);
rginda9f5222b2012-03-05 11:53:28 -0800319 }
320 ],
321
322 /**
rginda30f20f62012-04-05 16:36:19 -0700323 * If true, home/end will control the terminal scrollbar and shift home/end
324 * will send the VT keycodes. If false then home/end sends VT codes and
325 * shift home/end scrolls.
rginda9f5222b2012-03-05 11:53:28 -0800326 */
rginda30f20f62012-04-05 16:36:19 -0700327 ['home-keys-scroll', false, function(v) {
328 self.keyboard.homeKeysScroll = v;
329 }
330 ],
331
332 /**
rginda11057d52012-04-25 12:29:56 -0700333 * Max length of a DCS, OSC, PM, or APS sequence before we give up and
334 * ignore the code.
335 */
336 ['max-string-sequence', 1024, function(v) {
337 self.vt.maxStringSequence = v;
338 }
339 ],
340
341 /**
rginda30f20f62012-04-05 16:36:19 -0700342 * Set whether the meta key sends a leading escape or not.
343 */
344 ['meta-sends-escape', true, function(v) {
345 self.keyboard.metaSendsEscape = v;
rginda9f5222b2012-03-05 11:53:28 -0800346 }
347 ],
348
349 /**
rgindad5613292012-06-19 15:40:37 -0700350 * Set whether we should treat DEC mode 1002 (mouse cell motion tracking)
351 * as if it were 1000 (mouse click tracking).
352 *
353 * This makes it possible to use vi's ":set mouse=a" mode without losing
354 * access to the system text selection mechanism.
355 */
356 ['mouse-cell-motion-trick', false, function(v) {
357 self.vt.setMouseCellMotionTrick(v);
358 }
359 ],
360
361 /**
rginda4bba5e12012-06-20 16:15:30 -0700362 * Mouse paste button, or null to autodetect.
363 *
364 * For autodetect, we'll try to enable middle button paste for non-X11
365 * platforms.
366 *
367 * On X11 we move it to button 3, but that'll probably be a context menu
368 * in the future.
369 */
370 ['mouse-paste-button', null, function(v) {
371 self.syncMousePasteButton();
372 }
373 ],
374
375 /**
rginda9f5222b2012-03-05 11:53:28 -0800376 * If true, scroll to the bottom on any keystroke.
377 */
378 ['scroll-on-keystroke', true, function(v) {
379 self.scrollOnKeystroke_ = v;
380 }
381 ],
382
383 /**
384 * If true, scroll to the bottom on terminal output.
385 */
386 ['scroll-on-output', false, function(v) {
387 self.scrollOnOutput_ = v;
388 }
389 ],
390
391 /**
David Reveman8f552492012-03-28 12:18:41 -0400392 * The vertical scrollbar mode.
393 */
394 ['scrollbar-visible', true, function(v) {
395 self.setScrollbarVisible(v);
396 }
397 ],
rginda30f20f62012-04-05 16:36:19 -0700398
399 /**
rginda4bba5e12012-06-20 16:15:30 -0700400 * Shift + Insert pastes if true, sent to host if false.
401 */
402 ['shift-insert-paste', true, function(v) {
403 self.keyboard.shiftInsertPaste = v;
404 }
405 ],
406
407 /**
rgindaf522ce02012-04-17 17:49:17 -0700408 * The default environment variables.
409 */
410 ['environment', {TERM: 'xterm-256color'}, null],
411
412 /**
rginda30f20f62012-04-05 16:36:19 -0700413 * If true, page up/down will control the terminal scrollbar and shift
414 * page up/down will send the VT keycodes. If false then page up/down
415 * sends VT codes and shift page up/down scrolls.
416 */
417 ['page-keys-scroll', false, function(v) {
418 self.keyboard.pageKeysScroll = v;
419 }
420 ],
421
rginda9f5222b2012-03-05 11:53:28 -0800422 ]);
423
424 if (needSync)
425 this.prefs_.notifyAll();
426};
427
rginda8e92a692012-05-20 19:37:20 -0700428
429/**
430 * Set the color for the cursor.
431 *
432 * If you want this setting to persist, set it through prefs_, rather than
433 * with this method.
434 */
435hterm.Terminal.prototype.setCursorColor = function(color) {
436 this.cursorNode_.style.backgroundColor = color;
437 this.cursorNode_.style.borderColor = color;
438};
439
440/**
441 * Return the current cursor color as a string.
442 */
443hterm.Terminal.prototype.getCursorColor = function() {
444 return this.cursorNode_.style.backgroundColor;
445};
446
447/**
rgindad5613292012-06-19 15:40:37 -0700448 * Enable or disable mouse based text selection in the terminal.
449 */
450hterm.Terminal.prototype.setSelectionEnabled = function(state) {
451 this.enableMouseDragScroll = state;
452 this.scrollPort_.setSelectionEnabled(state);
453};
454
455/**
rginda8e92a692012-05-20 19:37:20 -0700456 * Set the background color.
457 *
458 * If you want this setting to persist, set it through prefs_, rather than
459 * with this method.
460 */
461hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700462 this.backgroundColor_ = lib.colors.normalizeCSS(color);
rginda8e92a692012-05-20 19:37:20 -0700463 this.scrollPort_.setBackgroundColor(color);
464};
465
rginda9f5222b2012-03-05 11:53:28 -0800466/**
467 * Return the current terminal background color.
468 *
469 * Intended for use by other classes, so we don't have to expose the entire
470 * prefs_ object.
471 */
472hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700473 return this.backgroundColor_;
474};
475
476/**
477 * Set the foreground color.
478 *
479 * If you want this setting to persist, set it through prefs_, rather than
480 * with this method.
481 */
482hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700483 this.foregroundColor_ = lib.colors.normalizeCSS(color);
rginda8e92a692012-05-20 19:37:20 -0700484 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800485};
486
487/**
488 * Return the current terminal foreground color.
489 *
490 * Intended for use by other classes, so we don't have to expose the entire
491 * prefs_ object.
492 */
493hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700494 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800495};
496
497/**
rginda87b86462011-12-14 13:48:03 -0800498 * Create a new instance of a terminal command and run it with a given
499 * argument string.
500 *
501 * @param {function} commandClass The constructor for a terminal command.
502 * @param {string} argString The argument string to pass to the command.
503 */
504hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700505 var environment = this.prefs_.get('environment');
506 if (typeof environment != 'object' || environment == null)
507 environment = {};
508
rginda87b86462011-12-14 13:48:03 -0800509 var self = this;
510 this.command = new commandClass(
511 { argString: argString || '',
512 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700513 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800514 onExit: function(code) {
515 self.io.pop();
516 self.io.println(hterm.msg('COMMAND_COMPLETE',
517 [self.command.commandName, code]));
rgindafeaf3142012-01-31 15:14:20 -0800518 self.uninstallKeyboard();
rginda87b86462011-12-14 13:48:03 -0800519 }
520 });
521
rgindafeaf3142012-01-31 15:14:20 -0800522 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800523 this.command.run();
524};
525
526/**
rgindafeaf3142012-01-31 15:14:20 -0800527 * Returns true if the current screen is the primary screen, false otherwise.
528 */
529hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700530 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800531};
532
533/**
534 * Install the keyboard handler for this terminal.
535 *
536 * This will prevent the browser from seeing any keystrokes sent to the
537 * terminal.
538 */
539hterm.Terminal.prototype.installKeyboard = function() {
540 this.keyboard.installKeyboard(this.document_.body.firstChild);
541}
542
543/**
544 * Uninstall the keyboard handler for this terminal.
545 */
546hterm.Terminal.prototype.uninstallKeyboard = function() {
547 this.keyboard.installKeyboard(null);
548}
549
550/**
rginda35c456b2012-02-09 17:29:05 -0800551 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800552 *
553 * Call setFontSize(0) to reset to the default font size.
554 *
555 * This function does not modify the font-size preference.
556 *
557 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800558 */
559hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800560 if (px === 0)
561 px = this.prefs_.get('font-size');
562
rginda35c456b2012-02-09 17:29:05 -0800563 this.scrollPort_.setFontSize(px);
564};
565
566/**
567 * Get the current font size.
568 */
569hterm.Terminal.prototype.getFontSize = function() {
570 return this.scrollPort_.getFontSize();
571};
572
573/**
rginda8e92a692012-05-20 19:37:20 -0700574 * Get the current font family.
575 */
576hterm.Terminal.prototype.getFontFamily = function() {
577 return this.scrollPort_.getFontFamily();
578};
579
580/**
rginda35c456b2012-02-09 17:29:05 -0800581 * Set the CSS "font-family" for this terminal.
582 */
rginda9f5222b2012-03-05 11:53:28 -0800583hterm.Terminal.prototype.syncFontFamily = function() {
584 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
585 this.prefs_.get('font-smoothing'));
586 this.syncBoldSafeState();
587};
588
rginda4bba5e12012-06-20 16:15:30 -0700589/**
590 * Set this.mousePasteButton based on the mouse-paste-button pref,
591 * autodetecting if necessary.
592 */
593hterm.Terminal.prototype.syncMousePasteButton = function() {
594 var button = this.prefs_.get('mouse-paste-button');
595 if (typeof button == 'number') {
596 this.mousePasteButton = button;
597 return;
598 }
599
600 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
601 if (!ary || ary[2] == 'CrOS') {
602 this.mousePasteButton = 2;
603 } else {
604 this.mousePasteButton = 3;
605 }
606};
607
608/**
609 * Enable or disable bold based on the enable-bold pref, autodetecting if
610 * necessary.
611 */
rginda9f5222b2012-03-05 11:53:28 -0800612hterm.Terminal.prototype.syncBoldSafeState = function() {
613 var enableBold = this.prefs_.get('enable-bold');
614 if (enableBold !== null) {
615 this.screen_.textAttributes.enableBold = enableBold;
616 return;
617 }
618
rgindaf7521392012-02-28 17:20:34 -0800619 var normalSize = this.scrollPort_.measureCharacterSize();
620 var boldSize = this.scrollPort_.measureCharacterSize('bold');
621
622 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800623 if (!isBoldSafe) {
624 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700625 'from normal. Font family is: ' +
626 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800627 }
rginda9f5222b2012-03-05 11:53:28 -0800628
629 this.screen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800630};
631
632/**
rginda87b86462011-12-14 13:48:03 -0800633 * Return a copy of the current cursor position.
634 *
635 * @return {hterm.RowCol} The RowCol object representing the current position.
636 */
637hterm.Terminal.prototype.saveCursor = function() {
638 return this.screen_.cursorPosition.clone();
639};
640
rgindaa19afe22012-01-25 15:40:22 -0800641hterm.Terminal.prototype.getTextAttributes = function() {
642 return this.screen_.textAttributes;
643};
644
rginda1a09aa02012-06-18 21:11:25 -0700645hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
646 this.screen_.textAttributes = textAttributes;
647};
648
rginda87b86462011-12-14 13:48:03 -0800649/**
rgindaf522ce02012-04-17 17:49:17 -0700650 * Return the current browser zoom factor applied to the terminal.
651 *
652 * @return {number} The current browser zoom factor.
653 */
654hterm.Terminal.prototype.getZoomFactor = function() {
655 return this.scrollPort_.characterSize.zoomFactor;
656};
657
658/**
rginda9846e2f2012-01-27 13:53:33 -0800659 * Change the title of this terminal's window.
660 */
661hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800662 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800663};
664
665/**
rginda87b86462011-12-14 13:48:03 -0800666 * Restore a previously saved cursor position.
667 *
668 * @param {hterm.RowCol} cursor The position to restore.
669 */
670hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700671 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
672 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800673 this.screen_.setCursorPosition(row, column);
674 if (cursor.column > column ||
675 cursor.column == column && cursor.overflow) {
676 this.screen_.cursorPosition.overflow = true;
677 }
rginda87b86462011-12-14 13:48:03 -0800678};
679
680/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400681 * Clear the cursor's overflow flag.
682 */
683hterm.Terminal.prototype.clearCursorOverflow = function() {
684 this.screen_.cursorPosition.overflow = false;
685};
686
687/**
rginda87b86462011-12-14 13:48:03 -0800688 * Set the width of the terminal, resizing the UI to match.
689 */
690hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800691 if (columnCount == null) {
692 this.div_.style.width = '100%';
693 return;
694 }
695
rginda35c456b2012-02-09 17:29:05 -0800696 this.div_.style.width = this.scrollPort_.characterSize.width *
697 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400698 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800699 this.scheduleSyncCursorPosition_();
700};
rginda87b86462011-12-14 13:48:03 -0800701
rgindac9bc5502012-01-18 11:48:44 -0800702/**
rginda35c456b2012-02-09 17:29:05 -0800703 * Set the height of the terminal, resizing the UI to match.
704 */
705hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800706 if (rowCount == null) {
707 this.div_.style.height = '100%';
708 return;
709 }
710
rginda35c456b2012-02-09 17:29:05 -0800711 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700712 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800713 this.realizeSize_(this.screenSize.width, rowCount);
714 this.scheduleSyncCursorPosition_();
715};
716
717/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400718 * Deal with terminal size changes.
719 *
720 */
721hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
722 if (columnCount != this.screenSize.width)
723 this.realizeWidth_(columnCount);
724
725 if (rowCount != this.screenSize.height)
726 this.realizeHeight_(rowCount);
727
728 // Send new terminal size to plugin.
729 this.io.onTerminalResize(columnCount, rowCount);
730};
731
732/**
rgindac9bc5502012-01-18 11:48:44 -0800733 * Deal with terminal width changes.
734 *
735 * This function does what needs to be done when the terminal width changes
736 * out from under us. It happens here rather than in onResize_() because this
737 * code may need to run synchronously to handle programmatic changes of
738 * terminal width.
739 *
740 * Relying on the browser to send us an async resize event means we may not be
741 * in the correct state yet when the next escape sequence hits.
742 */
743hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
744 var deltaColumns = columnCount - this.screen_.getWidth();
745
rginda87b86462011-12-14 13:48:03 -0800746 this.screenSize.width = columnCount;
747 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800748
749 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400750 if (this.defaultTabStops)
751 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800752 } else {
753 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400754 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800755 break;
756
757 this.tabStops_.pop();
758 }
759 }
760
761 this.screen_.setColumnCount(this.screenSize.width);
762};
763
764/**
765 * Deal with terminal height changes.
766 *
767 * This function does what needs to be done when the terminal height changes
768 * out from under us. It happens here rather than in onResize_() because this
769 * code may need to run synchronously to handle programmatic changes of
770 * terminal height.
771 *
772 * Relying on the browser to send us an async resize event means we may not be
773 * in the correct state yet when the next escape sequence hits.
774 */
775hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
776 var deltaRows = rowCount - this.screen_.getHeight();
777
778 this.screenSize.height = rowCount;
779
780 var cursor = this.saveCursor();
781
782 if (deltaRows < 0) {
783 // Screen got smaller.
784 deltaRows *= -1;
785 while (deltaRows) {
786 var lastRow = this.getRowCount() - 1;
787 if (lastRow - this.scrollbackRows_.length == cursor.row)
788 break;
789
790 if (this.getRowText(lastRow))
791 break;
792
793 this.screen_.popRow();
794 deltaRows--;
795 }
796
797 var ary = this.screen_.shiftRows(deltaRows);
798 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
799
800 // We just removed rows from the top of the screen, we need to update
801 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800802 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800803 } else if (deltaRows > 0) {
804 // Screen got larger.
805
806 if (deltaRows <= this.scrollbackRows_.length) {
807 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
808 var rows = this.scrollbackRows_.splice(
809 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
810 this.screen_.unshiftRows(rows);
811 deltaRows -= scrollbackCount;
812 cursor.row += scrollbackCount;
813 }
814
815 if (deltaRows)
816 this.appendRows_(deltaRows);
817 }
818
rginda35c456b2012-02-09 17:29:05 -0800819 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800820 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800821};
822
823/**
824 * Scroll the terminal to the top of the scrollback buffer.
825 */
826hterm.Terminal.prototype.scrollHome = function() {
827 this.scrollPort_.scrollRowToTop(0);
828};
829
830/**
831 * Scroll the terminal to the end.
832 */
833hterm.Terminal.prototype.scrollEnd = function() {
834 this.scrollPort_.scrollRowToBottom(this.getRowCount());
835};
836
837/**
838 * Scroll the terminal one page up (minus one line) relative to the current
839 * position.
840 */
841hterm.Terminal.prototype.scrollPageUp = function() {
842 var i = this.scrollPort_.getTopRowIndex();
843 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
844};
845
846/**
847 * Scroll the terminal one page down (minus one line) relative to the current
848 * position.
849 */
850hterm.Terminal.prototype.scrollPageDown = function() {
851 var i = this.scrollPort_.getTopRowIndex();
852 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800853};
854
rgindac9bc5502012-01-18 11:48:44 -0800855/**
856 * Full terminal reset.
857 */
rginda87b86462011-12-14 13:48:03 -0800858hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800859 this.clearAllTabStops();
860 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700861
862 this.clearHome(this.primaryScreen_);
863 this.primaryScreen_.textAttributes.reset();
864
865 this.clearHome(this.alternateScreen_);
866 this.alternateScreen_.textAttributes.reset();
867
rgindab8bc8932012-04-27 12:45:03 -0700868 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
869
rgindac9bc5502012-01-18 11:48:44 -0800870 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800871};
872
rgindac9bc5502012-01-18 11:48:44 -0800873/**
874 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700875 *
876 * Perform a soft reset to the default values listed in
877 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800878 */
rginda0f5c0292012-01-13 11:00:13 -0800879hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700880 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800881 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700882
rgindab8bc8932012-04-27 12:45:03 -0700883 // Xterm also resets the color palette on soft reset, even though it doesn't
884 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700885 this.primaryScreen_.textAttributes.resetColorPalette();
886 this.alternateScreen_.textAttributes.resetColorPalette();
887
rgindab8bc8932012-04-27 12:45:03 -0700888 // The xterm man page explicitly says this will happen on soft reset.
889 this.setVTScrollRegion(null, null);
890
891 // Xterm also shows the cursor on soft reset, but does not alter the blink
892 // state.
rgindaa19afe22012-01-25 15:40:22 -0800893 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800894};
895
rgindac9bc5502012-01-18 11:48:44 -0800896/**
897 * Move the cursor forward to the next tab stop, or to the last column
898 * if no more tab stops are set.
899 */
900hterm.Terminal.prototype.forwardTabStop = function() {
901 var column = this.screen_.cursorPosition.column;
902
903 for (var i = 0; i < this.tabStops_.length; i++) {
904 if (this.tabStops_[i] > column) {
905 this.setCursorColumn(this.tabStops_[i]);
906 return;
907 }
908 }
909
David Benjamin66e954d2012-05-05 21:08:12 -0400910 // xterm does not clear the overflow flag on HT or CHT.
911 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800912 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400913 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800914};
915
rgindac9bc5502012-01-18 11:48:44 -0800916/**
917 * Move the cursor backward to the previous tab stop, or to the first column
918 * if no previous tab stops are set.
919 */
920hterm.Terminal.prototype.backwardTabStop = function() {
921 var column = this.screen_.cursorPosition.column;
922
923 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
924 if (this.tabStops_[i] < column) {
925 this.setCursorColumn(this.tabStops_[i]);
926 return;
927 }
928 }
929
930 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800931};
932
rgindac9bc5502012-01-18 11:48:44 -0800933/**
934 * Set a tab stop at the given column.
935 *
936 * @param {int} column Zero based column.
937 */
938hterm.Terminal.prototype.setTabStop = function(column) {
939 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
940 if (this.tabStops_[i] == column)
941 return;
942
943 if (this.tabStops_[i] < column) {
944 this.tabStops_.splice(i + 1, 0, column);
945 return;
946 }
947 }
948
949 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800950};
951
rgindac9bc5502012-01-18 11:48:44 -0800952/**
953 * Clear the tab stop at the current cursor position.
954 *
955 * No effect if there is no tab stop at the current cursor position.
956 */
957hterm.Terminal.prototype.clearTabStopAtCursor = function() {
958 var column = this.screen_.cursorPosition.column;
959
960 var i = this.tabStops_.indexOf(column);
961 if (i == -1)
962 return;
963
964 this.tabStops_.splice(i, 1);
965};
966
967/**
968 * Clear all tab stops.
969 */
970hterm.Terminal.prototype.clearAllTabStops = function() {
971 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -0400972 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -0800973};
974
975/**
976 * Set up the default tab stops, starting from a given column.
977 *
978 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -0400979 * from the specified column, or 0 if no column is provided. It also flags
980 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -0800981 *
982 * This does not clear the existing tab stops first, use clearAllTabStops
983 * for that.
984 *
985 * @param {int} opt_start Optional starting zero based starting column, useful
986 * for filling out missing tab stops when the terminal is resized.
987 */
988hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
989 var start = opt_start || 0;
990 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -0400991 // Round start up to a default tab stop.
992 start = start - 1 - ((start - 1) % w) + w;
993 for (var i = start; i < this.screenSize.width; i += w) {
994 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -0800995 }
David Benjamin66e954d2012-05-05 21:08:12 -0400996
997 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -0800998};
999
rginda6d397402012-01-17 10:58:29 -08001000/**
rginda8ba33642011-12-14 12:31:31 -08001001 * Interpret a sequence of characters.
1002 *
1003 * Incomplete escape sequences are buffered until the next call.
1004 *
1005 * @param {string} str Sequence of characters to interpret or pass through.
1006 */
1007hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001008 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001009 this.scheduleSyncCursorPosition_();
1010};
1011
1012/**
1013 * Take over the given DIV for use as the terminal display.
1014 *
1015 * @param {HTMLDivElement} div The div to use as the terminal display.
1016 */
1017hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001018 this.div_ = div;
1019
rginda8ba33642011-12-14 12:31:31 -08001020 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001021 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001022 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1023 this.scrollPort_.setBackgroundPosition(
1024 this.prefs_.get('background-position'));
rginda30f20f62012-04-05 16:36:19 -07001025
rginda0918b652012-04-04 11:26:24 -07001026 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001027
rginda9f5222b2012-03-05 11:53:28 -08001028 this.setFontSize(this.prefs_.get('font-size'));
1029 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001030
David Reveman8f552492012-03-28 12:18:41 -04001031 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1032
rginda8ba33642011-12-14 12:31:31 -08001033 this.document_ = this.scrollPort_.getDocument();
1034
rginda4bba5e12012-06-20 16:15:30 -07001035 this.document_.body.oncontextmenu = function() { return false };
1036
1037 var onMouse = this.onMouse_.bind(this);
1038 this.document_.body.firstChild.addEventListener('mousedown', onMouse);
1039 this.document_.body.firstChild.addEventListener('mouseup', onMouse);
1040 this.document_.body.firstChild.addEventListener('mousemove', onMouse);
1041 this.scrollPort_.onScrollWheel = onMouse;
1042
rginda8e92a692012-05-20 19:37:20 -07001043 this.document_.body.firstChild.addEventListener(
1044 'focus', this.onFocusChange_.bind(this, true));
1045 this.document_.body.firstChild.addEventListener(
1046 'blur', this.onFocusChange_.bind(this, false));
1047
1048 var style = this.document_.createElement('style');
1049 style.textContent =
1050 ('.cursor-node[focus="false"] {' +
1051 ' box-sizing: border-box;' +
1052 ' background-color: transparent !important;' +
1053 ' border-width: 2px;' +
1054 ' border-style: solid;' +
1055 '}');
1056 this.document_.head.appendChild(style);
1057
rginda8ba33642011-12-14 12:31:31 -08001058 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001059 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001060 this.cursorNode_.style.cssText =
1061 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001062 'top: -99px;' +
1063 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001064 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1065 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001066 '-webkit-transition: opacity, background-color 100ms linear;');
1067 this.setCursorColor(this.prefs_.get('cursor-color'));
rgindad5613292012-06-19 15:40:37 -07001068
rginda8ba33642011-12-14 12:31:31 -08001069 this.document_.body.appendChild(this.cursorNode_);
1070
rgindad5613292012-06-19 15:40:37 -07001071 // When 'enableMouseDragScroll' is off we reposition this element directly
1072 // under the mouse cursor after a click. This makes Chrome associate
1073 // subsequent mousemove events with the scroll-blocker. Since the
1074 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1075 // events do not cause the scrollport to scroll.
1076 //
1077 // It's a hack, but it's the cleanest way I could find.
1078 this.scrollBlockerNode_ = this.document_.createElement('div');
1079 this.scrollBlockerNode_.style.cssText =
1080 ('position: absolute;' +
1081 'top: -99px;' +
1082 'display: block;' +
1083 'width: 10px;' +
1084 'height: 10px;');
1085 this.document_.body.appendChild(this.scrollBlockerNode_);
1086
1087 var onMouse = this.onMouse_.bind(this);
1088 this.scrollPort_.onScrollWheel = onMouse;
1089 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1090 ].forEach(function(event) {
1091 this.scrollBlockerNode_.addEventListener(event, onMouse);
1092 this.cursorNode_.addEventListener(event, onMouse);
1093 this.document_.addEventListener(event, onMouse);
1094 }.bind(this));
1095
1096 this.cursorNode_.addEventListener('mousedown', function() {
1097 setTimeout(this.focus.bind(this));
1098 }.bind(this));
1099
rgindade84e382012-04-20 15:39:31 -07001100 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda8ba33642011-12-14 12:31:31 -08001101 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001102
rginda87b86462011-12-14 13:48:03 -08001103 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001104 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001105};
1106
rginda0918b652012-04-04 11:26:24 -07001107/**
1108 * Return the HTML document that contains the terminal DOM nodes.
1109 */
rginda87b86462011-12-14 13:48:03 -08001110hterm.Terminal.prototype.getDocument = function() {
1111 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001112};
1113
1114/**
rginda0918b652012-04-04 11:26:24 -07001115 * Focus the terminal.
1116 */
1117hterm.Terminal.prototype.focus = function() {
1118 this.scrollPort_.focus();
1119};
1120
1121/**
rginda8ba33642011-12-14 12:31:31 -08001122 * Return the HTML Element for a given row index.
1123 *
1124 * This is a method from the RowProvider interface. The ScrollPort uses
1125 * it to fetch rows on demand as they are scrolled into view.
1126 *
1127 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1128 * pairs to conserve memory.
1129 *
1130 * @param {integer} index The zero-based row index, measured relative to the
1131 * start of the scrollback buffer. On-screen rows will always have the
1132 * largest indicies.
1133 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1134 */
1135hterm.Terminal.prototype.getRowNode = function(index) {
1136 if (index < this.scrollbackRows_.length)
1137 return this.scrollbackRows_[index];
1138
1139 var screenIndex = index - this.scrollbackRows_.length;
1140 return this.screen_.rowsArray[screenIndex];
1141};
1142
1143/**
1144 * Return the text content for a given range of rows.
1145 *
1146 * This is a method from the RowProvider interface. The ScrollPort uses
1147 * it to fetch text content on demand when the user attempts to copy their
1148 * selection to the clipboard.
1149 *
1150 * @param {integer} start The zero-based row index to start from, measured
1151 * relative to the start of the scrollback buffer. On-screen rows will
1152 * always have the largest indicies.
1153 * @param {integer} end The zero-based row index to end on, measured
1154 * relative to the start of the scrollback buffer.
1155 * @return {string} A single string containing the text value of the range of
1156 * rows. Lines will be newline delimited, with no trailing newline.
1157 */
1158hterm.Terminal.prototype.getRowsText = function(start, end) {
1159 var ary = [];
1160 for (var i = start; i < end; i++) {
1161 var node = this.getRowNode(i);
1162 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001163 if (i < end - 1 && !node.getAttribute('line-overflow'))
1164 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001165 }
1166
rgindaa09e7332012-08-17 12:49:51 -07001167 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001168};
1169
1170/**
1171 * Return the text content for a given row.
1172 *
1173 * This is a method from the RowProvider interface. The ScrollPort uses
1174 * it to fetch text content on demand when the user attempts to copy their
1175 * selection to the clipboard.
1176 *
1177 * @param {integer} index The zero-based row index to return, measured
1178 * relative to the start of the scrollback buffer. On-screen rows will
1179 * always have the largest indicies.
1180 * @return {string} A string containing the text value of the selected row.
1181 */
1182hterm.Terminal.prototype.getRowText = function(index) {
1183 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001184 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001185};
1186
1187/**
1188 * Return the total number of rows in the addressable screen and in the
1189 * scrollback buffer of this terminal.
1190 *
1191 * This is a method from the RowProvider interface. The ScrollPort uses
1192 * it to compute the size of the scrollbar.
1193 *
1194 * @return {integer} The number of rows in this terminal.
1195 */
1196hterm.Terminal.prototype.getRowCount = function() {
1197 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1198};
1199
1200/**
1201 * Create DOM nodes for new rows and append them to the end of the terminal.
1202 *
1203 * This is the only correct way to add a new DOM node for a row. Notice that
1204 * the new row is appended to the bottom of the list of rows, and does not
1205 * require renumbering (of the rowIndex property) of previous rows.
1206 *
1207 * If you think you want a new blank row somewhere in the middle of the
1208 * terminal, look into moveRows_().
1209 *
1210 * This method does not pay attention to vtScrollTop/Bottom, since you should
1211 * be using moveRows() in cases where they would matter.
1212 *
1213 * The cursor will be positioned at column 0 of the first inserted line.
1214 */
1215hterm.Terminal.prototype.appendRows_ = function(count) {
1216 var cursorRow = this.screen_.rowsArray.length;
1217 var offset = this.scrollbackRows_.length + cursorRow;
1218 for (var i = 0; i < count; i++) {
1219 var row = this.document_.createElement('x-row');
1220 row.appendChild(this.document_.createTextNode(''));
1221 row.rowIndex = offset + i;
1222 this.screen_.pushRow(row);
1223 }
1224
1225 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1226 if (extraRows > 0) {
1227 var ary = this.screen_.shiftRows(extraRows);
1228 Array.prototype.push.apply(this.scrollbackRows_, ary);
1229 this.scheduleScrollDown_();
1230 }
1231
1232 if (cursorRow >= this.screen_.rowsArray.length)
1233 cursorRow = this.screen_.rowsArray.length - 1;
1234
rginda87b86462011-12-14 13:48:03 -08001235 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001236};
1237
1238/**
1239 * Relocate rows from one part of the addressable screen to another.
1240 *
1241 * This is used to recycle rows during VT scrolls (those which are driven
1242 * by VT commands, rather than by the user manipulating the scrollbar.)
1243 *
1244 * In this case, the blank lines scrolled into the scroll region are made of
1245 * the nodes we scrolled off. These have their rowIndex properties carefully
1246 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001247 */
1248hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1249 var ary = this.screen_.removeRows(fromIndex, count);
1250 this.screen_.insertRows(toIndex, ary);
1251
1252 var start, end;
1253 if (fromIndex < toIndex) {
1254 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001255 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001256 } else {
1257 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001258 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001259 }
1260
1261 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001262 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001263};
1264
1265/**
1266 * Renumber the rowIndex property of the given range of rows.
1267 *
1268 * The start and end indicies are relative to the screen, not the scrollback.
1269 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001270 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001271 * no need to renumber scrollback rows.
1272 */
1273hterm.Terminal.prototype.renumberRows_ = function(start, end) {
1274 var offset = this.scrollbackRows_.length;
1275 for (var i = start; i < end; i++) {
1276 this.screen_.rowsArray[i].rowIndex = offset + i;
1277 }
1278};
1279
1280/**
1281 * Print a string to the terminal.
1282 *
1283 * This respects the current insert and wraparound modes. It will add new lines
1284 * to the end of the terminal, scrolling off the top into the scrollback buffer
1285 * if necessary.
1286 *
1287 * The string is *not* parsed for escape codes. Use the interpret() method if
1288 * that's what you're after.
1289 *
1290 * @param{string} str The string to print.
1291 */
1292hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001293 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001294
rgindaa9abdd82012-08-06 18:05:09 -07001295 while (startOffset < str.length) {
rgindaa09e7332012-08-17 12:49:51 -07001296 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1297 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001298 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001299 }
rgindaa19afe22012-01-25 15:40:22 -08001300
rgindaa9abdd82012-08-06 18:05:09 -07001301 var count = str.length - startOffset;
1302 var didOverflow = false;
1303 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001304
rgindaa9abdd82012-08-06 18:05:09 -07001305 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1306 didOverflow = true;
1307 count = this.screenSize.width - this.screen_.cursorPosition.column;
1308 }
rgindaa19afe22012-01-25 15:40:22 -08001309
rgindaa9abdd82012-08-06 18:05:09 -07001310 if (didOverflow && !this.options_.wraparound) {
1311 // If the string overflowed the line but wraparound is off, then the
1312 // last printed character should be the last of the string.
1313 // TODO: This will add to our problems with multibyte UTF-16 characters.
1314 substr = str.substr(startOffset, count - 1) +
1315 str.substr(str.length - 1);
1316 count = str.length;
1317 } else {
1318 substr = str.substr(startOffset, count);
1319 }
rgindaa19afe22012-01-25 15:40:22 -08001320
rgindaa9abdd82012-08-06 18:05:09 -07001321 if (this.options_.insertMode) {
1322 this.screen_.insertString(substr);
1323 } else {
1324 this.screen_.overwriteString(substr);
1325 }
1326
1327 this.screen_.maybeClipCurrentRow();
1328 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001329 }
rginda8ba33642011-12-14 12:31:31 -08001330
1331 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001332
rginda9f5222b2012-03-05 11:53:28 -08001333 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001334 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001335};
1336
1337/**
rginda87b86462011-12-14 13:48:03 -08001338 * Set the VT scroll region.
1339 *
rginda87b86462011-12-14 13:48:03 -08001340 * This also resets the cursor position to the absolute (0, 0) position, since
1341 * that's what xterm appears to do.
1342 *
1343 * @param {integer} scrollTop The zero-based top of the scroll region.
1344 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1345 * inclusive.
1346 */
1347hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1348 this.vtScrollTop_ = scrollTop;
1349 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001350};
1351
1352/**
rginda8ba33642011-12-14 12:31:31 -08001353 * Return the top row index according to the VT.
1354 *
1355 * This will return 0 unless the terminal has been told to restrict scrolling
1356 * to some lower row. It is used for some VT cursor positioning and scrolling
1357 * commands.
1358 *
1359 * @return {integer} The topmost row in the terminal's scroll region.
1360 */
1361hterm.Terminal.prototype.getVTScrollTop = function() {
1362 if (this.vtScrollTop_ != null)
1363 return this.vtScrollTop_;
1364
1365 return 0;
rginda87b86462011-12-14 13:48:03 -08001366};
rginda8ba33642011-12-14 12:31:31 -08001367
1368/**
1369 * Return the bottom row index according to the VT.
1370 *
1371 * This will return the height of the terminal unless the it has been told to
1372 * restrict scrolling to some higher row. It is used for some VT cursor
1373 * positioning and scrolling commands.
1374 *
1375 * @return {integer} The bottommost row in the terminal's scroll region.
1376 */
1377hterm.Terminal.prototype.getVTScrollBottom = function() {
1378 if (this.vtScrollBottom_ != null)
1379 return this.vtScrollBottom_;
1380
rginda87b86462011-12-14 13:48:03 -08001381 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001382}
1383
1384/**
1385 * Process a '\n' character.
1386 *
1387 * If the cursor is on the final row of the terminal this will append a new
1388 * blank row to the screen and scroll the topmost row into the scrollback
1389 * buffer.
1390 *
1391 * Otherwise, this moves the cursor to column zero of the next row.
1392 */
1393hterm.Terminal.prototype.newLine = function() {
1394 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001395 // If we're at the end of the screen we need to append a new line and
1396 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001397 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001398 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1399 // End of the scroll region does not affect the scrollback buffer.
1400 this.vtScrollUp(1);
1401 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001402 } else {
rginda87b86462011-12-14 13:48:03 -08001403 // Anywhere else in the screen just moves the cursor.
1404 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001405 }
1406};
1407
1408/**
1409 * Like newLine(), except maintain the cursor column.
1410 */
1411hterm.Terminal.prototype.lineFeed = function() {
1412 var column = this.screen_.cursorPosition.column;
1413 this.newLine();
1414 this.setCursorColumn(column);
1415};
1416
1417/**
rginda87b86462011-12-14 13:48:03 -08001418 * If autoCarriageReturn is set then newLine(), else lineFeed().
1419 */
1420hterm.Terminal.prototype.formFeed = function() {
1421 if (this.options_.autoCarriageReturn) {
1422 this.newLine();
1423 } else {
1424 this.lineFeed();
1425 }
1426};
1427
1428/**
1429 * Move the cursor up one row, possibly inserting a blank line.
1430 *
1431 * The cursor column is not changed.
1432 */
1433hterm.Terminal.prototype.reverseLineFeed = function() {
1434 var scrollTop = this.getVTScrollTop();
1435 var currentRow = this.screen_.cursorPosition.row;
1436
1437 if (currentRow == scrollTop) {
1438 this.insertLines(1);
1439 } else {
1440 this.setAbsoluteCursorRow(currentRow - 1);
1441 }
1442};
1443
1444/**
rginda8ba33642011-12-14 12:31:31 -08001445 * Replace all characters to the left of the current cursor with the space
1446 * character.
1447 *
1448 * TODO(rginda): This should probably *remove* the characters (not just replace
1449 * with a space) if there are no characters at or beyond the current cursor
1450 * position. Once it does that, it'll have the same text-attribute related
1451 * issues as hterm.Screen.prototype.clearCursorRow :/
1452 */
1453hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001454 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001455 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001456 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001457 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001458};
1459
1460/**
David Benjamin684a9b72012-05-01 17:19:58 -04001461 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001462 *
1463 * The cursor position is unchanged.
1464 *
1465 * TODO(rginda): Test that this works even when the cursor is positioned beyond
1466 * the end of the text.
1467 *
1468 * TODO(rginda): This likely has text-attribute related troubles similar to the
1469 * todo on hterm.Screen.prototype.clearCursorRow.
David Benjamin684a9b72012-05-01 17:19:58 -04001470 *
1471 * TODO(davidben): Probably better to not add the whitespace to the clipboard
1472 * if erasing to the end of the drawn portion of the line. That said, xterm
1473 * behaves the same here.
rginda8ba33642011-12-14 12:31:31 -08001474 */
1475hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001476 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001477
rginda87b86462011-12-14 13:48:03 -08001478 var maxCount = this.screenSize.width - cursor.column;
David Benjamin684a9b72012-05-01 17:19:58 -04001479 if (opt_count === undefined || opt_count >= maxCount) {
1480 this.screen_.deleteChars(maxCount);
1481 } else {
rgindacbbd7482012-06-13 15:06:16 -07001482 this.screen_.overwriteString(lib.f.getWhitespace(opt_count));
David Benjamin684a9b72012-05-01 17:19:58 -04001483 }
rginda87b86462011-12-14 13:48:03 -08001484 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001485 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001486};
1487
1488/**
1489 * Erase the current line.
1490 *
1491 * The cursor position is unchanged.
1492 *
1493 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1494 * has a text-attribute related TODO.
1495 */
1496hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001497 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001498 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001499 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001500 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001501};
1502
1503/**
David Benjamina08d78f2012-05-05 00:28:49 -04001504 * Erase all characters from the start of the screen to the current cursor
1505 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001506 *
1507 * The cursor position is unchanged.
1508 *
1509 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1510 * has a text-attribute related TODO.
1511 */
1512hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001513 var cursor = this.saveCursor();
1514
1515 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001516
David Benjamina08d78f2012-05-05 00:28:49 -04001517 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001518 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001519 this.screen_.clearCursorRow();
1520 }
1521
rginda87b86462011-12-14 13:48:03 -08001522 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001523 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001524};
1525
1526/**
1527 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001528 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001529 *
1530 * The cursor position is unchanged.
1531 *
1532 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1533 * has a text-attribute related TODO.
1534 */
1535hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001536 var cursor = this.saveCursor();
1537
1538 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001539
David Benjamina08d78f2012-05-05 00:28:49 -04001540 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001541 for (var i = cursor.row + 1; i <= bottom; i++) {
1542 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001543 this.screen_.clearCursorRow();
1544 }
1545
rginda87b86462011-12-14 13:48:03 -08001546 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001547 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001548};
1549
1550/**
1551 * Fill the terminal with a given character.
1552 *
1553 * This methods does not respect the VT scroll region.
1554 *
1555 * @param {string} ch The character to use for the fill.
1556 */
1557hterm.Terminal.prototype.fill = function(ch) {
1558 var cursor = this.saveCursor();
1559
1560 this.setAbsoluteCursorPosition(0, 0);
1561 for (var row = 0; row < this.screenSize.height; row++) {
1562 for (var col = 0; col < this.screenSize.width; col++) {
1563 this.setAbsoluteCursorPosition(row, col);
1564 this.screen_.overwriteString(ch);
1565 }
1566 }
1567
1568 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001569};
1570
1571/**
rginda9ea433c2012-03-16 11:57:00 -07001572 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001573 *
rginda9ea433c2012-03-16 11:57:00 -07001574 * This does not respect the scroll region.
1575 *
1576 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1577 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001578 *
1579 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1580 * has a text-attribute related TODO.
1581 */
rginda9ea433c2012-03-16 11:57:00 -07001582hterm.Terminal.prototype.clearHome = function(opt_screen) {
1583 var screen = opt_screen || this.screen_;
1584 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001585
rginda11057d52012-04-25 12:29:56 -07001586 if (bottom == 0) {
1587 // Empty screen, nothing to do.
1588 return;
1589 }
1590
rgindae4d29232012-01-19 10:47:13 -08001591 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001592 screen.setCursorPosition(i, 0);
1593 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001594 }
1595
rginda9ea433c2012-03-16 11:57:00 -07001596 screen.setCursorPosition(0, 0);
1597};
1598
1599/**
1600 * Erase the entire display without changing the cursor position.
1601 *
1602 * The cursor position is unchanged. This does not respect the scroll
1603 * region.
1604 *
1605 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1606 * to the current screen.
1607 *
1608 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1609 * has a text-attribute related TODO.
1610 */
1611hterm.Terminal.prototype.clear = function(opt_screen) {
1612 var screen = opt_screen || this.screen_;
1613 var cursor = screen.cursorPosition.clone();
1614 this.clearHome(screen);
1615 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001616};
1617
1618/**
1619 * VT command to insert lines at the current cursor row.
1620 *
1621 * This respects the current scroll region. Rows pushed off the bottom are
1622 * lost (they won't show up in the scrollback buffer).
1623 *
1624 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1625 * has a text-attribute related TODO.
1626 *
1627 * @param {integer} count The number of lines to insert.
1628 */
1629hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001630 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001631
1632 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001633 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001634
rgindae4d29232012-01-19 10:47:13 -08001635 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001636 if (start != cursor.row)
1637 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001638
1639 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001640 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001641 this.screen_.clearCursorRow();
1642 }
1643
rginda87b86462011-12-14 13:48:03 -08001644 cursor.column = 0;
1645 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001646};
1647
1648/**
1649 * VT command to delete lines at the current cursor row.
1650 *
1651 * New rows are added to the bottom of scroll region to take their place. New
1652 * rows are strictly there to take up space and have no content or style.
1653 */
1654hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001655 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001656
rginda87b86462011-12-14 13:48:03 -08001657 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001658 var bottom = this.getVTScrollBottom();
1659
rginda87b86462011-12-14 13:48:03 -08001660 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001661 count = Math.min(count, maxCount);
1662
rginda87b86462011-12-14 13:48:03 -08001663 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001664 if (count != maxCount)
1665 this.moveRows_(top, count, moveStart);
1666
1667 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001668 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001669 this.screen_.clearCursorRow();
1670 }
1671
rginda87b86462011-12-14 13:48:03 -08001672 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001673 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001674};
1675
1676/**
1677 * Inserts the given number of spaces at the current cursor position.
1678 *
rginda87b86462011-12-14 13:48:03 -08001679 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001680 */
1681hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001682 var cursor = this.saveCursor();
1683
rgindacbbd7482012-06-13 15:06:16 -07001684 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001685 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001686 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001687
1688 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001689 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001690};
1691
1692/**
1693 * Forward-delete the specified number of characters starting at the cursor
1694 * position.
1695 *
1696 * @param {integer} count The number of characters to delete.
1697 */
1698hterm.Terminal.prototype.deleteChars = function(count) {
1699 this.screen_.deleteChars(count);
David Benjamin54e8bf62012-06-01 22:31:40 -04001700 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001701};
1702
1703/**
1704 * Shift rows in the scroll region upwards by a given number of lines.
1705 *
1706 * New rows are inserted at the bottom of the scroll region to fill the
1707 * vacated rows. The new rows not filled out with the current text attributes.
1708 *
1709 * This function does not affect the scrollback rows at all. Rows shifted
1710 * off the top are lost.
1711 *
rginda87b86462011-12-14 13:48:03 -08001712 * The cursor position is not altered.
1713 *
rginda8ba33642011-12-14 12:31:31 -08001714 * @param {integer} count The number of rows to scroll.
1715 */
1716hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001717 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001718
rginda87b86462011-12-14 13:48:03 -08001719 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001720 this.deleteLines(count);
1721
rginda87b86462011-12-14 13:48:03 -08001722 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001723};
1724
1725/**
1726 * Shift rows below the cursor down by a given number of lines.
1727 *
1728 * This function respects the current scroll region.
1729 *
1730 * New rows are inserted at the top of the scroll region to fill the
1731 * vacated rows. The new rows not filled out with the current text attributes.
1732 *
1733 * This function does not affect the scrollback rows at all. Rows shifted
1734 * off the bottom are lost.
1735 *
1736 * @param {integer} count The number of rows to scroll.
1737 */
1738hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001739 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001740
rginda87b86462011-12-14 13:48:03 -08001741 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001742 this.insertLines(opt_count);
1743
rginda87b86462011-12-14 13:48:03 -08001744 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001745};
1746
rginda87b86462011-12-14 13:48:03 -08001747
rginda8ba33642011-12-14 12:31:31 -08001748/**
1749 * Set the cursor position.
1750 *
1751 * The cursor row is relative to the scroll region if the terminal has
1752 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1753 *
1754 * @param {integer} row The new zero-based cursor row.
1755 * @param {integer} row The new zero-based cursor column.
1756 */
1757hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1758 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001759 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001760 } else {
rginda87b86462011-12-14 13:48:03 -08001761 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001762 }
rginda87b86462011-12-14 13:48:03 -08001763};
rginda8ba33642011-12-14 12:31:31 -08001764
rginda87b86462011-12-14 13:48:03 -08001765hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1766 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001767 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1768 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001769 this.screen_.setCursorPosition(row, column);
1770};
1771
1772hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001773 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1774 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001775 this.screen_.setCursorPosition(row, column);
1776};
1777
1778/**
1779 * Set the cursor column.
1780 *
1781 * @param {integer} column The new zero-based cursor column.
1782 */
1783hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001784 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001785};
1786
1787/**
1788 * Return the cursor column.
1789 *
1790 * @return {integer} The zero-based cursor column.
1791 */
1792hterm.Terminal.prototype.getCursorColumn = function() {
1793 return this.screen_.cursorPosition.column;
1794};
1795
1796/**
1797 * Set the cursor row.
1798 *
1799 * The cursor row is relative to the scroll region if the terminal has
1800 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1801 *
1802 * @param {integer} row The new cursor row.
1803 */
rginda87b86462011-12-14 13:48:03 -08001804hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1805 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001806};
1807
1808/**
1809 * Return the cursor row.
1810 *
1811 * @return {integer} The zero-based cursor row.
1812 */
1813hterm.Terminal.prototype.getCursorRow = function(row) {
1814 return this.screen_.cursorPosition.row;
1815};
1816
1817/**
1818 * Request that the ScrollPort redraw itself soon.
1819 *
1820 * The redraw will happen asynchronously, soon after the call stack winds down.
1821 * Multiple calls will be coalesced into a single redraw.
1822 */
1823hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001824 if (this.timeouts_.redraw)
1825 return;
rginda8ba33642011-12-14 12:31:31 -08001826
1827 var self = this;
rginda87b86462011-12-14 13:48:03 -08001828 this.timeouts_.redraw = setTimeout(function() {
1829 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001830 self.scrollPort_.redraw_();
1831 }, 0);
1832};
1833
1834/**
1835 * Request that the ScrollPort be scrolled to the bottom.
1836 *
1837 * The scroll will happen asynchronously, soon after the call stack winds down.
1838 * Multiple calls will be coalesced into a single scroll.
1839 *
1840 * This affects the scrollbar position of the ScrollPort, and has nothing to
1841 * do with the VT scroll commands.
1842 */
1843hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1844 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001845 return;
rginda8ba33642011-12-14 12:31:31 -08001846
1847 var self = this;
1848 this.timeouts_.scrollDown = setTimeout(function() {
1849 delete self.timeouts_.scrollDown;
1850 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1851 }, 10);
1852};
1853
1854/**
1855 * Move the cursor up a specified number of rows.
1856 *
1857 * @param {integer} count The number of rows to move the cursor.
1858 */
1859hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001860 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001861};
1862
1863/**
1864 * Move the cursor down a specified number of rows.
1865 *
1866 * @param {integer} count The number of rows to move the cursor.
1867 */
1868hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001869 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001870 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1871 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1872 this.screenSize.height - 1);
1873
rgindacbbd7482012-06-13 15:06:16 -07001874 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001875 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001876 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001877};
1878
1879/**
1880 * Move the cursor left a specified number of columns.
1881 *
1882 * @param {integer} count The number of columns to move the cursor.
1883 */
1884hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001885 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001886};
1887
1888/**
1889 * Move the cursor right a specified number of columns.
1890 *
1891 * @param {integer} count The number of columns to move the cursor.
1892 */
1893hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001894 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001895 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001896 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001897 this.setCursorColumn(column);
1898};
1899
1900/**
1901 * Reverse the foreground and background colors of the terminal.
1902 *
1903 * This only affects text that was drawn with no attributes.
1904 *
1905 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1906 * been drawn with attributes that happen to coincide with the default
1907 * 'no-attribute' colors. My guess is probably not.
1908 */
1909hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001910 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001911 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001912 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1913 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001914 } else {
rginda9f5222b2012-03-05 11:53:28 -08001915 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1916 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001917 }
1918};
1919
1920/**
rginda87b86462011-12-14 13:48:03 -08001921 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001922 */
1923hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001924 if (this.bellAudio_.getAttribute('src'))
1925 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001926
rginda6d397402012-01-17 10:58:29 -08001927 this.cursorNode_.style.backgroundColor =
1928 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001929
1930 var self = this;
1931 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001932 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001933 }, 200);
rginda87b86462011-12-14 13:48:03 -08001934};
1935
1936/**
rginda8ba33642011-12-14 12:31:31 -08001937 * Set the origin mode bit.
1938 *
1939 * If origin mode is on, certain VT cursor and scrolling commands measure their
1940 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1941 * to the top of the addressable screen.
1942 *
1943 * Defaults to off.
1944 *
1945 * @param {boolean} state True to set origin mode, false to unset.
1946 */
1947hterm.Terminal.prototype.setOriginMode = function(state) {
1948 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001949 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001950};
1951
1952/**
1953 * Set the insert mode bit.
1954 *
1955 * If insert mode is on, existing text beyond the cursor position will be
1956 * shifted right to make room for new text. Otherwise, new text overwrites
1957 * any existing text.
1958 *
1959 * Defaults to off.
1960 *
1961 * @param {boolean} state True to set insert mode, false to unset.
1962 */
1963hterm.Terminal.prototype.setInsertMode = function(state) {
1964 this.options_.insertMode = state;
1965};
1966
1967/**
rginda87b86462011-12-14 13:48:03 -08001968 * Set the auto carriage return bit.
1969 *
1970 * If auto carriage return is on then a formfeed character is interpreted
1971 * as a newline, otherwise it's the same as a linefeed. The difference boils
1972 * down to whether or not the cursor column is reset.
1973 */
1974hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1975 this.options_.autoCarriageReturn = state;
1976};
1977
1978/**
rginda8ba33642011-12-14 12:31:31 -08001979 * Set the wraparound mode bit.
1980 *
1981 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1982 * to the start of the following row. Otherwise, the cursor is clamped to the
1983 * end of the screen and attempts to write past it are ignored.
1984 *
1985 * Defaults to on.
1986 *
1987 * @param {boolean} state True to set wraparound mode, false to unset.
1988 */
1989hterm.Terminal.prototype.setWraparound = function(state) {
1990 this.options_.wraparound = state;
1991};
1992
1993/**
1994 * Set the reverse-wraparound mode bit.
1995 *
1996 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1997 * to the end of the previous row. Otherwise, the cursor is clamped to column
1998 * 0.
1999 *
2000 * Defaults to off.
2001 *
2002 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2003 */
2004hterm.Terminal.prototype.setReverseWraparound = function(state) {
2005 this.options_.reverseWraparound = state;
2006};
2007
2008/**
2009 * Selects between the primary and alternate screens.
2010 *
2011 * If alternate mode is on, the alternate screen is active. Otherwise the
2012 * primary screen is active.
2013 *
2014 * Swapping screens has no effect on the scrollback buffer.
2015 *
2016 * Each screen maintains its own cursor position.
2017 *
2018 * Defaults to off.
2019 *
2020 * @param {boolean} state True to set alternate mode, false to unset.
2021 */
2022hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002023 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002024 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2025
rginda35c456b2012-02-09 17:29:05 -08002026 if (this.screen_.rowsArray.length &&
2027 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2028 // If the screen changed sizes while we were away, our rowIndexes may
2029 // be incorrect.
2030 var offset = this.scrollbackRows_.length;
2031 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002032 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002033 ary[i].rowIndex = offset + i;
2034 }
2035 }
rginda8ba33642011-12-14 12:31:31 -08002036
rginda35c456b2012-02-09 17:29:05 -08002037 this.realizeWidth_(this.screenSize.width);
2038 this.realizeHeight_(this.screenSize.height);
2039 this.scrollPort_.syncScrollHeight();
2040 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002041
rginda6d397402012-01-17 10:58:29 -08002042 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002043 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002044};
2045
2046/**
2047 * Set the cursor-blink mode bit.
2048 *
2049 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2050 * a visible cursor does not blink.
2051 *
2052 * You should make sure to turn blinking off if you're going to dispose of a
2053 * terminal, otherwise you'll leak a timeout.
2054 *
2055 * Defaults to on.
2056 *
2057 * @param {boolean} state True to set cursor-blink mode, false to unset.
2058 */
2059hterm.Terminal.prototype.setCursorBlink = function(state) {
2060 this.options_.cursorBlink = state;
2061
2062 if (!state && this.timeouts_.cursorBlink) {
2063 clearTimeout(this.timeouts_.cursorBlink);
2064 delete this.timeouts_.cursorBlink;
2065 }
2066
2067 if (this.options_.cursorVisible)
2068 this.setCursorVisible(true);
2069};
2070
2071/**
2072 * Set the cursor-visible mode bit.
2073 *
2074 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2075 *
2076 * Defaults to on.
2077 *
2078 * @param {boolean} state True to set cursor-visible mode, false to unset.
2079 */
2080hterm.Terminal.prototype.setCursorVisible = function(state) {
2081 this.options_.cursorVisible = state;
2082
2083 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002084 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002085 return;
2086 }
2087
rginda87b86462011-12-14 13:48:03 -08002088 this.syncCursorPosition_();
2089
2090 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002091
2092 if (this.options_.cursorBlink) {
2093 if (this.timeouts_.cursorBlink)
2094 return;
2095
2096 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2097 500);
2098 } else {
2099 if (this.timeouts_.cursorBlink) {
2100 clearTimeout(this.timeouts_.cursorBlink);
2101 delete this.timeouts_.cursorBlink;
2102 }
2103 }
2104};
2105
2106/**
rginda87b86462011-12-14 13:48:03 -08002107 * Synchronizes the visible cursor and document selection with the current
2108 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002109 */
2110hterm.Terminal.prototype.syncCursorPosition_ = function() {
2111 var topRowIndex = this.scrollPort_.getTopRowIndex();
2112 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2113 var cursorRowIndex = this.scrollbackRows_.length +
2114 this.screen_.cursorPosition.row;
2115
2116 if (cursorRowIndex > bottomRowIndex) {
2117 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002118 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002119 return;
2120 }
2121
rginda35c456b2012-02-09 17:29:05 -08002122 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
2123 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
2124
rginda8ba33642011-12-14 12:31:31 -08002125 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002126 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2127 'px';
2128 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2129 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002130
2131 this.cursorNode_.setAttribute('title',
2132 '(' + this.screen_.cursorPosition.row +
2133 ', ' + this.screen_.cursorPosition.column +
2134 ')');
2135
2136 // Update the caret for a11y purposes.
2137 var selection = this.document_.getSelection();
2138 if (selection && selection.isCollapsed)
2139 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002140};
2141
2142/**
2143 * Synchronizes the visible cursor with the current cursor coordinates.
2144 *
2145 * The sync will happen asynchronously, soon after the call stack winds down.
2146 * Multiple calls will be coalesced into a single sync.
2147 */
2148hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2149 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002150 return;
rginda8ba33642011-12-14 12:31:31 -08002151
2152 var self = this;
2153 this.timeouts_.syncCursor = setTimeout(function() {
2154 self.syncCursorPosition_();
2155 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002156 }, 0);
2157};
2158
rgindacc2996c2012-02-24 14:59:31 -08002159/**
rgindaf522ce02012-04-17 17:49:17 -07002160 * Show or hide the zoom warning.
2161 *
2162 * The zoom warning is a message warning the user that their browser zoom must
2163 * be set to 100% in order for hterm to function properly.
2164 *
2165 * @param {boolean} state True to show the message, false to hide it.
2166 */
2167hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2168 if (!this.zoomWarningNode_) {
2169 if (!state)
2170 return;
2171
2172 this.zoomWarningNode_ = this.document_.createElement('div');
2173 this.zoomWarningNode_.style.cssText = (
2174 'color: black;' +
2175 'background-color: #ff2222;' +
2176 'font-size: large;' +
2177 'border-radius: 8px;' +
2178 'opacity: 0.75;' +
2179 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2180 'top: 0.5em;' +
2181 'right: 1.2em;' +
2182 'position: absolute;' +
2183 '-webkit-text-size-adjust: none;' +
2184 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002185 }
2186
rgindade84e382012-04-20 15:39:31 -07002187 this.zoomWarningNode_.textContent = hterm.msg('ZOOM_WARNING') ||
2188 ('!! ' + parseInt(this.scrollPort_.characterSize.zoomFactor * 100) +
2189 '% !!');
rgindaf522ce02012-04-17 17:49:17 -07002190 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2191
2192 if (state) {
2193 if (!this.zoomWarningNode_.parentNode)
2194 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2195 } else if (this.zoomWarningNode_.parentNode) {
2196 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2197 }
2198};
2199
2200/**
rgindacc2996c2012-02-24 14:59:31 -08002201 * Show the terminal overlay for a given amount of time.
2202 *
2203 * The terminal overlay appears in inverse video in a large font, centered
2204 * over the terminal. You should probably keep the overlay message brief,
2205 * since it's in a large font and you probably aren't going to check the size
2206 * of the terminal first.
2207 *
2208 * @param {string} msg The text (not HTML) message to display in the overlay.
2209 * @param {number} opt_timeout The amount of time to wait before fading out
2210 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2211 * stay up forever (or until the next overlay).
2212 */
2213hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002214 if (!this.overlayNode_) {
2215 if (!this.div_)
2216 return;
2217
2218 this.overlayNode_ = this.document_.createElement('div');
2219 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002220 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002221 'font-size: xx-large;' +
2222 'opacity: 0.75;' +
2223 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2224 'position: absolute;' +
2225 '-webkit-user-select: none;' +
2226 '-webkit-transition: opacity 180ms ease-in;');
2227 }
2228
rginda9f5222b2012-03-05 11:53:28 -08002229 this.overlayNode_.style.color = this.prefs_.get('background-color');
2230 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2231 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2232
rgindaf0090c92012-02-10 14:58:52 -08002233 this.overlayNode_.textContent = msg;
2234 this.overlayNode_.style.opacity = '0.75';
2235
2236 if (!this.overlayNode_.parentNode)
2237 this.div_.appendChild(this.overlayNode_);
2238
2239 this.overlayNode_.style.top = (
2240 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
2241 this.overlayNode_.style.left = (
2242 this.div_.clientWidth - this.overlayNode_.clientWidth -
2243 this.scrollbarWidthPx) / 2;
2244
2245 var self = this;
2246
2247 if (this.overlayTimeout_)
2248 clearTimeout(this.overlayTimeout_);
2249
rgindacc2996c2012-02-24 14:59:31 -08002250 if (opt_timeout === null)
2251 return;
2252
rgindaf0090c92012-02-10 14:58:52 -08002253 this.overlayTimeout_ = setTimeout(function() {
2254 self.overlayNode_.style.opacity = '0';
2255 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002256 if (self.overlayNode_.parentNode)
2257 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002258 self.overlayTimeout_ = null;
2259 self.overlayNode_.style.opacity = '0.75';
2260 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002261 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002262};
2263
rginda4bba5e12012-06-20 16:15:30 -07002264/**
2265 * Paste from the system clipboard to the terminal.
2266 */
2267hterm.Terminal.prototype.paste = function() {
2268 hterm.pasteFromClipboard(this.document_);
2269};
2270
2271/**
2272 * Copy a string to the system clipboard.
2273 *
2274 * Note: If there is a selected range in the terminal, it'll be cleared.
2275 */
2276hterm.Terminal.prototype.copyStringToClipboard = function(str) {
rgindaa09e7332012-08-17 12:49:51 -07002277 this.showOverlay(hterm.msg('NOTIFY_COPY'), 500);
2278
2279 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002280 copySource.textContent = str;
2281 copySource.style.cssText = (
2282 '-webkit-user-select: text;' +
2283 'position: absolute;' +
2284 'top: -99px');
2285
2286 this.document_.body.appendChild(copySource);
2287 var selection = this.document_.getSelection();
2288 selection.selectAllChildren(copySource);
2289
rgindaa09e7332012-08-17 12:49:51 -07002290 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002291
2292 copySource.parentNode.removeChild(copySource);
2293};
2294
rgindaa09e7332012-08-17 12:49:51 -07002295hterm.Terminal.prototype.getSelectionText = function() {
2296 var selection = this.scrollPort_.selection;
2297 selection.sync();
2298
2299 if (selection.isCollapsed)
2300 return null;
2301
2302
2303 // Start offset measures from the beginning of the line.
2304 var startOffset = selection.startOffset;
2305 var node = selection.startNode;
2306 while (node.previousSibling) {
2307 node = node.previousSibling;
2308 startOffset += node.textContent.length;
2309 }
2310
2311 // End offset measures from the end of the line.
2312 var endOffset = selection.endNode.textContent.length - selection.endOffset;
2313 var node = selection.endNode;
2314 while (node.nextSibling) {
2315 node = node.nextSibling;
2316 endOffset += node.textContent.length;
2317 }
2318
2319 var rv = this.getRowsText(selection.startRow.rowIndex,
2320 selection.endRow.rowIndex + 1);
2321 return rv.substring(startOffset, rv.length - endOffset);
2322};
2323
rginda4bba5e12012-06-20 16:15:30 -07002324/**
2325 * Copy the current selection to the system clipboard, then clear it after a
2326 * short delay.
2327 */
2328hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002329 var text = this.getSelectionText();
2330 if (text != null)
2331 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002332};
2333
rgindaf0090c92012-02-10 14:58:52 -08002334hterm.Terminal.prototype.overlaySize = function() {
2335 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2336};
2337
rginda87b86462011-12-14 13:48:03 -08002338/**
2339 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2340 *
2341 * @param {string} string The VT string representing the keystroke.
2342 */
2343hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002344 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002345 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2346
2347 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08002348};
2349
2350/**
rgindad5613292012-06-19 15:40:37 -07002351 * Add the terminalRow and terminalColumn properties to mouse events and
2352 * then forward on to onMouse().
2353 *
2354 * The terminalRow and terminalColumn properties contain the (row, column)
2355 * coordinates for the mouse event.
2356 */
2357hterm.Terminal.prototype.onMouse_ = function(e) {
rginda4bba5e12012-06-20 16:15:30 -07002358 if (e.type == 'mousedown' && e.which == this.mousePasteButton) {
2359 this.paste();
2360 return;
2361 }
2362
2363 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2364 !this.document_.getSelection().isCollapsed) {
2365 this.copySelectionToClipboard();
2366 return;
2367 }
2368
rgindad5613292012-06-19 15:40:37 -07002369 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2370 this.scrollPort_.characterSize.height) + 1;
2371 e.terminalColumn = parseInt(e.clientX /
2372 this.scrollPort_.characterSize.width) + 1;
2373
2374 if (e.type == 'mousedown') {
2375 if (e.terminalColumn > this.screenSize.width) {
2376 // Mousedown in the scrollbar area.
2377 return;
2378 }
2379
2380 if (!this.enableMouseDragScroll) {
2381 // Move the scroll-blocker into place if we want to keep the scrollport
2382 // from scrolling.
2383 this.scrollBlockerNode_.engaged = true;
2384 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2385 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2386 }
2387 } else if (this.scrollBlockerNode_.engaged &&
2388 (e.type == 'mousemove' || e.type == 'mouseup')) {
2389 // Disengage the scroll-blocker after one of these events.
2390 this.scrollBlockerNode_.engaged = false;
2391 this.scrollBlockerNode_.style.top = '-99px';
2392 }
2393
2394 if (!e.processedByTerminalHandler_) {
2395 // We register our event handlers on the document, as well as the cursor
2396 // and the scroll blocker. Mouse events that occur on the cursor or
2397 // scroll blocker will also appear on the document, but we don't want to
2398 // process them twice.
2399 //
2400 // We can't just prevent bubbling because that has other side effects, so
2401 // we decorate the event object with this property instead.
2402 e.processedByTerminalHandler_ = true;
2403
2404 this.onMouse(e);
2405 }
2406};
2407
2408/**
2409 * Clients should override this if they care to know about mouse events.
2410 *
2411 * The event parameter will be a normal DOM mouse click event with additional
2412 * 'terminalRow' and 'terminalColumn' properties.
2413 */
2414hterm.Terminal.prototype.onMouse = function(e) { };
2415
2416/**
rginda8e92a692012-05-20 19:37:20 -07002417 * React when focus changes.
2418 */
2419hterm.Terminal.prototype.onFocusChange_ = function(state) {
2420 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
2421};
2422
2423/**
rginda8ba33642011-12-14 12:31:31 -08002424 * React when the ScrollPort is scrolled.
2425 */
2426hterm.Terminal.prototype.onScroll_ = function() {
2427 this.scheduleSyncCursorPosition_();
2428};
2429
2430/**
rginda9846e2f2012-01-27 13:53:33 -08002431 * React when text is pasted into the scrollPort.
2432 */
2433hterm.Terminal.prototype.onPaste_ = function(e) {
David Benjamin8f962172012-07-17 07:38:43 -04002434 this.io.onVTKeystroke(this.vt.encodeUTF8(e.text));
rginda9846e2f2012-01-27 13:53:33 -08002435};
2436
2437/**
rgindaa09e7332012-08-17 12:49:51 -07002438 * React when the user tries to copy from the scrollPort.
2439 */
2440hterm.Terminal.prototype.onCopy_ = function(e) {
2441 e.preventDefault();
2442 setTimeout(this.copySelectionToClipboard.bind(this), 200);
2443};
2444
2445/**
rginda8ba33642011-12-14 12:31:31 -08002446 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002447 *
2448 * Note: This function should not directly contain code that alters the internal
2449 * state of the terminal. That kind of code belongs in realizeWidth or
2450 * realizeHeight, so that it can be executed synchronously in the case of a
2451 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002452 */
2453hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002454 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002455 this.scrollPort_.characterSize.width);
2456 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2457 this.scrollPort_.characterSize.height);
2458
2459 if (!(columnCount || rowCount)) {
2460 // We avoid these situations since they happen sometimes when the terminal
2461 // gets removed from the document, and we can't deal with that.
2462 return;
2463 }
2464
rgindaa8ba17d2012-08-15 14:41:10 -07002465 var isNewSize = (columnCount != this.screenSize.width ||
2466 rowCount != this.screenSize.height);
2467
2468 // We do this even if the size didn't change, just to be sure everything is
2469 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002470 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002471 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002472
2473 if (isNewSize)
2474 this.overlaySize();
2475
2476 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002477};
2478
2479/**
2480 * Service the cursor blink timeout.
2481 */
2482hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002483 if (this.cursorNode_.style.opacity == '0') {
2484 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002485 } else {
rginda87b86462011-12-14 13:48:03 -08002486 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002487 }
2488};
David Reveman8f552492012-03-28 12:18:41 -04002489
2490/**
2491 * Set the scrollbar-visible mode bit.
2492 *
2493 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2494 * Otherwise it will not.
2495 *
2496 * Defaults to on.
2497 *
2498 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2499 */
2500hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2501 this.scrollPort_.setScrollbarVisible(state);
2502};