blob: b0a49210eb0819e83d0fbb98411fd24810152c0b [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));
rginda8ba33642011-12-14 12:31:31 -080054
rginda87b86462011-12-14 13:48:03 -080055 // The div that contains this terminal.
56 this.div_ = null;
57
rgindac9bc5502012-01-18 11:48:44 -080058 // The document that contains the scrollPort. Defaulted to the global
59 // document here so that the terminal is functional even if it hasn't been
60 // inserted into a document yet, but re-set in decorate().
61 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080062
rginda8ba33642011-12-14 12:31:31 -080063 // The rows that have scrolled off screen and are no longer addressable.
64 this.scrollbackRows_ = [];
65
rgindac9bc5502012-01-18 11:48:44 -080066 // Saved tab stops.
67 this.tabStops_ = [];
68
David Benjamin66e954d2012-05-05 21:08:12 -040069 // Keep track of whether default tab stops have been erased; after a TBC
70 // clears all tab stops, defaults aren't restored on resize until a reset.
71 this.defaultTabStops = true;
72
rginda8ba33642011-12-14 12:31:31 -080073 // The VT's notion of the top and bottom rows. Used during some VT
74 // cursor positioning and scrolling commands.
75 this.vtScrollTop_ = null;
76 this.vtScrollBottom_ = null;
77
78 // The DIV element for the visible cursor.
79 this.cursorNode_ = null;
80
rginda9f5222b2012-03-05 11:53:28 -080081 // These prefs are cached so we don't have to read from local storage with
82 // each output and keystroke.
83 this.scrollOnOutput_ = this.prefs_.get('scroll-on-output');
84 this.scrollOnKeystroke_ = this.prefs_.get('scroll-on-keystroke');
rginda8e92a692012-05-20 19:37:20 -070085 this.foregroundColor_ = this.prefs_.get('foreground-color');
86 this.backgroundColor_ = this.prefs_.get('background-color');
rginda9f5222b2012-03-05 11:53:28 -080087
rgindaf0090c92012-02-10 14:58:52 -080088 // Terminal bell sound.
89 this.bellAudio_ = this.document_.createElement('audio');
rginda9f5222b2012-03-05 11:53:28 -080090 this.bellAudio_.setAttribute('src', this.prefs_.get('audible-bell-sound'));
rgindaf0090c92012-02-10 14:58:52 -080091 this.bellAudio_.setAttribute('preload', 'auto');
92
rginda6d397402012-01-17 10:58:29 -080093 // Cursor position and attributes saved with DECSC.
94 this.savedOptions_ = {};
95
rginda8ba33642011-12-14 12:31:31 -080096 // The current mode bits for the terminal.
97 this.options_ = new hterm.Options();
98
99 // Timeouts we might need to clear.
100 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800101
102 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800103 this.vt = new hterm.VT(this);
rginda11057d52012-04-25 12:29:56 -0700104 this.vt.enable8BitControl = this.prefs_.get('enable-8-bit-control');
105 this.vt.maxStringSequence = this.prefs_.get('max-string-sequence');
rgindaa8ba17d2012-08-15 14:41:10 -0700106 this.vt.enableClipboardWrite = this.prefs_.get('enable-clipboard-write');
rginda87b86462011-12-14 13:48:03 -0800107
rgindafeaf3142012-01-31 15:14:20 -0800108 // The keyboard hander.
109 this.keyboard = new hterm.Keyboard(this);
110
rginda87b86462011-12-14 13:48:03 -0800111 // General IO interface that can be given to third parties without exposing
112 // the entire terminal object.
113 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800114
rgindad5613292012-06-19 15:40:37 -0700115 // True if mouse-click-drag should scroll the terminal.
116 this.enableMouseDragScroll = true;
117
rginda4bba5e12012-06-20 16:15:30 -0700118 this.copyOnSelect = this.prefs_.get('copy-on-select');
119 this.mousePasteButton = null;
120 this.syncMousePasteButton();
121
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400122 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800123 this.setDefaultTabStops();
rginda87b86462011-12-14 13:48:03 -0800124};
125
126/**
rginda35c456b2012-02-09 17:29:05 -0800127 * Default tab with of 8 to match xterm.
128 */
129hterm.Terminal.prototype.tabWidth = 8;
130
131/**
rginda35c456b2012-02-09 17:29:05 -0800132 * The assumed width of a scrollbar.
133 */
134hterm.Terminal.prototype.scrollbarWidthPx = 16;
135
136/**
rginda9f5222b2012-03-05 11:53:28 -0800137 * Select a preference profile.
138 *
139 * This will load the terminal preferences for the given profile name and
140 * associate subsequent preference changes with the new preference profile.
141 *
142 * @param {string} newName The name of the preference profile. Forward slash
143 * characters will be removed from the name.
144 */
145hterm.Terminal.prototype.setProfile = function(profileName) {
146 // If we already have a profile selected, we're going to need to re-sync
147 // with the new profile.
148 var needSync = !!this.profileName_;
149
150 this.profileName_ = profileName.replace(/\//g, '');
151
rgindacbbd7482012-06-13 15:06:16 -0700152 this.prefs_ = new lib.PreferenceManager(
rginda9f5222b2012-03-05 11:53:28 -0800153 '/hterm/prefs/profiles/' + this.profileName_);
154
155 var self = this;
156 this.prefs_.definePreferences
rginda30f20f62012-04-05 16:36:19 -0700157 ([
158 /**
159 * Set whether the alt key acts as a meta key or as a distinct alt key.
rginda9f5222b2012-03-05 11:53:28 -0800160 */
rginda30f20f62012-04-05 16:36:19 -0700161 ['alt-is-meta', false, function(v) {
rgindaf9c36852012-05-09 11:08:39 -0700162 self.keyboard.altIsMeta = v;
rginda9f5222b2012-03-05 11:53:28 -0800163 }
164 ],
165
rginda30f20f62012-04-05 16:36:19 -0700166 /**
rginda39bdf6f2012-04-10 16:50:55 -0700167 * Controls how the alt key is handled.
168 *
169 * escape....... Send an ESC prefix.
170 * 8-bit........ Add 128 to the unshifted character as in xterm.
171 * browser-key.. Wait for the keypress event and see what the browser says.
172 * (This won't work well on platforms where the browser
173 * performs a default action for some alt sequences.)
rginda30f20f62012-04-05 16:36:19 -0700174 */
rginda39bdf6f2012-04-10 16:50:55 -0700175 ['alt-sends-what', 'escape', function(v) {
176 if (!/^(escape|8-bit|browser-key)$/.test(v))
177 v = 'escape';
178
rgindaf9c36852012-05-09 11:08:39 -0700179 self.keyboard.altSendsWhat = v;
rginda30f20f62012-04-05 16:36:19 -0700180 }
181 ],
182
183 /**
184 * Terminal bell sound. Empty string for no audible bell.
185 */
186 ['audible-bell-sound', '../audio/bell.ogg', function(v) {
187 self.bellAudio_.setAttribute('src', v);
188 }
189 ],
190
191 /**
192 * The background color for text with no other color attributes.
193 */
194 ['background-color', 'rgb(16, 16, 16)', function(v) {
rginda8e92a692012-05-20 19:37:20 -0700195 self.setBackgroundColor(v);
rginda9f5222b2012-03-05 11:53:28 -0800196 }
197 ],
198
199 /**
rginda30f20f62012-04-05 16:36:19 -0700200 * The background image.
rginda30f20f62012-04-05 16:36:19 -0700201 */
rginda8e92a692012-05-20 19:37:20 -0700202 ['background-image', '',
rginda30f20f62012-04-05 16:36:19 -0700203 function(v) {
204 self.scrollPort_.setBackgroundImage(v);
205 }
206 ],
207
208 /**
Philip Douglass959b49d2012-05-30 13:29:29 -0400209 * The background image size,
210 *
211 * Defaults to none.
212 */
213 ['background-size', '', function(v) {
214 self.scrollPort_.setBackgroundSize(v);
215 }
216 ],
217
218 /**
219 * The background image position,
220 *
221 * Defaults to none.
222 */
223 ['background-position', '', function(v) {
224 self.scrollPort_.setBackgroundPosition(v);
225 }
226 ],
227
228 /**
rginda30f20f62012-04-05 16:36:19 -0700229 * If true, the backspace should send BS ('\x08', aka ^H). Otherwise
230 * the backspace key should send '\x7f'.
231 */
232 ['backspace-sends-backspace', false, function(v) {
233 self.keyboard.backspaceSendsBackspace = v;
234 }
235 ],
236
237 /**
rgindade84e382012-04-20 15:39:31 -0700238 * Whether or not to blink the cursor by default.
239 */
240 ['cursor-blink', false, function(v) {
241 self.setCursorBlink(!!v);
242 }
243 ],
244
245 /**
rginda30f20f62012-04-05 16:36:19 -0700246 * The color of the visible cursor.
247 */
248 ['cursor-color', 'rgba(255,0,0,0.5)', function(v) {
rginda8e92a692012-05-20 19:37:20 -0700249 self.setCursorColor(v);
rginda30f20f62012-04-05 16:36:19 -0700250 }
251 ],
252
253 /**
rginda4bba5e12012-06-20 16:15:30 -0700254 * Automatically copy mouse selection to the clipboard.
255 */
256 ['copy-on-select', true, function(v) {
257 self.copyOnSelect = !!v;
258 }
259 ],
260
261 /**
rginda11057d52012-04-25 12:29:56 -0700262 * True to enable 8-bit control characters, false to ignore them.
263 *
264 * We'll respect the two-byte versions of these control characters
265 * regardless of this setting.
266 */
267 ['enable-8-bit-control', false, function(v) {
268 self.vt.enable8BitControl = !!v;
269 }
270 ],
271
272 /**
rginda30f20f62012-04-05 16:36:19 -0700273 * True if we should use bold weight font for text with the bold/bright
274 * attribute. False to use bright colors only. Null to autodetect.
275 */
276 ['enable-bold', null, function(v) {
277 self.syncBoldSafeState();
278 }
279 ],
280
281 /**
rgindaa8ba17d2012-08-15 14:41:10 -0700282 * Allow the host to write directly to the system clipboard.
283 */
284 ['enable-clipboard-write', true, function(v) {
285 self.vt.enableClipboardWrite = !!v;
286 }
287 ],
288
289 /**
rginda9f5222b2012-03-05 11:53:28 -0800290 * Default font family for the terminal text.
291 */
292 ['font-family', ('"DejaVu Sans Mono", "Everson Mono", ' +
rgindaa8ba17d2012-08-15 14:41:10 -0700293 'FreeMono, "Menlo", "Terminal", ' +
rginda9f5222b2012-03-05 11:53:28 -0800294 'monospace'),
295 function(v) { self.syncFontFamily() }
296 ],
297
298 /**
rginda30f20f62012-04-05 16:36:19 -0700299 * The default font size in pixels.
300 */
301 ['font-size', 15, function(v) {
302 self.setFontSize(v);
303 }
304 ],
305
306 /**
rginda9f5222b2012-03-05 11:53:28 -0800307 * Anti-aliasing.
308 */
309 ['font-smoothing', 'antialiased',
310 function(v) { self.syncFontFamily() }
311 ],
312
313 /**
rginda30f20f62012-04-05 16:36:19 -0700314 * The foreground color for text with no other color attributes.
rginda9f5222b2012-03-05 11:53:28 -0800315 */
rginda30f20f62012-04-05 16:36:19 -0700316 ['foreground-color', 'rgb(240, 240, 240)', function(v) {
rginda8e92a692012-05-20 19:37:20 -0700317 self.setForegroundColor(v);
rginda9f5222b2012-03-05 11:53:28 -0800318 }
319 ],
320
321 /**
rginda30f20f62012-04-05 16:36:19 -0700322 * If true, home/end will control the terminal scrollbar and shift home/end
323 * will send the VT keycodes. If false then home/end sends VT codes and
324 * shift home/end scrolls.
rginda9f5222b2012-03-05 11:53:28 -0800325 */
rginda30f20f62012-04-05 16:36:19 -0700326 ['home-keys-scroll', false, function(v) {
327 self.keyboard.homeKeysScroll = v;
328 }
329 ],
330
331 /**
rginda11057d52012-04-25 12:29:56 -0700332 * Max length of a DCS, OSC, PM, or APS sequence before we give up and
333 * ignore the code.
334 */
335 ['max-string-sequence', 1024, function(v) {
336 self.vt.maxStringSequence = v;
337 }
338 ],
339
340 /**
rginda30f20f62012-04-05 16:36:19 -0700341 * Set whether the meta key sends a leading escape or not.
342 */
343 ['meta-sends-escape', true, function(v) {
344 self.keyboard.metaSendsEscape = v;
rginda9f5222b2012-03-05 11:53:28 -0800345 }
346 ],
347
348 /**
rgindad5613292012-06-19 15:40:37 -0700349 * Set whether we should treat DEC mode 1002 (mouse cell motion tracking)
350 * as if it were 1000 (mouse click tracking).
351 *
352 * This makes it possible to use vi's ":set mouse=a" mode without losing
353 * access to the system text selection mechanism.
354 */
355 ['mouse-cell-motion-trick', false, function(v) {
356 self.vt.setMouseCellMotionTrick(v);
357 }
358 ],
359
360 /**
rginda4bba5e12012-06-20 16:15:30 -0700361 * Mouse paste button, or null to autodetect.
362 *
363 * For autodetect, we'll try to enable middle button paste for non-X11
364 * platforms.
365 *
366 * On X11 we move it to button 3, but that'll probably be a context menu
367 * in the future.
368 */
369 ['mouse-paste-button', null, function(v) {
370 self.syncMousePasteButton();
371 }
372 ],
373
374 /**
rginda9f5222b2012-03-05 11:53:28 -0800375 * If true, scroll to the bottom on any keystroke.
376 */
377 ['scroll-on-keystroke', true, function(v) {
378 self.scrollOnKeystroke_ = v;
379 }
380 ],
381
382 /**
383 * If true, scroll to the bottom on terminal output.
384 */
385 ['scroll-on-output', false, function(v) {
386 self.scrollOnOutput_ = v;
387 }
388 ],
389
390 /**
David Reveman8f552492012-03-28 12:18:41 -0400391 * The vertical scrollbar mode.
392 */
393 ['scrollbar-visible', true, function(v) {
394 self.setScrollbarVisible(v);
395 }
396 ],
rginda30f20f62012-04-05 16:36:19 -0700397
398 /**
rginda4bba5e12012-06-20 16:15:30 -0700399 * Shift + Insert pastes if true, sent to host if false.
400 */
401 ['shift-insert-paste', true, function(v) {
402 self.keyboard.shiftInsertPaste = v;
403 }
404 ],
405
406 /**
rgindaf522ce02012-04-17 17:49:17 -0700407 * The default environment variables.
408 */
409 ['environment', {TERM: 'xterm-256color'}, null],
410
411 /**
rginda30f20f62012-04-05 16:36:19 -0700412 * If true, page up/down will control the terminal scrollbar and shift
413 * page up/down will send the VT keycodes. If false then page up/down
414 * sends VT codes and shift page up/down scrolls.
415 */
416 ['page-keys-scroll', false, function(v) {
417 self.keyboard.pageKeysScroll = v;
418 }
419 ],
420
rginda9f5222b2012-03-05 11:53:28 -0800421 ]);
422
423 if (needSync)
424 this.prefs_.notifyAll();
425};
426
rginda8e92a692012-05-20 19:37:20 -0700427
428/**
429 * Set the color for the cursor.
430 *
431 * If you want this setting to persist, set it through prefs_, rather than
432 * with this method.
433 */
434hterm.Terminal.prototype.setCursorColor = function(color) {
435 this.cursorNode_.style.backgroundColor = color;
436 this.cursorNode_.style.borderColor = color;
437};
438
439/**
440 * Return the current cursor color as a string.
441 */
442hterm.Terminal.prototype.getCursorColor = function() {
443 return this.cursorNode_.style.backgroundColor;
444};
445
446/**
rgindad5613292012-06-19 15:40:37 -0700447 * Enable or disable mouse based text selection in the terminal.
448 */
449hterm.Terminal.prototype.setSelectionEnabled = function(state) {
450 this.enableMouseDragScroll = state;
451 this.scrollPort_.setSelectionEnabled(state);
452};
453
454/**
rginda8e92a692012-05-20 19:37:20 -0700455 * Set the background color.
456 *
457 * If you want this setting to persist, set it through prefs_, rather than
458 * with this method.
459 */
460hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700461 this.backgroundColor_ = lib.colors.normalizeCSS(color);
rginda8e92a692012-05-20 19:37:20 -0700462 this.scrollPort_.setBackgroundColor(color);
463};
464
rginda9f5222b2012-03-05 11:53:28 -0800465/**
466 * Return the current terminal background color.
467 *
468 * Intended for use by other classes, so we don't have to expose the entire
469 * prefs_ object.
470 */
471hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700472 return this.backgroundColor_;
473};
474
475/**
476 * Set the foreground color.
477 *
478 * If you want this setting to persist, set it through prefs_, rather than
479 * with this method.
480 */
481hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700482 this.foregroundColor_ = lib.colors.normalizeCSS(color);
rginda8e92a692012-05-20 19:37:20 -0700483 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800484};
485
486/**
487 * Return the current terminal foreground color.
488 *
489 * Intended for use by other classes, so we don't have to expose the entire
490 * prefs_ object.
491 */
492hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700493 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800494};
495
496/**
rginda87b86462011-12-14 13:48:03 -0800497 * Create a new instance of a terminal command and run it with a given
498 * argument string.
499 *
500 * @param {function} commandClass The constructor for a terminal command.
501 * @param {string} argString The argument string to pass to the command.
502 */
503hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700504 var environment = this.prefs_.get('environment');
505 if (typeof environment != 'object' || environment == null)
506 environment = {};
507
rginda87b86462011-12-14 13:48:03 -0800508 var self = this;
509 this.command = new commandClass(
510 { argString: argString || '',
511 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700512 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800513 onExit: function(code) {
514 self.io.pop();
515 self.io.println(hterm.msg('COMMAND_COMPLETE',
516 [self.command.commandName, code]));
rgindafeaf3142012-01-31 15:14:20 -0800517 self.uninstallKeyboard();
rginda87b86462011-12-14 13:48:03 -0800518 }
519 });
520
rgindafeaf3142012-01-31 15:14:20 -0800521 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800522 this.command.run();
523};
524
525/**
rgindafeaf3142012-01-31 15:14:20 -0800526 * Returns true if the current screen is the primary screen, false otherwise.
527 */
528hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700529 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800530};
531
532/**
533 * Install the keyboard handler for this terminal.
534 *
535 * This will prevent the browser from seeing any keystrokes sent to the
536 * terminal.
537 */
538hterm.Terminal.prototype.installKeyboard = function() {
539 this.keyboard.installKeyboard(this.document_.body.firstChild);
540}
541
542/**
543 * Uninstall the keyboard handler for this terminal.
544 */
545hterm.Terminal.prototype.uninstallKeyboard = function() {
546 this.keyboard.installKeyboard(null);
547}
548
549/**
rginda35c456b2012-02-09 17:29:05 -0800550 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800551 *
552 * Call setFontSize(0) to reset to the default font size.
553 *
554 * This function does not modify the font-size preference.
555 *
556 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800557 */
558hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800559 if (px === 0)
560 px = this.prefs_.get('font-size');
561
rginda35c456b2012-02-09 17:29:05 -0800562 this.scrollPort_.setFontSize(px);
563};
564
565/**
566 * Get the current font size.
567 */
568hterm.Terminal.prototype.getFontSize = function() {
569 return this.scrollPort_.getFontSize();
570};
571
572/**
rginda8e92a692012-05-20 19:37:20 -0700573 * Get the current font family.
574 */
575hterm.Terminal.prototype.getFontFamily = function() {
576 return this.scrollPort_.getFontFamily();
577};
578
579/**
rginda35c456b2012-02-09 17:29:05 -0800580 * Set the CSS "font-family" for this terminal.
581 */
rginda9f5222b2012-03-05 11:53:28 -0800582hterm.Terminal.prototype.syncFontFamily = function() {
583 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
584 this.prefs_.get('font-smoothing'));
585 this.syncBoldSafeState();
586};
587
rginda4bba5e12012-06-20 16:15:30 -0700588/**
589 * Set this.mousePasteButton based on the mouse-paste-button pref,
590 * autodetecting if necessary.
591 */
592hterm.Terminal.prototype.syncMousePasteButton = function() {
593 var button = this.prefs_.get('mouse-paste-button');
594 if (typeof button == 'number') {
595 this.mousePasteButton = button;
596 return;
597 }
598
599 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
600 if (!ary || ary[2] == 'CrOS') {
601 this.mousePasteButton = 2;
602 } else {
603 this.mousePasteButton = 3;
604 }
605};
606
607/**
608 * Enable or disable bold based on the enable-bold pref, autodetecting if
609 * necessary.
610 */
rginda9f5222b2012-03-05 11:53:28 -0800611hterm.Terminal.prototype.syncBoldSafeState = function() {
612 var enableBold = this.prefs_.get('enable-bold');
613 if (enableBold !== null) {
614 this.screen_.textAttributes.enableBold = enableBold;
615 return;
616 }
617
rgindaf7521392012-02-28 17:20:34 -0800618 var normalSize = this.scrollPort_.measureCharacterSize();
619 var boldSize = this.scrollPort_.measureCharacterSize('bold');
620
621 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800622 if (!isBoldSafe) {
623 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700624 'from normal. Font family is: ' +
625 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800626 }
rginda9f5222b2012-03-05 11:53:28 -0800627
628 this.screen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800629};
630
631/**
rginda87b86462011-12-14 13:48:03 -0800632 * Return a copy of the current cursor position.
633 *
634 * @return {hterm.RowCol} The RowCol object representing the current position.
635 */
636hterm.Terminal.prototype.saveCursor = function() {
637 return this.screen_.cursorPosition.clone();
638};
639
rgindaa19afe22012-01-25 15:40:22 -0800640hterm.Terminal.prototype.getTextAttributes = function() {
641 return this.screen_.textAttributes;
642};
643
rginda1a09aa02012-06-18 21:11:25 -0700644hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
645 this.screen_.textAttributes = textAttributes;
646};
647
rginda87b86462011-12-14 13:48:03 -0800648/**
rgindaf522ce02012-04-17 17:49:17 -0700649 * Return the current browser zoom factor applied to the terminal.
650 *
651 * @return {number} The current browser zoom factor.
652 */
653hterm.Terminal.prototype.getZoomFactor = function() {
654 return this.scrollPort_.characterSize.zoomFactor;
655};
656
657/**
rginda9846e2f2012-01-27 13:53:33 -0800658 * Change the title of this terminal's window.
659 */
660hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800661 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800662};
663
664/**
rginda87b86462011-12-14 13:48:03 -0800665 * Restore a previously saved cursor position.
666 *
667 * @param {hterm.RowCol} cursor The position to restore.
668 */
669hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700670 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
671 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800672 this.screen_.setCursorPosition(row, column);
673 if (cursor.column > column ||
674 cursor.column == column && cursor.overflow) {
675 this.screen_.cursorPosition.overflow = true;
676 }
rginda87b86462011-12-14 13:48:03 -0800677};
678
679/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400680 * Clear the cursor's overflow flag.
681 */
682hterm.Terminal.prototype.clearCursorOverflow = function() {
683 this.screen_.cursorPosition.overflow = false;
684};
685
686/**
rginda87b86462011-12-14 13:48:03 -0800687 * Set the width of the terminal, resizing the UI to match.
688 */
689hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800690 if (columnCount == null) {
691 this.div_.style.width = '100%';
692 return;
693 }
694
rginda35c456b2012-02-09 17:29:05 -0800695 this.div_.style.width = this.scrollPort_.characterSize.width *
696 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400697 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800698 this.scheduleSyncCursorPosition_();
699};
rginda87b86462011-12-14 13:48:03 -0800700
rgindac9bc5502012-01-18 11:48:44 -0800701/**
rginda35c456b2012-02-09 17:29:05 -0800702 * Set the height of the terminal, resizing the UI to match.
703 */
704hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800705 if (rowCount == null) {
706 this.div_.style.height = '100%';
707 return;
708 }
709
rginda35c456b2012-02-09 17:29:05 -0800710 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700711 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800712 this.realizeSize_(this.screenSize.width, rowCount);
713 this.scheduleSyncCursorPosition_();
714};
715
716/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400717 * Deal with terminal size changes.
718 *
719 */
720hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
721 if (columnCount != this.screenSize.width)
722 this.realizeWidth_(columnCount);
723
724 if (rowCount != this.screenSize.height)
725 this.realizeHeight_(rowCount);
726
727 // Send new terminal size to plugin.
728 this.io.onTerminalResize(columnCount, rowCount);
729};
730
731/**
rgindac9bc5502012-01-18 11:48:44 -0800732 * Deal with terminal width changes.
733 *
734 * This function does what needs to be done when the terminal width changes
735 * out from under us. It happens here rather than in onResize_() because this
736 * code may need to run synchronously to handle programmatic changes of
737 * terminal width.
738 *
739 * Relying on the browser to send us an async resize event means we may not be
740 * in the correct state yet when the next escape sequence hits.
741 */
742hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
743 var deltaColumns = columnCount - this.screen_.getWidth();
744
rginda87b86462011-12-14 13:48:03 -0800745 this.screenSize.width = columnCount;
746 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800747
748 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400749 if (this.defaultTabStops)
750 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800751 } else {
752 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400753 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800754 break;
755
756 this.tabStops_.pop();
757 }
758 }
759
760 this.screen_.setColumnCount(this.screenSize.width);
761};
762
763/**
764 * Deal with terminal height changes.
765 *
766 * This function does what needs to be done when the terminal height changes
767 * out from under us. It happens here rather than in onResize_() because this
768 * code may need to run synchronously to handle programmatic changes of
769 * terminal height.
770 *
771 * Relying on the browser to send us an async resize event means we may not be
772 * in the correct state yet when the next escape sequence hits.
773 */
774hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
775 var deltaRows = rowCount - this.screen_.getHeight();
776
777 this.screenSize.height = rowCount;
778
779 var cursor = this.saveCursor();
780
781 if (deltaRows < 0) {
782 // Screen got smaller.
783 deltaRows *= -1;
784 while (deltaRows) {
785 var lastRow = this.getRowCount() - 1;
786 if (lastRow - this.scrollbackRows_.length == cursor.row)
787 break;
788
789 if (this.getRowText(lastRow))
790 break;
791
792 this.screen_.popRow();
793 deltaRows--;
794 }
795
796 var ary = this.screen_.shiftRows(deltaRows);
797 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
798
799 // We just removed rows from the top of the screen, we need to update
800 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800801 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800802 } else if (deltaRows > 0) {
803 // Screen got larger.
804
805 if (deltaRows <= this.scrollbackRows_.length) {
806 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
807 var rows = this.scrollbackRows_.splice(
808 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
809 this.screen_.unshiftRows(rows);
810 deltaRows -= scrollbackCount;
811 cursor.row += scrollbackCount;
812 }
813
814 if (deltaRows)
815 this.appendRows_(deltaRows);
816 }
817
rginda35c456b2012-02-09 17:29:05 -0800818 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800819 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800820};
821
822/**
823 * Scroll the terminal to the top of the scrollback buffer.
824 */
825hterm.Terminal.prototype.scrollHome = function() {
826 this.scrollPort_.scrollRowToTop(0);
827};
828
829/**
830 * Scroll the terminal to the end.
831 */
832hterm.Terminal.prototype.scrollEnd = function() {
833 this.scrollPort_.scrollRowToBottom(this.getRowCount());
834};
835
836/**
837 * Scroll the terminal one page up (minus one line) relative to the current
838 * position.
839 */
840hterm.Terminal.prototype.scrollPageUp = function() {
841 var i = this.scrollPort_.getTopRowIndex();
842 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
843};
844
845/**
846 * Scroll the terminal one page down (minus one line) relative to the current
847 * position.
848 */
849hterm.Terminal.prototype.scrollPageDown = function() {
850 var i = this.scrollPort_.getTopRowIndex();
851 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800852};
853
rgindac9bc5502012-01-18 11:48:44 -0800854/**
855 * Full terminal reset.
856 */
rginda87b86462011-12-14 13:48:03 -0800857hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800858 this.clearAllTabStops();
859 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700860
861 this.clearHome(this.primaryScreen_);
862 this.primaryScreen_.textAttributes.reset();
863
864 this.clearHome(this.alternateScreen_);
865 this.alternateScreen_.textAttributes.reset();
866
rgindab8bc8932012-04-27 12:45:03 -0700867 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
868
rgindac9bc5502012-01-18 11:48:44 -0800869 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800870};
871
rgindac9bc5502012-01-18 11:48:44 -0800872/**
873 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700874 *
875 * Perform a soft reset to the default values listed in
876 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800877 */
rginda0f5c0292012-01-13 11:00:13 -0800878hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700879 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800880 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700881
rgindab8bc8932012-04-27 12:45:03 -0700882 // Xterm also resets the color palette on soft reset, even though it doesn't
883 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700884 this.primaryScreen_.textAttributes.resetColorPalette();
885 this.alternateScreen_.textAttributes.resetColorPalette();
886
rgindab8bc8932012-04-27 12:45:03 -0700887 // The xterm man page explicitly says this will happen on soft reset.
888 this.setVTScrollRegion(null, null);
889
890 // Xterm also shows the cursor on soft reset, but does not alter the blink
891 // state.
rgindaa19afe22012-01-25 15:40:22 -0800892 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800893};
894
rgindac9bc5502012-01-18 11:48:44 -0800895/**
896 * Move the cursor forward to the next tab stop, or to the last column
897 * if no more tab stops are set.
898 */
899hterm.Terminal.prototype.forwardTabStop = function() {
900 var column = this.screen_.cursorPosition.column;
901
902 for (var i = 0; i < this.tabStops_.length; i++) {
903 if (this.tabStops_[i] > column) {
904 this.setCursorColumn(this.tabStops_[i]);
905 return;
906 }
907 }
908
David Benjamin66e954d2012-05-05 21:08:12 -0400909 // xterm does not clear the overflow flag on HT or CHT.
910 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800911 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400912 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800913};
914
rgindac9bc5502012-01-18 11:48:44 -0800915/**
916 * Move the cursor backward to the previous tab stop, or to the first column
917 * if no previous tab stops are set.
918 */
919hterm.Terminal.prototype.backwardTabStop = function() {
920 var column = this.screen_.cursorPosition.column;
921
922 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
923 if (this.tabStops_[i] < column) {
924 this.setCursorColumn(this.tabStops_[i]);
925 return;
926 }
927 }
928
929 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800930};
931
rgindac9bc5502012-01-18 11:48:44 -0800932/**
933 * Set a tab stop at the given column.
934 *
935 * @param {int} column Zero based column.
936 */
937hterm.Terminal.prototype.setTabStop = function(column) {
938 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
939 if (this.tabStops_[i] == column)
940 return;
941
942 if (this.tabStops_[i] < column) {
943 this.tabStops_.splice(i + 1, 0, column);
944 return;
945 }
946 }
947
948 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800949};
950
rgindac9bc5502012-01-18 11:48:44 -0800951/**
952 * Clear the tab stop at the current cursor position.
953 *
954 * No effect if there is no tab stop at the current cursor position.
955 */
956hterm.Terminal.prototype.clearTabStopAtCursor = function() {
957 var column = this.screen_.cursorPosition.column;
958
959 var i = this.tabStops_.indexOf(column);
960 if (i == -1)
961 return;
962
963 this.tabStops_.splice(i, 1);
964};
965
966/**
967 * Clear all tab stops.
968 */
969hterm.Terminal.prototype.clearAllTabStops = function() {
970 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -0400971 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -0800972};
973
974/**
975 * Set up the default tab stops, starting from a given column.
976 *
977 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -0400978 * from the specified column, or 0 if no column is provided. It also flags
979 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -0800980 *
981 * This does not clear the existing tab stops first, use clearAllTabStops
982 * for that.
983 *
984 * @param {int} opt_start Optional starting zero based starting column, useful
985 * for filling out missing tab stops when the terminal is resized.
986 */
987hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
988 var start = opt_start || 0;
989 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -0400990 // Round start up to a default tab stop.
991 start = start - 1 - ((start - 1) % w) + w;
992 for (var i = start; i < this.screenSize.width; i += w) {
993 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -0800994 }
David Benjamin66e954d2012-05-05 21:08:12 -0400995
996 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -0800997};
998
rginda6d397402012-01-17 10:58:29 -0800999/**
rginda8ba33642011-12-14 12:31:31 -08001000 * Interpret a sequence of characters.
1001 *
1002 * Incomplete escape sequences are buffered until the next call.
1003 *
1004 * @param {string} str Sequence of characters to interpret or pass through.
1005 */
1006hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001007 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001008 this.scheduleSyncCursorPosition_();
1009};
1010
1011/**
1012 * Take over the given DIV for use as the terminal display.
1013 *
1014 * @param {HTMLDivElement} div The div to use as the terminal display.
1015 */
1016hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001017 this.div_ = div;
1018
rginda8ba33642011-12-14 12:31:31 -08001019 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001020 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001021 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1022 this.scrollPort_.setBackgroundPosition(
1023 this.prefs_.get('background-position'));
rginda30f20f62012-04-05 16:36:19 -07001024
rginda0918b652012-04-04 11:26:24 -07001025 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001026
rginda9f5222b2012-03-05 11:53:28 -08001027 this.setFontSize(this.prefs_.get('font-size'));
1028 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001029
David Reveman8f552492012-03-28 12:18:41 -04001030 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1031
rginda8ba33642011-12-14 12:31:31 -08001032 this.document_ = this.scrollPort_.getDocument();
1033
rginda4bba5e12012-06-20 16:15:30 -07001034 this.document_.body.oncontextmenu = function() { return false };
1035
1036 var onMouse = this.onMouse_.bind(this);
1037 this.document_.body.firstChild.addEventListener('mousedown', onMouse);
1038 this.document_.body.firstChild.addEventListener('mouseup', onMouse);
1039 this.document_.body.firstChild.addEventListener('mousemove', onMouse);
1040 this.scrollPort_.onScrollWheel = onMouse;
1041
rginda8e92a692012-05-20 19:37:20 -07001042 this.document_.body.firstChild.addEventListener(
1043 'focus', this.onFocusChange_.bind(this, true));
1044 this.document_.body.firstChild.addEventListener(
1045 'blur', this.onFocusChange_.bind(this, false));
1046
1047 var style = this.document_.createElement('style');
1048 style.textContent =
1049 ('.cursor-node[focus="false"] {' +
1050 ' box-sizing: border-box;' +
1051 ' background-color: transparent !important;' +
1052 ' border-width: 2px;' +
1053 ' border-style: solid;' +
1054 '}');
1055 this.document_.head.appendChild(style);
1056
rginda8ba33642011-12-14 12:31:31 -08001057 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001058 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001059 this.cursorNode_.style.cssText =
1060 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001061 'top: -99px;' +
1062 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001063 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1064 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001065 '-webkit-transition: opacity, background-color 100ms linear;');
1066 this.setCursorColor(this.prefs_.get('cursor-color'));
rgindad5613292012-06-19 15:40:37 -07001067
rginda8ba33642011-12-14 12:31:31 -08001068 this.document_.body.appendChild(this.cursorNode_);
1069
rgindad5613292012-06-19 15:40:37 -07001070 // When 'enableMouseDragScroll' is off we reposition this element directly
1071 // under the mouse cursor after a click. This makes Chrome associate
1072 // subsequent mousemove events with the scroll-blocker. Since the
1073 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1074 // events do not cause the scrollport to scroll.
1075 //
1076 // It's a hack, but it's the cleanest way I could find.
1077 this.scrollBlockerNode_ = this.document_.createElement('div');
1078 this.scrollBlockerNode_.style.cssText =
1079 ('position: absolute;' +
1080 'top: -99px;' +
1081 'display: block;' +
1082 'width: 10px;' +
1083 'height: 10px;');
1084 this.document_.body.appendChild(this.scrollBlockerNode_);
1085
1086 var onMouse = this.onMouse_.bind(this);
1087 this.scrollPort_.onScrollWheel = onMouse;
1088 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1089 ].forEach(function(event) {
1090 this.scrollBlockerNode_.addEventListener(event, onMouse);
1091 this.cursorNode_.addEventListener(event, onMouse);
1092 this.document_.addEventListener(event, onMouse);
1093 }.bind(this));
1094
1095 this.cursorNode_.addEventListener('mousedown', function() {
1096 setTimeout(this.focus.bind(this));
1097 }.bind(this));
1098
rgindade84e382012-04-20 15:39:31 -07001099 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda8ba33642011-12-14 12:31:31 -08001100 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001101
rginda87b86462011-12-14 13:48:03 -08001102 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001103 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001104};
1105
rginda0918b652012-04-04 11:26:24 -07001106/**
1107 * Return the HTML document that contains the terminal DOM nodes.
1108 */
rginda87b86462011-12-14 13:48:03 -08001109hterm.Terminal.prototype.getDocument = function() {
1110 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001111};
1112
1113/**
rginda0918b652012-04-04 11:26:24 -07001114 * Focus the terminal.
1115 */
1116hterm.Terminal.prototype.focus = function() {
1117 this.scrollPort_.focus();
1118};
1119
1120/**
rginda8ba33642011-12-14 12:31:31 -08001121 * Return the HTML Element for a given row index.
1122 *
1123 * This is a method from the RowProvider interface. The ScrollPort uses
1124 * it to fetch rows on demand as they are scrolled into view.
1125 *
1126 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1127 * pairs to conserve memory.
1128 *
1129 * @param {integer} index The zero-based row index, measured relative to the
1130 * start of the scrollback buffer. On-screen rows will always have the
1131 * largest indicies.
1132 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1133 */
1134hterm.Terminal.prototype.getRowNode = function(index) {
1135 if (index < this.scrollbackRows_.length)
1136 return this.scrollbackRows_[index];
1137
1138 var screenIndex = index - this.scrollbackRows_.length;
1139 return this.screen_.rowsArray[screenIndex];
1140};
1141
1142/**
1143 * Return the text content for a given range of rows.
1144 *
1145 * This is a method from the RowProvider interface. The ScrollPort uses
1146 * it to fetch text content on demand when the user attempts to copy their
1147 * selection to the clipboard.
1148 *
1149 * @param {integer} start The zero-based row index to start from, measured
1150 * relative to the start of the scrollback buffer. On-screen rows will
1151 * always have the largest indicies.
1152 * @param {integer} end The zero-based row index to end on, measured
1153 * relative to the start of the scrollback buffer.
1154 * @return {string} A single string containing the text value of the range of
1155 * rows. Lines will be newline delimited, with no trailing newline.
1156 */
1157hterm.Terminal.prototype.getRowsText = function(start, end) {
1158 var ary = [];
1159 for (var i = start; i < end; i++) {
1160 var node = this.getRowNode(i);
1161 ary.push(node.textContent);
1162 }
1163
1164 return ary.join('\n');
1165};
1166
1167/**
1168 * Return the text content for a given row.
1169 *
1170 * This is a method from the RowProvider interface. The ScrollPort uses
1171 * it to fetch text content on demand when the user attempts to copy their
1172 * selection to the clipboard.
1173 *
1174 * @param {integer} index The zero-based row index to return, measured
1175 * relative to the start of the scrollback buffer. On-screen rows will
1176 * always have the largest indicies.
1177 * @return {string} A string containing the text value of the selected row.
1178 */
1179hterm.Terminal.prototype.getRowText = function(index) {
1180 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001181 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001182};
1183
1184/**
1185 * Return the total number of rows in the addressable screen and in the
1186 * scrollback buffer of this terminal.
1187 *
1188 * This is a method from the RowProvider interface. The ScrollPort uses
1189 * it to compute the size of the scrollbar.
1190 *
1191 * @return {integer} The number of rows in this terminal.
1192 */
1193hterm.Terminal.prototype.getRowCount = function() {
1194 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1195};
1196
1197/**
1198 * Create DOM nodes for new rows and append them to the end of the terminal.
1199 *
1200 * This is the only correct way to add a new DOM node for a row. Notice that
1201 * the new row is appended to the bottom of the list of rows, and does not
1202 * require renumbering (of the rowIndex property) of previous rows.
1203 *
1204 * If you think you want a new blank row somewhere in the middle of the
1205 * terminal, look into moveRows_().
1206 *
1207 * This method does not pay attention to vtScrollTop/Bottom, since you should
1208 * be using moveRows() in cases where they would matter.
1209 *
1210 * The cursor will be positioned at column 0 of the first inserted line.
1211 */
1212hterm.Terminal.prototype.appendRows_ = function(count) {
1213 var cursorRow = this.screen_.rowsArray.length;
1214 var offset = this.scrollbackRows_.length + cursorRow;
1215 for (var i = 0; i < count; i++) {
1216 var row = this.document_.createElement('x-row');
1217 row.appendChild(this.document_.createTextNode(''));
1218 row.rowIndex = offset + i;
1219 this.screen_.pushRow(row);
1220 }
1221
1222 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1223 if (extraRows > 0) {
1224 var ary = this.screen_.shiftRows(extraRows);
1225 Array.prototype.push.apply(this.scrollbackRows_, ary);
1226 this.scheduleScrollDown_();
1227 }
1228
1229 if (cursorRow >= this.screen_.rowsArray.length)
1230 cursorRow = this.screen_.rowsArray.length - 1;
1231
rginda87b86462011-12-14 13:48:03 -08001232 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001233};
1234
1235/**
1236 * Relocate rows from one part of the addressable screen to another.
1237 *
1238 * This is used to recycle rows during VT scrolls (those which are driven
1239 * by VT commands, rather than by the user manipulating the scrollbar.)
1240 *
1241 * In this case, the blank lines scrolled into the scroll region are made of
1242 * the nodes we scrolled off. These have their rowIndex properties carefully
1243 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001244 */
1245hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1246 var ary = this.screen_.removeRows(fromIndex, count);
1247 this.screen_.insertRows(toIndex, ary);
1248
1249 var start, end;
1250 if (fromIndex < toIndex) {
1251 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001252 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001253 } else {
1254 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001255 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001256 }
1257
1258 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001259 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001260};
1261
1262/**
1263 * Renumber the rowIndex property of the given range of rows.
1264 *
1265 * The start and end indicies are relative to the screen, not the scrollback.
1266 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001267 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001268 * no need to renumber scrollback rows.
1269 */
1270hterm.Terminal.prototype.renumberRows_ = function(start, end) {
1271 var offset = this.scrollbackRows_.length;
1272 for (var i = start; i < end; i++) {
1273 this.screen_.rowsArray[i].rowIndex = offset + i;
1274 }
1275};
1276
1277/**
1278 * Print a string to the terminal.
1279 *
1280 * This respects the current insert and wraparound modes. It will add new lines
1281 * to the end of the terminal, scrolling off the top into the scrollback buffer
1282 * if necessary.
1283 *
1284 * The string is *not* parsed for escape codes. Use the interpret() method if
1285 * that's what you're after.
1286 *
1287 * @param{string} str The string to print.
1288 */
1289hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001290 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001291
rgindaa9abdd82012-08-06 18:05:09 -07001292 while (startOffset < str.length) {
1293 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
rginda35c456b2012-02-09 17:29:05 -08001294 this.newLine();
rgindaa19afe22012-01-25 15:40:22 -08001295
rgindaa9abdd82012-08-06 18:05:09 -07001296 var count = str.length - startOffset;
1297 var didOverflow = false;
1298 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001299
rgindaa9abdd82012-08-06 18:05:09 -07001300 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1301 didOverflow = true;
1302 count = this.screenSize.width - this.screen_.cursorPosition.column;
1303 }
rgindaa19afe22012-01-25 15:40:22 -08001304
rgindaa9abdd82012-08-06 18:05:09 -07001305 if (didOverflow && !this.options_.wraparound) {
1306 // If the string overflowed the line but wraparound is off, then the
1307 // last printed character should be the last of the string.
1308 // TODO: This will add to our problems with multibyte UTF-16 characters.
1309 substr = str.substr(startOffset, count - 1) +
1310 str.substr(str.length - 1);
1311 count = str.length;
1312 } else {
1313 substr = str.substr(startOffset, count);
1314 }
rgindaa19afe22012-01-25 15:40:22 -08001315
rgindaa9abdd82012-08-06 18:05:09 -07001316 if (this.options_.insertMode) {
1317 this.screen_.insertString(substr);
1318 } else {
1319 this.screen_.overwriteString(substr);
1320 }
1321
1322 this.screen_.maybeClipCurrentRow();
1323 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001324 }
rginda8ba33642011-12-14 12:31:31 -08001325
1326 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001327
rginda9f5222b2012-03-05 11:53:28 -08001328 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001329 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001330};
1331
1332/**
rginda87b86462011-12-14 13:48:03 -08001333 * Set the VT scroll region.
1334 *
rginda87b86462011-12-14 13:48:03 -08001335 * This also resets the cursor position to the absolute (0, 0) position, since
1336 * that's what xterm appears to do.
1337 *
1338 * @param {integer} scrollTop The zero-based top of the scroll region.
1339 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1340 * inclusive.
1341 */
1342hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1343 this.vtScrollTop_ = scrollTop;
1344 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001345};
1346
1347/**
rginda8ba33642011-12-14 12:31:31 -08001348 * Return the top row index according to the VT.
1349 *
1350 * This will return 0 unless the terminal has been told to restrict scrolling
1351 * to some lower row. It is used for some VT cursor positioning and scrolling
1352 * commands.
1353 *
1354 * @return {integer} The topmost row in the terminal's scroll region.
1355 */
1356hterm.Terminal.prototype.getVTScrollTop = function() {
1357 if (this.vtScrollTop_ != null)
1358 return this.vtScrollTop_;
1359
1360 return 0;
rginda87b86462011-12-14 13:48:03 -08001361};
rginda8ba33642011-12-14 12:31:31 -08001362
1363/**
1364 * Return the bottom row index according to the VT.
1365 *
1366 * This will return the height of the terminal unless the it has been told to
1367 * restrict scrolling to some higher row. It is used for some VT cursor
1368 * positioning and scrolling commands.
1369 *
1370 * @return {integer} The bottommost row in the terminal's scroll region.
1371 */
1372hterm.Terminal.prototype.getVTScrollBottom = function() {
1373 if (this.vtScrollBottom_ != null)
1374 return this.vtScrollBottom_;
1375
rginda87b86462011-12-14 13:48:03 -08001376 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001377}
1378
1379/**
1380 * Process a '\n' character.
1381 *
1382 * If the cursor is on the final row of the terminal this will append a new
1383 * blank row to the screen and scroll the topmost row into the scrollback
1384 * buffer.
1385 *
1386 * Otherwise, this moves the cursor to column zero of the next row.
1387 */
1388hterm.Terminal.prototype.newLine = function() {
1389 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001390 // If we're at the end of the screen we need to append a new line and
1391 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001392 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001393 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1394 // End of the scroll region does not affect the scrollback buffer.
1395 this.vtScrollUp(1);
1396 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001397 } else {
rginda87b86462011-12-14 13:48:03 -08001398 // Anywhere else in the screen just moves the cursor.
1399 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001400 }
1401};
1402
1403/**
1404 * Like newLine(), except maintain the cursor column.
1405 */
1406hterm.Terminal.prototype.lineFeed = function() {
1407 var column = this.screen_.cursorPosition.column;
1408 this.newLine();
1409 this.setCursorColumn(column);
1410};
1411
1412/**
rginda87b86462011-12-14 13:48:03 -08001413 * If autoCarriageReturn is set then newLine(), else lineFeed().
1414 */
1415hterm.Terminal.prototype.formFeed = function() {
1416 if (this.options_.autoCarriageReturn) {
1417 this.newLine();
1418 } else {
1419 this.lineFeed();
1420 }
1421};
1422
1423/**
1424 * Move the cursor up one row, possibly inserting a blank line.
1425 *
1426 * The cursor column is not changed.
1427 */
1428hterm.Terminal.prototype.reverseLineFeed = function() {
1429 var scrollTop = this.getVTScrollTop();
1430 var currentRow = this.screen_.cursorPosition.row;
1431
1432 if (currentRow == scrollTop) {
1433 this.insertLines(1);
1434 } else {
1435 this.setAbsoluteCursorRow(currentRow - 1);
1436 }
1437};
1438
1439/**
rginda8ba33642011-12-14 12:31:31 -08001440 * Replace all characters to the left of the current cursor with the space
1441 * character.
1442 *
1443 * TODO(rginda): This should probably *remove* the characters (not just replace
1444 * with a space) if there are no characters at or beyond the current cursor
1445 * position. Once it does that, it'll have the same text-attribute related
1446 * issues as hterm.Screen.prototype.clearCursorRow :/
1447 */
1448hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001449 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001450 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001451 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001452 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001453};
1454
1455/**
David Benjamin684a9b72012-05-01 17:19:58 -04001456 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001457 *
1458 * The cursor position is unchanged.
1459 *
1460 * TODO(rginda): Test that this works even when the cursor is positioned beyond
1461 * the end of the text.
1462 *
1463 * TODO(rginda): This likely has text-attribute related troubles similar to the
1464 * todo on hterm.Screen.prototype.clearCursorRow.
David Benjamin684a9b72012-05-01 17:19:58 -04001465 *
1466 * TODO(davidben): Probably better to not add the whitespace to the clipboard
1467 * if erasing to the end of the drawn portion of the line. That said, xterm
1468 * behaves the same here.
rginda8ba33642011-12-14 12:31:31 -08001469 */
1470hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001471 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001472
rginda87b86462011-12-14 13:48:03 -08001473 var maxCount = this.screenSize.width - cursor.column;
David Benjamin684a9b72012-05-01 17:19:58 -04001474 if (opt_count === undefined || opt_count >= maxCount) {
1475 this.screen_.deleteChars(maxCount);
1476 } else {
rgindacbbd7482012-06-13 15:06:16 -07001477 this.screen_.overwriteString(lib.f.getWhitespace(opt_count));
David Benjamin684a9b72012-05-01 17:19:58 -04001478 }
rginda87b86462011-12-14 13:48:03 -08001479 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001480 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001481};
1482
1483/**
1484 * Erase the current line.
1485 *
1486 * The cursor position is unchanged.
1487 *
1488 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1489 * has a text-attribute related TODO.
1490 */
1491hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001492 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001493 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001494 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001495 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001496};
1497
1498/**
David Benjamina08d78f2012-05-05 00:28:49 -04001499 * Erase all characters from the start of the screen to the current cursor
1500 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001501 *
1502 * The cursor position is unchanged.
1503 *
1504 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1505 * has a text-attribute related TODO.
1506 */
1507hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001508 var cursor = this.saveCursor();
1509
1510 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001511
David Benjamina08d78f2012-05-05 00:28:49 -04001512 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001513 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001514 this.screen_.clearCursorRow();
1515 }
1516
rginda87b86462011-12-14 13:48:03 -08001517 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001518 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001519};
1520
1521/**
1522 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001523 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001524 *
1525 * The cursor position is unchanged.
1526 *
1527 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1528 * has a text-attribute related TODO.
1529 */
1530hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001531 var cursor = this.saveCursor();
1532
1533 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001534
David Benjamina08d78f2012-05-05 00:28:49 -04001535 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001536 for (var i = cursor.row + 1; i <= bottom; i++) {
1537 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001538 this.screen_.clearCursorRow();
1539 }
1540
rginda87b86462011-12-14 13:48:03 -08001541 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001542 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001543};
1544
1545/**
1546 * Fill the terminal with a given character.
1547 *
1548 * This methods does not respect the VT scroll region.
1549 *
1550 * @param {string} ch The character to use for the fill.
1551 */
1552hterm.Terminal.prototype.fill = function(ch) {
1553 var cursor = this.saveCursor();
1554
1555 this.setAbsoluteCursorPosition(0, 0);
1556 for (var row = 0; row < this.screenSize.height; row++) {
1557 for (var col = 0; col < this.screenSize.width; col++) {
1558 this.setAbsoluteCursorPosition(row, col);
1559 this.screen_.overwriteString(ch);
1560 }
1561 }
1562
1563 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001564};
1565
1566/**
rginda9ea433c2012-03-16 11:57:00 -07001567 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001568 *
rginda9ea433c2012-03-16 11:57:00 -07001569 * This does not respect the scroll region.
1570 *
1571 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1572 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001573 *
1574 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1575 * has a text-attribute related TODO.
1576 */
rginda9ea433c2012-03-16 11:57:00 -07001577hterm.Terminal.prototype.clearHome = function(opt_screen) {
1578 var screen = opt_screen || this.screen_;
1579 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001580
rginda11057d52012-04-25 12:29:56 -07001581 if (bottom == 0) {
1582 // Empty screen, nothing to do.
1583 return;
1584 }
1585
rgindae4d29232012-01-19 10:47:13 -08001586 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001587 screen.setCursorPosition(i, 0);
1588 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001589 }
1590
rginda9ea433c2012-03-16 11:57:00 -07001591 screen.setCursorPosition(0, 0);
1592};
1593
1594/**
1595 * Erase the entire display without changing the cursor position.
1596 *
1597 * The cursor position is unchanged. This does not respect the scroll
1598 * region.
1599 *
1600 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1601 * to the current screen.
1602 *
1603 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1604 * has a text-attribute related TODO.
1605 */
1606hterm.Terminal.prototype.clear = function(opt_screen) {
1607 var screen = opt_screen || this.screen_;
1608 var cursor = screen.cursorPosition.clone();
1609 this.clearHome(screen);
1610 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001611};
1612
1613/**
1614 * VT command to insert lines at the current cursor row.
1615 *
1616 * This respects the current scroll region. Rows pushed off the bottom are
1617 * lost (they won't show up in the scrollback buffer).
1618 *
1619 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1620 * has a text-attribute related TODO.
1621 *
1622 * @param {integer} count The number of lines to insert.
1623 */
1624hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001625 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001626
1627 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001628 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001629
rgindae4d29232012-01-19 10:47:13 -08001630 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001631 if (start != cursor.row)
1632 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001633
1634 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001635 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001636 this.screen_.clearCursorRow();
1637 }
1638
rginda87b86462011-12-14 13:48:03 -08001639 cursor.column = 0;
1640 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001641};
1642
1643/**
1644 * VT command to delete lines at the current cursor row.
1645 *
1646 * New rows are added to the bottom of scroll region to take their place. New
1647 * rows are strictly there to take up space and have no content or style.
1648 */
1649hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001650 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001651
rginda87b86462011-12-14 13:48:03 -08001652 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001653 var bottom = this.getVTScrollBottom();
1654
rginda87b86462011-12-14 13:48:03 -08001655 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001656 count = Math.min(count, maxCount);
1657
rginda87b86462011-12-14 13:48:03 -08001658 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001659 if (count != maxCount)
1660 this.moveRows_(top, count, moveStart);
1661
1662 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001663 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001664 this.screen_.clearCursorRow();
1665 }
1666
rginda87b86462011-12-14 13:48:03 -08001667 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001668 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001669};
1670
1671/**
1672 * Inserts the given number of spaces at the current cursor position.
1673 *
rginda87b86462011-12-14 13:48:03 -08001674 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001675 */
1676hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001677 var cursor = this.saveCursor();
1678
rgindacbbd7482012-06-13 15:06:16 -07001679 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001680 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001681 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001682
1683 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001684 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001685};
1686
1687/**
1688 * Forward-delete the specified number of characters starting at the cursor
1689 * position.
1690 *
1691 * @param {integer} count The number of characters to delete.
1692 */
1693hterm.Terminal.prototype.deleteChars = function(count) {
1694 this.screen_.deleteChars(count);
David Benjamin54e8bf62012-06-01 22:31:40 -04001695 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001696};
1697
1698/**
1699 * Shift rows in the scroll region upwards by a given number of lines.
1700 *
1701 * New rows are inserted at the bottom of the scroll region to fill the
1702 * vacated rows. The new rows not filled out with the current text attributes.
1703 *
1704 * This function does not affect the scrollback rows at all. Rows shifted
1705 * off the top are lost.
1706 *
rginda87b86462011-12-14 13:48:03 -08001707 * The cursor position is not altered.
1708 *
rginda8ba33642011-12-14 12:31:31 -08001709 * @param {integer} count The number of rows to scroll.
1710 */
1711hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001712 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001713
rginda87b86462011-12-14 13:48:03 -08001714 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001715 this.deleteLines(count);
1716
rginda87b86462011-12-14 13:48:03 -08001717 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001718};
1719
1720/**
1721 * Shift rows below the cursor down by a given number of lines.
1722 *
1723 * This function respects the current scroll region.
1724 *
1725 * New rows are inserted at the top of the scroll region to fill the
1726 * vacated rows. The new rows not filled out with the current text attributes.
1727 *
1728 * This function does not affect the scrollback rows at all. Rows shifted
1729 * off the bottom are lost.
1730 *
1731 * @param {integer} count The number of rows to scroll.
1732 */
1733hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001734 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001735
rginda87b86462011-12-14 13:48:03 -08001736 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001737 this.insertLines(opt_count);
1738
rginda87b86462011-12-14 13:48:03 -08001739 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001740};
1741
rginda87b86462011-12-14 13:48:03 -08001742
rginda8ba33642011-12-14 12:31:31 -08001743/**
1744 * Set the cursor position.
1745 *
1746 * The cursor row is relative to the scroll region if the terminal has
1747 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1748 *
1749 * @param {integer} row The new zero-based cursor row.
1750 * @param {integer} row The new zero-based cursor column.
1751 */
1752hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1753 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001754 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001755 } else {
rginda87b86462011-12-14 13:48:03 -08001756 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001757 }
rginda87b86462011-12-14 13:48:03 -08001758};
rginda8ba33642011-12-14 12:31:31 -08001759
rginda87b86462011-12-14 13:48:03 -08001760hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1761 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001762 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1763 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001764 this.screen_.setCursorPosition(row, column);
1765};
1766
1767hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001768 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1769 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001770 this.screen_.setCursorPosition(row, column);
1771};
1772
1773/**
1774 * Set the cursor column.
1775 *
1776 * @param {integer} column The new zero-based cursor column.
1777 */
1778hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001779 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001780};
1781
1782/**
1783 * Return the cursor column.
1784 *
1785 * @return {integer} The zero-based cursor column.
1786 */
1787hterm.Terminal.prototype.getCursorColumn = function() {
1788 return this.screen_.cursorPosition.column;
1789};
1790
1791/**
1792 * Set the cursor row.
1793 *
1794 * The cursor row is relative to the scroll region if the terminal has
1795 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1796 *
1797 * @param {integer} row The new cursor row.
1798 */
rginda87b86462011-12-14 13:48:03 -08001799hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1800 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001801};
1802
1803/**
1804 * Return the cursor row.
1805 *
1806 * @return {integer} The zero-based cursor row.
1807 */
1808hterm.Terminal.prototype.getCursorRow = function(row) {
1809 return this.screen_.cursorPosition.row;
1810};
1811
1812/**
1813 * Request that the ScrollPort redraw itself soon.
1814 *
1815 * The redraw will happen asynchronously, soon after the call stack winds down.
1816 * Multiple calls will be coalesced into a single redraw.
1817 */
1818hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001819 if (this.timeouts_.redraw)
1820 return;
rginda8ba33642011-12-14 12:31:31 -08001821
1822 var self = this;
rginda87b86462011-12-14 13:48:03 -08001823 this.timeouts_.redraw = setTimeout(function() {
1824 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001825 self.scrollPort_.redraw_();
1826 }, 0);
1827};
1828
1829/**
1830 * Request that the ScrollPort be scrolled to the bottom.
1831 *
1832 * The scroll will happen asynchronously, soon after the call stack winds down.
1833 * Multiple calls will be coalesced into a single scroll.
1834 *
1835 * This affects the scrollbar position of the ScrollPort, and has nothing to
1836 * do with the VT scroll commands.
1837 */
1838hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1839 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001840 return;
rginda8ba33642011-12-14 12:31:31 -08001841
1842 var self = this;
1843 this.timeouts_.scrollDown = setTimeout(function() {
1844 delete self.timeouts_.scrollDown;
1845 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1846 }, 10);
1847};
1848
1849/**
1850 * Move the cursor up a specified number of rows.
1851 *
1852 * @param {integer} count The number of rows to move the cursor.
1853 */
1854hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001855 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001856};
1857
1858/**
1859 * Move the cursor down a specified number of rows.
1860 *
1861 * @param {integer} count The number of rows to move the cursor.
1862 */
1863hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001864 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001865 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1866 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1867 this.screenSize.height - 1);
1868
rgindacbbd7482012-06-13 15:06:16 -07001869 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001870 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001871 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001872};
1873
1874/**
1875 * Move the cursor left a specified number of columns.
1876 *
1877 * @param {integer} count The number of columns to move the cursor.
1878 */
1879hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001880 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001881};
1882
1883/**
1884 * Move the cursor right a specified number of columns.
1885 *
1886 * @param {integer} count The number of columns to move the cursor.
1887 */
1888hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001889 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001890 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001891 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001892 this.setCursorColumn(column);
1893};
1894
1895/**
1896 * Reverse the foreground and background colors of the terminal.
1897 *
1898 * This only affects text that was drawn with no attributes.
1899 *
1900 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1901 * been drawn with attributes that happen to coincide with the default
1902 * 'no-attribute' colors. My guess is probably not.
1903 */
1904hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001905 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001906 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001907 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1908 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001909 } else {
rginda9f5222b2012-03-05 11:53:28 -08001910 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1911 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001912 }
1913};
1914
1915/**
rginda87b86462011-12-14 13:48:03 -08001916 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001917 */
1918hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001919 if (this.bellAudio_.getAttribute('src'))
1920 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001921
rginda6d397402012-01-17 10:58:29 -08001922 this.cursorNode_.style.backgroundColor =
1923 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001924
1925 var self = this;
1926 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001927 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001928 }, 200);
rginda87b86462011-12-14 13:48:03 -08001929};
1930
1931/**
rginda8ba33642011-12-14 12:31:31 -08001932 * Set the origin mode bit.
1933 *
1934 * If origin mode is on, certain VT cursor and scrolling commands measure their
1935 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1936 * to the top of the addressable screen.
1937 *
1938 * Defaults to off.
1939 *
1940 * @param {boolean} state True to set origin mode, false to unset.
1941 */
1942hterm.Terminal.prototype.setOriginMode = function(state) {
1943 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001944 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001945};
1946
1947/**
1948 * Set the insert mode bit.
1949 *
1950 * If insert mode is on, existing text beyond the cursor position will be
1951 * shifted right to make room for new text. Otherwise, new text overwrites
1952 * any existing text.
1953 *
1954 * Defaults to off.
1955 *
1956 * @param {boolean} state True to set insert mode, false to unset.
1957 */
1958hterm.Terminal.prototype.setInsertMode = function(state) {
1959 this.options_.insertMode = state;
1960};
1961
1962/**
rginda87b86462011-12-14 13:48:03 -08001963 * Set the auto carriage return bit.
1964 *
1965 * If auto carriage return is on then a formfeed character is interpreted
1966 * as a newline, otherwise it's the same as a linefeed. The difference boils
1967 * down to whether or not the cursor column is reset.
1968 */
1969hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1970 this.options_.autoCarriageReturn = state;
1971};
1972
1973/**
rginda8ba33642011-12-14 12:31:31 -08001974 * Set the wraparound mode bit.
1975 *
1976 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1977 * to the start of the following row. Otherwise, the cursor is clamped to the
1978 * end of the screen and attempts to write past it are ignored.
1979 *
1980 * Defaults to on.
1981 *
1982 * @param {boolean} state True to set wraparound mode, false to unset.
1983 */
1984hterm.Terminal.prototype.setWraparound = function(state) {
1985 this.options_.wraparound = state;
1986};
1987
1988/**
1989 * Set the reverse-wraparound mode bit.
1990 *
1991 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1992 * to the end of the previous row. Otherwise, the cursor is clamped to column
1993 * 0.
1994 *
1995 * Defaults to off.
1996 *
1997 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1998 */
1999hterm.Terminal.prototype.setReverseWraparound = function(state) {
2000 this.options_.reverseWraparound = state;
2001};
2002
2003/**
2004 * Selects between the primary and alternate screens.
2005 *
2006 * If alternate mode is on, the alternate screen is active. Otherwise the
2007 * primary screen is active.
2008 *
2009 * Swapping screens has no effect on the scrollback buffer.
2010 *
2011 * Each screen maintains its own cursor position.
2012 *
2013 * Defaults to off.
2014 *
2015 * @param {boolean} state True to set alternate mode, false to unset.
2016 */
2017hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002018 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002019 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2020
rginda35c456b2012-02-09 17:29:05 -08002021 if (this.screen_.rowsArray.length &&
2022 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2023 // If the screen changed sizes while we were away, our rowIndexes may
2024 // be incorrect.
2025 var offset = this.scrollbackRows_.length;
2026 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002027 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002028 ary[i].rowIndex = offset + i;
2029 }
2030 }
rginda8ba33642011-12-14 12:31:31 -08002031
rginda35c456b2012-02-09 17:29:05 -08002032 this.realizeWidth_(this.screenSize.width);
2033 this.realizeHeight_(this.screenSize.height);
2034 this.scrollPort_.syncScrollHeight();
2035 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002036
rginda6d397402012-01-17 10:58:29 -08002037 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002038 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002039};
2040
2041/**
2042 * Set the cursor-blink mode bit.
2043 *
2044 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2045 * a visible cursor does not blink.
2046 *
2047 * You should make sure to turn blinking off if you're going to dispose of a
2048 * terminal, otherwise you'll leak a timeout.
2049 *
2050 * Defaults to on.
2051 *
2052 * @param {boolean} state True to set cursor-blink mode, false to unset.
2053 */
2054hterm.Terminal.prototype.setCursorBlink = function(state) {
2055 this.options_.cursorBlink = state;
2056
2057 if (!state && this.timeouts_.cursorBlink) {
2058 clearTimeout(this.timeouts_.cursorBlink);
2059 delete this.timeouts_.cursorBlink;
2060 }
2061
2062 if (this.options_.cursorVisible)
2063 this.setCursorVisible(true);
2064};
2065
2066/**
2067 * Set the cursor-visible mode bit.
2068 *
2069 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2070 *
2071 * Defaults to on.
2072 *
2073 * @param {boolean} state True to set cursor-visible mode, false to unset.
2074 */
2075hterm.Terminal.prototype.setCursorVisible = function(state) {
2076 this.options_.cursorVisible = state;
2077
2078 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002079 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002080 return;
2081 }
2082
rginda87b86462011-12-14 13:48:03 -08002083 this.syncCursorPosition_();
2084
2085 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002086
2087 if (this.options_.cursorBlink) {
2088 if (this.timeouts_.cursorBlink)
2089 return;
2090
2091 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2092 500);
2093 } else {
2094 if (this.timeouts_.cursorBlink) {
2095 clearTimeout(this.timeouts_.cursorBlink);
2096 delete this.timeouts_.cursorBlink;
2097 }
2098 }
2099};
2100
2101/**
rginda87b86462011-12-14 13:48:03 -08002102 * Synchronizes the visible cursor and document selection with the current
2103 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002104 */
2105hterm.Terminal.prototype.syncCursorPosition_ = function() {
2106 var topRowIndex = this.scrollPort_.getTopRowIndex();
2107 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2108 var cursorRowIndex = this.scrollbackRows_.length +
2109 this.screen_.cursorPosition.row;
2110
2111 if (cursorRowIndex > bottomRowIndex) {
2112 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002113 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002114 return;
2115 }
2116
rginda35c456b2012-02-09 17:29:05 -08002117 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
2118 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
2119
rginda8ba33642011-12-14 12:31:31 -08002120 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002121 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2122 'px';
2123 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2124 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002125
2126 this.cursorNode_.setAttribute('title',
2127 '(' + this.screen_.cursorPosition.row +
2128 ', ' + this.screen_.cursorPosition.column +
2129 ')');
2130
2131 // Update the caret for a11y purposes.
2132 var selection = this.document_.getSelection();
2133 if (selection && selection.isCollapsed)
2134 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002135};
2136
2137/**
2138 * Synchronizes the visible cursor with the current cursor coordinates.
2139 *
2140 * The sync will happen asynchronously, soon after the call stack winds down.
2141 * Multiple calls will be coalesced into a single sync.
2142 */
2143hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2144 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002145 return;
rginda8ba33642011-12-14 12:31:31 -08002146
2147 var self = this;
2148 this.timeouts_.syncCursor = setTimeout(function() {
2149 self.syncCursorPosition_();
2150 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002151 }, 0);
2152};
2153
rgindacc2996c2012-02-24 14:59:31 -08002154/**
rgindaf522ce02012-04-17 17:49:17 -07002155 * Show or hide the zoom warning.
2156 *
2157 * The zoom warning is a message warning the user that their browser zoom must
2158 * be set to 100% in order for hterm to function properly.
2159 *
2160 * @param {boolean} state True to show the message, false to hide it.
2161 */
2162hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2163 if (!this.zoomWarningNode_) {
2164 if (!state)
2165 return;
2166
2167 this.zoomWarningNode_ = this.document_.createElement('div');
2168 this.zoomWarningNode_.style.cssText = (
2169 'color: black;' +
2170 'background-color: #ff2222;' +
2171 'font-size: large;' +
2172 'border-radius: 8px;' +
2173 'opacity: 0.75;' +
2174 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2175 'top: 0.5em;' +
2176 'right: 1.2em;' +
2177 'position: absolute;' +
2178 '-webkit-text-size-adjust: none;' +
2179 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002180 }
2181
rgindade84e382012-04-20 15:39:31 -07002182 this.zoomWarningNode_.textContent = hterm.msg('ZOOM_WARNING') ||
2183 ('!! ' + parseInt(this.scrollPort_.characterSize.zoomFactor * 100) +
2184 '% !!');
rgindaf522ce02012-04-17 17:49:17 -07002185 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2186
2187 if (state) {
2188 if (!this.zoomWarningNode_.parentNode)
2189 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2190 } else if (this.zoomWarningNode_.parentNode) {
2191 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2192 }
2193};
2194
2195/**
rgindacc2996c2012-02-24 14:59:31 -08002196 * Show the terminal overlay for a given amount of time.
2197 *
2198 * The terminal overlay appears in inverse video in a large font, centered
2199 * over the terminal. You should probably keep the overlay message brief,
2200 * since it's in a large font and you probably aren't going to check the size
2201 * of the terminal first.
2202 *
2203 * @param {string} msg The text (not HTML) message to display in the overlay.
2204 * @param {number} opt_timeout The amount of time to wait before fading out
2205 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2206 * stay up forever (or until the next overlay).
2207 */
2208hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002209 if (!this.overlayNode_) {
2210 if (!this.div_)
2211 return;
2212
2213 this.overlayNode_ = this.document_.createElement('div');
2214 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002215 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002216 'font-size: xx-large;' +
2217 'opacity: 0.75;' +
2218 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2219 'position: absolute;' +
2220 '-webkit-user-select: none;' +
2221 '-webkit-transition: opacity 180ms ease-in;');
2222 }
2223
rginda9f5222b2012-03-05 11:53:28 -08002224 this.overlayNode_.style.color = this.prefs_.get('background-color');
2225 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2226 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2227
rgindaf0090c92012-02-10 14:58:52 -08002228 this.overlayNode_.textContent = msg;
2229 this.overlayNode_.style.opacity = '0.75';
2230
2231 if (!this.overlayNode_.parentNode)
2232 this.div_.appendChild(this.overlayNode_);
2233
2234 this.overlayNode_.style.top = (
2235 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
2236 this.overlayNode_.style.left = (
2237 this.div_.clientWidth - this.overlayNode_.clientWidth -
2238 this.scrollbarWidthPx) / 2;
2239
2240 var self = this;
2241
2242 if (this.overlayTimeout_)
2243 clearTimeout(this.overlayTimeout_);
2244
rgindacc2996c2012-02-24 14:59:31 -08002245 if (opt_timeout === null)
2246 return;
2247
rgindaf0090c92012-02-10 14:58:52 -08002248 this.overlayTimeout_ = setTimeout(function() {
2249 self.overlayNode_.style.opacity = '0';
2250 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002251 if (self.overlayNode_.parentNode)
2252 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002253 self.overlayTimeout_ = null;
2254 self.overlayNode_.style.opacity = '0.75';
2255 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002256 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002257};
2258
rginda4bba5e12012-06-20 16:15:30 -07002259/**
2260 * Paste from the system clipboard to the terminal.
2261 */
2262hterm.Terminal.prototype.paste = function() {
2263 hterm.pasteFromClipboard(this.document_);
2264};
2265
2266/**
2267 * Copy a string to the system clipboard.
2268 *
2269 * Note: If there is a selected range in the terminal, it'll be cleared.
2270 */
2271hterm.Terminal.prototype.copyStringToClipboard = function(str) {
2272 var copySource = this.document_.createElement('div');
2273 copySource.textContent = str;
2274 copySource.style.cssText = (
2275 '-webkit-user-select: text;' +
2276 'position: absolute;' +
2277 'top: -99px');
2278
2279 this.document_.body.appendChild(copySource);
2280 var selection = this.document_.getSelection();
2281 selection.selectAllChildren(copySource);
2282
2283 this.copySelectionToClipboard();
2284
2285 copySource.parentNode.removeChild(copySource);
2286};
2287
2288/**
2289 * Copy the current selection to the system clipboard, then clear it after a
2290 * short delay.
2291 */
2292hterm.Terminal.prototype.copySelectionToClipboard = function() {
2293 hterm.copySelectionToClipboard(this.document_);
2294 this.showOverlay(hterm.msg('NOTIFY_COPY'), 500);
2295
2296 if (this.copyTimeout_)
2297 clearTimeout(this.copyTimeout_);
2298
2299 this.copyTimeout_ = setTimeout(function() {
2300 var selection = this.document_.getSelection();
2301 if (!selection.isCollapsed)
2302 selection.collapseToEnd();
2303 this.copyTimeout_ = null;
2304 }.bind(this), 500);
2305};
2306
rgindaf0090c92012-02-10 14:58:52 -08002307hterm.Terminal.prototype.overlaySize = function() {
2308 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2309};
2310
rginda87b86462011-12-14 13:48:03 -08002311/**
2312 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2313 *
2314 * @param {string} string The VT string representing the keystroke.
2315 */
2316hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002317 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002318 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2319
2320 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08002321};
2322
2323/**
rgindad5613292012-06-19 15:40:37 -07002324 * Add the terminalRow and terminalColumn properties to mouse events and
2325 * then forward on to onMouse().
2326 *
2327 * The terminalRow and terminalColumn properties contain the (row, column)
2328 * coordinates for the mouse event.
2329 */
2330hterm.Terminal.prototype.onMouse_ = function(e) {
rginda4bba5e12012-06-20 16:15:30 -07002331 if (e.type == 'mousedown' && e.which == this.mousePasteButton) {
2332 this.paste();
2333 return;
2334 }
2335
2336 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2337 !this.document_.getSelection().isCollapsed) {
2338 this.copySelectionToClipboard();
2339 return;
2340 }
2341
rgindad5613292012-06-19 15:40:37 -07002342 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2343 this.scrollPort_.characterSize.height) + 1;
2344 e.terminalColumn = parseInt(e.clientX /
2345 this.scrollPort_.characterSize.width) + 1;
2346
2347 if (e.type == 'mousedown') {
2348 if (e.terminalColumn > this.screenSize.width) {
2349 // Mousedown in the scrollbar area.
2350 return;
2351 }
2352
2353 if (!this.enableMouseDragScroll) {
2354 // Move the scroll-blocker into place if we want to keep the scrollport
2355 // from scrolling.
2356 this.scrollBlockerNode_.engaged = true;
2357 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2358 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2359 }
2360 } else if (this.scrollBlockerNode_.engaged &&
2361 (e.type == 'mousemove' || e.type == 'mouseup')) {
2362 // Disengage the scroll-blocker after one of these events.
2363 this.scrollBlockerNode_.engaged = false;
2364 this.scrollBlockerNode_.style.top = '-99px';
2365 }
2366
2367 if (!e.processedByTerminalHandler_) {
2368 // We register our event handlers on the document, as well as the cursor
2369 // and the scroll blocker. Mouse events that occur on the cursor or
2370 // scroll blocker will also appear on the document, but we don't want to
2371 // process them twice.
2372 //
2373 // We can't just prevent bubbling because that has other side effects, so
2374 // we decorate the event object with this property instead.
2375 e.processedByTerminalHandler_ = true;
2376
2377 this.onMouse(e);
2378 }
2379};
2380
2381/**
2382 * Clients should override this if they care to know about mouse events.
2383 *
2384 * The event parameter will be a normal DOM mouse click event with additional
2385 * 'terminalRow' and 'terminalColumn' properties.
2386 */
2387hterm.Terminal.prototype.onMouse = function(e) { };
2388
2389/**
rginda8e92a692012-05-20 19:37:20 -07002390 * React when focus changes.
2391 */
2392hterm.Terminal.prototype.onFocusChange_ = function(state) {
2393 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
2394};
2395
2396/**
rginda8ba33642011-12-14 12:31:31 -08002397 * React when the ScrollPort is scrolled.
2398 */
2399hterm.Terminal.prototype.onScroll_ = function() {
2400 this.scheduleSyncCursorPosition_();
2401};
2402
2403/**
rginda9846e2f2012-01-27 13:53:33 -08002404 * React when text is pasted into the scrollPort.
2405 */
2406hterm.Terminal.prototype.onPaste_ = function(e) {
David Benjamin8f962172012-07-17 07:38:43 -04002407 this.io.onVTKeystroke(this.vt.encodeUTF8(e.text));
rginda9846e2f2012-01-27 13:53:33 -08002408};
2409
2410/**
rginda8ba33642011-12-14 12:31:31 -08002411 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002412 *
2413 * Note: This function should not directly contain code that alters the internal
2414 * state of the terminal. That kind of code belongs in realizeWidth or
2415 * realizeHeight, so that it can be executed synchronously in the case of a
2416 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002417 */
2418hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002419 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002420 this.scrollPort_.characterSize.width);
2421 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2422 this.scrollPort_.characterSize.height);
2423
2424 if (!(columnCount || rowCount)) {
2425 // We avoid these situations since they happen sometimes when the terminal
2426 // gets removed from the document, and we can't deal with that.
2427 return;
2428 }
2429
rgindaa8ba17d2012-08-15 14:41:10 -07002430 var isNewSize = (columnCount != this.screenSize.width ||
2431 rowCount != this.screenSize.height);
2432
2433 // We do this even if the size didn't change, just to be sure everything is
2434 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002435 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002436 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002437
2438 if (isNewSize)
2439 this.overlaySize();
2440
2441 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002442};
2443
2444/**
2445 * Service the cursor blink timeout.
2446 */
2447hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002448 if (this.cursorNode_.style.opacity == '0') {
2449 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002450 } else {
rginda87b86462011-12-14 13:48:03 -08002451 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002452 }
2453};
David Reveman8f552492012-03-28 12:18:41 -04002454
2455/**
2456 * Set the scrollbar-visible mode bit.
2457 *
2458 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2459 * Otherwise it will not.
2460 *
2461 * Defaults to on.
2462 *
2463 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2464 */
2465hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2466 this.scrollPort_.setScrollbarVisible(state);
2467};