blob: 1776940ebbdf863f20c8c2fc5507666c8e2fdbf6 [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) {
rgindaa9abdd82012-08-06 18:05:09 -07001281 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001282
rgindaa9abdd82012-08-06 18:05:09 -07001283 while (startOffset < str.length) {
1284 if (this.options_.wraparound && this.screen_.cursorPosition.overflow)
rginda35c456b2012-02-09 17:29:05 -08001285 this.newLine();
rgindaa19afe22012-01-25 15:40:22 -08001286
rgindaa9abdd82012-08-06 18:05:09 -07001287 var count = str.length - startOffset;
1288 var didOverflow = false;
1289 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001290
rgindaa9abdd82012-08-06 18:05:09 -07001291 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1292 didOverflow = true;
1293 count = this.screenSize.width - this.screen_.cursorPosition.column;
1294 }
rgindaa19afe22012-01-25 15:40:22 -08001295
rgindaa9abdd82012-08-06 18:05:09 -07001296 if (didOverflow && !this.options_.wraparound) {
1297 // If the string overflowed the line but wraparound is off, then the
1298 // last printed character should be the last of the string.
1299 // TODO: This will add to our problems with multibyte UTF-16 characters.
1300 substr = str.substr(startOffset, count - 1) +
1301 str.substr(str.length - 1);
1302 count = str.length;
1303 } else {
1304 substr = str.substr(startOffset, count);
1305 }
rgindaa19afe22012-01-25 15:40:22 -08001306
rgindaa9abdd82012-08-06 18:05:09 -07001307 if (this.options_.insertMode) {
1308 this.screen_.insertString(substr);
1309 } else {
1310 this.screen_.overwriteString(substr);
1311 }
1312
1313 this.screen_.maybeClipCurrentRow();
1314 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001315 }
rginda8ba33642011-12-14 12:31:31 -08001316
1317 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001318
rginda9f5222b2012-03-05 11:53:28 -08001319 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001320 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001321};
1322
1323/**
rginda87b86462011-12-14 13:48:03 -08001324 * Set the VT scroll region.
1325 *
rginda87b86462011-12-14 13:48:03 -08001326 * This also resets the cursor position to the absolute (0, 0) position, since
1327 * that's what xterm appears to do.
1328 *
1329 * @param {integer} scrollTop The zero-based top of the scroll region.
1330 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1331 * inclusive.
1332 */
1333hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1334 this.vtScrollTop_ = scrollTop;
1335 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001336};
1337
1338/**
rginda8ba33642011-12-14 12:31:31 -08001339 * Return the top row index according to the VT.
1340 *
1341 * This will return 0 unless the terminal has been told to restrict scrolling
1342 * to some lower row. It is used for some VT cursor positioning and scrolling
1343 * commands.
1344 *
1345 * @return {integer} The topmost row in the terminal's scroll region.
1346 */
1347hterm.Terminal.prototype.getVTScrollTop = function() {
1348 if (this.vtScrollTop_ != null)
1349 return this.vtScrollTop_;
1350
1351 return 0;
rginda87b86462011-12-14 13:48:03 -08001352};
rginda8ba33642011-12-14 12:31:31 -08001353
1354/**
1355 * Return the bottom row index according to the VT.
1356 *
1357 * This will return the height of the terminal unless the it has been told to
1358 * restrict scrolling to some higher row. It is used for some VT cursor
1359 * positioning and scrolling commands.
1360 *
1361 * @return {integer} The bottommost row in the terminal's scroll region.
1362 */
1363hterm.Terminal.prototype.getVTScrollBottom = function() {
1364 if (this.vtScrollBottom_ != null)
1365 return this.vtScrollBottom_;
1366
rginda87b86462011-12-14 13:48:03 -08001367 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001368}
1369
1370/**
1371 * Process a '\n' character.
1372 *
1373 * If the cursor is on the final row of the terminal this will append a new
1374 * blank row to the screen and scroll the topmost row into the scrollback
1375 * buffer.
1376 *
1377 * Otherwise, this moves the cursor to column zero of the next row.
1378 */
1379hterm.Terminal.prototype.newLine = function() {
1380 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001381 // If we're at the end of the screen we need to append a new line and
1382 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001383 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001384 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1385 // End of the scroll region does not affect the scrollback buffer.
1386 this.vtScrollUp(1);
1387 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001388 } else {
rginda87b86462011-12-14 13:48:03 -08001389 // Anywhere else in the screen just moves the cursor.
1390 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001391 }
1392};
1393
1394/**
1395 * Like newLine(), except maintain the cursor column.
1396 */
1397hterm.Terminal.prototype.lineFeed = function() {
1398 var column = this.screen_.cursorPosition.column;
1399 this.newLine();
1400 this.setCursorColumn(column);
1401};
1402
1403/**
rginda87b86462011-12-14 13:48:03 -08001404 * If autoCarriageReturn is set then newLine(), else lineFeed().
1405 */
1406hterm.Terminal.prototype.formFeed = function() {
1407 if (this.options_.autoCarriageReturn) {
1408 this.newLine();
1409 } else {
1410 this.lineFeed();
1411 }
1412};
1413
1414/**
1415 * Move the cursor up one row, possibly inserting a blank line.
1416 *
1417 * The cursor column is not changed.
1418 */
1419hterm.Terminal.prototype.reverseLineFeed = function() {
1420 var scrollTop = this.getVTScrollTop();
1421 var currentRow = this.screen_.cursorPosition.row;
1422
1423 if (currentRow == scrollTop) {
1424 this.insertLines(1);
1425 } else {
1426 this.setAbsoluteCursorRow(currentRow - 1);
1427 }
1428};
1429
1430/**
rginda8ba33642011-12-14 12:31:31 -08001431 * Replace all characters to the left of the current cursor with the space
1432 * character.
1433 *
1434 * TODO(rginda): This should probably *remove* the characters (not just replace
1435 * with a space) if there are no characters at or beyond the current cursor
1436 * position. Once it does that, it'll have the same text-attribute related
1437 * issues as hterm.Screen.prototype.clearCursorRow :/
1438 */
1439hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001440 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001441 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001442 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001443 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001444};
1445
1446/**
David Benjamin684a9b72012-05-01 17:19:58 -04001447 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001448 *
1449 * The cursor position is unchanged.
1450 *
1451 * TODO(rginda): Test that this works even when the cursor is positioned beyond
1452 * the end of the text.
1453 *
1454 * TODO(rginda): This likely has text-attribute related troubles similar to the
1455 * todo on hterm.Screen.prototype.clearCursorRow.
David Benjamin684a9b72012-05-01 17:19:58 -04001456 *
1457 * TODO(davidben): Probably better to not add the whitespace to the clipboard
1458 * if erasing to the end of the drawn portion of the line. That said, xterm
1459 * behaves the same here.
rginda8ba33642011-12-14 12:31:31 -08001460 */
1461hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001462 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001463
rginda87b86462011-12-14 13:48:03 -08001464 var maxCount = this.screenSize.width - cursor.column;
David Benjamin684a9b72012-05-01 17:19:58 -04001465 if (opt_count === undefined || opt_count >= maxCount) {
1466 this.screen_.deleteChars(maxCount);
1467 } else {
rgindacbbd7482012-06-13 15:06:16 -07001468 this.screen_.overwriteString(lib.f.getWhitespace(opt_count));
David Benjamin684a9b72012-05-01 17:19:58 -04001469 }
rginda87b86462011-12-14 13:48:03 -08001470 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001471 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001472};
1473
1474/**
1475 * Erase the current line.
1476 *
1477 * The cursor position is unchanged.
1478 *
1479 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1480 * has a text-attribute related TODO.
1481 */
1482hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001483 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001484 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001485 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001486 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001487};
1488
1489/**
David Benjamina08d78f2012-05-05 00:28:49 -04001490 * Erase all characters from the start of the screen to the current cursor
1491 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001492 *
1493 * The cursor position is unchanged.
1494 *
1495 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1496 * has a text-attribute related TODO.
1497 */
1498hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001499 var cursor = this.saveCursor();
1500
1501 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001502
David Benjamina08d78f2012-05-05 00:28:49 -04001503 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001504 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001505 this.screen_.clearCursorRow();
1506 }
1507
rginda87b86462011-12-14 13:48:03 -08001508 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001509 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001510};
1511
1512/**
1513 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001514 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001515 *
1516 * The cursor position is unchanged.
1517 *
1518 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1519 * has a text-attribute related TODO.
1520 */
1521hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001522 var cursor = this.saveCursor();
1523
1524 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001525
David Benjamina08d78f2012-05-05 00:28:49 -04001526 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001527 for (var i = cursor.row + 1; i <= bottom; i++) {
1528 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001529 this.screen_.clearCursorRow();
1530 }
1531
rginda87b86462011-12-14 13:48:03 -08001532 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001533 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001534};
1535
1536/**
1537 * Fill the terminal with a given character.
1538 *
1539 * This methods does not respect the VT scroll region.
1540 *
1541 * @param {string} ch The character to use for the fill.
1542 */
1543hterm.Terminal.prototype.fill = function(ch) {
1544 var cursor = this.saveCursor();
1545
1546 this.setAbsoluteCursorPosition(0, 0);
1547 for (var row = 0; row < this.screenSize.height; row++) {
1548 for (var col = 0; col < this.screenSize.width; col++) {
1549 this.setAbsoluteCursorPosition(row, col);
1550 this.screen_.overwriteString(ch);
1551 }
1552 }
1553
1554 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001555};
1556
1557/**
rginda9ea433c2012-03-16 11:57:00 -07001558 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001559 *
rginda9ea433c2012-03-16 11:57:00 -07001560 * This does not respect the scroll region.
1561 *
1562 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1563 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001564 *
1565 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1566 * has a text-attribute related TODO.
1567 */
rginda9ea433c2012-03-16 11:57:00 -07001568hterm.Terminal.prototype.clearHome = function(opt_screen) {
1569 var screen = opt_screen || this.screen_;
1570 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001571
rginda11057d52012-04-25 12:29:56 -07001572 if (bottom == 0) {
1573 // Empty screen, nothing to do.
1574 return;
1575 }
1576
rgindae4d29232012-01-19 10:47:13 -08001577 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001578 screen.setCursorPosition(i, 0);
1579 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001580 }
1581
rginda9ea433c2012-03-16 11:57:00 -07001582 screen.setCursorPosition(0, 0);
1583};
1584
1585/**
1586 * Erase the entire display without changing the cursor position.
1587 *
1588 * The cursor position is unchanged. This does not respect the scroll
1589 * region.
1590 *
1591 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1592 * to the current screen.
1593 *
1594 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1595 * has a text-attribute related TODO.
1596 */
1597hterm.Terminal.prototype.clear = function(opt_screen) {
1598 var screen = opt_screen || this.screen_;
1599 var cursor = screen.cursorPosition.clone();
1600 this.clearHome(screen);
1601 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001602};
1603
1604/**
1605 * VT command to insert lines at the current cursor row.
1606 *
1607 * This respects the current scroll region. Rows pushed off the bottom are
1608 * lost (they won't show up in the scrollback buffer).
1609 *
1610 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1611 * has a text-attribute related TODO.
1612 *
1613 * @param {integer} count The number of lines to insert.
1614 */
1615hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001616 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001617
1618 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001619 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001620
rgindae4d29232012-01-19 10:47:13 -08001621 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001622 if (start != cursor.row)
1623 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001624
1625 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001626 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001627 this.screen_.clearCursorRow();
1628 }
1629
rginda87b86462011-12-14 13:48:03 -08001630 cursor.column = 0;
1631 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001632};
1633
1634/**
1635 * VT command to delete lines at the current cursor row.
1636 *
1637 * New rows are added to the bottom of scroll region to take their place. New
1638 * rows are strictly there to take up space and have no content or style.
1639 */
1640hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001641 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001642
rginda87b86462011-12-14 13:48:03 -08001643 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001644 var bottom = this.getVTScrollBottom();
1645
rginda87b86462011-12-14 13:48:03 -08001646 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001647 count = Math.min(count, maxCount);
1648
rginda87b86462011-12-14 13:48:03 -08001649 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001650 if (count != maxCount)
1651 this.moveRows_(top, count, moveStart);
1652
1653 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001654 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001655 this.screen_.clearCursorRow();
1656 }
1657
rginda87b86462011-12-14 13:48:03 -08001658 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001659 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001660};
1661
1662/**
1663 * Inserts the given number of spaces at the current cursor position.
1664 *
rginda87b86462011-12-14 13:48:03 -08001665 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001666 */
1667hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001668 var cursor = this.saveCursor();
1669
rgindacbbd7482012-06-13 15:06:16 -07001670 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001671 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001672 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001673
1674 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001675 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001676};
1677
1678/**
1679 * Forward-delete the specified number of characters starting at the cursor
1680 * position.
1681 *
1682 * @param {integer} count The number of characters to delete.
1683 */
1684hterm.Terminal.prototype.deleteChars = function(count) {
1685 this.screen_.deleteChars(count);
David Benjamin54e8bf62012-06-01 22:31:40 -04001686 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001687};
1688
1689/**
1690 * Shift rows in the scroll region upwards by a given number of lines.
1691 *
1692 * New rows are inserted at the bottom of the scroll region to fill the
1693 * vacated rows. The new rows not filled out with the current text attributes.
1694 *
1695 * This function does not affect the scrollback rows at all. Rows shifted
1696 * off the top are lost.
1697 *
rginda87b86462011-12-14 13:48:03 -08001698 * The cursor position is not altered.
1699 *
rginda8ba33642011-12-14 12:31:31 -08001700 * @param {integer} count The number of rows to scroll.
1701 */
1702hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001703 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001704
rginda87b86462011-12-14 13:48:03 -08001705 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001706 this.deleteLines(count);
1707
rginda87b86462011-12-14 13:48:03 -08001708 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001709};
1710
1711/**
1712 * Shift rows below the cursor down by a given number of lines.
1713 *
1714 * This function respects the current scroll region.
1715 *
1716 * New rows are inserted at the top of the scroll region to fill the
1717 * vacated rows. The new rows not filled out with the current text attributes.
1718 *
1719 * This function does not affect the scrollback rows at all. Rows shifted
1720 * off the bottom are lost.
1721 *
1722 * @param {integer} count The number of rows to scroll.
1723 */
1724hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001725 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001726
rginda87b86462011-12-14 13:48:03 -08001727 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001728 this.insertLines(opt_count);
1729
rginda87b86462011-12-14 13:48:03 -08001730 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001731};
1732
rginda87b86462011-12-14 13:48:03 -08001733
rginda8ba33642011-12-14 12:31:31 -08001734/**
1735 * Set the cursor position.
1736 *
1737 * The cursor row is relative to the scroll region if the terminal has
1738 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1739 *
1740 * @param {integer} row The new zero-based cursor row.
1741 * @param {integer} row The new zero-based cursor column.
1742 */
1743hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1744 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001745 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001746 } else {
rginda87b86462011-12-14 13:48:03 -08001747 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001748 }
rginda87b86462011-12-14 13:48:03 -08001749};
rginda8ba33642011-12-14 12:31:31 -08001750
rginda87b86462011-12-14 13:48:03 -08001751hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1752 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001753 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1754 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001755 this.screen_.setCursorPosition(row, column);
1756};
1757
1758hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001759 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1760 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001761 this.screen_.setCursorPosition(row, column);
1762};
1763
1764/**
1765 * Set the cursor column.
1766 *
1767 * @param {integer} column The new zero-based cursor column.
1768 */
1769hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001770 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001771};
1772
1773/**
1774 * Return the cursor column.
1775 *
1776 * @return {integer} The zero-based cursor column.
1777 */
1778hterm.Terminal.prototype.getCursorColumn = function() {
1779 return this.screen_.cursorPosition.column;
1780};
1781
1782/**
1783 * Set the cursor row.
1784 *
1785 * The cursor row is relative to the scroll region if the terminal has
1786 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1787 *
1788 * @param {integer} row The new cursor row.
1789 */
rginda87b86462011-12-14 13:48:03 -08001790hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1791 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001792};
1793
1794/**
1795 * Return the cursor row.
1796 *
1797 * @return {integer} The zero-based cursor row.
1798 */
1799hterm.Terminal.prototype.getCursorRow = function(row) {
1800 return this.screen_.cursorPosition.row;
1801};
1802
1803/**
1804 * Request that the ScrollPort redraw itself soon.
1805 *
1806 * The redraw will happen asynchronously, soon after the call stack winds down.
1807 * Multiple calls will be coalesced into a single redraw.
1808 */
1809hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001810 if (this.timeouts_.redraw)
1811 return;
rginda8ba33642011-12-14 12:31:31 -08001812
1813 var self = this;
rginda87b86462011-12-14 13:48:03 -08001814 this.timeouts_.redraw = setTimeout(function() {
1815 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001816 self.scrollPort_.redraw_();
1817 }, 0);
1818};
1819
1820/**
1821 * Request that the ScrollPort be scrolled to the bottom.
1822 *
1823 * The scroll will happen asynchronously, soon after the call stack winds down.
1824 * Multiple calls will be coalesced into a single scroll.
1825 *
1826 * This affects the scrollbar position of the ScrollPort, and has nothing to
1827 * do with the VT scroll commands.
1828 */
1829hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1830 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001831 return;
rginda8ba33642011-12-14 12:31:31 -08001832
1833 var self = this;
1834 this.timeouts_.scrollDown = setTimeout(function() {
1835 delete self.timeouts_.scrollDown;
1836 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1837 }, 10);
1838};
1839
1840/**
1841 * Move the cursor up a specified number of rows.
1842 *
1843 * @param {integer} count The number of rows to move the cursor.
1844 */
1845hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001846 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001847};
1848
1849/**
1850 * Move the cursor down a specified number of rows.
1851 *
1852 * @param {integer} count The number of rows to move the cursor.
1853 */
1854hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001855 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001856 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1857 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1858 this.screenSize.height - 1);
1859
rgindacbbd7482012-06-13 15:06:16 -07001860 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001861 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001862 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001863};
1864
1865/**
1866 * Move the cursor left a specified number of columns.
1867 *
1868 * @param {integer} count The number of columns to move the cursor.
1869 */
1870hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001871 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001872};
1873
1874/**
1875 * Move the cursor right a specified number of columns.
1876 *
1877 * @param {integer} count The number of columns to move the cursor.
1878 */
1879hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001880 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001881 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001882 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001883 this.setCursorColumn(column);
1884};
1885
1886/**
1887 * Reverse the foreground and background colors of the terminal.
1888 *
1889 * This only affects text that was drawn with no attributes.
1890 *
1891 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1892 * been drawn with attributes that happen to coincide with the default
1893 * 'no-attribute' colors. My guess is probably not.
1894 */
1895hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001896 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001897 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001898 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1899 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001900 } else {
rginda9f5222b2012-03-05 11:53:28 -08001901 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1902 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001903 }
1904};
1905
1906/**
rginda87b86462011-12-14 13:48:03 -08001907 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001908 */
1909hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001910 if (this.bellAudio_.getAttribute('src'))
1911 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001912
rginda6d397402012-01-17 10:58:29 -08001913 this.cursorNode_.style.backgroundColor =
1914 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001915
1916 var self = this;
1917 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001918 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001919 }, 200);
rginda87b86462011-12-14 13:48:03 -08001920};
1921
1922/**
rginda8ba33642011-12-14 12:31:31 -08001923 * Set the origin mode bit.
1924 *
1925 * If origin mode is on, certain VT cursor and scrolling commands measure their
1926 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1927 * to the top of the addressable screen.
1928 *
1929 * Defaults to off.
1930 *
1931 * @param {boolean} state True to set origin mode, false to unset.
1932 */
1933hterm.Terminal.prototype.setOriginMode = function(state) {
1934 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001935 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001936};
1937
1938/**
1939 * Set the insert mode bit.
1940 *
1941 * If insert mode is on, existing text beyond the cursor position will be
1942 * shifted right to make room for new text. Otherwise, new text overwrites
1943 * any existing text.
1944 *
1945 * Defaults to off.
1946 *
1947 * @param {boolean} state True to set insert mode, false to unset.
1948 */
1949hterm.Terminal.prototype.setInsertMode = function(state) {
1950 this.options_.insertMode = state;
1951};
1952
1953/**
rginda87b86462011-12-14 13:48:03 -08001954 * Set the auto carriage return bit.
1955 *
1956 * If auto carriage return is on then a formfeed character is interpreted
1957 * as a newline, otherwise it's the same as a linefeed. The difference boils
1958 * down to whether or not the cursor column is reset.
1959 */
1960hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1961 this.options_.autoCarriageReturn = state;
1962};
1963
1964/**
rginda8ba33642011-12-14 12:31:31 -08001965 * Set the wraparound mode bit.
1966 *
1967 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1968 * to the start of the following row. Otherwise, the cursor is clamped to the
1969 * end of the screen and attempts to write past it are ignored.
1970 *
1971 * Defaults to on.
1972 *
1973 * @param {boolean} state True to set wraparound mode, false to unset.
1974 */
1975hterm.Terminal.prototype.setWraparound = function(state) {
1976 this.options_.wraparound = state;
1977};
1978
1979/**
1980 * Set the reverse-wraparound mode bit.
1981 *
1982 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1983 * to the end of the previous row. Otherwise, the cursor is clamped to column
1984 * 0.
1985 *
1986 * Defaults to off.
1987 *
1988 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1989 */
1990hterm.Terminal.prototype.setReverseWraparound = function(state) {
1991 this.options_.reverseWraparound = state;
1992};
1993
1994/**
1995 * Selects between the primary and alternate screens.
1996 *
1997 * If alternate mode is on, the alternate screen is active. Otherwise the
1998 * primary screen is active.
1999 *
2000 * Swapping screens has no effect on the scrollback buffer.
2001 *
2002 * Each screen maintains its own cursor position.
2003 *
2004 * Defaults to off.
2005 *
2006 * @param {boolean} state True to set alternate mode, false to unset.
2007 */
2008hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002009 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002010 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2011
rginda35c456b2012-02-09 17:29:05 -08002012 if (this.screen_.rowsArray.length &&
2013 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2014 // If the screen changed sizes while we were away, our rowIndexes may
2015 // be incorrect.
2016 var offset = this.scrollbackRows_.length;
2017 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002018 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002019 ary[i].rowIndex = offset + i;
2020 }
2021 }
rginda8ba33642011-12-14 12:31:31 -08002022
rginda35c456b2012-02-09 17:29:05 -08002023 this.realizeWidth_(this.screenSize.width);
2024 this.realizeHeight_(this.screenSize.height);
2025 this.scrollPort_.syncScrollHeight();
2026 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002027
rginda6d397402012-01-17 10:58:29 -08002028 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002029 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002030};
2031
2032/**
2033 * Set the cursor-blink mode bit.
2034 *
2035 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2036 * a visible cursor does not blink.
2037 *
2038 * You should make sure to turn blinking off if you're going to dispose of a
2039 * terminal, otherwise you'll leak a timeout.
2040 *
2041 * Defaults to on.
2042 *
2043 * @param {boolean} state True to set cursor-blink mode, false to unset.
2044 */
2045hterm.Terminal.prototype.setCursorBlink = function(state) {
2046 this.options_.cursorBlink = state;
2047
2048 if (!state && this.timeouts_.cursorBlink) {
2049 clearTimeout(this.timeouts_.cursorBlink);
2050 delete this.timeouts_.cursorBlink;
2051 }
2052
2053 if (this.options_.cursorVisible)
2054 this.setCursorVisible(true);
2055};
2056
2057/**
2058 * Set the cursor-visible mode bit.
2059 *
2060 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2061 *
2062 * Defaults to on.
2063 *
2064 * @param {boolean} state True to set cursor-visible mode, false to unset.
2065 */
2066hterm.Terminal.prototype.setCursorVisible = function(state) {
2067 this.options_.cursorVisible = state;
2068
2069 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002070 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002071 return;
2072 }
2073
rginda87b86462011-12-14 13:48:03 -08002074 this.syncCursorPosition_();
2075
2076 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002077
2078 if (this.options_.cursorBlink) {
2079 if (this.timeouts_.cursorBlink)
2080 return;
2081
2082 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2083 500);
2084 } else {
2085 if (this.timeouts_.cursorBlink) {
2086 clearTimeout(this.timeouts_.cursorBlink);
2087 delete this.timeouts_.cursorBlink;
2088 }
2089 }
2090};
2091
2092/**
rginda87b86462011-12-14 13:48:03 -08002093 * Synchronizes the visible cursor and document selection with the current
2094 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002095 */
2096hterm.Terminal.prototype.syncCursorPosition_ = function() {
2097 var topRowIndex = this.scrollPort_.getTopRowIndex();
2098 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2099 var cursorRowIndex = this.scrollbackRows_.length +
2100 this.screen_.cursorPosition.row;
2101
2102 if (cursorRowIndex > bottomRowIndex) {
2103 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002104 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002105 return;
2106 }
2107
rginda35c456b2012-02-09 17:29:05 -08002108 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
2109 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
2110
rginda8ba33642011-12-14 12:31:31 -08002111 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002112 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2113 'px';
2114 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2115 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002116
2117 this.cursorNode_.setAttribute('title',
2118 '(' + this.screen_.cursorPosition.row +
2119 ', ' + this.screen_.cursorPosition.column +
2120 ')');
2121
2122 // Update the caret for a11y purposes.
2123 var selection = this.document_.getSelection();
2124 if (selection && selection.isCollapsed)
2125 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002126};
2127
2128/**
2129 * Synchronizes the visible cursor with the current cursor coordinates.
2130 *
2131 * The sync will happen asynchronously, soon after the call stack winds down.
2132 * Multiple calls will be coalesced into a single sync.
2133 */
2134hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2135 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002136 return;
rginda8ba33642011-12-14 12:31:31 -08002137
2138 var self = this;
2139 this.timeouts_.syncCursor = setTimeout(function() {
2140 self.syncCursorPosition_();
2141 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002142 }, 0);
2143};
2144
rgindacc2996c2012-02-24 14:59:31 -08002145/**
rgindaf522ce02012-04-17 17:49:17 -07002146 * Show or hide the zoom warning.
2147 *
2148 * The zoom warning is a message warning the user that their browser zoom must
2149 * be set to 100% in order for hterm to function properly.
2150 *
2151 * @param {boolean} state True to show the message, false to hide it.
2152 */
2153hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2154 if (!this.zoomWarningNode_) {
2155 if (!state)
2156 return;
2157
2158 this.zoomWarningNode_ = this.document_.createElement('div');
2159 this.zoomWarningNode_.style.cssText = (
2160 'color: black;' +
2161 'background-color: #ff2222;' +
2162 'font-size: large;' +
2163 'border-radius: 8px;' +
2164 'opacity: 0.75;' +
2165 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2166 'top: 0.5em;' +
2167 'right: 1.2em;' +
2168 'position: absolute;' +
2169 '-webkit-text-size-adjust: none;' +
2170 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002171 }
2172
rgindade84e382012-04-20 15:39:31 -07002173 this.zoomWarningNode_.textContent = hterm.msg('ZOOM_WARNING') ||
2174 ('!! ' + parseInt(this.scrollPort_.characterSize.zoomFactor * 100) +
2175 '% !!');
rgindaf522ce02012-04-17 17:49:17 -07002176 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2177
2178 if (state) {
2179 if (!this.zoomWarningNode_.parentNode)
2180 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2181 } else if (this.zoomWarningNode_.parentNode) {
2182 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2183 }
2184};
2185
2186/**
rgindacc2996c2012-02-24 14:59:31 -08002187 * Show the terminal overlay for a given amount of time.
2188 *
2189 * The terminal overlay appears in inverse video in a large font, centered
2190 * over the terminal. You should probably keep the overlay message brief,
2191 * since it's in a large font and you probably aren't going to check the size
2192 * of the terminal first.
2193 *
2194 * @param {string} msg The text (not HTML) message to display in the overlay.
2195 * @param {number} opt_timeout The amount of time to wait before fading out
2196 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2197 * stay up forever (or until the next overlay).
2198 */
2199hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002200 if (!this.overlayNode_) {
2201 if (!this.div_)
2202 return;
2203
2204 this.overlayNode_ = this.document_.createElement('div');
2205 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002206 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002207 'font-size: xx-large;' +
2208 'opacity: 0.75;' +
2209 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2210 'position: absolute;' +
2211 '-webkit-user-select: none;' +
2212 '-webkit-transition: opacity 180ms ease-in;');
2213 }
2214
rginda9f5222b2012-03-05 11:53:28 -08002215 this.overlayNode_.style.color = this.prefs_.get('background-color');
2216 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2217 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2218
rgindaf0090c92012-02-10 14:58:52 -08002219 this.overlayNode_.textContent = msg;
2220 this.overlayNode_.style.opacity = '0.75';
2221
2222 if (!this.overlayNode_.parentNode)
2223 this.div_.appendChild(this.overlayNode_);
2224
2225 this.overlayNode_.style.top = (
2226 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
2227 this.overlayNode_.style.left = (
2228 this.div_.clientWidth - this.overlayNode_.clientWidth -
2229 this.scrollbarWidthPx) / 2;
2230
2231 var self = this;
2232
2233 if (this.overlayTimeout_)
2234 clearTimeout(this.overlayTimeout_);
2235
rgindacc2996c2012-02-24 14:59:31 -08002236 if (opt_timeout === null)
2237 return;
2238
rgindaf0090c92012-02-10 14:58:52 -08002239 this.overlayTimeout_ = setTimeout(function() {
2240 self.overlayNode_.style.opacity = '0';
2241 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002242 if (self.overlayNode_.parentNode)
2243 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002244 self.overlayTimeout_ = null;
2245 self.overlayNode_.style.opacity = '0.75';
2246 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002247 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002248};
2249
rginda4bba5e12012-06-20 16:15:30 -07002250/**
2251 * Paste from the system clipboard to the terminal.
2252 */
2253hterm.Terminal.prototype.paste = function() {
2254 hterm.pasteFromClipboard(this.document_);
2255};
2256
2257/**
2258 * Copy a string to the system clipboard.
2259 *
2260 * Note: If there is a selected range in the terminal, it'll be cleared.
2261 */
2262hterm.Terminal.prototype.copyStringToClipboard = function(str) {
2263 var copySource = this.document_.createElement('div');
2264 copySource.textContent = str;
2265 copySource.style.cssText = (
2266 '-webkit-user-select: text;' +
2267 'position: absolute;' +
2268 'top: -99px');
2269
2270 this.document_.body.appendChild(copySource);
2271 var selection = this.document_.getSelection();
2272 selection.selectAllChildren(copySource);
2273
2274 this.copySelectionToClipboard();
2275
2276 copySource.parentNode.removeChild(copySource);
2277};
2278
2279/**
2280 * Copy the current selection to the system clipboard, then clear it after a
2281 * short delay.
2282 */
2283hterm.Terminal.prototype.copySelectionToClipboard = function() {
2284 hterm.copySelectionToClipboard(this.document_);
2285 this.showOverlay(hterm.msg('NOTIFY_COPY'), 500);
2286
2287 if (this.copyTimeout_)
2288 clearTimeout(this.copyTimeout_);
2289
2290 this.copyTimeout_ = setTimeout(function() {
2291 var selection = this.document_.getSelection();
2292 if (!selection.isCollapsed)
2293 selection.collapseToEnd();
2294 this.copyTimeout_ = null;
2295 }.bind(this), 500);
2296};
2297
rgindaf0090c92012-02-10 14:58:52 -08002298hterm.Terminal.prototype.overlaySize = function() {
2299 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2300};
2301
rginda87b86462011-12-14 13:48:03 -08002302/**
2303 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2304 *
2305 * @param {string} string The VT string representing the keystroke.
2306 */
2307hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002308 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002309 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2310
2311 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08002312};
2313
2314/**
rgindad5613292012-06-19 15:40:37 -07002315 * Add the terminalRow and terminalColumn properties to mouse events and
2316 * then forward on to onMouse().
2317 *
2318 * The terminalRow and terminalColumn properties contain the (row, column)
2319 * coordinates for the mouse event.
2320 */
2321hterm.Terminal.prototype.onMouse_ = function(e) {
rginda4bba5e12012-06-20 16:15:30 -07002322 if (e.type == 'mousedown' && e.which == this.mousePasteButton) {
2323 this.paste();
2324 return;
2325 }
2326
2327 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2328 !this.document_.getSelection().isCollapsed) {
2329 this.copySelectionToClipboard();
2330 return;
2331 }
2332
rgindad5613292012-06-19 15:40:37 -07002333 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2334 this.scrollPort_.characterSize.height) + 1;
2335 e.terminalColumn = parseInt(e.clientX /
2336 this.scrollPort_.characterSize.width) + 1;
2337
2338 if (e.type == 'mousedown') {
2339 if (e.terminalColumn > this.screenSize.width) {
2340 // Mousedown in the scrollbar area.
2341 return;
2342 }
2343
2344 if (!this.enableMouseDragScroll) {
2345 // Move the scroll-blocker into place if we want to keep the scrollport
2346 // from scrolling.
2347 this.scrollBlockerNode_.engaged = true;
2348 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2349 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2350 }
2351 } else if (this.scrollBlockerNode_.engaged &&
2352 (e.type == 'mousemove' || e.type == 'mouseup')) {
2353 // Disengage the scroll-blocker after one of these events.
2354 this.scrollBlockerNode_.engaged = false;
2355 this.scrollBlockerNode_.style.top = '-99px';
2356 }
2357
2358 if (!e.processedByTerminalHandler_) {
2359 // We register our event handlers on the document, as well as the cursor
2360 // and the scroll blocker. Mouse events that occur on the cursor or
2361 // scroll blocker will also appear on the document, but we don't want to
2362 // process them twice.
2363 //
2364 // We can't just prevent bubbling because that has other side effects, so
2365 // we decorate the event object with this property instead.
2366 e.processedByTerminalHandler_ = true;
2367
2368 this.onMouse(e);
2369 }
2370};
2371
2372/**
2373 * Clients should override this if they care to know about mouse events.
2374 *
2375 * The event parameter will be a normal DOM mouse click event with additional
2376 * 'terminalRow' and 'terminalColumn' properties.
2377 */
2378hterm.Terminal.prototype.onMouse = function(e) { };
2379
2380/**
rginda8e92a692012-05-20 19:37:20 -07002381 * React when focus changes.
2382 */
2383hterm.Terminal.prototype.onFocusChange_ = function(state) {
2384 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
2385};
2386
2387/**
rginda8ba33642011-12-14 12:31:31 -08002388 * React when the ScrollPort is scrolled.
2389 */
2390hterm.Terminal.prototype.onScroll_ = function() {
2391 this.scheduleSyncCursorPosition_();
2392};
2393
2394/**
rginda9846e2f2012-01-27 13:53:33 -08002395 * React when text is pasted into the scrollPort.
2396 */
2397hterm.Terminal.prototype.onPaste_ = function(e) {
David Benjamin8f962172012-07-17 07:38:43 -04002398 this.io.onVTKeystroke(this.vt.encodeUTF8(e.text));
rginda9846e2f2012-01-27 13:53:33 -08002399};
2400
2401/**
rginda8ba33642011-12-14 12:31:31 -08002402 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002403 *
2404 * Note: This function should not directly contain code that alters the internal
2405 * state of the terminal. That kind of code belongs in realizeWidth or
2406 * realizeHeight, so that it can be executed synchronously in the case of a
2407 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002408 */
2409hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002410 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002411 this.scrollPort_.characterSize.width);
2412 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2413 this.scrollPort_.characterSize.height);
2414
2415 if (!(columnCount || rowCount)) {
2416 // We avoid these situations since they happen sometimes when the terminal
2417 // gets removed from the document, and we can't deal with that.
2418 return;
2419 }
2420
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002421 this.realizeSize_(columnCount, rowCount);
rgindac9bc5502012-01-18 11:48:44 -08002422 this.scheduleSyncCursorPosition_();
rgindaf522ce02012-04-17 17:49:17 -07002423 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaf0090c92012-02-10 14:58:52 -08002424 this.overlaySize();
rginda8ba33642011-12-14 12:31:31 -08002425};
2426
2427/**
2428 * Service the cursor blink timeout.
2429 */
2430hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002431 if (this.cursorNode_.style.opacity == '0') {
2432 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002433 } else {
rginda87b86462011-12-14 13:48:03 -08002434 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002435 }
2436};
David Reveman8f552492012-03-28 12:18:41 -04002437
2438/**
2439 * Set the scrollbar-visible mode bit.
2440 *
2441 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2442 * Otherwise it will not.
2443 *
2444 * Defaults to on.
2445 *
2446 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2447 */
2448hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2449 this.scrollPort_.setScrollbarVisible(state);
2450};