blob: bc95b462ccebd3efc6ba09afce91ddbef194de08 [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
7lib.rtdep('lib.colors', 'lib.PreferenceManager',
8 'hterm.msg',
9 'hterm.Keyboard', 'hterm.Options', 'hterm.Screen',
10 'hterm.ScrollPort', 'hterm.Size', 'hterm.VT');
11
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
29 * @param {string} opt_profileName Optional preference profile name. If not
30 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
rginda9f5222b2012-03-05 11:53:28 -080032hterm.Terminal = function(opt_profileName) {
33 this.profileName_ = null;
34 this.setProfile(opt_profileName || 'default');
35
rginda8ba33642011-12-14 12:31:31 -080036 // Two screen instances.
37 this.primaryScreen_ = new hterm.Screen();
38 this.alternateScreen_ = new hterm.Screen();
39
40 // The "current" screen.
41 this.screen_ = this.primaryScreen_;
42
rginda8ba33642011-12-14 12:31:31 -080043 // The local notion of the screen size. ScreenBuffers also have a size which
44 // indicates their present size. During size changes, the two may disagree.
45 // Also, the inactive screen's size is not altered until it is made the active
46 // screen.
47 this.screenSize = new hterm.Size(0, 0);
48
rginda8ba33642011-12-14 12:31:31 -080049 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080050 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080051 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
52 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080053 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070054 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080055
rginda87b86462011-12-14 13:48:03 -080056 // The div that contains this terminal.
57 this.div_ = null;
58
rgindac9bc5502012-01-18 11:48:44 -080059 // The document that contains the scrollPort. Defaulted to the global
60 // document here so that the terminal is functional even if it hasn't been
61 // inserted into a document yet, but re-set in decorate().
62 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080063
rginda8ba33642011-12-14 12:31:31 -080064 // The rows that have scrolled off screen and are no longer addressable.
65 this.scrollbackRows_ = [];
66
rgindac9bc5502012-01-18 11:48:44 -080067 // Saved tab stops.
68 this.tabStops_ = [];
69
David Benjamin66e954d2012-05-05 21:08:12 -040070 // Keep track of whether default tab stops have been erased; after a TBC
71 // clears all tab stops, defaults aren't restored on resize until a reset.
72 this.defaultTabStops = true;
73
rginda8ba33642011-12-14 12:31:31 -080074 // The VT's notion of the top and bottom rows. Used during some VT
75 // cursor positioning and scrolling commands.
76 this.vtScrollTop_ = null;
77 this.vtScrollBottom_ = null;
78
79 // The DIV element for the visible cursor.
80 this.cursorNode_ = null;
81
rginda9f5222b2012-03-05 11:53:28 -080082 // These prefs are cached so we don't have to read from local storage with
83 // each output and keystroke.
84 this.scrollOnOutput_ = this.prefs_.get('scroll-on-output');
85 this.scrollOnKeystroke_ = this.prefs_.get('scroll-on-keystroke');
rginda8e92a692012-05-20 19:37:20 -070086 this.foregroundColor_ = this.prefs_.get('foreground-color');
87 this.backgroundColor_ = this.prefs_.get('background-color');
rginda9f5222b2012-03-05 11:53:28 -080088
rgindaf0090c92012-02-10 14:58:52 -080089 // Terminal bell sound.
90 this.bellAudio_ = this.document_.createElement('audio');
rginda9f5222b2012-03-05 11:53:28 -080091 this.bellAudio_.setAttribute('src', this.prefs_.get('audible-bell-sound'));
rgindaf0090c92012-02-10 14:58:52 -080092 this.bellAudio_.setAttribute('preload', 'auto');
93
rginda6d397402012-01-17 10:58:29 -080094 // Cursor position and attributes saved with DECSC.
95 this.savedOptions_ = {};
96
rginda8ba33642011-12-14 12:31:31 -080097 // The current mode bits for the terminal.
98 this.options_ = new hterm.Options();
99
100 // Timeouts we might need to clear.
101 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800102
103 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800104 this.vt = new hterm.VT(this);
rginda11057d52012-04-25 12:29:56 -0700105 this.vt.enable8BitControl = this.prefs_.get('enable-8-bit-control');
106 this.vt.maxStringSequence = this.prefs_.get('max-string-sequence');
rgindaa8ba17d2012-08-15 14:41:10 -0700107 this.vt.enableClipboardWrite = this.prefs_.get('enable-clipboard-write');
rginda87b86462011-12-14 13:48:03 -0800108
rgindafeaf3142012-01-31 15:14:20 -0800109 // The keyboard hander.
110 this.keyboard = new hterm.Keyboard(this);
111
rginda87b86462011-12-14 13:48:03 -0800112 // General IO interface that can be given to third parties without exposing
113 // the entire terminal object.
114 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800115
rgindad5613292012-06-19 15:40:37 -0700116 // True if mouse-click-drag should scroll the terminal.
117 this.enableMouseDragScroll = true;
118
rginda4bba5e12012-06-20 16:15:30 -0700119 this.copyOnSelect = this.prefs_.get('copy-on-select');
120 this.mousePasteButton = null;
121 this.syncMousePasteButton();
122
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400123 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800124 this.setDefaultTabStops();
rginda87b86462011-12-14 13:48:03 -0800125};
126
127/**
rginda35c456b2012-02-09 17:29:05 -0800128 * Default tab with of 8 to match xterm.
129 */
130hterm.Terminal.prototype.tabWidth = 8;
131
132/**
rginda35c456b2012-02-09 17:29:05 -0800133 * The assumed width of a scrollbar.
134 */
135hterm.Terminal.prototype.scrollbarWidthPx = 16;
136
137/**
rginda9f5222b2012-03-05 11:53:28 -0800138 * Select a preference profile.
139 *
140 * This will load the terminal preferences for the given profile name and
141 * associate subsequent preference changes with the new preference profile.
142 *
143 * @param {string} newName The name of the preference profile. Forward slash
144 * characters will be removed from the name.
145 */
146hterm.Terminal.prototype.setProfile = function(profileName) {
147 // If we already have a profile selected, we're going to need to re-sync
148 // with the new profile.
149 var needSync = !!this.profileName_;
150
151 this.profileName_ = profileName.replace(/\//g, '');
152
rgindacbbd7482012-06-13 15:06:16 -0700153 this.prefs_ = new lib.PreferenceManager(
rginda9f5222b2012-03-05 11:53:28 -0800154 '/hterm/prefs/profiles/' + this.profileName_);
155
156 var self = this;
157 this.prefs_.definePreferences
rginda30f20f62012-04-05 16:36:19 -0700158 ([
159 /**
160 * Set whether the alt key acts as a meta key or as a distinct alt key.
rginda9f5222b2012-03-05 11:53:28 -0800161 */
rginda30f20f62012-04-05 16:36:19 -0700162 ['alt-is-meta', false, function(v) {
rgindaf9c36852012-05-09 11:08:39 -0700163 self.keyboard.altIsMeta = v;
rginda9f5222b2012-03-05 11:53:28 -0800164 }
165 ],
166
rginda30f20f62012-04-05 16:36:19 -0700167 /**
rginda39bdf6f2012-04-10 16:50:55 -0700168 * Controls how the alt key is handled.
169 *
170 * escape....... Send an ESC prefix.
171 * 8-bit........ Add 128 to the unshifted character as in xterm.
172 * browser-key.. Wait for the keypress event and see what the browser says.
173 * (This won't work well on platforms where the browser
174 * performs a default action for some alt sequences.)
rginda30f20f62012-04-05 16:36:19 -0700175 */
rginda39bdf6f2012-04-10 16:50:55 -0700176 ['alt-sends-what', 'escape', function(v) {
177 if (!/^(escape|8-bit|browser-key)$/.test(v))
178 v = 'escape';
179
rgindaf9c36852012-05-09 11:08:39 -0700180 self.keyboard.altSendsWhat = v;
rginda30f20f62012-04-05 16:36:19 -0700181 }
182 ],
183
184 /**
185 * Terminal bell sound. Empty string for no audible bell.
186 */
187 ['audible-bell-sound', '../audio/bell.ogg', function(v) {
188 self.bellAudio_.setAttribute('src', v);
189 }
190 ],
191
192 /**
193 * The background color for text with no other color attributes.
194 */
195 ['background-color', 'rgb(16, 16, 16)', function(v) {
rginda8e92a692012-05-20 19:37:20 -0700196 self.setBackgroundColor(v);
rginda9f5222b2012-03-05 11:53:28 -0800197 }
198 ],
199
200 /**
rginda30f20f62012-04-05 16:36:19 -0700201 * The background image.
rginda30f20f62012-04-05 16:36:19 -0700202 */
rginda8e92a692012-05-20 19:37:20 -0700203 ['background-image', '',
rginda30f20f62012-04-05 16:36:19 -0700204 function(v) {
205 self.scrollPort_.setBackgroundImage(v);
206 }
207 ],
208
209 /**
Philip Douglass959b49d2012-05-30 13:29:29 -0400210 * The background image size,
211 *
212 * Defaults to none.
213 */
214 ['background-size', '', function(v) {
215 self.scrollPort_.setBackgroundSize(v);
216 }
217 ],
218
219 /**
220 * The background image position,
221 *
222 * Defaults to none.
223 */
224 ['background-position', '', function(v) {
225 self.scrollPort_.setBackgroundPosition(v);
226 }
227 ],
228
229 /**
rginda30f20f62012-04-05 16:36:19 -0700230 * If true, the backspace should send BS ('\x08', aka ^H). Otherwise
231 * the backspace key should send '\x7f'.
232 */
233 ['backspace-sends-backspace', false, function(v) {
234 self.keyboard.backspaceSendsBackspace = v;
235 }
236 ],
237
238 /**
rginda9875d902012-08-20 16:21:57 -0700239 * Whether or not to close the window when the command exits.
240 */
241 ['close-on-exit', true, null],
242
243 /**
rgindade84e382012-04-20 15:39:31 -0700244 * Whether or not to blink the cursor by default.
245 */
246 ['cursor-blink', false, function(v) {
247 self.setCursorBlink(!!v);
248 }
249 ],
250
251 /**
rginda30f20f62012-04-05 16:36:19 -0700252 * The color of the visible cursor.
253 */
254 ['cursor-color', 'rgba(255,0,0,0.5)', function(v) {
rginda8e92a692012-05-20 19:37:20 -0700255 self.setCursorColor(v);
rginda30f20f62012-04-05 16:36:19 -0700256 }
257 ],
258
259 /**
rginda4bba5e12012-06-20 16:15:30 -0700260 * Automatically copy mouse selection to the clipboard.
261 */
262 ['copy-on-select', true, function(v) {
263 self.copyOnSelect = !!v;
264 }
265 ],
266
267 /**
rginda11057d52012-04-25 12:29:56 -0700268 * True to enable 8-bit control characters, false to ignore them.
269 *
270 * We'll respect the two-byte versions of these control characters
271 * regardless of this setting.
272 */
273 ['enable-8-bit-control', false, function(v) {
274 self.vt.enable8BitControl = !!v;
275 }
276 ],
277
278 /**
rginda30f20f62012-04-05 16:36:19 -0700279 * True if we should use bold weight font for text with the bold/bright
280 * attribute. False to use bright colors only. Null to autodetect.
281 */
282 ['enable-bold', null, function(v) {
283 self.syncBoldSafeState();
284 }
285 ],
286
287 /**
rgindaa8ba17d2012-08-15 14:41:10 -0700288 * Allow the host to write directly to the system clipboard.
289 */
Robert Ginda9fb38222012-09-11 14:19:12 -0700290 ['enable-clipboard-notice', true, null],
291
292 /**
293 * Allow the host to write directly to the system clipboard.
294 */
rgindaa8ba17d2012-08-15 14:41:10 -0700295 ['enable-clipboard-write', true, function(v) {
296 self.vt.enableClipboardWrite = !!v;
297 }
298 ],
299
300 /**
rginda9f5222b2012-03-05 11:53:28 -0800301 * Default font family for the terminal text.
302 */
303 ['font-family', ('"DejaVu Sans Mono", "Everson Mono", ' +
rgindaa8ba17d2012-08-15 14:41:10 -0700304 'FreeMono, "Menlo", "Terminal", ' +
rginda9f5222b2012-03-05 11:53:28 -0800305 'monospace'),
306 function(v) { self.syncFontFamily() }
307 ],
308
309 /**
rginda30f20f62012-04-05 16:36:19 -0700310 * The default font size in pixels.
311 */
312 ['font-size', 15, function(v) {
313 self.setFontSize(v);
314 }
315 ],
316
317 /**
rginda9f5222b2012-03-05 11:53:28 -0800318 * Anti-aliasing.
319 */
320 ['font-smoothing', 'antialiased',
321 function(v) { self.syncFontFamily() }
322 ],
323
324 /**
rginda30f20f62012-04-05 16:36:19 -0700325 * The foreground color for text with no other color attributes.
rginda9f5222b2012-03-05 11:53:28 -0800326 */
rginda30f20f62012-04-05 16:36:19 -0700327 ['foreground-color', 'rgb(240, 240, 240)', function(v) {
rginda8e92a692012-05-20 19:37:20 -0700328 self.setForegroundColor(v);
rginda9f5222b2012-03-05 11:53:28 -0800329 }
330 ],
331
332 /**
rginda30f20f62012-04-05 16:36:19 -0700333 * If true, home/end will control the terminal scrollbar and shift home/end
334 * will send the VT keycodes. If false then home/end sends VT codes and
335 * shift home/end scrolls.
rginda9f5222b2012-03-05 11:53:28 -0800336 */
rginda30f20f62012-04-05 16:36:19 -0700337 ['home-keys-scroll', false, function(v) {
338 self.keyboard.homeKeysScroll = v;
339 }
340 ],
341
342 /**
rginda11057d52012-04-25 12:29:56 -0700343 * Max length of a DCS, OSC, PM, or APS sequence before we give up and
344 * ignore the code.
345 */
Robert Ginda9fb38222012-09-11 14:19:12 -0700346 ['max-string-sequence', 100000, function(v) {
rginda11057d52012-04-25 12:29:56 -0700347 self.vt.maxStringSequence = v;
348 }
349 ],
350
351 /**
rginda30f20f62012-04-05 16:36:19 -0700352 * Set whether the meta key sends a leading escape or not.
353 */
354 ['meta-sends-escape', true, function(v) {
355 self.keyboard.metaSendsEscape = v;
rginda9f5222b2012-03-05 11:53:28 -0800356 }
357 ],
358
359 /**
rgindad5613292012-06-19 15:40:37 -0700360 * Set whether we should treat DEC mode 1002 (mouse cell motion tracking)
361 * as if it were 1000 (mouse click tracking).
362 *
363 * This makes it possible to use vi's ":set mouse=a" mode without losing
364 * access to the system text selection mechanism.
365 */
366 ['mouse-cell-motion-trick', false, function(v) {
367 self.vt.setMouseCellMotionTrick(v);
368 }
369 ],
370
371 /**
rginda4bba5e12012-06-20 16:15:30 -0700372 * Mouse paste button, or null to autodetect.
373 *
374 * For autodetect, we'll try to enable middle button paste for non-X11
375 * platforms.
376 *
377 * On X11 we move it to button 3, but that'll probably be a context menu
378 * in the future.
379 */
380 ['mouse-paste-button', null, function(v) {
381 self.syncMousePasteButton();
382 }
383 ],
384
385 /**
rginda9f5222b2012-03-05 11:53:28 -0800386 * If true, scroll to the bottom on any keystroke.
387 */
388 ['scroll-on-keystroke', true, function(v) {
389 self.scrollOnKeystroke_ = v;
390 }
391 ],
392
393 /**
394 * If true, scroll to the bottom on terminal output.
395 */
396 ['scroll-on-output', false, function(v) {
397 self.scrollOnOutput_ = v;
398 }
399 ],
400
401 /**
David Reveman8f552492012-03-28 12:18:41 -0400402 * The vertical scrollbar mode.
403 */
404 ['scrollbar-visible', true, function(v) {
405 self.setScrollbarVisible(v);
406 }
407 ],
rginda30f20f62012-04-05 16:36:19 -0700408
409 /**
rginda4bba5e12012-06-20 16:15:30 -0700410 * Shift + Insert pastes if true, sent to host if false.
411 */
412 ['shift-insert-paste', true, function(v) {
413 self.keyboard.shiftInsertPaste = v;
414 }
415 ],
416
417 /**
rgindaf522ce02012-04-17 17:49:17 -0700418 * The default environment variables.
419 */
420 ['environment', {TERM: 'xterm-256color'}, null],
421
422 /**
rginda30f20f62012-04-05 16:36:19 -0700423 * If true, page up/down will control the terminal scrollbar and shift
424 * page up/down will send the VT keycodes. If false then page up/down
425 * sends VT codes and shift page up/down scrolls.
426 */
427 ['page-keys-scroll', false, function(v) {
428 self.keyboard.pageKeysScroll = v;
429 }
430 ],
431
rginda9f5222b2012-03-05 11:53:28 -0800432 ]);
433
434 if (needSync)
435 this.prefs_.notifyAll();
436};
437
rginda8e92a692012-05-20 19:37:20 -0700438
439/**
440 * Set the color for the cursor.
441 *
442 * If you want this setting to persist, set it through prefs_, rather than
443 * with this method.
444 */
445hterm.Terminal.prototype.setCursorColor = function(color) {
446 this.cursorNode_.style.backgroundColor = color;
447 this.cursorNode_.style.borderColor = color;
448};
449
450/**
451 * Return the current cursor color as a string.
452 */
453hterm.Terminal.prototype.getCursorColor = function() {
454 return this.cursorNode_.style.backgroundColor;
455};
456
457/**
rgindad5613292012-06-19 15:40:37 -0700458 * Enable or disable mouse based text selection in the terminal.
459 */
460hterm.Terminal.prototype.setSelectionEnabled = function(state) {
461 this.enableMouseDragScroll = state;
462 this.scrollPort_.setSelectionEnabled(state);
463};
464
465/**
rginda8e92a692012-05-20 19:37:20 -0700466 * Set the background color.
467 *
468 * If you want this setting to persist, set it through prefs_, rather than
469 * with this method.
470 */
471hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700472 this.backgroundColor_ = lib.colors.normalizeCSS(color);
rginda8e92a692012-05-20 19:37:20 -0700473 this.scrollPort_.setBackgroundColor(color);
474};
475
rginda9f5222b2012-03-05 11:53:28 -0800476/**
477 * Return the current terminal background color.
478 *
479 * Intended for use by other classes, so we don't have to expose the entire
480 * prefs_ object.
481 */
482hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700483 return this.backgroundColor_;
484};
485
486/**
487 * Set the foreground color.
488 *
489 * If you want this setting to persist, set it through prefs_, rather than
490 * with this method.
491 */
492hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700493 this.foregroundColor_ = lib.colors.normalizeCSS(color);
rginda8e92a692012-05-20 19:37:20 -0700494 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800495};
496
497/**
498 * Return the current terminal foreground color.
499 *
500 * Intended for use by other classes, so we don't have to expose the entire
501 * prefs_ object.
502 */
503hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700504 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800505};
506
507/**
rginda87b86462011-12-14 13:48:03 -0800508 * Create a new instance of a terminal command and run it with a given
509 * argument string.
510 *
511 * @param {function} commandClass The constructor for a terminal command.
512 * @param {string} argString The argument string to pass to the command.
513 */
514hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700515 var environment = this.prefs_.get('environment');
516 if (typeof environment != 'object' || environment == null)
517 environment = {};
518
rginda87b86462011-12-14 13:48:03 -0800519 var self = this;
520 this.command = new commandClass(
521 { argString: argString || '',
522 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700523 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800524 onExit: function(code) {
525 self.io.pop();
526 self.io.println(hterm.msg('COMMAND_COMPLETE',
527 [self.command.commandName, code]));
rgindafeaf3142012-01-31 15:14:20 -0800528 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700529 if (self.prefs_.get('close-on-exit'))
530 window.close();
rginda87b86462011-12-14 13:48:03 -0800531 }
532 });
533
rgindafeaf3142012-01-31 15:14:20 -0800534 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800535 this.command.run();
536};
537
538/**
rgindafeaf3142012-01-31 15:14:20 -0800539 * Returns true if the current screen is the primary screen, false otherwise.
540 */
541hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700542 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800543};
544
545/**
546 * Install the keyboard handler for this terminal.
547 *
548 * This will prevent the browser from seeing any keystrokes sent to the
549 * terminal.
550 */
551hterm.Terminal.prototype.installKeyboard = function() {
552 this.keyboard.installKeyboard(this.document_.body.firstChild);
553}
554
555/**
556 * Uninstall the keyboard handler for this terminal.
557 */
558hterm.Terminal.prototype.uninstallKeyboard = function() {
559 this.keyboard.installKeyboard(null);
560}
561
562/**
rginda35c456b2012-02-09 17:29:05 -0800563 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800564 *
565 * Call setFontSize(0) to reset to the default font size.
566 *
567 * This function does not modify the font-size preference.
568 *
569 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800570 */
571hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800572 if (px === 0)
573 px = this.prefs_.get('font-size');
574
rginda35c456b2012-02-09 17:29:05 -0800575 this.scrollPort_.setFontSize(px);
576};
577
578/**
579 * Get the current font size.
580 */
581hterm.Terminal.prototype.getFontSize = function() {
582 return this.scrollPort_.getFontSize();
583};
584
585/**
rginda8e92a692012-05-20 19:37:20 -0700586 * Get the current font family.
587 */
588hterm.Terminal.prototype.getFontFamily = function() {
589 return this.scrollPort_.getFontFamily();
590};
591
592/**
rginda35c456b2012-02-09 17:29:05 -0800593 * Set the CSS "font-family" for this terminal.
594 */
rginda9f5222b2012-03-05 11:53:28 -0800595hterm.Terminal.prototype.syncFontFamily = function() {
596 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
597 this.prefs_.get('font-smoothing'));
598 this.syncBoldSafeState();
599};
600
rginda4bba5e12012-06-20 16:15:30 -0700601/**
602 * Set this.mousePasteButton based on the mouse-paste-button pref,
603 * autodetecting if necessary.
604 */
605hterm.Terminal.prototype.syncMousePasteButton = function() {
606 var button = this.prefs_.get('mouse-paste-button');
607 if (typeof button == 'number') {
608 this.mousePasteButton = button;
609 return;
610 }
611
612 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
613 if (!ary || ary[2] == 'CrOS') {
614 this.mousePasteButton = 2;
615 } else {
616 this.mousePasteButton = 3;
617 }
618};
619
620/**
621 * Enable or disable bold based on the enable-bold pref, autodetecting if
622 * necessary.
623 */
rginda9f5222b2012-03-05 11:53:28 -0800624hterm.Terminal.prototype.syncBoldSafeState = function() {
625 var enableBold = this.prefs_.get('enable-bold');
626 if (enableBold !== null) {
627 this.screen_.textAttributes.enableBold = enableBold;
628 return;
629 }
630
rgindaf7521392012-02-28 17:20:34 -0800631 var normalSize = this.scrollPort_.measureCharacterSize();
632 var boldSize = this.scrollPort_.measureCharacterSize('bold');
633
634 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800635 if (!isBoldSafe) {
636 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700637 'from normal. Font family is: ' +
638 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800639 }
rginda9f5222b2012-03-05 11:53:28 -0800640
641 this.screen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800642};
643
644/**
rginda87b86462011-12-14 13:48:03 -0800645 * Return a copy of the current cursor position.
646 *
647 * @return {hterm.RowCol} The RowCol object representing the current position.
648 */
649hterm.Terminal.prototype.saveCursor = function() {
650 return this.screen_.cursorPosition.clone();
651};
652
rgindaa19afe22012-01-25 15:40:22 -0800653hterm.Terminal.prototype.getTextAttributes = function() {
654 return this.screen_.textAttributes;
655};
656
rginda1a09aa02012-06-18 21:11:25 -0700657hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
658 this.screen_.textAttributes = textAttributes;
659};
660
rginda87b86462011-12-14 13:48:03 -0800661/**
rgindaf522ce02012-04-17 17:49:17 -0700662 * Return the current browser zoom factor applied to the terminal.
663 *
664 * @return {number} The current browser zoom factor.
665 */
666hterm.Terminal.prototype.getZoomFactor = function() {
667 return this.scrollPort_.characterSize.zoomFactor;
668};
669
670/**
rginda9846e2f2012-01-27 13:53:33 -0800671 * Change the title of this terminal's window.
672 */
673hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800674 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800675};
676
677/**
rginda87b86462011-12-14 13:48:03 -0800678 * Restore a previously saved cursor position.
679 *
680 * @param {hterm.RowCol} cursor The position to restore.
681 */
682hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700683 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
684 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800685 this.screen_.setCursorPosition(row, column);
686 if (cursor.column > column ||
687 cursor.column == column && cursor.overflow) {
688 this.screen_.cursorPosition.overflow = true;
689 }
rginda87b86462011-12-14 13:48:03 -0800690};
691
692/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400693 * Clear the cursor's overflow flag.
694 */
695hterm.Terminal.prototype.clearCursorOverflow = function() {
696 this.screen_.cursorPosition.overflow = false;
697};
698
699/**
rginda87b86462011-12-14 13:48:03 -0800700 * Set the width of the terminal, resizing the UI to match.
701 */
702hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800703 if (columnCount == null) {
704 this.div_.style.width = '100%';
705 return;
706 }
707
rginda35c456b2012-02-09 17:29:05 -0800708 this.div_.style.width = this.scrollPort_.characterSize.width *
709 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400710 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800711 this.scheduleSyncCursorPosition_();
712};
rginda87b86462011-12-14 13:48:03 -0800713
rgindac9bc5502012-01-18 11:48:44 -0800714/**
rginda35c456b2012-02-09 17:29:05 -0800715 * Set the height of the terminal, resizing the UI to match.
716 */
717hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800718 if (rowCount == null) {
719 this.div_.style.height = '100%';
720 return;
721 }
722
rginda35c456b2012-02-09 17:29:05 -0800723 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700724 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800725 this.realizeSize_(this.screenSize.width, rowCount);
726 this.scheduleSyncCursorPosition_();
727};
728
729/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400730 * Deal with terminal size changes.
731 *
732 */
733hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
734 if (columnCount != this.screenSize.width)
735 this.realizeWidth_(columnCount);
736
737 if (rowCount != this.screenSize.height)
738 this.realizeHeight_(rowCount);
739
740 // Send new terminal size to plugin.
741 this.io.onTerminalResize(columnCount, rowCount);
742};
743
744/**
rgindac9bc5502012-01-18 11:48:44 -0800745 * Deal with terminal width changes.
746 *
747 * This function does what needs to be done when the terminal width changes
748 * out from under us. It happens here rather than in onResize_() because this
749 * code may need to run synchronously to handle programmatic changes of
750 * terminal width.
751 *
752 * Relying on the browser to send us an async resize event means we may not be
753 * in the correct state yet when the next escape sequence hits.
754 */
755hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700756 if (columnCount <= 0)
757 throw new Error('Attempt to realize bad width: ' + columnCount);
758
rgindac9bc5502012-01-18 11:48:44 -0800759 var deltaColumns = columnCount - this.screen_.getWidth();
760
rginda87b86462011-12-14 13:48:03 -0800761 this.screenSize.width = columnCount;
762 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800763
764 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400765 if (this.defaultTabStops)
766 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800767 } else {
768 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400769 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800770 break;
771
772 this.tabStops_.pop();
773 }
774 }
775
776 this.screen_.setColumnCount(this.screenSize.width);
777};
778
779/**
780 * Deal with terminal height changes.
781 *
782 * This function does what needs to be done when the terminal height changes
783 * out from under us. It happens here rather than in onResize_() because this
784 * code may need to run synchronously to handle programmatic changes of
785 * terminal height.
786 *
787 * Relying on the browser to send us an async resize event means we may not be
788 * in the correct state yet when the next escape sequence hits.
789 */
790hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700791 if (rowCount <= 0)
792 throw new Error('Attempt to realize bad height: ' + rowCount);
793
rgindac9bc5502012-01-18 11:48:44 -0800794 var deltaRows = rowCount - this.screen_.getHeight();
795
796 this.screenSize.height = rowCount;
797
798 var cursor = this.saveCursor();
799
800 if (deltaRows < 0) {
801 // Screen got smaller.
802 deltaRows *= -1;
803 while (deltaRows) {
804 var lastRow = this.getRowCount() - 1;
805 if (lastRow - this.scrollbackRows_.length == cursor.row)
806 break;
807
808 if (this.getRowText(lastRow))
809 break;
810
811 this.screen_.popRow();
812 deltaRows--;
813 }
814
815 var ary = this.screen_.shiftRows(deltaRows);
816 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
817
818 // We just removed rows from the top of the screen, we need to update
819 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800820 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800821 } else if (deltaRows > 0) {
822 // Screen got larger.
823
824 if (deltaRows <= this.scrollbackRows_.length) {
825 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
826 var rows = this.scrollbackRows_.splice(
827 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
828 this.screen_.unshiftRows(rows);
829 deltaRows -= scrollbackCount;
830 cursor.row += scrollbackCount;
831 }
832
833 if (deltaRows)
834 this.appendRows_(deltaRows);
835 }
836
rginda35c456b2012-02-09 17:29:05 -0800837 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800838 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800839};
840
841/**
842 * Scroll the terminal to the top of the scrollback buffer.
843 */
844hterm.Terminal.prototype.scrollHome = function() {
845 this.scrollPort_.scrollRowToTop(0);
846};
847
848/**
849 * Scroll the terminal to the end.
850 */
851hterm.Terminal.prototype.scrollEnd = function() {
852 this.scrollPort_.scrollRowToBottom(this.getRowCount());
853};
854
855/**
856 * Scroll the terminal one page up (minus one line) relative to the current
857 * position.
858 */
859hterm.Terminal.prototype.scrollPageUp = function() {
860 var i = this.scrollPort_.getTopRowIndex();
861 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
862};
863
864/**
865 * Scroll the terminal one page down (minus one line) relative to the current
866 * position.
867 */
868hterm.Terminal.prototype.scrollPageDown = function() {
869 var i = this.scrollPort_.getTopRowIndex();
870 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800871};
872
rgindac9bc5502012-01-18 11:48:44 -0800873/**
874 * Full terminal reset.
875 */
rginda87b86462011-12-14 13:48:03 -0800876hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800877 this.clearAllTabStops();
878 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700879
880 this.clearHome(this.primaryScreen_);
881 this.primaryScreen_.textAttributes.reset();
882
883 this.clearHome(this.alternateScreen_);
884 this.alternateScreen_.textAttributes.reset();
885
rgindab8bc8932012-04-27 12:45:03 -0700886 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
887
rgindac9bc5502012-01-18 11:48:44 -0800888 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800889};
890
rgindac9bc5502012-01-18 11:48:44 -0800891/**
892 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700893 *
894 * Perform a soft reset to the default values listed in
895 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800896 */
rginda0f5c0292012-01-13 11:00:13 -0800897hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700898 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800899 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700900
rgindab8bc8932012-04-27 12:45:03 -0700901 // Xterm also resets the color palette on soft reset, even though it doesn't
902 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700903 this.primaryScreen_.textAttributes.resetColorPalette();
904 this.alternateScreen_.textAttributes.resetColorPalette();
905
rgindab8bc8932012-04-27 12:45:03 -0700906 // The xterm man page explicitly says this will happen on soft reset.
907 this.setVTScrollRegion(null, null);
908
909 // Xterm also shows the cursor on soft reset, but does not alter the blink
910 // state.
rgindaa19afe22012-01-25 15:40:22 -0800911 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800912};
913
rgindac9bc5502012-01-18 11:48:44 -0800914/**
915 * Move the cursor forward to the next tab stop, or to the last column
916 * if no more tab stops are set.
917 */
918hterm.Terminal.prototype.forwardTabStop = function() {
919 var column = this.screen_.cursorPosition.column;
920
921 for (var i = 0; i < this.tabStops_.length; i++) {
922 if (this.tabStops_[i] > column) {
923 this.setCursorColumn(this.tabStops_[i]);
924 return;
925 }
926 }
927
David Benjamin66e954d2012-05-05 21:08:12 -0400928 // xterm does not clear the overflow flag on HT or CHT.
929 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800930 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400931 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800932};
933
rgindac9bc5502012-01-18 11:48:44 -0800934/**
935 * Move the cursor backward to the previous tab stop, or to the first column
936 * if no previous tab stops are set.
937 */
938hterm.Terminal.prototype.backwardTabStop = function() {
939 var column = this.screen_.cursorPosition.column;
940
941 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
942 if (this.tabStops_[i] < column) {
943 this.setCursorColumn(this.tabStops_[i]);
944 return;
945 }
946 }
947
948 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800949};
950
rgindac9bc5502012-01-18 11:48:44 -0800951/**
952 * Set a tab stop at the given column.
953 *
954 * @param {int} column Zero based column.
955 */
956hterm.Terminal.prototype.setTabStop = function(column) {
957 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
958 if (this.tabStops_[i] == column)
959 return;
960
961 if (this.tabStops_[i] < column) {
962 this.tabStops_.splice(i + 1, 0, column);
963 return;
964 }
965 }
966
967 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800968};
969
rgindac9bc5502012-01-18 11:48:44 -0800970/**
971 * Clear the tab stop at the current cursor position.
972 *
973 * No effect if there is no tab stop at the current cursor position.
974 */
975hterm.Terminal.prototype.clearTabStopAtCursor = function() {
976 var column = this.screen_.cursorPosition.column;
977
978 var i = this.tabStops_.indexOf(column);
979 if (i == -1)
980 return;
981
982 this.tabStops_.splice(i, 1);
983};
984
985/**
986 * Clear all tab stops.
987 */
988hterm.Terminal.prototype.clearAllTabStops = function() {
989 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -0400990 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -0800991};
992
993/**
994 * Set up the default tab stops, starting from a given column.
995 *
996 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -0400997 * from the specified column, or 0 if no column is provided. It also flags
998 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -0800999 *
1000 * This does not clear the existing tab stops first, use clearAllTabStops
1001 * for that.
1002 *
1003 * @param {int} opt_start Optional starting zero based starting column, useful
1004 * for filling out missing tab stops when the terminal is resized.
1005 */
1006hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1007 var start = opt_start || 0;
1008 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001009 // Round start up to a default tab stop.
1010 start = start - 1 - ((start - 1) % w) + w;
1011 for (var i = start; i < this.screenSize.width; i += w) {
1012 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001013 }
David Benjamin66e954d2012-05-05 21:08:12 -04001014
1015 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001016};
1017
rginda6d397402012-01-17 10:58:29 -08001018/**
rginda8ba33642011-12-14 12:31:31 -08001019 * Interpret a sequence of characters.
1020 *
1021 * Incomplete escape sequences are buffered until the next call.
1022 *
1023 * @param {string} str Sequence of characters to interpret or pass through.
1024 */
1025hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001026 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001027 this.scheduleSyncCursorPosition_();
1028};
1029
1030/**
1031 * Take over the given DIV for use as the terminal display.
1032 *
1033 * @param {HTMLDivElement} div The div to use as the terminal display.
1034 */
1035hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001036 this.div_ = div;
1037
rginda8ba33642011-12-14 12:31:31 -08001038 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001039 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001040 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1041 this.scrollPort_.setBackgroundPosition(
1042 this.prefs_.get('background-position'));
rginda30f20f62012-04-05 16:36:19 -07001043
rginda0918b652012-04-04 11:26:24 -07001044 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001045
rginda9f5222b2012-03-05 11:53:28 -08001046 this.setFontSize(this.prefs_.get('font-size'));
1047 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001048
David Reveman8f552492012-03-28 12:18:41 -04001049 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1050
rginda8ba33642011-12-14 12:31:31 -08001051 this.document_ = this.scrollPort_.getDocument();
1052
rginda4bba5e12012-06-20 16:15:30 -07001053 this.document_.body.oncontextmenu = function() { return false };
1054
1055 var onMouse = this.onMouse_.bind(this);
1056 this.document_.body.firstChild.addEventListener('mousedown', onMouse);
1057 this.document_.body.firstChild.addEventListener('mouseup', onMouse);
1058 this.document_.body.firstChild.addEventListener('mousemove', onMouse);
1059 this.scrollPort_.onScrollWheel = onMouse;
1060
rginda8e92a692012-05-20 19:37:20 -07001061 this.document_.body.firstChild.addEventListener(
1062 'focus', this.onFocusChange_.bind(this, true));
1063 this.document_.body.firstChild.addEventListener(
1064 'blur', this.onFocusChange_.bind(this, false));
1065
1066 var style = this.document_.createElement('style');
1067 style.textContent =
1068 ('.cursor-node[focus="false"] {' +
1069 ' box-sizing: border-box;' +
1070 ' background-color: transparent !important;' +
1071 ' border-width: 2px;' +
1072 ' border-style: solid;' +
1073 '}');
1074 this.document_.head.appendChild(style);
1075
rginda8ba33642011-12-14 12:31:31 -08001076 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001077 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001078 this.cursorNode_.style.cssText =
1079 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001080 'top: -99px;' +
1081 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001082 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1083 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001084 '-webkit-transition: opacity, background-color 100ms linear;');
1085 this.setCursorColor(this.prefs_.get('cursor-color'));
rgindad5613292012-06-19 15:40:37 -07001086
rginda8ba33642011-12-14 12:31:31 -08001087 this.document_.body.appendChild(this.cursorNode_);
1088
rgindad5613292012-06-19 15:40:37 -07001089 // When 'enableMouseDragScroll' is off we reposition this element directly
1090 // under the mouse cursor after a click. This makes Chrome associate
1091 // subsequent mousemove events with the scroll-blocker. Since the
1092 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1093 // events do not cause the scrollport to scroll.
1094 //
1095 // It's a hack, but it's the cleanest way I could find.
1096 this.scrollBlockerNode_ = this.document_.createElement('div');
1097 this.scrollBlockerNode_.style.cssText =
1098 ('position: absolute;' +
1099 'top: -99px;' +
1100 'display: block;' +
1101 'width: 10px;' +
1102 'height: 10px;');
1103 this.document_.body.appendChild(this.scrollBlockerNode_);
1104
1105 var onMouse = this.onMouse_.bind(this);
1106 this.scrollPort_.onScrollWheel = onMouse;
1107 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1108 ].forEach(function(event) {
1109 this.scrollBlockerNode_.addEventListener(event, onMouse);
1110 this.cursorNode_.addEventListener(event, onMouse);
1111 this.document_.addEventListener(event, onMouse);
1112 }.bind(this));
1113
1114 this.cursorNode_.addEventListener('mousedown', function() {
1115 setTimeout(this.focus.bind(this));
1116 }.bind(this));
1117
rgindade84e382012-04-20 15:39:31 -07001118 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda8ba33642011-12-14 12:31:31 -08001119 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001120
rginda87b86462011-12-14 13:48:03 -08001121 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001122 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001123};
1124
rginda0918b652012-04-04 11:26:24 -07001125/**
1126 * Return the HTML document that contains the terminal DOM nodes.
1127 */
rginda87b86462011-12-14 13:48:03 -08001128hterm.Terminal.prototype.getDocument = function() {
1129 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001130};
1131
1132/**
rginda0918b652012-04-04 11:26:24 -07001133 * Focus the terminal.
1134 */
1135hterm.Terminal.prototype.focus = function() {
1136 this.scrollPort_.focus();
1137};
1138
1139/**
rginda8ba33642011-12-14 12:31:31 -08001140 * Return the HTML Element for a given row index.
1141 *
1142 * This is a method from the RowProvider interface. The ScrollPort uses
1143 * it to fetch rows on demand as they are scrolled into view.
1144 *
1145 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1146 * pairs to conserve memory.
1147 *
1148 * @param {integer} index The zero-based row index, measured relative to the
1149 * start of the scrollback buffer. On-screen rows will always have the
1150 * largest indicies.
1151 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1152 */
1153hterm.Terminal.prototype.getRowNode = function(index) {
1154 if (index < this.scrollbackRows_.length)
1155 return this.scrollbackRows_[index];
1156
1157 var screenIndex = index - this.scrollbackRows_.length;
1158 return this.screen_.rowsArray[screenIndex];
1159};
1160
1161/**
1162 * Return the text content for a given range of rows.
1163 *
1164 * This is a method from the RowProvider interface. The ScrollPort uses
1165 * it to fetch text content on demand when the user attempts to copy their
1166 * selection to the clipboard.
1167 *
1168 * @param {integer} start The zero-based row index to start from, measured
1169 * relative to the start of the scrollback buffer. On-screen rows will
1170 * always have the largest indicies.
1171 * @param {integer} end The zero-based row index to end on, measured
1172 * relative to the start of the scrollback buffer.
1173 * @return {string} A single string containing the text value of the range of
1174 * rows. Lines will be newline delimited, with no trailing newline.
1175 */
1176hterm.Terminal.prototype.getRowsText = function(start, end) {
1177 var ary = [];
1178 for (var i = start; i < end; i++) {
1179 var node = this.getRowNode(i);
1180 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001181 if (i < end - 1 && !node.getAttribute('line-overflow'))
1182 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001183 }
1184
rgindaa09e7332012-08-17 12:49:51 -07001185 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001186};
1187
1188/**
1189 * Return the text content for a given row.
1190 *
1191 * This is a method from the RowProvider interface. The ScrollPort uses
1192 * it to fetch text content on demand when the user attempts to copy their
1193 * selection to the clipboard.
1194 *
1195 * @param {integer} index The zero-based row index to return, measured
1196 * relative to the start of the scrollback buffer. On-screen rows will
1197 * always have the largest indicies.
1198 * @return {string} A string containing the text value of the selected row.
1199 */
1200hterm.Terminal.prototype.getRowText = function(index) {
1201 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001202 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001203};
1204
1205/**
1206 * Return the total number of rows in the addressable screen and in the
1207 * scrollback buffer of this terminal.
1208 *
1209 * This is a method from the RowProvider interface. The ScrollPort uses
1210 * it to compute the size of the scrollbar.
1211 *
1212 * @return {integer} The number of rows in this terminal.
1213 */
1214hterm.Terminal.prototype.getRowCount = function() {
1215 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1216};
1217
1218/**
1219 * Create DOM nodes for new rows and append them to the end of the terminal.
1220 *
1221 * This is the only correct way to add a new DOM node for a row. Notice that
1222 * the new row is appended to the bottom of the list of rows, and does not
1223 * require renumbering (of the rowIndex property) of previous rows.
1224 *
1225 * If you think you want a new blank row somewhere in the middle of the
1226 * terminal, look into moveRows_().
1227 *
1228 * This method does not pay attention to vtScrollTop/Bottom, since you should
1229 * be using moveRows() in cases where they would matter.
1230 *
1231 * The cursor will be positioned at column 0 of the first inserted line.
1232 */
1233hterm.Terminal.prototype.appendRows_ = function(count) {
1234 var cursorRow = this.screen_.rowsArray.length;
1235 var offset = this.scrollbackRows_.length + cursorRow;
1236 for (var i = 0; i < count; i++) {
1237 var row = this.document_.createElement('x-row');
1238 row.appendChild(this.document_.createTextNode(''));
1239 row.rowIndex = offset + i;
1240 this.screen_.pushRow(row);
1241 }
1242
1243 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1244 if (extraRows > 0) {
1245 var ary = this.screen_.shiftRows(extraRows);
1246 Array.prototype.push.apply(this.scrollbackRows_, ary);
1247 this.scheduleScrollDown_();
1248 }
1249
1250 if (cursorRow >= this.screen_.rowsArray.length)
1251 cursorRow = this.screen_.rowsArray.length - 1;
1252
rginda87b86462011-12-14 13:48:03 -08001253 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001254};
1255
1256/**
1257 * Relocate rows from one part of the addressable screen to another.
1258 *
1259 * This is used to recycle rows during VT scrolls (those which are driven
1260 * by VT commands, rather than by the user manipulating the scrollbar.)
1261 *
1262 * In this case, the blank lines scrolled into the scroll region are made of
1263 * the nodes we scrolled off. These have their rowIndex properties carefully
1264 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001265 */
1266hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1267 var ary = this.screen_.removeRows(fromIndex, count);
1268 this.screen_.insertRows(toIndex, ary);
1269
1270 var start, end;
1271 if (fromIndex < toIndex) {
1272 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001273 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001274 } else {
1275 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001276 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001277 }
1278
1279 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001280 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001281};
1282
1283/**
1284 * Renumber the rowIndex property of the given range of rows.
1285 *
1286 * The start and end indicies are relative to the screen, not the scrollback.
1287 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001288 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001289 * no need to renumber scrollback rows.
1290 */
1291hterm.Terminal.prototype.renumberRows_ = function(start, end) {
1292 var offset = this.scrollbackRows_.length;
1293 for (var i = start; i < end; i++) {
1294 this.screen_.rowsArray[i].rowIndex = offset + i;
1295 }
1296};
1297
1298/**
1299 * Print a string to the terminal.
1300 *
1301 * This respects the current insert and wraparound modes. It will add new lines
1302 * to the end of the terminal, scrolling off the top into the scrollback buffer
1303 * if necessary.
1304 *
1305 * The string is *not* parsed for escape codes. Use the interpret() method if
1306 * that's what you're after.
1307 *
1308 * @param{string} str The string to print.
1309 */
1310hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001311 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001312
rgindaa9abdd82012-08-06 18:05:09 -07001313 while (startOffset < str.length) {
rgindaa09e7332012-08-17 12:49:51 -07001314 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1315 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001316 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001317 }
rgindaa19afe22012-01-25 15:40:22 -08001318
rgindaa9abdd82012-08-06 18:05:09 -07001319 var count = str.length - startOffset;
1320 var didOverflow = false;
1321 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001322
rgindaa9abdd82012-08-06 18:05:09 -07001323 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1324 didOverflow = true;
1325 count = this.screenSize.width - this.screen_.cursorPosition.column;
1326 }
rgindaa19afe22012-01-25 15:40:22 -08001327
rgindaa9abdd82012-08-06 18:05:09 -07001328 if (didOverflow && !this.options_.wraparound) {
1329 // If the string overflowed the line but wraparound is off, then the
1330 // last printed character should be the last of the string.
1331 // TODO: This will add to our problems with multibyte UTF-16 characters.
1332 substr = str.substr(startOffset, count - 1) +
1333 str.substr(str.length - 1);
1334 count = str.length;
1335 } else {
1336 substr = str.substr(startOffset, count);
1337 }
rgindaa19afe22012-01-25 15:40:22 -08001338
rgindaa9abdd82012-08-06 18:05:09 -07001339 if (this.options_.insertMode) {
1340 this.screen_.insertString(substr);
1341 } else {
1342 this.screen_.overwriteString(substr);
1343 }
1344
1345 this.screen_.maybeClipCurrentRow();
1346 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001347 }
rginda8ba33642011-12-14 12:31:31 -08001348
1349 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001350
rginda9f5222b2012-03-05 11:53:28 -08001351 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001352 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001353};
1354
1355/**
rginda87b86462011-12-14 13:48:03 -08001356 * Set the VT scroll region.
1357 *
rginda87b86462011-12-14 13:48:03 -08001358 * This also resets the cursor position to the absolute (0, 0) position, since
1359 * that's what xterm appears to do.
1360 *
1361 * @param {integer} scrollTop The zero-based top of the scroll region.
1362 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1363 * inclusive.
1364 */
1365hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1366 this.vtScrollTop_ = scrollTop;
1367 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001368};
1369
1370/**
rginda8ba33642011-12-14 12:31:31 -08001371 * Return the top row index according to the VT.
1372 *
1373 * This will return 0 unless the terminal has been told to restrict scrolling
1374 * to some lower row. It is used for some VT cursor positioning and scrolling
1375 * commands.
1376 *
1377 * @return {integer} The topmost row in the terminal's scroll region.
1378 */
1379hterm.Terminal.prototype.getVTScrollTop = function() {
1380 if (this.vtScrollTop_ != null)
1381 return this.vtScrollTop_;
1382
1383 return 0;
rginda87b86462011-12-14 13:48:03 -08001384};
rginda8ba33642011-12-14 12:31:31 -08001385
1386/**
1387 * Return the bottom row index according to the VT.
1388 *
1389 * This will return the height of the terminal unless the it has been told to
1390 * restrict scrolling to some higher row. It is used for some VT cursor
1391 * positioning and scrolling commands.
1392 *
1393 * @return {integer} The bottommost row in the terminal's scroll region.
1394 */
1395hterm.Terminal.prototype.getVTScrollBottom = function() {
1396 if (this.vtScrollBottom_ != null)
1397 return this.vtScrollBottom_;
1398
rginda87b86462011-12-14 13:48:03 -08001399 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001400}
1401
1402/**
1403 * Process a '\n' character.
1404 *
1405 * If the cursor is on the final row of the terminal this will append a new
1406 * blank row to the screen and scroll the topmost row into the scrollback
1407 * buffer.
1408 *
1409 * Otherwise, this moves the cursor to column zero of the next row.
1410 */
1411hterm.Terminal.prototype.newLine = function() {
1412 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001413 // If we're at the end of the screen we need to append a new line and
1414 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001415 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001416 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1417 // End of the scroll region does not affect the scrollback buffer.
1418 this.vtScrollUp(1);
1419 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001420 } else {
rginda87b86462011-12-14 13:48:03 -08001421 // Anywhere else in the screen just moves the cursor.
1422 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001423 }
1424};
1425
1426/**
1427 * Like newLine(), except maintain the cursor column.
1428 */
1429hterm.Terminal.prototype.lineFeed = function() {
1430 var column = this.screen_.cursorPosition.column;
1431 this.newLine();
1432 this.setCursorColumn(column);
1433};
1434
1435/**
rginda87b86462011-12-14 13:48:03 -08001436 * If autoCarriageReturn is set then newLine(), else lineFeed().
1437 */
1438hterm.Terminal.prototype.formFeed = function() {
1439 if (this.options_.autoCarriageReturn) {
1440 this.newLine();
1441 } else {
1442 this.lineFeed();
1443 }
1444};
1445
1446/**
1447 * Move the cursor up one row, possibly inserting a blank line.
1448 *
1449 * The cursor column is not changed.
1450 */
1451hterm.Terminal.prototype.reverseLineFeed = function() {
1452 var scrollTop = this.getVTScrollTop();
1453 var currentRow = this.screen_.cursorPosition.row;
1454
1455 if (currentRow == scrollTop) {
1456 this.insertLines(1);
1457 } else {
1458 this.setAbsoluteCursorRow(currentRow - 1);
1459 }
1460};
1461
1462/**
rginda8ba33642011-12-14 12:31:31 -08001463 * Replace all characters to the left of the current cursor with the space
1464 * character.
1465 *
1466 * TODO(rginda): This should probably *remove* the characters (not just replace
1467 * with a space) if there are no characters at or beyond the current cursor
1468 * position. Once it does that, it'll have the same text-attribute related
1469 * issues as hterm.Screen.prototype.clearCursorRow :/
1470 */
1471hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001472 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001473 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001474 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001475 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001476};
1477
1478/**
David Benjamin684a9b72012-05-01 17:19:58 -04001479 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001480 *
1481 * The cursor position is unchanged.
1482 *
1483 * TODO(rginda): Test that this works even when the cursor is positioned beyond
1484 * the end of the text.
1485 *
1486 * TODO(rginda): This likely has text-attribute related troubles similar to the
1487 * todo on hterm.Screen.prototype.clearCursorRow.
David Benjamin684a9b72012-05-01 17:19:58 -04001488 *
1489 * TODO(davidben): Probably better to not add the whitespace to the clipboard
1490 * if erasing to the end of the drawn portion of the line. That said, xterm
1491 * behaves the same here.
rginda8ba33642011-12-14 12:31:31 -08001492 */
1493hterm.Terminal.prototype.eraseToRight = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001494 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001495
rginda87b86462011-12-14 13:48:03 -08001496 var maxCount = this.screenSize.width - cursor.column;
David Benjamin684a9b72012-05-01 17:19:58 -04001497 if (opt_count === undefined || opt_count >= maxCount) {
1498 this.screen_.deleteChars(maxCount);
1499 } else {
rgindacbbd7482012-06-13 15:06:16 -07001500 this.screen_.overwriteString(lib.f.getWhitespace(opt_count));
David Benjamin684a9b72012-05-01 17:19:58 -04001501 }
rginda87b86462011-12-14 13:48:03 -08001502 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001503 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001504};
1505
1506/**
1507 * Erase the current line.
1508 *
1509 * The cursor position is unchanged.
1510 *
1511 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1512 * has a text-attribute related TODO.
1513 */
1514hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001515 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001516 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001517 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001518 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001519};
1520
1521/**
David Benjamina08d78f2012-05-05 00:28:49 -04001522 * Erase all characters from the start of the screen to the current cursor
1523 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001524 *
1525 * The cursor position is unchanged.
1526 *
1527 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1528 * has a text-attribute related TODO.
1529 */
1530hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001531 var cursor = this.saveCursor();
1532
1533 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001534
David Benjamina08d78f2012-05-05 00:28:49 -04001535 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001536 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001537 this.screen_.clearCursorRow();
1538 }
1539
rginda87b86462011-12-14 13:48:03 -08001540 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001541 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001542};
1543
1544/**
1545 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001546 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001547 *
1548 * The cursor position is unchanged.
1549 *
1550 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1551 * has a text-attribute related TODO.
1552 */
1553hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001554 var cursor = this.saveCursor();
1555
1556 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001557
David Benjamina08d78f2012-05-05 00:28:49 -04001558 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001559 for (var i = cursor.row + 1; i <= bottom; i++) {
1560 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001561 this.screen_.clearCursorRow();
1562 }
1563
rginda87b86462011-12-14 13:48:03 -08001564 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001565 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001566};
1567
1568/**
1569 * Fill the terminal with a given character.
1570 *
1571 * This methods does not respect the VT scroll region.
1572 *
1573 * @param {string} ch The character to use for the fill.
1574 */
1575hterm.Terminal.prototype.fill = function(ch) {
1576 var cursor = this.saveCursor();
1577
1578 this.setAbsoluteCursorPosition(0, 0);
1579 for (var row = 0; row < this.screenSize.height; row++) {
1580 for (var col = 0; col < this.screenSize.width; col++) {
1581 this.setAbsoluteCursorPosition(row, col);
1582 this.screen_.overwriteString(ch);
1583 }
1584 }
1585
1586 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001587};
1588
1589/**
rginda9ea433c2012-03-16 11:57:00 -07001590 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001591 *
rginda9ea433c2012-03-16 11:57:00 -07001592 * This does not respect the scroll region.
1593 *
1594 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1595 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001596 *
1597 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1598 * has a text-attribute related TODO.
1599 */
rginda9ea433c2012-03-16 11:57:00 -07001600hterm.Terminal.prototype.clearHome = function(opt_screen) {
1601 var screen = opt_screen || this.screen_;
1602 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001603
rginda11057d52012-04-25 12:29:56 -07001604 if (bottom == 0) {
1605 // Empty screen, nothing to do.
1606 return;
1607 }
1608
rgindae4d29232012-01-19 10:47:13 -08001609 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001610 screen.setCursorPosition(i, 0);
1611 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001612 }
1613
rginda9ea433c2012-03-16 11:57:00 -07001614 screen.setCursorPosition(0, 0);
1615};
1616
1617/**
1618 * Erase the entire display without changing the cursor position.
1619 *
1620 * The cursor position is unchanged. This does not respect the scroll
1621 * region.
1622 *
1623 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1624 * to the current screen.
1625 *
1626 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1627 * has a text-attribute related TODO.
1628 */
1629hterm.Terminal.prototype.clear = function(opt_screen) {
1630 var screen = opt_screen || this.screen_;
1631 var cursor = screen.cursorPosition.clone();
1632 this.clearHome(screen);
1633 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001634};
1635
1636/**
1637 * VT command to insert lines at the current cursor row.
1638 *
1639 * This respects the current scroll region. Rows pushed off the bottom are
1640 * lost (they won't show up in the scrollback buffer).
1641 *
1642 * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which
1643 * has a text-attribute related TODO.
1644 *
1645 * @param {integer} count The number of lines to insert.
1646 */
1647hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001648 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001649
1650 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001651 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001652
rgindae4d29232012-01-19 10:47:13 -08001653 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001654 if (start != cursor.row)
1655 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001656
1657 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001658 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001659 this.screen_.clearCursorRow();
1660 }
1661
rginda87b86462011-12-14 13:48:03 -08001662 cursor.column = 0;
1663 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001664};
1665
1666/**
1667 * VT command to delete lines at the current cursor row.
1668 *
1669 * New rows are added to the bottom of scroll region to take their place. New
1670 * rows are strictly there to take up space and have no content or style.
1671 */
1672hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001673 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001674
rginda87b86462011-12-14 13:48:03 -08001675 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001676 var bottom = this.getVTScrollBottom();
1677
rginda87b86462011-12-14 13:48:03 -08001678 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001679 count = Math.min(count, maxCount);
1680
rginda87b86462011-12-14 13:48:03 -08001681 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001682 if (count != maxCount)
1683 this.moveRows_(top, count, moveStart);
1684
1685 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001686 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001687 this.screen_.clearCursorRow();
1688 }
1689
rginda87b86462011-12-14 13:48:03 -08001690 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001691 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001692};
1693
1694/**
1695 * Inserts the given number of spaces at the current cursor position.
1696 *
rginda87b86462011-12-14 13:48:03 -08001697 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001698 */
1699hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001700 var cursor = this.saveCursor();
1701
rgindacbbd7482012-06-13 15:06:16 -07001702 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001703 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001704 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001705
1706 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001707 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001708};
1709
1710/**
1711 * Forward-delete the specified number of characters starting at the cursor
1712 * position.
1713 *
1714 * @param {integer} count The number of characters to delete.
1715 */
1716hterm.Terminal.prototype.deleteChars = function(count) {
1717 this.screen_.deleteChars(count);
David Benjamin54e8bf62012-06-01 22:31:40 -04001718 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001719};
1720
1721/**
1722 * Shift rows in the scroll region upwards by a given number of lines.
1723 *
1724 * New rows are inserted at the bottom of the scroll region to fill the
1725 * vacated rows. The new rows not filled out with the current text attributes.
1726 *
1727 * This function does not affect the scrollback rows at all. Rows shifted
1728 * off the top are lost.
1729 *
rginda87b86462011-12-14 13:48:03 -08001730 * The cursor position is not altered.
1731 *
rginda8ba33642011-12-14 12:31:31 -08001732 * @param {integer} count The number of rows to scroll.
1733 */
1734hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001735 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001736
rginda87b86462011-12-14 13:48:03 -08001737 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001738 this.deleteLines(count);
1739
rginda87b86462011-12-14 13:48:03 -08001740 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001741};
1742
1743/**
1744 * Shift rows below the cursor down by a given number of lines.
1745 *
1746 * This function respects the current scroll region.
1747 *
1748 * New rows are inserted at the top of the scroll region to fill the
1749 * vacated rows. The new rows not filled out with the current text attributes.
1750 *
1751 * This function does not affect the scrollback rows at all. Rows shifted
1752 * off the bottom are lost.
1753 *
1754 * @param {integer} count The number of rows to scroll.
1755 */
1756hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001757 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001758
rginda87b86462011-12-14 13:48:03 -08001759 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001760 this.insertLines(opt_count);
1761
rginda87b86462011-12-14 13:48:03 -08001762 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001763};
1764
rginda87b86462011-12-14 13:48:03 -08001765
rginda8ba33642011-12-14 12:31:31 -08001766/**
1767 * Set the cursor position.
1768 *
1769 * The cursor row is relative to the scroll region if the terminal has
1770 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1771 *
1772 * @param {integer} row The new zero-based cursor row.
1773 * @param {integer} row The new zero-based cursor column.
1774 */
1775hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1776 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001777 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001778 } else {
rginda87b86462011-12-14 13:48:03 -08001779 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001780 }
rginda87b86462011-12-14 13:48:03 -08001781};
rginda8ba33642011-12-14 12:31:31 -08001782
rginda87b86462011-12-14 13:48:03 -08001783hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1784 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001785 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1786 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001787 this.screen_.setCursorPosition(row, column);
1788};
1789
1790hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001791 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1792 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001793 this.screen_.setCursorPosition(row, column);
1794};
1795
1796/**
1797 * Set the cursor column.
1798 *
1799 * @param {integer} column The new zero-based cursor column.
1800 */
1801hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001802 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001803};
1804
1805/**
1806 * Return the cursor column.
1807 *
1808 * @return {integer} The zero-based cursor column.
1809 */
1810hterm.Terminal.prototype.getCursorColumn = function() {
1811 return this.screen_.cursorPosition.column;
1812};
1813
1814/**
1815 * Set the cursor row.
1816 *
1817 * The cursor row is relative to the scroll region if the terminal has
1818 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1819 *
1820 * @param {integer} row The new cursor row.
1821 */
rginda87b86462011-12-14 13:48:03 -08001822hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1823 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001824};
1825
1826/**
1827 * Return the cursor row.
1828 *
1829 * @return {integer} The zero-based cursor row.
1830 */
1831hterm.Terminal.prototype.getCursorRow = function(row) {
1832 return this.screen_.cursorPosition.row;
1833};
1834
1835/**
1836 * Request that the ScrollPort redraw itself soon.
1837 *
1838 * The redraw will happen asynchronously, soon after the call stack winds down.
1839 * Multiple calls will be coalesced into a single redraw.
1840 */
1841hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001842 if (this.timeouts_.redraw)
1843 return;
rginda8ba33642011-12-14 12:31:31 -08001844
1845 var self = this;
rginda87b86462011-12-14 13:48:03 -08001846 this.timeouts_.redraw = setTimeout(function() {
1847 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001848 self.scrollPort_.redraw_();
1849 }, 0);
1850};
1851
1852/**
1853 * Request that the ScrollPort be scrolled to the bottom.
1854 *
1855 * The scroll will happen asynchronously, soon after the call stack winds down.
1856 * Multiple calls will be coalesced into a single scroll.
1857 *
1858 * This affects the scrollbar position of the ScrollPort, and has nothing to
1859 * do with the VT scroll commands.
1860 */
1861hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1862 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001863 return;
rginda8ba33642011-12-14 12:31:31 -08001864
1865 var self = this;
1866 this.timeouts_.scrollDown = setTimeout(function() {
1867 delete self.timeouts_.scrollDown;
1868 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1869 }, 10);
1870};
1871
1872/**
1873 * Move the cursor up a specified number of rows.
1874 *
1875 * @param {integer} count The number of rows to move the cursor.
1876 */
1877hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001878 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001879};
1880
1881/**
1882 * Move the cursor down a specified number of rows.
1883 *
1884 * @param {integer} count The number of rows to move the cursor.
1885 */
1886hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001887 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001888 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1889 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1890 this.screenSize.height - 1);
1891
rgindacbbd7482012-06-13 15:06:16 -07001892 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001893 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001894 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001895};
1896
1897/**
1898 * Move the cursor left a specified number of columns.
1899 *
1900 * @param {integer} count The number of columns to move the cursor.
1901 */
1902hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001903 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001904};
1905
1906/**
1907 * Move the cursor right a specified number of columns.
1908 *
1909 * @param {integer} count The number of columns to move the cursor.
1910 */
1911hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001912 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001913 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001914 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001915 this.setCursorColumn(column);
1916};
1917
1918/**
1919 * Reverse the foreground and background colors of the terminal.
1920 *
1921 * This only affects text that was drawn with no attributes.
1922 *
1923 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1924 * been drawn with attributes that happen to coincide with the default
1925 * 'no-attribute' colors. My guess is probably not.
1926 */
1927hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001928 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001929 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001930 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1931 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001932 } else {
rginda9f5222b2012-03-05 11:53:28 -08001933 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1934 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001935 }
1936};
1937
1938/**
rginda87b86462011-12-14 13:48:03 -08001939 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001940 */
1941hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001942 if (this.bellAudio_.getAttribute('src'))
1943 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001944
rginda6d397402012-01-17 10:58:29 -08001945 this.cursorNode_.style.backgroundColor =
1946 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001947
1948 var self = this;
1949 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001950 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001951 }, 200);
rginda87b86462011-12-14 13:48:03 -08001952};
1953
1954/**
rginda8ba33642011-12-14 12:31:31 -08001955 * Set the origin mode bit.
1956 *
1957 * If origin mode is on, certain VT cursor and scrolling commands measure their
1958 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1959 * to the top of the addressable screen.
1960 *
1961 * Defaults to off.
1962 *
1963 * @param {boolean} state True to set origin mode, false to unset.
1964 */
1965hterm.Terminal.prototype.setOriginMode = function(state) {
1966 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001967 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001968};
1969
1970/**
1971 * Set the insert mode bit.
1972 *
1973 * If insert mode is on, existing text beyond the cursor position will be
1974 * shifted right to make room for new text. Otherwise, new text overwrites
1975 * any existing text.
1976 *
1977 * Defaults to off.
1978 *
1979 * @param {boolean} state True to set insert mode, false to unset.
1980 */
1981hterm.Terminal.prototype.setInsertMode = function(state) {
1982 this.options_.insertMode = state;
1983};
1984
1985/**
rginda87b86462011-12-14 13:48:03 -08001986 * Set the auto carriage return bit.
1987 *
1988 * If auto carriage return is on then a formfeed character is interpreted
1989 * as a newline, otherwise it's the same as a linefeed. The difference boils
1990 * down to whether or not the cursor column is reset.
1991 */
1992hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1993 this.options_.autoCarriageReturn = state;
1994};
1995
1996/**
rginda8ba33642011-12-14 12:31:31 -08001997 * Set the wraparound mode bit.
1998 *
1999 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2000 * to the start of the following row. Otherwise, the cursor is clamped to the
2001 * end of the screen and attempts to write past it are ignored.
2002 *
2003 * Defaults to on.
2004 *
2005 * @param {boolean} state True to set wraparound mode, false to unset.
2006 */
2007hterm.Terminal.prototype.setWraparound = function(state) {
2008 this.options_.wraparound = state;
2009};
2010
2011/**
2012 * Set the reverse-wraparound mode bit.
2013 *
2014 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2015 * to the end of the previous row. Otherwise, the cursor is clamped to column
2016 * 0.
2017 *
2018 * Defaults to off.
2019 *
2020 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2021 */
2022hterm.Terminal.prototype.setReverseWraparound = function(state) {
2023 this.options_.reverseWraparound = state;
2024};
2025
2026/**
2027 * Selects between the primary and alternate screens.
2028 *
2029 * If alternate mode is on, the alternate screen is active. Otherwise the
2030 * primary screen is active.
2031 *
2032 * Swapping screens has no effect on the scrollback buffer.
2033 *
2034 * Each screen maintains its own cursor position.
2035 *
2036 * Defaults to off.
2037 *
2038 * @param {boolean} state True to set alternate mode, false to unset.
2039 */
2040hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002041 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002042 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2043
rginda35c456b2012-02-09 17:29:05 -08002044 if (this.screen_.rowsArray.length &&
2045 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2046 // If the screen changed sizes while we were away, our rowIndexes may
2047 // be incorrect.
2048 var offset = this.scrollbackRows_.length;
2049 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002050 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002051 ary[i].rowIndex = offset + i;
2052 }
2053 }
rginda8ba33642011-12-14 12:31:31 -08002054
rginda35c456b2012-02-09 17:29:05 -08002055 this.realizeWidth_(this.screenSize.width);
2056 this.realizeHeight_(this.screenSize.height);
2057 this.scrollPort_.syncScrollHeight();
2058 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002059
rginda6d397402012-01-17 10:58:29 -08002060 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002061 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002062};
2063
2064/**
2065 * Set the cursor-blink mode bit.
2066 *
2067 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2068 * a visible cursor does not blink.
2069 *
2070 * You should make sure to turn blinking off if you're going to dispose of a
2071 * terminal, otherwise you'll leak a timeout.
2072 *
2073 * Defaults to on.
2074 *
2075 * @param {boolean} state True to set cursor-blink mode, false to unset.
2076 */
2077hterm.Terminal.prototype.setCursorBlink = function(state) {
2078 this.options_.cursorBlink = state;
2079
2080 if (!state && this.timeouts_.cursorBlink) {
2081 clearTimeout(this.timeouts_.cursorBlink);
2082 delete this.timeouts_.cursorBlink;
2083 }
2084
2085 if (this.options_.cursorVisible)
2086 this.setCursorVisible(true);
2087};
2088
2089/**
2090 * Set the cursor-visible mode bit.
2091 *
2092 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2093 *
2094 * Defaults to on.
2095 *
2096 * @param {boolean} state True to set cursor-visible mode, false to unset.
2097 */
2098hterm.Terminal.prototype.setCursorVisible = function(state) {
2099 this.options_.cursorVisible = state;
2100
2101 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002102 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002103 return;
2104 }
2105
rginda87b86462011-12-14 13:48:03 -08002106 this.syncCursorPosition_();
2107
2108 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002109
2110 if (this.options_.cursorBlink) {
2111 if (this.timeouts_.cursorBlink)
2112 return;
2113
2114 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2115 500);
2116 } else {
2117 if (this.timeouts_.cursorBlink) {
2118 clearTimeout(this.timeouts_.cursorBlink);
2119 delete this.timeouts_.cursorBlink;
2120 }
2121 }
2122};
2123
2124/**
rginda87b86462011-12-14 13:48:03 -08002125 * Synchronizes the visible cursor and document selection with the current
2126 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002127 */
2128hterm.Terminal.prototype.syncCursorPosition_ = function() {
2129 var topRowIndex = this.scrollPort_.getTopRowIndex();
2130 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2131 var cursorRowIndex = this.scrollbackRows_.length +
2132 this.screen_.cursorPosition.row;
2133
2134 if (cursorRowIndex > bottomRowIndex) {
2135 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002136 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002137 return;
2138 }
2139
rginda35c456b2012-02-09 17:29:05 -08002140 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
2141 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
2142
rginda8ba33642011-12-14 12:31:31 -08002143 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002144 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2145 'px';
2146 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2147 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002148
2149 this.cursorNode_.setAttribute('title',
2150 '(' + this.screen_.cursorPosition.row +
2151 ', ' + this.screen_.cursorPosition.column +
2152 ')');
2153
2154 // Update the caret for a11y purposes.
2155 var selection = this.document_.getSelection();
2156 if (selection && selection.isCollapsed)
2157 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002158};
2159
2160/**
2161 * Synchronizes the visible cursor with the current cursor coordinates.
2162 *
2163 * The sync will happen asynchronously, soon after the call stack winds down.
2164 * Multiple calls will be coalesced into a single sync.
2165 */
2166hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2167 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002168 return;
rginda8ba33642011-12-14 12:31:31 -08002169
2170 var self = this;
2171 this.timeouts_.syncCursor = setTimeout(function() {
2172 self.syncCursorPosition_();
2173 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002174 }, 0);
2175};
2176
rgindacc2996c2012-02-24 14:59:31 -08002177/**
rgindaf522ce02012-04-17 17:49:17 -07002178 * Show or hide the zoom warning.
2179 *
2180 * The zoom warning is a message warning the user that their browser zoom must
2181 * be set to 100% in order for hterm to function properly.
2182 *
2183 * @param {boolean} state True to show the message, false to hide it.
2184 */
2185hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2186 if (!this.zoomWarningNode_) {
2187 if (!state)
2188 return;
2189
2190 this.zoomWarningNode_ = this.document_.createElement('div');
2191 this.zoomWarningNode_.style.cssText = (
2192 'color: black;' +
2193 'background-color: #ff2222;' +
2194 'font-size: large;' +
2195 'border-radius: 8px;' +
2196 'opacity: 0.75;' +
2197 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2198 'top: 0.5em;' +
2199 'right: 1.2em;' +
2200 'position: absolute;' +
2201 '-webkit-text-size-adjust: none;' +
2202 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002203 }
2204
rgindade84e382012-04-20 15:39:31 -07002205 this.zoomWarningNode_.textContent = hterm.msg('ZOOM_WARNING') ||
2206 ('!! ' + parseInt(this.scrollPort_.characterSize.zoomFactor * 100) +
2207 '% !!');
rgindaf522ce02012-04-17 17:49:17 -07002208 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2209
2210 if (state) {
2211 if (!this.zoomWarningNode_.parentNode)
2212 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2213 } else if (this.zoomWarningNode_.parentNode) {
2214 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2215 }
2216};
2217
2218/**
rgindacc2996c2012-02-24 14:59:31 -08002219 * Show the terminal overlay for a given amount of time.
2220 *
2221 * The terminal overlay appears in inverse video in a large font, centered
2222 * over the terminal. You should probably keep the overlay message brief,
2223 * since it's in a large font and you probably aren't going to check the size
2224 * of the terminal first.
2225 *
2226 * @param {string} msg The text (not HTML) message to display in the overlay.
2227 * @param {number} opt_timeout The amount of time to wait before fading out
2228 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2229 * stay up forever (or until the next overlay).
2230 */
2231hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002232 if (!this.overlayNode_) {
2233 if (!this.div_)
2234 return;
2235
2236 this.overlayNode_ = this.document_.createElement('div');
2237 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002238 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002239 'font-size: xx-large;' +
2240 'opacity: 0.75;' +
2241 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2242 'position: absolute;' +
2243 '-webkit-user-select: none;' +
2244 '-webkit-transition: opacity 180ms ease-in;');
2245 }
2246
rginda9f5222b2012-03-05 11:53:28 -08002247 this.overlayNode_.style.color = this.prefs_.get('background-color');
2248 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2249 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2250
rgindaf0090c92012-02-10 14:58:52 -08002251 this.overlayNode_.textContent = msg;
2252 this.overlayNode_.style.opacity = '0.75';
2253
2254 if (!this.overlayNode_.parentNode)
2255 this.div_.appendChild(this.overlayNode_);
2256
2257 this.overlayNode_.style.top = (
2258 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
2259 this.overlayNode_.style.left = (
2260 this.div_.clientWidth - this.overlayNode_.clientWidth -
2261 this.scrollbarWidthPx) / 2;
2262
2263 var self = this;
2264
2265 if (this.overlayTimeout_)
2266 clearTimeout(this.overlayTimeout_);
2267
rgindacc2996c2012-02-24 14:59:31 -08002268 if (opt_timeout === null)
2269 return;
2270
rgindaf0090c92012-02-10 14:58:52 -08002271 this.overlayTimeout_ = setTimeout(function() {
2272 self.overlayNode_.style.opacity = '0';
2273 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002274 if (self.overlayNode_.parentNode)
2275 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002276 self.overlayTimeout_ = null;
2277 self.overlayNode_.style.opacity = '0.75';
2278 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002279 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002280};
2281
rginda4bba5e12012-06-20 16:15:30 -07002282/**
2283 * Paste from the system clipboard to the terminal.
2284 */
2285hterm.Terminal.prototype.paste = function() {
2286 hterm.pasteFromClipboard(this.document_);
2287};
2288
2289/**
2290 * Copy a string to the system clipboard.
2291 *
2292 * Note: If there is a selected range in the terminal, it'll be cleared.
2293 */
2294hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda9fb38222012-09-11 14:19:12 -07002295 if (this.prefs_.get('enable-clipboard-notice'))
2296 setTimeout(this.showOverlay.bind(this, hterm.msg('NOTIFY_COPY'), 500), 200);
rgindaa09e7332012-08-17 12:49:51 -07002297
2298 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002299 copySource.textContent = str;
2300 copySource.style.cssText = (
2301 '-webkit-user-select: text;' +
2302 'position: absolute;' +
2303 'top: -99px');
2304
2305 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002306
rginda4bba5e12012-06-20 16:15:30 -07002307 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002308 var anchorNode = selection.anchorNode;
2309 var anchorOffset = selection.anchorOffset;
2310 var focusNode = selection.focusNode;
2311 var focusOffset = selection.focusOffset;
2312
rginda4bba5e12012-06-20 16:15:30 -07002313 selection.selectAllChildren(copySource);
2314
rgindaa09e7332012-08-17 12:49:51 -07002315 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002316
rgindafaa74742012-08-21 13:34:03 -07002317 selection.collapse(anchorNode, anchorOffset);
2318 selection.extend(focusNode, focusOffset);
2319
rginda4bba5e12012-06-20 16:15:30 -07002320 copySource.parentNode.removeChild(copySource);
2321};
2322
rgindaa09e7332012-08-17 12:49:51 -07002323hterm.Terminal.prototype.getSelectionText = function() {
2324 var selection = this.scrollPort_.selection;
2325 selection.sync();
2326
2327 if (selection.isCollapsed)
2328 return null;
2329
2330
2331 // Start offset measures from the beginning of the line.
2332 var startOffset = selection.startOffset;
2333 var node = selection.startNode;
Robert Gindafdbb3f22012-09-06 20:23:06 -07002334 if (node.nodeName != 'X-ROW') {
2335 // If the selection doesn't start on an x-row node, then it must be
2336 // somewhere inside the x-row. Add any characters from previous siblings
2337 // into the start offset.
2338 while (node.previousSibling) {
2339 node = node.previousSibling;
2340 startOffset += node.textContent.length;
2341 }
rgindaa09e7332012-08-17 12:49:51 -07002342 }
2343
2344 // End offset measures from the end of the line.
2345 var endOffset = selection.endNode.textContent.length - selection.endOffset;
2346 var node = selection.endNode;
Robert Gindafdbb3f22012-09-06 20:23:06 -07002347 if (node.nodeName != 'X-ROW') {
2348 // If the selection doesn't end on an x-row node, then it must be
2349 // somewhere inside the x-row. Add any characters from following siblings
2350 // into the end offset.
2351 while (node.nextSibling) {
2352 node = node.nextSibling;
2353 endOffset += node.textContent.length;
2354 }
rgindaa09e7332012-08-17 12:49:51 -07002355 }
2356
2357 var rv = this.getRowsText(selection.startRow.rowIndex,
2358 selection.endRow.rowIndex + 1);
2359 return rv.substring(startOffset, rv.length - endOffset);
2360};
2361
rginda4bba5e12012-06-20 16:15:30 -07002362/**
2363 * Copy the current selection to the system clipboard, then clear it after a
2364 * short delay.
2365 */
2366hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002367 var text = this.getSelectionText();
2368 if (text != null)
2369 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002370};
2371
rgindaf0090c92012-02-10 14:58:52 -08002372hterm.Terminal.prototype.overlaySize = function() {
2373 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2374};
2375
rginda87b86462011-12-14 13:48:03 -08002376/**
2377 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2378 *
2379 * @param {string} string The VT string representing the keystroke.
2380 */
2381hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002382 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002383 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2384
2385 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08002386};
2387
2388/**
rgindad5613292012-06-19 15:40:37 -07002389 * Add the terminalRow and terminalColumn properties to mouse events and
2390 * then forward on to onMouse().
2391 *
2392 * The terminalRow and terminalColumn properties contain the (row, column)
2393 * coordinates for the mouse event.
2394 */
2395hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002396 if (e.processedByTerminalHandler_) {
2397 // We register our event handlers on the document, as well as the cursor
2398 // and the scroll blocker. Mouse events that occur on the cursor or
2399 // scroll blocker will also appear on the document, but we don't want to
2400 // process them twice.
2401 //
2402 // We can't just prevent bubbling because that has other side effects, so
2403 // we decorate the event object with this property instead.
2404 return;
2405 }
2406
2407 e.processedByTerminalHandler_ = true;
2408
rginda4bba5e12012-06-20 16:15:30 -07002409 if (e.type == 'mousedown' && e.which == this.mousePasteButton) {
2410 this.paste();
2411 return;
2412 }
2413
2414 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2415 !this.document_.getSelection().isCollapsed) {
rgindafaa74742012-08-21 13:34:03 -07002416 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002417 return;
2418 }
2419
rgindad5613292012-06-19 15:40:37 -07002420 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2421 this.scrollPort_.characterSize.height) + 1;
2422 e.terminalColumn = parseInt(e.clientX /
2423 this.scrollPort_.characterSize.width) + 1;
2424
2425 if (e.type == 'mousedown') {
2426 if (e.terminalColumn > this.screenSize.width) {
2427 // Mousedown in the scrollbar area.
2428 return;
2429 }
2430
2431 if (!this.enableMouseDragScroll) {
2432 // Move the scroll-blocker into place if we want to keep the scrollport
2433 // from scrolling.
2434 this.scrollBlockerNode_.engaged = true;
2435 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2436 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2437 }
2438 } else if (this.scrollBlockerNode_.engaged &&
2439 (e.type == 'mousemove' || e.type == 'mouseup')) {
2440 // Disengage the scroll-blocker after one of these events.
2441 this.scrollBlockerNode_.engaged = false;
2442 this.scrollBlockerNode_.style.top = '-99px';
2443 }
2444
rgindafaa74742012-08-21 13:34:03 -07002445 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002446};
2447
2448/**
2449 * Clients should override this if they care to know about mouse events.
2450 *
2451 * The event parameter will be a normal DOM mouse click event with additional
2452 * 'terminalRow' and 'terminalColumn' properties.
2453 */
2454hterm.Terminal.prototype.onMouse = function(e) { };
2455
2456/**
rginda8e92a692012-05-20 19:37:20 -07002457 * React when focus changes.
2458 */
2459hterm.Terminal.prototype.onFocusChange_ = function(state) {
2460 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
2461};
2462
2463/**
rginda8ba33642011-12-14 12:31:31 -08002464 * React when the ScrollPort is scrolled.
2465 */
2466hterm.Terminal.prototype.onScroll_ = function() {
2467 this.scheduleSyncCursorPosition_();
2468};
2469
2470/**
rginda9846e2f2012-01-27 13:53:33 -08002471 * React when text is pasted into the scrollPort.
2472 */
2473hterm.Terminal.prototype.onPaste_ = function(e) {
David Benjamin8f962172012-07-17 07:38:43 -04002474 this.io.onVTKeystroke(this.vt.encodeUTF8(e.text));
rginda9846e2f2012-01-27 13:53:33 -08002475};
2476
2477/**
rgindaa09e7332012-08-17 12:49:51 -07002478 * React when the user tries to copy from the scrollPort.
2479 */
2480hterm.Terminal.prototype.onCopy_ = function(e) {
2481 e.preventDefault();
rgindafaa74742012-08-21 13:34:03 -07002482 this.copySelectionToClipboard();
rgindaa09e7332012-08-17 12:49:51 -07002483};
2484
2485/**
rginda8ba33642011-12-14 12:31:31 -08002486 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002487 *
2488 * Note: This function should not directly contain code that alters the internal
2489 * state of the terminal. That kind of code belongs in realizeWidth or
2490 * realizeHeight, so that it can be executed synchronously in the case of a
2491 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002492 */
2493hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002494 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002495 this.scrollPort_.characterSize.width);
2496 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2497 this.scrollPort_.characterSize.height);
2498
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002499 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002500 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002501 // gets removed from the document or during the initial load, and we can't
2502 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002503 return;
2504 }
2505
rgindaa8ba17d2012-08-15 14:41:10 -07002506 var isNewSize = (columnCount != this.screenSize.width ||
2507 rowCount != this.screenSize.height);
2508
2509 // We do this even if the size didn't change, just to be sure everything is
2510 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002511 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002512 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002513
2514 if (isNewSize)
2515 this.overlaySize();
2516
2517 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002518};
2519
2520/**
2521 * Service the cursor blink timeout.
2522 */
2523hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002524 if (this.cursorNode_.style.opacity == '0') {
2525 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002526 } else {
rginda87b86462011-12-14 13:48:03 -08002527 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002528 }
2529};
David Reveman8f552492012-03-28 12:18:41 -04002530
2531/**
2532 * Set the scrollbar-visible mode bit.
2533 *
2534 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2535 * Otherwise it will not.
2536 *
2537 * Defaults to on.
2538 *
2539 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2540 */
2541hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2542 this.scrollPort_.setScrollbarVisible(state);
2543};