blob: 7c2df579def0aa5d92f4a2a846dc9d92132e5600 [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');
rginda87b86462011-12-14 13:48:03 -0800106
rgindafeaf3142012-01-31 15:14:20 -0800107 // The keyboard hander.
108 this.keyboard = new hterm.Keyboard(this);
109
rginda87b86462011-12-14 13:48:03 -0800110 // General IO interface that can be given to third parties without exposing
111 // the entire terminal object.
112 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800113
rgindad5613292012-06-19 15:40:37 -0700114 // True if mouse-click-drag should scroll the terminal.
115 this.enableMouseDragScroll = true;
116
rginda4bba5e12012-06-20 16:15:30 -0700117 this.copyOnSelect = this.prefs_.get('copy-on-select');
118 this.mousePasteButton = null;
119 this.syncMousePasteButton();
120
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400121 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800122 this.setDefaultTabStops();
rginda87b86462011-12-14 13:48:03 -0800123};
124
125/**
rginda35c456b2012-02-09 17:29:05 -0800126 * Default tab with of 8 to match xterm.
127 */
128hterm.Terminal.prototype.tabWidth = 8;
129
130/**
rginda35c456b2012-02-09 17:29:05 -0800131 * The assumed width of a scrollbar.
132 */
133hterm.Terminal.prototype.scrollbarWidthPx = 16;
134
135/**
rginda9f5222b2012-03-05 11:53:28 -0800136 * Select a preference profile.
137 *
138 * This will load the terminal preferences for the given profile name and
139 * associate subsequent preference changes with the new preference profile.
140 *
141 * @param {string} newName The name of the preference profile. Forward slash
142 * characters will be removed from the name.
143 */
144hterm.Terminal.prototype.setProfile = function(profileName) {
145 // If we already have a profile selected, we're going to need to re-sync
146 // with the new profile.
147 var needSync = !!this.profileName_;
148
149 this.profileName_ = profileName.replace(/\//g, '');
150
rgindacbbd7482012-06-13 15:06:16 -0700151 this.prefs_ = new lib.PreferenceManager(
rginda9f5222b2012-03-05 11:53:28 -0800152 '/hterm/prefs/profiles/' + this.profileName_);
153
154 var self = this;
155 this.prefs_.definePreferences
rginda30f20f62012-04-05 16:36:19 -0700156 ([
157 /**
158 * Set whether the alt key acts as a meta key or as a distinct alt key.
rginda9f5222b2012-03-05 11:53:28 -0800159 */
rginda30f20f62012-04-05 16:36:19 -0700160 ['alt-is-meta', false, function(v) {
rgindaf9c36852012-05-09 11:08:39 -0700161 self.keyboard.altIsMeta = v;
rginda9f5222b2012-03-05 11:53:28 -0800162 }
163 ],
164
rginda30f20f62012-04-05 16:36:19 -0700165 /**
rginda39bdf6f2012-04-10 16:50:55 -0700166 * Controls how the alt key is handled.
167 *
168 * escape....... Send an ESC prefix.
169 * 8-bit........ Add 128 to the unshifted character as in xterm.
170 * browser-key.. Wait for the keypress event and see what the browser says.
171 * (This won't work well on platforms where the browser
172 * performs a default action for some alt sequences.)
rginda30f20f62012-04-05 16:36:19 -0700173 */
rginda39bdf6f2012-04-10 16:50:55 -0700174 ['alt-sends-what', 'escape', function(v) {
175 if (!/^(escape|8-bit|browser-key)$/.test(v))
176 v = 'escape';
177
rgindaf9c36852012-05-09 11:08:39 -0700178 self.keyboard.altSendsWhat = v;
rginda30f20f62012-04-05 16:36:19 -0700179 }
180 ],
181
182 /**
183 * Terminal bell sound. Empty string for no audible bell.
184 */
185 ['audible-bell-sound', '../audio/bell.ogg', function(v) {
186 self.bellAudio_.setAttribute('src', v);
187 }
188 ],
189
190 /**
191 * The background color for text with no other color attributes.
192 */
193 ['background-color', 'rgb(16, 16, 16)', function(v) {
rginda8e92a692012-05-20 19:37:20 -0700194 self.setBackgroundColor(v);
rginda9f5222b2012-03-05 11:53:28 -0800195 }
196 ],
197
198 /**
rginda30f20f62012-04-05 16:36:19 -0700199 * The background image.
rginda30f20f62012-04-05 16:36:19 -0700200 */
rginda8e92a692012-05-20 19:37:20 -0700201 ['background-image', '',
rginda30f20f62012-04-05 16:36:19 -0700202 function(v) {
203 self.scrollPort_.setBackgroundImage(v);
204 }
205 ],
206
207 /**
Philip Douglass959b49d2012-05-30 13:29:29 -0400208 * The background image size,
209 *
210 * Defaults to none.
211 */
212 ['background-size', '', function(v) {
213 self.scrollPort_.setBackgroundSize(v);
214 }
215 ],
216
217 /**
218 * The background image position,
219 *
220 * Defaults to none.
221 */
222 ['background-position', '', function(v) {
223 self.scrollPort_.setBackgroundPosition(v);
224 }
225 ],
226
227 /**
rginda30f20f62012-04-05 16:36:19 -0700228 * If true, the backspace should send BS ('\x08', aka ^H). Otherwise
229 * the backspace key should send '\x7f'.
230 */
231 ['backspace-sends-backspace', false, function(v) {
232 self.keyboard.backspaceSendsBackspace = v;
233 }
234 ],
235
236 /**
rgindade84e382012-04-20 15:39:31 -0700237 * Whether or not to blink the cursor by default.
238 */
239 ['cursor-blink', false, function(v) {
240 self.setCursorBlink(!!v);
241 }
242 ],
243
244 /**
rginda30f20f62012-04-05 16:36:19 -0700245 * The color of the visible cursor.
246 */
247 ['cursor-color', 'rgba(255,0,0,0.5)', function(v) {
rginda8e92a692012-05-20 19:37:20 -0700248 self.setCursorColor(v);
rginda30f20f62012-04-05 16:36:19 -0700249 }
250 ],
251
252 /**
rginda4bba5e12012-06-20 16:15:30 -0700253 * Automatically copy mouse selection to the clipboard.
254 */
255 ['copy-on-select', true, function(v) {
256 self.copyOnSelect = !!v;
257 }
258 ],
259
260 /**
rginda11057d52012-04-25 12:29:56 -0700261 * True to enable 8-bit control characters, false to ignore them.
262 *
263 * We'll respect the two-byte versions of these control characters
264 * regardless of this setting.
265 */
266 ['enable-8-bit-control', false, function(v) {
267 self.vt.enable8BitControl = !!v;
268 }
269 ],
270
271 /**
rginda30f20f62012-04-05 16:36:19 -0700272 * True if we should use bold weight font for text with the bold/bright
273 * attribute. False to use bright colors only. Null to autodetect.
274 */
275 ['enable-bold', null, function(v) {
276 self.syncBoldSafeState();
277 }
278 ],
279
280 /**
rginda9f5222b2012-03-05 11:53:28 -0800281 * Default font family for the terminal text.
282 */
283 ['font-family', ('"DejaVu Sans Mono", "Everson Mono", ' +
284 'FreeMono, "Menlo", "Lucida Console", ' +
285 'monospace'),
286 function(v) { self.syncFontFamily() }
287 ],
288
289 /**
rginda30f20f62012-04-05 16:36:19 -0700290 * The default font size in pixels.
291 */
292 ['font-size', 15, function(v) {
293 self.setFontSize(v);
294 }
295 ],
296
297 /**
rginda9f5222b2012-03-05 11:53:28 -0800298 * Anti-aliasing.
299 */
300 ['font-smoothing', 'antialiased',
301 function(v) { self.syncFontFamily() }
302 ],
303
304 /**
rginda30f20f62012-04-05 16:36:19 -0700305 * The foreground color for text with no other color attributes.
rginda9f5222b2012-03-05 11:53:28 -0800306 */
rginda30f20f62012-04-05 16:36:19 -0700307 ['foreground-color', 'rgb(240, 240, 240)', function(v) {
rginda8e92a692012-05-20 19:37:20 -0700308 self.setForegroundColor(v);
rginda9f5222b2012-03-05 11:53:28 -0800309 }
310 ],
311
312 /**
rginda30f20f62012-04-05 16:36:19 -0700313 * If true, home/end will control the terminal scrollbar and shift home/end
314 * will send the VT keycodes. If false then home/end sends VT codes and
315 * shift home/end scrolls.
rginda9f5222b2012-03-05 11:53:28 -0800316 */
rginda30f20f62012-04-05 16:36:19 -0700317 ['home-keys-scroll', false, function(v) {
318 self.keyboard.homeKeysScroll = v;
319 }
320 ],
321
322 /**
rginda11057d52012-04-25 12:29:56 -0700323 * Max length of a DCS, OSC, PM, or APS sequence before we give up and
324 * ignore the code.
325 */
326 ['max-string-sequence', 1024, function(v) {
327 self.vt.maxStringSequence = v;
328 }
329 ],
330
331 /**
rginda30f20f62012-04-05 16:36:19 -0700332 * Set whether the meta key sends a leading escape or not.
333 */
334 ['meta-sends-escape', true, function(v) {
335 self.keyboard.metaSendsEscape = v;
rginda9f5222b2012-03-05 11:53:28 -0800336 }
337 ],
338
339 /**
rgindad5613292012-06-19 15:40:37 -0700340 * Set whether we should treat DEC mode 1002 (mouse cell motion tracking)
341 * as if it were 1000 (mouse click tracking).
342 *
343 * This makes it possible to use vi's ":set mouse=a" mode without losing
344 * access to the system text selection mechanism.
345 */
346 ['mouse-cell-motion-trick', false, function(v) {
347 self.vt.setMouseCellMotionTrick(v);
348 }
349 ],
350
351 /**
rginda4bba5e12012-06-20 16:15:30 -0700352 * Mouse paste button, or null to autodetect.
353 *
354 * For autodetect, we'll try to enable middle button paste for non-X11
355 * platforms.
356 *
357 * On X11 we move it to button 3, but that'll probably be a context menu
358 * in the future.
359 */
360 ['mouse-paste-button', null, function(v) {
361 self.syncMousePasteButton();
362 }
363 ],
364
365 /**
rginda9f5222b2012-03-05 11:53:28 -0800366 * If true, scroll to the bottom on any keystroke.
367 */
368 ['scroll-on-keystroke', true, function(v) {
369 self.scrollOnKeystroke_ = v;
370 }
371 ],
372
373 /**
374 * If true, scroll to the bottom on terminal output.
375 */
376 ['scroll-on-output', false, function(v) {
377 self.scrollOnOutput_ = v;
378 }
379 ],
380
381 /**
David Reveman8f552492012-03-28 12:18:41 -0400382 * The vertical scrollbar mode.
383 */
384 ['scrollbar-visible', true, function(v) {
385 self.setScrollbarVisible(v);
386 }
387 ],
rginda30f20f62012-04-05 16:36:19 -0700388
389 /**
rginda4bba5e12012-06-20 16:15:30 -0700390 * Shift + Insert pastes if true, sent to host if false.
391 */
392 ['shift-insert-paste', true, function(v) {
393 self.keyboard.shiftInsertPaste = v;
394 }
395 ],
396
397 /**
rgindaf522ce02012-04-17 17:49:17 -0700398 * The default environment variables.
399 */
400 ['environment', {TERM: 'xterm-256color'}, null],
401
402 /**
rginda30f20f62012-04-05 16:36:19 -0700403 * If true, page up/down will control the terminal scrollbar and shift
404 * page up/down will send the VT keycodes. If false then page up/down
405 * sends VT codes and shift page up/down scrolls.
406 */
407 ['page-keys-scroll', false, function(v) {
408 self.keyboard.pageKeysScroll = v;
409 }
410 ],
411
rginda9f5222b2012-03-05 11:53:28 -0800412 ]);
413
414 if (needSync)
415 this.prefs_.notifyAll();
416};
417
rginda8e92a692012-05-20 19:37:20 -0700418
419/**
420 * Set the color for the cursor.
421 *
422 * If you want this setting to persist, set it through prefs_, rather than
423 * with this method.
424 */
425hterm.Terminal.prototype.setCursorColor = function(color) {
426 this.cursorNode_.style.backgroundColor = color;
427 this.cursorNode_.style.borderColor = color;
428};
429
430/**
431 * Return the current cursor color as a string.
432 */
433hterm.Terminal.prototype.getCursorColor = function() {
434 return this.cursorNode_.style.backgroundColor;
435};
436
437/**
rgindad5613292012-06-19 15:40:37 -0700438 * Enable or disable mouse based text selection in the terminal.
439 */
440hterm.Terminal.prototype.setSelectionEnabled = function(state) {
441 this.enableMouseDragScroll = state;
442 this.scrollPort_.setSelectionEnabled(state);
443};
444
445/**
rginda8e92a692012-05-20 19:37:20 -0700446 * Set the background color.
447 *
448 * If you want this setting to persist, set it through prefs_, rather than
449 * with this method.
450 */
451hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700452 this.backgroundColor_ = lib.colors.normalizeCSS(color);
rginda8e92a692012-05-20 19:37:20 -0700453 this.scrollPort_.setBackgroundColor(color);
454};
455
rginda9f5222b2012-03-05 11:53:28 -0800456/**
457 * Return the current terminal background color.
458 *
459 * Intended for use by other classes, so we don't have to expose the entire
460 * prefs_ object.
461 */
462hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700463 return this.backgroundColor_;
464};
465
466/**
467 * Set the foreground color.
468 *
469 * If you want this setting to persist, set it through prefs_, rather than
470 * with this method.
471 */
472hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700473 this.foregroundColor_ = lib.colors.normalizeCSS(color);
rginda8e92a692012-05-20 19:37:20 -0700474 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800475};
476
477/**
478 * Return the current terminal foreground color.
479 *
480 * Intended for use by other classes, so we don't have to expose the entire
481 * prefs_ object.
482 */
483hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700484 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800485};
486
487/**
rginda87b86462011-12-14 13:48:03 -0800488 * Create a new instance of a terminal command and run it with a given
489 * argument string.
490 *
491 * @param {function} commandClass The constructor for a terminal command.
492 * @param {string} argString The argument string to pass to the command.
493 */
494hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700495 var environment = this.prefs_.get('environment');
496 if (typeof environment != 'object' || environment == null)
497 environment = {};
498
rginda87b86462011-12-14 13:48:03 -0800499 var self = this;
500 this.command = new commandClass(
501 { argString: argString || '',
502 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700503 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800504 onExit: function(code) {
505 self.io.pop();
506 self.io.println(hterm.msg('COMMAND_COMPLETE',
507 [self.command.commandName, code]));
rgindafeaf3142012-01-31 15:14:20 -0800508 self.uninstallKeyboard();
rginda87b86462011-12-14 13:48:03 -0800509 }
510 });
511
rgindafeaf3142012-01-31 15:14:20 -0800512 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800513 this.command.run();
514};
515
516/**
rgindafeaf3142012-01-31 15:14:20 -0800517 * Returns true if the current screen is the primary screen, false otherwise.
518 */
519hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700520 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800521};
522
523/**
524 * Install the keyboard handler for this terminal.
525 *
526 * This will prevent the browser from seeing any keystrokes sent to the
527 * terminal.
528 */
529hterm.Terminal.prototype.installKeyboard = function() {
530 this.keyboard.installKeyboard(this.document_.body.firstChild);
531}
532
533/**
534 * Uninstall the keyboard handler for this terminal.
535 */
536hterm.Terminal.prototype.uninstallKeyboard = function() {
537 this.keyboard.installKeyboard(null);
538}
539
540/**
rginda35c456b2012-02-09 17:29:05 -0800541 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800542 *
543 * Call setFontSize(0) to reset to the default font size.
544 *
545 * This function does not modify the font-size preference.
546 *
547 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800548 */
549hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800550 if (px === 0)
551 px = this.prefs_.get('font-size');
552
rginda35c456b2012-02-09 17:29:05 -0800553 this.scrollPort_.setFontSize(px);
554};
555
556/**
557 * Get the current font size.
558 */
559hterm.Terminal.prototype.getFontSize = function() {
560 return this.scrollPort_.getFontSize();
561};
562
563/**
rginda8e92a692012-05-20 19:37:20 -0700564 * Get the current font family.
565 */
566hterm.Terminal.prototype.getFontFamily = function() {
567 return this.scrollPort_.getFontFamily();
568};
569
570/**
rginda35c456b2012-02-09 17:29:05 -0800571 * Set the CSS "font-family" for this terminal.
572 */
rginda9f5222b2012-03-05 11:53:28 -0800573hterm.Terminal.prototype.syncFontFamily = function() {
574 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
575 this.prefs_.get('font-smoothing'));
576 this.syncBoldSafeState();
577};
578
rginda4bba5e12012-06-20 16:15:30 -0700579/**
580 * Set this.mousePasteButton based on the mouse-paste-button pref,
581 * autodetecting if necessary.
582 */
583hterm.Terminal.prototype.syncMousePasteButton = function() {
584 var button = this.prefs_.get('mouse-paste-button');
585 if (typeof button == 'number') {
586 this.mousePasteButton = button;
587 return;
588 }
589
590 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
591 if (!ary || ary[2] == 'CrOS') {
592 this.mousePasteButton = 2;
593 } else {
594 this.mousePasteButton = 3;
595 }
596};
597
598/**
599 * Enable or disable bold based on the enable-bold pref, autodetecting if
600 * necessary.
601 */
rginda9f5222b2012-03-05 11:53:28 -0800602hterm.Terminal.prototype.syncBoldSafeState = function() {
603 var enableBold = this.prefs_.get('enable-bold');
604 if (enableBold !== null) {
605 this.screen_.textAttributes.enableBold = enableBold;
606 return;
607 }
608
rgindaf7521392012-02-28 17:20:34 -0800609 var normalSize = this.scrollPort_.measureCharacterSize();
610 var boldSize = this.scrollPort_.measureCharacterSize('bold');
611
612 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800613 if (!isBoldSafe) {
614 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700615 'from normal. Font family is: ' +
616 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800617 }
rginda9f5222b2012-03-05 11:53:28 -0800618
619 this.screen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800620};
621
622/**
rginda87b86462011-12-14 13:48:03 -0800623 * Return a copy of the current cursor position.
624 *
625 * @return {hterm.RowCol} The RowCol object representing the current position.
626 */
627hterm.Terminal.prototype.saveCursor = function() {
628 return this.screen_.cursorPosition.clone();
629};
630
rgindaa19afe22012-01-25 15:40:22 -0800631hterm.Terminal.prototype.getTextAttributes = function() {
632 return this.screen_.textAttributes;
633};
634
rginda1a09aa02012-06-18 21:11:25 -0700635hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
636 this.screen_.textAttributes = textAttributes;
637};
638
rginda87b86462011-12-14 13:48:03 -0800639/**
rgindaf522ce02012-04-17 17:49:17 -0700640 * Return the current browser zoom factor applied to the terminal.
641 *
642 * @return {number} The current browser zoom factor.
643 */
644hterm.Terminal.prototype.getZoomFactor = function() {
645 return this.scrollPort_.characterSize.zoomFactor;
646};
647
648/**
rginda9846e2f2012-01-27 13:53:33 -0800649 * Change the title of this terminal's window.
650 */
651hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800652 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800653};
654
655/**
rginda87b86462011-12-14 13:48:03 -0800656 * Restore a previously saved cursor position.
657 *
658 * @param {hterm.RowCol} cursor The position to restore.
659 */
660hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700661 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
662 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800663 this.screen_.setCursorPosition(row, column);
664 if (cursor.column > column ||
665 cursor.column == column && cursor.overflow) {
666 this.screen_.cursorPosition.overflow = true;
667 }
rginda87b86462011-12-14 13:48:03 -0800668};
669
670/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400671 * Clear the cursor's overflow flag.
672 */
673hterm.Terminal.prototype.clearCursorOverflow = function() {
674 this.screen_.cursorPosition.overflow = false;
675};
676
677/**
rginda87b86462011-12-14 13:48:03 -0800678 * Set the width of the terminal, resizing the UI to match.
679 */
680hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800681 if (columnCount == null) {
682 this.div_.style.width = '100%';
683 return;
684 }
685
rginda35c456b2012-02-09 17:29:05 -0800686 this.div_.style.width = this.scrollPort_.characterSize.width *
687 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400688 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800689 this.scheduleSyncCursorPosition_();
690};
rginda87b86462011-12-14 13:48:03 -0800691
rgindac9bc5502012-01-18 11:48:44 -0800692/**
rginda35c456b2012-02-09 17:29:05 -0800693 * Set the height of the terminal, resizing the UI to match.
694 */
695hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800696 if (rowCount == null) {
697 this.div_.style.height = '100%';
698 return;
699 }
700
rginda35c456b2012-02-09 17:29:05 -0800701 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700702 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800703 this.realizeSize_(this.screenSize.width, rowCount);
704 this.scheduleSyncCursorPosition_();
705};
706
707/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400708 * Deal with terminal size changes.
709 *
710 */
711hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
712 if (columnCount != this.screenSize.width)
713 this.realizeWidth_(columnCount);
714
715 if (rowCount != this.screenSize.height)
716 this.realizeHeight_(rowCount);
717
718 // Send new terminal size to plugin.
719 this.io.onTerminalResize(columnCount, rowCount);
720};
721
722/**
rgindac9bc5502012-01-18 11:48:44 -0800723 * Deal with terminal width changes.
724 *
725 * This function does what needs to be done when the terminal width changes
726 * out from under us. It happens here rather than in onResize_() because this
727 * code may need to run synchronously to handle programmatic changes of
728 * terminal width.
729 *
730 * Relying on the browser to send us an async resize event means we may not be
731 * in the correct state yet when the next escape sequence hits.
732 */
733hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
734 var deltaColumns = columnCount - this.screen_.getWidth();
735
rginda87b86462011-12-14 13:48:03 -0800736 this.screenSize.width = columnCount;
737 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800738
739 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400740 if (this.defaultTabStops)
741 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800742 } else {
743 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400744 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800745 break;
746
747 this.tabStops_.pop();
748 }
749 }
750
751 this.screen_.setColumnCount(this.screenSize.width);
752};
753
754/**
755 * Deal with terminal height changes.
756 *
757 * This function does what needs to be done when the terminal height changes
758 * out from under us. It happens here rather than in onResize_() because this
759 * code may need to run synchronously to handle programmatic changes of
760 * terminal height.
761 *
762 * Relying on the browser to send us an async resize event means we may not be
763 * in the correct state yet when the next escape sequence hits.
764 */
765hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
766 var deltaRows = rowCount - this.screen_.getHeight();
767
768 this.screenSize.height = rowCount;
769
770 var cursor = this.saveCursor();
771
772 if (deltaRows < 0) {
773 // Screen got smaller.
774 deltaRows *= -1;
775 while (deltaRows) {
776 var lastRow = this.getRowCount() - 1;
777 if (lastRow - this.scrollbackRows_.length == cursor.row)
778 break;
779
780 if (this.getRowText(lastRow))
781 break;
782
783 this.screen_.popRow();
784 deltaRows--;
785 }
786
787 var ary = this.screen_.shiftRows(deltaRows);
788 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
789
790 // We just removed rows from the top of the screen, we need to update
791 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800792 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800793 } else if (deltaRows > 0) {
794 // Screen got larger.
795
796 if (deltaRows <= this.scrollbackRows_.length) {
797 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
798 var rows = this.scrollbackRows_.splice(
799 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
800 this.screen_.unshiftRows(rows);
801 deltaRows -= scrollbackCount;
802 cursor.row += scrollbackCount;
803 }
804
805 if (deltaRows)
806 this.appendRows_(deltaRows);
807 }
808
rginda35c456b2012-02-09 17:29:05 -0800809 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800810 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800811};
812
813/**
814 * Scroll the terminal to the top of the scrollback buffer.
815 */
816hterm.Terminal.prototype.scrollHome = function() {
817 this.scrollPort_.scrollRowToTop(0);
818};
819
820/**
821 * Scroll the terminal to the end.
822 */
823hterm.Terminal.prototype.scrollEnd = function() {
824 this.scrollPort_.scrollRowToBottom(this.getRowCount());
825};
826
827/**
828 * Scroll the terminal one page up (minus one line) relative to the current
829 * position.
830 */
831hterm.Terminal.prototype.scrollPageUp = function() {
832 var i = this.scrollPort_.getTopRowIndex();
833 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
834};
835
836/**
837 * Scroll the terminal one page down (minus one line) relative to the current
838 * position.
839 */
840hterm.Terminal.prototype.scrollPageDown = function() {
841 var i = this.scrollPort_.getTopRowIndex();
842 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800843};
844
rgindac9bc5502012-01-18 11:48:44 -0800845/**
846 * Full terminal reset.
847 */
rginda87b86462011-12-14 13:48:03 -0800848hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800849 this.clearAllTabStops();
850 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700851
852 this.clearHome(this.primaryScreen_);
853 this.primaryScreen_.textAttributes.reset();
854
855 this.clearHome(this.alternateScreen_);
856 this.alternateScreen_.textAttributes.reset();
857
rgindab8bc8932012-04-27 12:45:03 -0700858 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
859
rgindac9bc5502012-01-18 11:48:44 -0800860 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800861};
862
rgindac9bc5502012-01-18 11:48:44 -0800863/**
864 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700865 *
866 * Perform a soft reset to the default values listed in
867 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800868 */
rginda0f5c0292012-01-13 11:00:13 -0800869hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700870 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800871 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700872
rgindab8bc8932012-04-27 12:45:03 -0700873 // Xterm also resets the color palette on soft reset, even though it doesn't
874 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700875 this.primaryScreen_.textAttributes.resetColorPalette();
876 this.alternateScreen_.textAttributes.resetColorPalette();
877
rgindab8bc8932012-04-27 12:45:03 -0700878 // The xterm man page explicitly says this will happen on soft reset.
879 this.setVTScrollRegion(null, null);
880
881 // Xterm also shows the cursor on soft reset, but does not alter the blink
882 // state.
rgindaa19afe22012-01-25 15:40:22 -0800883 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800884};
885
rgindac9bc5502012-01-18 11:48:44 -0800886/**
887 * Move the cursor forward to the next tab stop, or to the last column
888 * if no more tab stops are set.
889 */
890hterm.Terminal.prototype.forwardTabStop = function() {
891 var column = this.screen_.cursorPosition.column;
892
893 for (var i = 0; i < this.tabStops_.length; i++) {
894 if (this.tabStops_[i] > column) {
895 this.setCursorColumn(this.tabStops_[i]);
896 return;
897 }
898 }
899
David Benjamin66e954d2012-05-05 21:08:12 -0400900 // xterm does not clear the overflow flag on HT or CHT.
901 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800902 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400903 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800904};
905
rgindac9bc5502012-01-18 11:48:44 -0800906/**
907 * Move the cursor backward to the previous tab stop, or to the first column
908 * if no previous tab stops are set.
909 */
910hterm.Terminal.prototype.backwardTabStop = function() {
911 var column = this.screen_.cursorPosition.column;
912
913 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
914 if (this.tabStops_[i] < column) {
915 this.setCursorColumn(this.tabStops_[i]);
916 return;
917 }
918 }
919
920 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800921};
922
rgindac9bc5502012-01-18 11:48:44 -0800923/**
924 * Set a tab stop at the given column.
925 *
926 * @param {int} column Zero based column.
927 */
928hterm.Terminal.prototype.setTabStop = function(column) {
929 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
930 if (this.tabStops_[i] == column)
931 return;
932
933 if (this.tabStops_[i] < column) {
934 this.tabStops_.splice(i + 1, 0, column);
935 return;
936 }
937 }
938
939 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800940};
941
rgindac9bc5502012-01-18 11:48:44 -0800942/**
943 * Clear the tab stop at the current cursor position.
944 *
945 * No effect if there is no tab stop at the current cursor position.
946 */
947hterm.Terminal.prototype.clearTabStopAtCursor = function() {
948 var column = this.screen_.cursorPosition.column;
949
950 var i = this.tabStops_.indexOf(column);
951 if (i == -1)
952 return;
953
954 this.tabStops_.splice(i, 1);
955};
956
957/**
958 * Clear all tab stops.
959 */
960hterm.Terminal.prototype.clearAllTabStops = function() {
961 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -0400962 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -0800963};
964
965/**
966 * Set up the default tab stops, starting from a given column.
967 *
968 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -0400969 * from the specified column, or 0 if no column is provided. It also flags
970 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -0800971 *
972 * This does not clear the existing tab stops first, use clearAllTabStops
973 * for that.
974 *
975 * @param {int} opt_start Optional starting zero based starting column, useful
976 * for filling out missing tab stops when the terminal is resized.
977 */
978hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
979 var start = opt_start || 0;
980 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -0400981 // Round start up to a default tab stop.
982 start = start - 1 - ((start - 1) % w) + w;
983 for (var i = start; i < this.screenSize.width; i += w) {
984 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -0800985 }
David Benjamin66e954d2012-05-05 21:08:12 -0400986
987 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -0800988};
989
rginda6d397402012-01-17 10:58:29 -0800990/**
rginda8ba33642011-12-14 12:31:31 -0800991 * Interpret a sequence of characters.
992 *
993 * Incomplete escape sequences are buffered until the next call.
994 *
995 * @param {string} str Sequence of characters to interpret or pass through.
996 */
997hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800998 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800999 this.scheduleSyncCursorPosition_();
1000};
1001
1002/**
1003 * Take over the given DIV for use as the terminal display.
1004 *
1005 * @param {HTMLDivElement} div The div to use as the terminal display.
1006 */
1007hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001008 this.div_ = div;
1009
rginda8ba33642011-12-14 12:31:31 -08001010 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001011 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001012 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1013 this.scrollPort_.setBackgroundPosition(
1014 this.prefs_.get('background-position'));
rginda30f20f62012-04-05 16:36:19 -07001015
rginda0918b652012-04-04 11:26:24 -07001016 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001017
rginda9f5222b2012-03-05 11:53:28 -08001018 this.setFontSize(this.prefs_.get('font-size'));
1019 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001020
David Reveman8f552492012-03-28 12:18:41 -04001021 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1022
rginda8ba33642011-12-14 12:31:31 -08001023 this.document_ = this.scrollPort_.getDocument();
1024
rginda4bba5e12012-06-20 16:15:30 -07001025 this.document_.body.oncontextmenu = function() { return false };
1026
1027 var onMouse = this.onMouse_.bind(this);
1028 this.document_.body.firstChild.addEventListener('mousedown', onMouse);
1029 this.document_.body.firstChild.addEventListener('mouseup', onMouse);
1030 this.document_.body.firstChild.addEventListener('mousemove', onMouse);
1031 this.scrollPort_.onScrollWheel = onMouse;
1032
rginda8e92a692012-05-20 19:37:20 -07001033 this.document_.body.firstChild.addEventListener(
1034 'focus', this.onFocusChange_.bind(this, true));
1035 this.document_.body.firstChild.addEventListener(
1036 'blur', this.onFocusChange_.bind(this, false));
1037
1038 var style = this.document_.createElement('style');
1039 style.textContent =
1040 ('.cursor-node[focus="false"] {' +
1041 ' box-sizing: border-box;' +
1042 ' background-color: transparent !important;' +
1043 ' border-width: 2px;' +
1044 ' border-style: solid;' +
1045 '}');
1046 this.document_.head.appendChild(style);
1047
rginda8ba33642011-12-14 12:31:31 -08001048 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001049 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001050 this.cursorNode_.style.cssText =
1051 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001052 'top: -99px;' +
1053 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001054 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1055 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001056 '-webkit-transition: opacity, background-color 100ms linear;');
1057 this.setCursorColor(this.prefs_.get('cursor-color'));
rgindad5613292012-06-19 15:40:37 -07001058
rginda8ba33642011-12-14 12:31:31 -08001059 this.document_.body.appendChild(this.cursorNode_);
1060
rgindad5613292012-06-19 15:40:37 -07001061 // When 'enableMouseDragScroll' is off we reposition this element directly
1062 // under the mouse cursor after a click. This makes Chrome associate
1063 // subsequent mousemove events with the scroll-blocker. Since the
1064 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1065 // events do not cause the scrollport to scroll.
1066 //
1067 // It's a hack, but it's the cleanest way I could find.
1068 this.scrollBlockerNode_ = this.document_.createElement('div');
1069 this.scrollBlockerNode_.style.cssText =
1070 ('position: absolute;' +
1071 'top: -99px;' +
1072 'display: block;' +
1073 'width: 10px;' +
1074 'height: 10px;');
1075 this.document_.body.appendChild(this.scrollBlockerNode_);
1076
1077 var onMouse = this.onMouse_.bind(this);
1078 this.scrollPort_.onScrollWheel = onMouse;
1079 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1080 ].forEach(function(event) {
1081 this.scrollBlockerNode_.addEventListener(event, onMouse);
1082 this.cursorNode_.addEventListener(event, onMouse);
1083 this.document_.addEventListener(event, onMouse);
1084 }.bind(this));
1085
1086 this.cursorNode_.addEventListener('mousedown', function() {
1087 setTimeout(this.focus.bind(this));
1088 }.bind(this));
1089
rgindade84e382012-04-20 15:39:31 -07001090 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda8ba33642011-12-14 12:31:31 -08001091 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001092
rginda87b86462011-12-14 13:48:03 -08001093 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001094 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001095};
1096
rginda0918b652012-04-04 11:26:24 -07001097/**
1098 * Return the HTML document that contains the terminal DOM nodes.
1099 */
rginda87b86462011-12-14 13:48:03 -08001100hterm.Terminal.prototype.getDocument = function() {
1101 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001102};
1103
1104/**
rginda0918b652012-04-04 11:26:24 -07001105 * Focus the terminal.
1106 */
1107hterm.Terminal.prototype.focus = function() {
1108 this.scrollPort_.focus();
1109};
1110
1111/**
rginda8ba33642011-12-14 12:31:31 -08001112 * Return the HTML Element for a given row index.
1113 *
1114 * This is a method from the RowProvider interface. The ScrollPort uses
1115 * it to fetch rows on demand as they are scrolled into view.
1116 *
1117 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1118 * pairs to conserve memory.
1119 *
1120 * @param {integer} index The zero-based row index, measured relative to the
1121 * start of the scrollback buffer. On-screen rows will always have the
1122 * largest indicies.
1123 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1124 */
1125hterm.Terminal.prototype.getRowNode = function(index) {
1126 if (index < this.scrollbackRows_.length)
1127 return this.scrollbackRows_[index];
1128
1129 var screenIndex = index - this.scrollbackRows_.length;
1130 return this.screen_.rowsArray[screenIndex];
1131};
1132
1133/**
1134 * Return the text content for a given range of rows.
1135 *
1136 * This is a method from the RowProvider interface. The ScrollPort uses
1137 * it to fetch text content on demand when the user attempts to copy their
1138 * selection to the clipboard.
1139 *
1140 * @param {integer} start The zero-based row index to start from, measured
1141 * relative to the start of the scrollback buffer. On-screen rows will
1142 * always have the largest indicies.
1143 * @param {integer} end The zero-based row index to end on, measured
1144 * relative to the start of the scrollback buffer.
1145 * @return {string} A single string containing the text value of the range of
1146 * rows. Lines will be newline delimited, with no trailing newline.
1147 */
1148hterm.Terminal.prototype.getRowsText = function(start, end) {
1149 var ary = [];
1150 for (var i = start; i < end; i++) {
1151 var node = this.getRowNode(i);
1152 ary.push(node.textContent);
1153 }
1154
1155 return ary.join('\n');
1156};
1157
1158/**
1159 * Return the text content for a given row.
1160 *
1161 * This is a method from the RowProvider interface. The ScrollPort uses
1162 * it to fetch text content on demand when the user attempts to copy their
1163 * selection to the clipboard.
1164 *
1165 * @param {integer} index The zero-based row index to return, measured
1166 * relative to the start of the scrollback buffer. On-screen rows will
1167 * always have the largest indicies.
1168 * @return {string} A string containing the text value of the selected row.
1169 */
1170hterm.Terminal.prototype.getRowText = function(index) {
1171 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001172 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001173};
1174
1175/**
1176 * Return the total number of rows in the addressable screen and in the
1177 * scrollback buffer of this terminal.
1178 *
1179 * This is a method from the RowProvider interface. The ScrollPort uses
1180 * it to compute the size of the scrollbar.
1181 *
1182 * @return {integer} The number of rows in this terminal.
1183 */
1184hterm.Terminal.prototype.getRowCount = function() {
1185 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1186};
1187
1188/**
1189 * Create DOM nodes for new rows and append them to the end of the terminal.
1190 *
1191 * This is the only correct way to add a new DOM node for a row. Notice that
1192 * the new row is appended to the bottom of the list of rows, and does not
1193 * require renumbering (of the rowIndex property) of previous rows.
1194 *
1195 * If you think you want a new blank row somewhere in the middle of the
1196 * terminal, look into moveRows_().
1197 *
1198 * This method does not pay attention to vtScrollTop/Bottom, since you should
1199 * be using moveRows() in cases where they would matter.
1200 *
1201 * The cursor will be positioned at column 0 of the first inserted line.
1202 */
1203hterm.Terminal.prototype.appendRows_ = function(count) {
1204 var cursorRow = this.screen_.rowsArray.length;
1205 var offset = this.scrollbackRows_.length + cursorRow;
1206 for (var i = 0; i < count; i++) {
1207 var row = this.document_.createElement('x-row');
1208 row.appendChild(this.document_.createTextNode(''));
1209 row.rowIndex = offset + i;
1210 this.screen_.pushRow(row);
1211 }
1212
1213 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1214 if (extraRows > 0) {
1215 var ary = this.screen_.shiftRows(extraRows);
1216 Array.prototype.push.apply(this.scrollbackRows_, ary);
1217 this.scheduleScrollDown_();
1218 }
1219
1220 if (cursorRow >= this.screen_.rowsArray.length)
1221 cursorRow = this.screen_.rowsArray.length - 1;
1222
rginda87b86462011-12-14 13:48:03 -08001223 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001224};
1225
1226/**
1227 * Relocate rows from one part of the addressable screen to another.
1228 *
1229 * This is used to recycle rows during VT scrolls (those which are driven
1230 * by VT commands, rather than by the user manipulating the scrollbar.)
1231 *
1232 * In this case, the blank lines scrolled into the scroll region are made of
1233 * the nodes we scrolled off. These have their rowIndex properties carefully
1234 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001235 */
1236hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1237 var ary = this.screen_.removeRows(fromIndex, count);
1238 this.screen_.insertRows(toIndex, ary);
1239
1240 var start, end;
1241 if (fromIndex < toIndex) {
1242 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001243 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001244 } else {
1245 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001246 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001247 }
1248
1249 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001250 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001251};
1252
1253/**
1254 * Renumber the rowIndex property of the given range of rows.
1255 *
1256 * The start and end indicies are relative to the screen, not the scrollback.
1257 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001258 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001259 * no need to renumber scrollback rows.
1260 */
1261hterm.Terminal.prototype.renumberRows_ = function(start, end) {
1262 var offset = this.scrollbackRows_.length;
1263 for (var i = start; i < end; i++) {
1264 this.screen_.rowsArray[i].rowIndex = offset + i;
1265 }
1266};
1267
1268/**
1269 * Print a string to the terminal.
1270 *
1271 * This respects the current insert and wraparound modes. It will add new lines
1272 * to the end of the terminal, scrolling off the top into the scrollback buffer
1273 * if necessary.
1274 *
1275 * The string is *not* parsed for escape codes. Use the interpret() method if
1276 * that's what you're after.
1277 *
1278 * @param{string} str The string to print.
1279 */
1280hterm.Terminal.prototype.print = function(str) {
rgindaa19afe22012-01-25 15:40:22 -08001281 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
1282 this.newLine();
rginda2312fff2012-01-05 16:20:52 -08001283
rgindaa19afe22012-01-25 15:40:22 -08001284 if (this.options_.insertMode) {
1285 this.screen_.insertString(str);
1286 } else {
1287 this.screen_.overwriteString(str);
1288 }
1289
1290 var overflow = this.screen_.maybeClipCurrentRow();
1291
1292 if (this.options_.wraparound && overflow) {
1293 var lastColumn;
1294
1295 do {
rginda35c456b2012-02-09 17:29:05 -08001296 this.newLine();
1297 lastColumn = overflow.characterLength;
rgindaa19afe22012-01-25 15:40:22 -08001298
1299 if (!this.options_.insertMode)
1300 this.screen_.deleteChars(overflow.characterLength);
1301
1302 this.screen_.prependNodes(overflow);
rgindaa19afe22012-01-25 15:40:22 -08001303
1304 overflow = this.screen_.maybeClipCurrentRow();
1305 } while (overflow);
1306
1307 this.setCursorColumn(lastColumn);
1308 }
rginda8ba33642011-12-14 12:31:31 -08001309
1310 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001311
rginda9f5222b2012-03-05 11:53:28 -08001312 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001313 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001314};
1315
1316/**
rginda87b86462011-12-14 13:48:03 -08001317 * Set the VT scroll region.
1318 *
rginda87b86462011-12-14 13:48:03 -08001319 * This also resets the cursor position to the absolute (0, 0) position, since
1320 * that's what xterm appears to do.
1321 *
1322 * @param {integer} scrollTop The zero-based top of the scroll region.
1323 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1324 * inclusive.
1325 */
1326hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1327 this.vtScrollTop_ = scrollTop;
1328 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001329};
1330
1331/**
rginda8ba33642011-12-14 12:31:31 -08001332 * Return the top row index according to the VT.
1333 *
1334 * This will return 0 unless the terminal has been told to restrict scrolling
1335 * to some lower row. It is used for some VT cursor positioning and scrolling
1336 * commands.
1337 *
1338 * @return {integer} The topmost row in the terminal's scroll region.
1339 */
1340hterm.Terminal.prototype.getVTScrollTop = function() {
1341 if (this.vtScrollTop_ != null)
1342 return this.vtScrollTop_;
1343
1344 return 0;
rginda87b86462011-12-14 13:48:03 -08001345};
rginda8ba33642011-12-14 12:31:31 -08001346
1347/**
1348 * Return the bottom row index according to the VT.
1349 *
1350 * This will return the height of the terminal unless the it has been told to
1351 * restrict scrolling to some higher row. It is used for some VT cursor
1352 * positioning and scrolling commands.
1353 *
1354 * @return {integer} The bottommost row in the terminal's scroll region.
1355 */
1356hterm.Terminal.prototype.getVTScrollBottom = function() {
1357 if (this.vtScrollBottom_ != null)
1358 return this.vtScrollBottom_;
1359
rginda87b86462011-12-14 13:48:03 -08001360 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001361}
1362
1363/**
1364 * Process a '\n' character.
1365 *
1366 * If the cursor is on the final row of the terminal this will append a new
1367 * blank row to the screen and scroll the topmost row into the scrollback
1368 * buffer.
1369 *
1370 * Otherwise, this moves the cursor to column zero of the next row.
1371 */
1372hterm.Terminal.prototype.newLine = function() {
1373 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001374 // If we're at the end of the screen we need to append a new line and
1375 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001376 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001377 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1378 // End of the scroll region does not affect the scrollback buffer.
1379 this.vtScrollUp(1);
1380 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001381 } else {
rginda87b86462011-12-14 13:48:03 -08001382 // Anywhere else in the screen just moves the cursor.
1383 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001384 }
1385};
1386
1387/**
1388 * Like newLine(), except maintain the cursor column.
1389 */
1390hterm.Terminal.prototype.lineFeed = function() {
1391 var column = this.screen_.cursorPosition.column;
1392 this.newLine();
1393 this.setCursorColumn(column);
1394};
1395
1396/**
rginda87b86462011-12-14 13:48:03 -08001397 * If autoCarriageReturn is set then newLine(), else lineFeed().
1398 */
1399hterm.Terminal.prototype.formFeed = function() {
1400 if (this.options_.autoCarriageReturn) {
1401 this.newLine();
1402 } else {
1403 this.lineFeed();
1404 }
1405};
1406
1407/**
1408 * Move the cursor up one row, possibly inserting a blank line.
1409 *
1410 * The cursor column is not changed.
1411 */
1412hterm.Terminal.prototype.reverseLineFeed = function() {
1413 var scrollTop = this.getVTScrollTop();
1414 var currentRow = this.screen_.cursorPosition.row;
1415
1416 if (currentRow == scrollTop) {
1417 this.insertLines(1);
1418 } else {
1419 this.setAbsoluteCursorRow(currentRow - 1);
1420 }
1421};
1422
1423/**
rginda8ba33642011-12-14 12:31:31 -08001424 * Replace all characters to the left of the current cursor with the space
1425 * character.
1426 *
1427 * TODO(rginda): This should probably *remove* the characters (not just replace
1428 * with a space) if there are no characters at or beyond the current cursor
1429 * position. Once it does that, it'll have the same text-attribute related
1430 * issues as hterm.Screen.prototype.clearCursorRow :/
1431 */
1432hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001433 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001434 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001435 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001436 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001437};
1438
1439/**
David Benjamin684a9b72012-05-01 17:19:58 -04001440 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001441 *
1442 * The cursor position is unchanged.
1443 *
1444 * TODO(rginda): Test that this works even when the cursor is positioned beyond
1445 * the end of the text.
1446 *
1447 * TODO(rginda): This likely has text-attribute related troubles similar to the
1448 * todo on hterm.Screen.prototype.clearCursorRow.
David Benjamin684a9b72012-05-01 17:19:58 -04001449 *
1450 * TODO(davidben): Probably better to not add the whitespace to the clipboard
1451 * if erasing to the end of the drawn portion of the line. That said, xterm
1452 * behaves the same here.
rginda8ba33642011-12-14 12:31:31 -08001453 */
1454hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001455 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001456
rginda87b86462011-12-14 13:48:03 -08001457 var maxCount = this.screenSize.width - cursor.column;
David Benjamin684a9b72012-05-01 17:19:58 -04001458 if (opt_count === undefined || opt_count >= maxCount) {
1459 this.screen_.deleteChars(maxCount);
1460 } else {
rgindacbbd7482012-06-13 15:06:16 -07001461 this.screen_.overwriteString(lib.f.getWhitespace(opt_count));
David Benjamin684a9b72012-05-01 17:19:58 -04001462 }
rginda87b86462011-12-14 13:48:03 -08001463 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001464 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001465};
1466
1467/**
1468 * Erase the current line.
1469 *
1470 * The cursor position is unchanged.
1471 *
1472 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1473 * has a text-attribute related TODO.
1474 */
1475hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001476 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001477 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001478 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001479 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001480};
1481
1482/**
David Benjamina08d78f2012-05-05 00:28:49 -04001483 * Erase all characters from the start of the screen to the current cursor
1484 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001485 *
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.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001492 var cursor = this.saveCursor();
1493
1494 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001495
David Benjamina08d78f2012-05-05 00:28:49 -04001496 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001497 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001498 this.screen_.clearCursorRow();
1499 }
1500
rginda87b86462011-12-14 13:48:03 -08001501 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001502 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001503};
1504
1505/**
1506 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001507 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001508 *
1509 * The cursor position is unchanged.
1510 *
1511 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1512 * has a text-attribute related TODO.
1513 */
1514hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001515 var cursor = this.saveCursor();
1516
1517 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001518
David Benjamina08d78f2012-05-05 00:28:49 -04001519 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001520 for (var i = cursor.row + 1; i <= bottom; i++) {
1521 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001522 this.screen_.clearCursorRow();
1523 }
1524
rginda87b86462011-12-14 13:48:03 -08001525 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001526 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001527};
1528
1529/**
1530 * Fill the terminal with a given character.
1531 *
1532 * This methods does not respect the VT scroll region.
1533 *
1534 * @param {string} ch The character to use for the fill.
1535 */
1536hterm.Terminal.prototype.fill = function(ch) {
1537 var cursor = this.saveCursor();
1538
1539 this.setAbsoluteCursorPosition(0, 0);
1540 for (var row = 0; row < this.screenSize.height; row++) {
1541 for (var col = 0; col < this.screenSize.width; col++) {
1542 this.setAbsoluteCursorPosition(row, col);
1543 this.screen_.overwriteString(ch);
1544 }
1545 }
1546
1547 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001548};
1549
1550/**
rginda9ea433c2012-03-16 11:57:00 -07001551 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001552 *
rginda9ea433c2012-03-16 11:57:00 -07001553 * This does not respect the scroll region.
1554 *
1555 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1556 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001557 *
1558 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1559 * has a text-attribute related TODO.
1560 */
rginda9ea433c2012-03-16 11:57:00 -07001561hterm.Terminal.prototype.clearHome = function(opt_screen) {
1562 var screen = opt_screen || this.screen_;
1563 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001564
rginda11057d52012-04-25 12:29:56 -07001565 if (bottom == 0) {
1566 // Empty screen, nothing to do.
1567 return;
1568 }
1569
rgindae4d29232012-01-19 10:47:13 -08001570 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001571 screen.setCursorPosition(i, 0);
1572 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001573 }
1574
rginda9ea433c2012-03-16 11:57:00 -07001575 screen.setCursorPosition(0, 0);
1576};
1577
1578/**
1579 * Erase the entire display without changing the cursor position.
1580 *
1581 * The cursor position is unchanged. This does not respect the scroll
1582 * region.
1583 *
1584 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1585 * to the current screen.
1586 *
1587 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1588 * has a text-attribute related TODO.
1589 */
1590hterm.Terminal.prototype.clear = function(opt_screen) {
1591 var screen = opt_screen || this.screen_;
1592 var cursor = screen.cursorPosition.clone();
1593 this.clearHome(screen);
1594 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001595};
1596
1597/**
1598 * VT command to insert lines at the current cursor row.
1599 *
1600 * This respects the current scroll region. Rows pushed off the bottom are
1601 * lost (they won't show up in the scrollback buffer).
1602 *
1603 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1604 * has a text-attribute related TODO.
1605 *
1606 * @param {integer} count The number of lines to insert.
1607 */
1608hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001609 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001610
1611 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001612 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001613
rgindae4d29232012-01-19 10:47:13 -08001614 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001615 if (start != cursor.row)
1616 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001617
1618 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001619 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001620 this.screen_.clearCursorRow();
1621 }
1622
rginda87b86462011-12-14 13:48:03 -08001623 cursor.column = 0;
1624 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001625};
1626
1627/**
1628 * VT command to delete lines at the current cursor row.
1629 *
1630 * New rows are added to the bottom of scroll region to take their place. New
1631 * rows are strictly there to take up space and have no content or style.
1632 */
1633hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001634 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001635
rginda87b86462011-12-14 13:48:03 -08001636 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001637 var bottom = this.getVTScrollBottom();
1638
rginda87b86462011-12-14 13:48:03 -08001639 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001640 count = Math.min(count, maxCount);
1641
rginda87b86462011-12-14 13:48:03 -08001642 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001643 if (count != maxCount)
1644 this.moveRows_(top, count, moveStart);
1645
1646 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001647 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001648 this.screen_.clearCursorRow();
1649 }
1650
rginda87b86462011-12-14 13:48:03 -08001651 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001652 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001653};
1654
1655/**
1656 * Inserts the given number of spaces at the current cursor position.
1657 *
rginda87b86462011-12-14 13:48:03 -08001658 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001659 */
1660hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001661 var cursor = this.saveCursor();
1662
rgindacbbd7482012-06-13 15:06:16 -07001663 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001664 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001665 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001666
1667 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001668 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001669};
1670
1671/**
1672 * Forward-delete the specified number of characters starting at the cursor
1673 * position.
1674 *
1675 * @param {integer} count The number of characters to delete.
1676 */
1677hterm.Terminal.prototype.deleteChars = function(count) {
1678 this.screen_.deleteChars(count);
David Benjamin54e8bf62012-06-01 22:31:40 -04001679 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001680};
1681
1682/**
1683 * Shift rows in the scroll region upwards by a given number of lines.
1684 *
1685 * New rows are inserted at the bottom of the scroll region to fill the
1686 * vacated rows. The new rows not filled out with the current text attributes.
1687 *
1688 * This function does not affect the scrollback rows at all. Rows shifted
1689 * off the top are lost.
1690 *
rginda87b86462011-12-14 13:48:03 -08001691 * The cursor position is not altered.
1692 *
rginda8ba33642011-12-14 12:31:31 -08001693 * @param {integer} count The number of rows to scroll.
1694 */
1695hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001696 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001697
rginda87b86462011-12-14 13:48:03 -08001698 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001699 this.deleteLines(count);
1700
rginda87b86462011-12-14 13:48:03 -08001701 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001702};
1703
1704/**
1705 * Shift rows below the cursor down by a given number of lines.
1706 *
1707 * This function respects the current scroll region.
1708 *
1709 * New rows are inserted at the top of the scroll region to fill the
1710 * vacated rows. The new rows not filled out with the current text attributes.
1711 *
1712 * This function does not affect the scrollback rows at all. Rows shifted
1713 * off the bottom are lost.
1714 *
1715 * @param {integer} count The number of rows to scroll.
1716 */
1717hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001718 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001719
rginda87b86462011-12-14 13:48:03 -08001720 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001721 this.insertLines(opt_count);
1722
rginda87b86462011-12-14 13:48:03 -08001723 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001724};
1725
rginda87b86462011-12-14 13:48:03 -08001726
rginda8ba33642011-12-14 12:31:31 -08001727/**
1728 * Set the cursor position.
1729 *
1730 * The cursor row is relative to the scroll region if the terminal has
1731 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1732 *
1733 * @param {integer} row The new zero-based cursor row.
1734 * @param {integer} row The new zero-based cursor column.
1735 */
1736hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1737 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001738 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001739 } else {
rginda87b86462011-12-14 13:48:03 -08001740 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001741 }
rginda87b86462011-12-14 13:48:03 -08001742};
rginda8ba33642011-12-14 12:31:31 -08001743
rginda87b86462011-12-14 13:48:03 -08001744hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1745 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001746 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1747 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001748 this.screen_.setCursorPosition(row, column);
1749};
1750
1751hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001752 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1753 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001754 this.screen_.setCursorPosition(row, column);
1755};
1756
1757/**
1758 * Set the cursor column.
1759 *
1760 * @param {integer} column The new zero-based cursor column.
1761 */
1762hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001763 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001764};
1765
1766/**
1767 * Return the cursor column.
1768 *
1769 * @return {integer} The zero-based cursor column.
1770 */
1771hterm.Terminal.prototype.getCursorColumn = function() {
1772 return this.screen_.cursorPosition.column;
1773};
1774
1775/**
1776 * Set the cursor row.
1777 *
1778 * The cursor row is relative to the scroll region if the terminal has
1779 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1780 *
1781 * @param {integer} row The new cursor row.
1782 */
rginda87b86462011-12-14 13:48:03 -08001783hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1784 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001785};
1786
1787/**
1788 * Return the cursor row.
1789 *
1790 * @return {integer} The zero-based cursor row.
1791 */
1792hterm.Terminal.prototype.getCursorRow = function(row) {
1793 return this.screen_.cursorPosition.row;
1794};
1795
1796/**
1797 * Request that the ScrollPort redraw itself soon.
1798 *
1799 * The redraw will happen asynchronously, soon after the call stack winds down.
1800 * Multiple calls will be coalesced into a single redraw.
1801 */
1802hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001803 if (this.timeouts_.redraw)
1804 return;
rginda8ba33642011-12-14 12:31:31 -08001805
1806 var self = this;
rginda87b86462011-12-14 13:48:03 -08001807 this.timeouts_.redraw = setTimeout(function() {
1808 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001809 self.scrollPort_.redraw_();
1810 }, 0);
1811};
1812
1813/**
1814 * Request that the ScrollPort be scrolled to the bottom.
1815 *
1816 * The scroll will happen asynchronously, soon after the call stack winds down.
1817 * Multiple calls will be coalesced into a single scroll.
1818 *
1819 * This affects the scrollbar position of the ScrollPort, and has nothing to
1820 * do with the VT scroll commands.
1821 */
1822hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1823 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001824 return;
rginda8ba33642011-12-14 12:31:31 -08001825
1826 var self = this;
1827 this.timeouts_.scrollDown = setTimeout(function() {
1828 delete self.timeouts_.scrollDown;
1829 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1830 }, 10);
1831};
1832
1833/**
1834 * Move the cursor up a specified number of rows.
1835 *
1836 * @param {integer} count The number of rows to move the cursor.
1837 */
1838hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001839 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001840};
1841
1842/**
1843 * Move the cursor down a specified number of rows.
1844 *
1845 * @param {integer} count The number of rows to move the cursor.
1846 */
1847hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001848 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001849 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1850 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1851 this.screenSize.height - 1);
1852
rgindacbbd7482012-06-13 15:06:16 -07001853 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001854 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001855 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001856};
1857
1858/**
1859 * Move the cursor left a specified number of columns.
1860 *
1861 * @param {integer} count The number of columns to move the cursor.
1862 */
1863hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001864 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001865};
1866
1867/**
1868 * Move the cursor right a specified number of columns.
1869 *
1870 * @param {integer} count The number of columns to move the cursor.
1871 */
1872hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001873 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001874 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001875 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001876 this.setCursorColumn(column);
1877};
1878
1879/**
1880 * Reverse the foreground and background colors of the terminal.
1881 *
1882 * This only affects text that was drawn with no attributes.
1883 *
1884 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1885 * been drawn with attributes that happen to coincide with the default
1886 * 'no-attribute' colors. My guess is probably not.
1887 */
1888hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001889 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001890 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001891 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1892 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001893 } else {
rginda9f5222b2012-03-05 11:53:28 -08001894 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1895 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001896 }
1897};
1898
1899/**
rginda87b86462011-12-14 13:48:03 -08001900 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001901 */
1902hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001903 if (this.bellAudio_.getAttribute('src'))
1904 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001905
rginda6d397402012-01-17 10:58:29 -08001906 this.cursorNode_.style.backgroundColor =
1907 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001908
1909 var self = this;
1910 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001911 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001912 }, 200);
rginda87b86462011-12-14 13:48:03 -08001913};
1914
1915/**
rginda8ba33642011-12-14 12:31:31 -08001916 * Set the origin mode bit.
1917 *
1918 * If origin mode is on, certain VT cursor and scrolling commands measure their
1919 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1920 * to the top of the addressable screen.
1921 *
1922 * Defaults to off.
1923 *
1924 * @param {boolean} state True to set origin mode, false to unset.
1925 */
1926hterm.Terminal.prototype.setOriginMode = function(state) {
1927 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001928 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001929};
1930
1931/**
1932 * Set the insert mode bit.
1933 *
1934 * If insert mode is on, existing text beyond the cursor position will be
1935 * shifted right to make room for new text. Otherwise, new text overwrites
1936 * any existing text.
1937 *
1938 * Defaults to off.
1939 *
1940 * @param {boolean} state True to set insert mode, false to unset.
1941 */
1942hterm.Terminal.prototype.setInsertMode = function(state) {
1943 this.options_.insertMode = state;
1944};
1945
1946/**
rginda87b86462011-12-14 13:48:03 -08001947 * Set the auto carriage return bit.
1948 *
1949 * If auto carriage return is on then a formfeed character is interpreted
1950 * as a newline, otherwise it's the same as a linefeed. The difference boils
1951 * down to whether or not the cursor column is reset.
1952 */
1953hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1954 this.options_.autoCarriageReturn = state;
1955};
1956
1957/**
rginda8ba33642011-12-14 12:31:31 -08001958 * Set the wraparound mode bit.
1959 *
1960 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1961 * to the start of the following row. Otherwise, the cursor is clamped to the
1962 * end of the screen and attempts to write past it are ignored.
1963 *
1964 * Defaults to on.
1965 *
1966 * @param {boolean} state True to set wraparound mode, false to unset.
1967 */
1968hterm.Terminal.prototype.setWraparound = function(state) {
1969 this.options_.wraparound = state;
1970};
1971
1972/**
1973 * Set the reverse-wraparound mode bit.
1974 *
1975 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1976 * to the end of the previous row. Otherwise, the cursor is clamped to column
1977 * 0.
1978 *
1979 * Defaults to off.
1980 *
1981 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1982 */
1983hterm.Terminal.prototype.setReverseWraparound = function(state) {
1984 this.options_.reverseWraparound = state;
1985};
1986
1987/**
1988 * Selects between the primary and alternate screens.
1989 *
1990 * If alternate mode is on, the alternate screen is active. Otherwise the
1991 * primary screen is active.
1992 *
1993 * Swapping screens has no effect on the scrollback buffer.
1994 *
1995 * Each screen maintains its own cursor position.
1996 *
1997 * Defaults to off.
1998 *
1999 * @param {boolean} state True to set alternate mode, false to unset.
2000 */
2001hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002002 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002003 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2004
rginda35c456b2012-02-09 17:29:05 -08002005 if (this.screen_.rowsArray.length &&
2006 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2007 // If the screen changed sizes while we were away, our rowIndexes may
2008 // be incorrect.
2009 var offset = this.scrollbackRows_.length;
2010 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002011 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002012 ary[i].rowIndex = offset + i;
2013 }
2014 }
rginda8ba33642011-12-14 12:31:31 -08002015
rginda35c456b2012-02-09 17:29:05 -08002016 this.realizeWidth_(this.screenSize.width);
2017 this.realizeHeight_(this.screenSize.height);
2018 this.scrollPort_.syncScrollHeight();
2019 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002020
rginda6d397402012-01-17 10:58:29 -08002021 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002022 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002023};
2024
2025/**
2026 * Set the cursor-blink mode bit.
2027 *
2028 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2029 * a visible cursor does not blink.
2030 *
2031 * You should make sure to turn blinking off if you're going to dispose of a
2032 * terminal, otherwise you'll leak a timeout.
2033 *
2034 * Defaults to on.
2035 *
2036 * @param {boolean} state True to set cursor-blink mode, false to unset.
2037 */
2038hterm.Terminal.prototype.setCursorBlink = function(state) {
2039 this.options_.cursorBlink = state;
2040
2041 if (!state && this.timeouts_.cursorBlink) {
2042 clearTimeout(this.timeouts_.cursorBlink);
2043 delete this.timeouts_.cursorBlink;
2044 }
2045
2046 if (this.options_.cursorVisible)
2047 this.setCursorVisible(true);
2048};
2049
2050/**
2051 * Set the cursor-visible mode bit.
2052 *
2053 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2054 *
2055 * Defaults to on.
2056 *
2057 * @param {boolean} state True to set cursor-visible mode, false to unset.
2058 */
2059hterm.Terminal.prototype.setCursorVisible = function(state) {
2060 this.options_.cursorVisible = state;
2061
2062 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002063 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002064 return;
2065 }
2066
rginda87b86462011-12-14 13:48:03 -08002067 this.syncCursorPosition_();
2068
2069 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002070
2071 if (this.options_.cursorBlink) {
2072 if (this.timeouts_.cursorBlink)
2073 return;
2074
2075 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2076 500);
2077 } else {
2078 if (this.timeouts_.cursorBlink) {
2079 clearTimeout(this.timeouts_.cursorBlink);
2080 delete this.timeouts_.cursorBlink;
2081 }
2082 }
2083};
2084
2085/**
rginda87b86462011-12-14 13:48:03 -08002086 * Synchronizes the visible cursor and document selection with the current
2087 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002088 */
2089hterm.Terminal.prototype.syncCursorPosition_ = function() {
2090 var topRowIndex = this.scrollPort_.getTopRowIndex();
2091 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2092 var cursorRowIndex = this.scrollbackRows_.length +
2093 this.screen_.cursorPosition.row;
2094
2095 if (cursorRowIndex > bottomRowIndex) {
2096 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002097 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002098 return;
2099 }
2100
rginda35c456b2012-02-09 17:29:05 -08002101 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
2102 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
2103
rginda8ba33642011-12-14 12:31:31 -08002104 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002105 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2106 'px';
2107 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2108 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002109
2110 this.cursorNode_.setAttribute('title',
2111 '(' + this.screen_.cursorPosition.row +
2112 ', ' + this.screen_.cursorPosition.column +
2113 ')');
2114
2115 // Update the caret for a11y purposes.
2116 var selection = this.document_.getSelection();
2117 if (selection && selection.isCollapsed)
2118 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002119};
2120
2121/**
2122 * Synchronizes the visible cursor with the current cursor coordinates.
2123 *
2124 * The sync will happen asynchronously, soon after the call stack winds down.
2125 * Multiple calls will be coalesced into a single sync.
2126 */
2127hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2128 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002129 return;
rginda8ba33642011-12-14 12:31:31 -08002130
2131 var self = this;
2132 this.timeouts_.syncCursor = setTimeout(function() {
2133 self.syncCursorPosition_();
2134 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002135 }, 0);
2136};
2137
rgindacc2996c2012-02-24 14:59:31 -08002138/**
rgindaf522ce02012-04-17 17:49:17 -07002139 * Show or hide the zoom warning.
2140 *
2141 * The zoom warning is a message warning the user that their browser zoom must
2142 * be set to 100% in order for hterm to function properly.
2143 *
2144 * @param {boolean} state True to show the message, false to hide it.
2145 */
2146hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2147 if (!this.zoomWarningNode_) {
2148 if (!state)
2149 return;
2150
2151 this.zoomWarningNode_ = this.document_.createElement('div');
2152 this.zoomWarningNode_.style.cssText = (
2153 'color: black;' +
2154 'background-color: #ff2222;' +
2155 'font-size: large;' +
2156 'border-radius: 8px;' +
2157 'opacity: 0.75;' +
2158 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2159 'top: 0.5em;' +
2160 'right: 1.2em;' +
2161 'position: absolute;' +
2162 '-webkit-text-size-adjust: none;' +
2163 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002164 }
2165
rgindade84e382012-04-20 15:39:31 -07002166 this.zoomWarningNode_.textContent = hterm.msg('ZOOM_WARNING') ||
2167 ('!! ' + parseInt(this.scrollPort_.characterSize.zoomFactor * 100) +
2168 '% !!');
rgindaf522ce02012-04-17 17:49:17 -07002169 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2170
2171 if (state) {
2172 if (!this.zoomWarningNode_.parentNode)
2173 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2174 } else if (this.zoomWarningNode_.parentNode) {
2175 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2176 }
2177};
2178
2179/**
rgindacc2996c2012-02-24 14:59:31 -08002180 * Show the terminal overlay for a given amount of time.
2181 *
2182 * The terminal overlay appears in inverse video in a large font, centered
2183 * over the terminal. You should probably keep the overlay message brief,
2184 * since it's in a large font and you probably aren't going to check the size
2185 * of the terminal first.
2186 *
2187 * @param {string} msg The text (not HTML) message to display in the overlay.
2188 * @param {number} opt_timeout The amount of time to wait before fading out
2189 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2190 * stay up forever (or until the next overlay).
2191 */
2192hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002193 if (!this.overlayNode_) {
2194 if (!this.div_)
2195 return;
2196
2197 this.overlayNode_ = this.document_.createElement('div');
2198 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002199 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002200 'font-size: xx-large;' +
2201 'opacity: 0.75;' +
2202 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2203 'position: absolute;' +
2204 '-webkit-user-select: none;' +
2205 '-webkit-transition: opacity 180ms ease-in;');
2206 }
2207
rginda9f5222b2012-03-05 11:53:28 -08002208 this.overlayNode_.style.color = this.prefs_.get('background-color');
2209 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2210 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2211
rgindaf0090c92012-02-10 14:58:52 -08002212 this.overlayNode_.textContent = msg;
2213 this.overlayNode_.style.opacity = '0.75';
2214
2215 if (!this.overlayNode_.parentNode)
2216 this.div_.appendChild(this.overlayNode_);
2217
2218 this.overlayNode_.style.top = (
2219 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
2220 this.overlayNode_.style.left = (
2221 this.div_.clientWidth - this.overlayNode_.clientWidth -
2222 this.scrollbarWidthPx) / 2;
2223
2224 var self = this;
2225
2226 if (this.overlayTimeout_)
2227 clearTimeout(this.overlayTimeout_);
2228
rgindacc2996c2012-02-24 14:59:31 -08002229 if (opt_timeout === null)
2230 return;
2231
rgindaf0090c92012-02-10 14:58:52 -08002232 this.overlayTimeout_ = setTimeout(function() {
2233 self.overlayNode_.style.opacity = '0';
2234 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002235 if (self.overlayNode_.parentNode)
2236 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002237 self.overlayTimeout_ = null;
2238 self.overlayNode_.style.opacity = '0.75';
2239 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002240 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002241};
2242
rginda4bba5e12012-06-20 16:15:30 -07002243/**
2244 * Paste from the system clipboard to the terminal.
2245 */
2246hterm.Terminal.prototype.paste = function() {
2247 hterm.pasteFromClipboard(this.document_);
2248};
2249
2250/**
2251 * Copy a string to the system clipboard.
2252 *
2253 * Note: If there is a selected range in the terminal, it'll be cleared.
2254 */
2255hterm.Terminal.prototype.copyStringToClipboard = function(str) {
2256 var copySource = this.document_.createElement('div');
2257 copySource.textContent = str;
2258 copySource.style.cssText = (
2259 '-webkit-user-select: text;' +
2260 'position: absolute;' +
2261 'top: -99px');
2262
2263 this.document_.body.appendChild(copySource);
2264 var selection = this.document_.getSelection();
2265 selection.selectAllChildren(copySource);
2266
2267 this.copySelectionToClipboard();
2268
2269 copySource.parentNode.removeChild(copySource);
2270};
2271
2272/**
2273 * Copy the current selection to the system clipboard, then clear it after a
2274 * short delay.
2275 */
2276hterm.Terminal.prototype.copySelectionToClipboard = function() {
2277 hterm.copySelectionToClipboard(this.document_);
2278 this.showOverlay(hterm.msg('NOTIFY_COPY'), 500);
2279
2280 if (this.copyTimeout_)
2281 clearTimeout(this.copyTimeout_);
2282
2283 this.copyTimeout_ = setTimeout(function() {
2284 var selection = this.document_.getSelection();
2285 if (!selection.isCollapsed)
2286 selection.collapseToEnd();
2287 this.copyTimeout_ = null;
2288 }.bind(this), 500);
2289};
2290
rgindaf0090c92012-02-10 14:58:52 -08002291hterm.Terminal.prototype.overlaySize = function() {
2292 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2293};
2294
rginda87b86462011-12-14 13:48:03 -08002295/**
2296 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2297 *
2298 * @param {string} string The VT string representing the keystroke.
2299 */
2300hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002301 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002302 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2303
2304 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08002305};
2306
2307/**
rgindad5613292012-06-19 15:40:37 -07002308 * Add the terminalRow and terminalColumn properties to mouse events and
2309 * then forward on to onMouse().
2310 *
2311 * The terminalRow and terminalColumn properties contain the (row, column)
2312 * coordinates for the mouse event.
2313 */
2314hterm.Terminal.prototype.onMouse_ = function(e) {
rginda4bba5e12012-06-20 16:15:30 -07002315 if (e.type == 'mousedown' && e.which == this.mousePasteButton) {
2316 this.paste();
2317 return;
2318 }
2319
2320 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2321 !this.document_.getSelection().isCollapsed) {
2322 this.copySelectionToClipboard();
2323 return;
2324 }
2325
rgindad5613292012-06-19 15:40:37 -07002326 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2327 this.scrollPort_.characterSize.height) + 1;
2328 e.terminalColumn = parseInt(e.clientX /
2329 this.scrollPort_.characterSize.width) + 1;
2330
2331 if (e.type == 'mousedown') {
2332 if (e.terminalColumn > this.screenSize.width) {
2333 // Mousedown in the scrollbar area.
2334 return;
2335 }
2336
2337 if (!this.enableMouseDragScroll) {
2338 // Move the scroll-blocker into place if we want to keep the scrollport
2339 // from scrolling.
2340 this.scrollBlockerNode_.engaged = true;
2341 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2342 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2343 }
2344 } else if (this.scrollBlockerNode_.engaged &&
2345 (e.type == 'mousemove' || e.type == 'mouseup')) {
2346 // Disengage the scroll-blocker after one of these events.
2347 this.scrollBlockerNode_.engaged = false;
2348 this.scrollBlockerNode_.style.top = '-99px';
2349 }
2350
2351 if (!e.processedByTerminalHandler_) {
2352 // We register our event handlers on the document, as well as the cursor
2353 // and the scroll blocker. Mouse events that occur on the cursor or
2354 // scroll blocker will also appear on the document, but we don't want to
2355 // process them twice.
2356 //
2357 // We can't just prevent bubbling because that has other side effects, so
2358 // we decorate the event object with this property instead.
2359 e.processedByTerminalHandler_ = true;
2360
2361 this.onMouse(e);
2362 }
2363};
2364
2365/**
2366 * Clients should override this if they care to know about mouse events.
2367 *
2368 * The event parameter will be a normal DOM mouse click event with additional
2369 * 'terminalRow' and 'terminalColumn' properties.
2370 */
2371hterm.Terminal.prototype.onMouse = function(e) { };
2372
2373/**
rginda8e92a692012-05-20 19:37:20 -07002374 * React when focus changes.
2375 */
2376hterm.Terminal.prototype.onFocusChange_ = function(state) {
2377 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
2378};
2379
2380/**
rginda8ba33642011-12-14 12:31:31 -08002381 * React when the ScrollPort is scrolled.
2382 */
2383hterm.Terminal.prototype.onScroll_ = function() {
2384 this.scheduleSyncCursorPosition_();
2385};
2386
2387/**
rginda9846e2f2012-01-27 13:53:33 -08002388 * React when text is pasted into the scrollPort.
2389 */
2390hterm.Terminal.prototype.onPaste_ = function(e) {
David Benjamin8f962172012-07-17 07:38:43 -04002391 this.io.onVTKeystroke(this.vt.encodeUTF8(e.text));
rginda9846e2f2012-01-27 13:53:33 -08002392};
2393
2394/**
rginda8ba33642011-12-14 12:31:31 -08002395 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002396 *
2397 * Note: This function should not directly contain code that alters the internal
2398 * state of the terminal. That kind of code belongs in realizeWidth or
2399 * realizeHeight, so that it can be executed synchronously in the case of a
2400 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002401 */
2402hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002403 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002404 this.scrollPort_.characterSize.width);
2405 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2406 this.scrollPort_.characterSize.height);
2407
2408 if (!(columnCount || rowCount)) {
2409 // We avoid these situations since they happen sometimes when the terminal
2410 // gets removed from the document, and we can't deal with that.
2411 return;
2412 }
2413
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002414 this.realizeSize_(columnCount, rowCount);
rgindac9bc5502012-01-18 11:48:44 -08002415 this.scheduleSyncCursorPosition_();
rgindaf522ce02012-04-17 17:49:17 -07002416 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaf0090c92012-02-10 14:58:52 -08002417 this.overlaySize();
rginda8ba33642011-12-14 12:31:31 -08002418};
2419
2420/**
2421 * Service the cursor blink timeout.
2422 */
2423hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002424 if (this.cursorNode_.style.opacity == '0') {
2425 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002426 } else {
rginda87b86462011-12-14 13:48:03 -08002427 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002428 }
2429};
David Reveman8f552492012-03-28 12:18:41 -04002430
2431/**
2432 * Set the scrollbar-visible mode bit.
2433 *
2434 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2435 * Otherwise it will not.
2436 *
2437 * Defaults to on.
2438 *
2439 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2440 */
2441hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2442 this.scrollPort_.setScrollbarVisible(state);
2443};