blob: 0cb9db5392902b836cb95cb0e9be547419acfa2d [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 *
Robert Ginda7fd57082012-09-25 14:41:47 -07001483 * TODO(davidben): Probably better to not add the whitespace to the clipboard.
1484 * That said, xterm behaves the same here.
rginda8ba33642011-12-14 12:31:31 -08001485 */
1486hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001487 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1488 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
rginda87b86462011-12-14 13:48:03 -08001489 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001490 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001491 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001492 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001493};
1494
1495/**
1496 * Erase the current line.
1497 *
1498 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001499 */
1500hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001501 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001502 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001503 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001504 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001505};
1506
1507/**
David Benjamina08d78f2012-05-05 00:28:49 -04001508 * Erase all characters from the start of the screen to the current cursor
1509 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001510 *
1511 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001512 */
1513hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001514 var cursor = this.saveCursor();
1515
1516 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001517
David Benjamina08d78f2012-05-05 00:28:49 -04001518 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001519 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001520 this.screen_.clearCursorRow();
1521 }
1522
rginda87b86462011-12-14 13:48:03 -08001523 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001524 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001525};
1526
1527/**
1528 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001529 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001530 *
1531 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001532 */
1533hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001534 var cursor = this.saveCursor();
1535
1536 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001537
David Benjamina08d78f2012-05-05 00:28:49 -04001538 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001539 for (var i = cursor.row + 1; i <= bottom; i++) {
1540 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001541 this.screen_.clearCursorRow();
1542 }
1543
rginda87b86462011-12-14 13:48:03 -08001544 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001545 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001546};
1547
1548/**
1549 * Fill the terminal with a given character.
1550 *
1551 * This methods does not respect the VT scroll region.
1552 *
1553 * @param {string} ch The character to use for the fill.
1554 */
1555hterm.Terminal.prototype.fill = function(ch) {
1556 var cursor = this.saveCursor();
1557
1558 this.setAbsoluteCursorPosition(0, 0);
1559 for (var row = 0; row < this.screenSize.height; row++) {
1560 for (var col = 0; col < this.screenSize.width; col++) {
1561 this.setAbsoluteCursorPosition(row, col);
1562 this.screen_.overwriteString(ch);
1563 }
1564 }
1565
1566 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001567};
1568
1569/**
rginda9ea433c2012-03-16 11:57:00 -07001570 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001571 *
rginda9ea433c2012-03-16 11:57:00 -07001572 * This does not respect the scroll region.
1573 *
1574 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1575 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001576 */
rginda9ea433c2012-03-16 11:57:00 -07001577hterm.Terminal.prototype.clearHome = function(opt_screen) {
1578 var screen = opt_screen || this.screen_;
1579 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001580
rginda11057d52012-04-25 12:29:56 -07001581 if (bottom == 0) {
1582 // Empty screen, nothing to do.
1583 return;
1584 }
1585
rgindae4d29232012-01-19 10:47:13 -08001586 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001587 screen.setCursorPosition(i, 0);
1588 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001589 }
1590
rginda9ea433c2012-03-16 11:57:00 -07001591 screen.setCursorPosition(0, 0);
1592};
1593
1594/**
1595 * Erase the entire display without changing the cursor position.
1596 *
1597 * The cursor position is unchanged. This does not respect the scroll
1598 * region.
1599 *
1600 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1601 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001602 */
1603hterm.Terminal.prototype.clear = function(opt_screen) {
1604 var screen = opt_screen || this.screen_;
1605 var cursor = screen.cursorPosition.clone();
1606 this.clearHome(screen);
1607 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001608};
1609
1610/**
1611 * VT command to insert lines at the current cursor row.
1612 *
1613 * This respects the current scroll region. Rows pushed off the bottom are
1614 * lost (they won't show up in the scrollback buffer).
1615 *
rginda8ba33642011-12-14 12:31:31 -08001616 * @param {integer} count The number of lines to insert.
1617 */
1618hterm.Terminal.prototype.insertLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001619 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001620
1621 var bottom = this.getVTScrollBottom();
rginda87b86462011-12-14 13:48:03 -08001622 count = Math.min(count, bottom - cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001623
rgindae4d29232012-01-19 10:47:13 -08001624 var start = bottom - count + 1;
rginda87b86462011-12-14 13:48:03 -08001625 if (start != cursor.row)
1626 this.moveRows_(start, count, cursor.row);
rginda8ba33642011-12-14 12:31:31 -08001627
1628 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001629 this.setAbsoluteCursorPosition(cursor.row + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001630 this.screen_.clearCursorRow();
1631 }
1632
rginda87b86462011-12-14 13:48:03 -08001633 cursor.column = 0;
1634 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001635};
1636
1637/**
1638 * VT command to delete lines at the current cursor row.
1639 *
1640 * New rows are added to the bottom of scroll region to take their place. New
1641 * rows are strictly there to take up space and have no content or style.
1642 */
1643hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001644 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001645
rginda87b86462011-12-14 13:48:03 -08001646 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001647 var bottom = this.getVTScrollBottom();
1648
rginda87b86462011-12-14 13:48:03 -08001649 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001650 count = Math.min(count, maxCount);
1651
rginda87b86462011-12-14 13:48:03 -08001652 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001653 if (count != maxCount)
1654 this.moveRows_(top, count, moveStart);
1655
1656 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001657 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001658 this.screen_.clearCursorRow();
1659 }
1660
rginda87b86462011-12-14 13:48:03 -08001661 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001662 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001663};
1664
1665/**
1666 * Inserts the given number of spaces at the current cursor position.
1667 *
rginda87b86462011-12-14 13:48:03 -08001668 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001669 */
1670hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001671 var cursor = this.saveCursor();
1672
rgindacbbd7482012-06-13 15:06:16 -07001673 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001674 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001675 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001676
1677 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001678 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001679};
1680
1681/**
1682 * Forward-delete the specified number of characters starting at the cursor
1683 * position.
1684 *
1685 * @param {integer} count The number of characters to delete.
1686 */
1687hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001688 var deleted = this.screen_.deleteChars(count);
1689 if (deleted && !this.screen_.textAttributes.isDefault()) {
1690 var cursor = this.saveCursor();
1691 this.setCursorColumn(this.screenSize.width - deleted);
1692 this.screen_.insertString(lib.f.getWhitespace(deleted));
1693 this.restoreCursor(cursor);
1694 }
1695
David Benjamin54e8bf62012-06-01 22:31:40 -04001696 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001697};
1698
1699/**
1700 * Shift rows in the scroll region upwards by a given number of lines.
1701 *
1702 * New rows are inserted at the bottom of the scroll region to fill the
1703 * vacated rows. The new rows not filled out with the current text attributes.
1704 *
1705 * This function does not affect the scrollback rows at all. Rows shifted
1706 * off the top are lost.
1707 *
rginda87b86462011-12-14 13:48:03 -08001708 * The cursor position is not altered.
1709 *
rginda8ba33642011-12-14 12:31:31 -08001710 * @param {integer} count The number of rows to scroll.
1711 */
1712hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001713 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001714
rginda87b86462011-12-14 13:48:03 -08001715 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001716 this.deleteLines(count);
1717
rginda87b86462011-12-14 13:48:03 -08001718 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001719};
1720
1721/**
1722 * Shift rows below the cursor down by a given number of lines.
1723 *
1724 * This function respects the current scroll region.
1725 *
1726 * New rows are inserted at the top of the scroll region to fill the
1727 * vacated rows. The new rows not filled out with the current text attributes.
1728 *
1729 * This function does not affect the scrollback rows at all. Rows shifted
1730 * off the bottom are lost.
1731 *
1732 * @param {integer} count The number of rows to scroll.
1733 */
1734hterm.Terminal.prototype.vtScrollDown = function(opt_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.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001738 this.insertLines(opt_count);
1739
rginda87b86462011-12-14 13:48:03 -08001740 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001741};
1742
rginda87b86462011-12-14 13:48:03 -08001743
rginda8ba33642011-12-14 12:31:31 -08001744/**
1745 * Set the cursor position.
1746 *
1747 * The cursor row is relative to the scroll region if the terminal has
1748 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1749 *
1750 * @param {integer} row The new zero-based cursor row.
1751 * @param {integer} row The new zero-based cursor column.
1752 */
1753hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1754 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001755 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001756 } else {
rginda87b86462011-12-14 13:48:03 -08001757 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001758 }
rginda87b86462011-12-14 13:48:03 -08001759};
rginda8ba33642011-12-14 12:31:31 -08001760
rginda87b86462011-12-14 13:48:03 -08001761hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1762 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001763 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1764 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001765 this.screen_.setCursorPosition(row, column);
1766};
1767
1768hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001769 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1770 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001771 this.screen_.setCursorPosition(row, column);
1772};
1773
1774/**
1775 * Set the cursor column.
1776 *
1777 * @param {integer} column The new zero-based cursor column.
1778 */
1779hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001780 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001781};
1782
1783/**
1784 * Return the cursor column.
1785 *
1786 * @return {integer} The zero-based cursor column.
1787 */
1788hterm.Terminal.prototype.getCursorColumn = function() {
1789 return this.screen_.cursorPosition.column;
1790};
1791
1792/**
1793 * Set the cursor row.
1794 *
1795 * The cursor row is relative to the scroll region if the terminal has
1796 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1797 *
1798 * @param {integer} row The new cursor row.
1799 */
rginda87b86462011-12-14 13:48:03 -08001800hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1801 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001802};
1803
1804/**
1805 * Return the cursor row.
1806 *
1807 * @return {integer} The zero-based cursor row.
1808 */
1809hterm.Terminal.prototype.getCursorRow = function(row) {
1810 return this.screen_.cursorPosition.row;
1811};
1812
1813/**
1814 * Request that the ScrollPort redraw itself soon.
1815 *
1816 * The redraw will happen asynchronously, soon after the call stack winds down.
1817 * Multiple calls will be coalesced into a single redraw.
1818 */
1819hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001820 if (this.timeouts_.redraw)
1821 return;
rginda8ba33642011-12-14 12:31:31 -08001822
1823 var self = this;
rginda87b86462011-12-14 13:48:03 -08001824 this.timeouts_.redraw = setTimeout(function() {
1825 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001826 self.scrollPort_.redraw_();
1827 }, 0);
1828};
1829
1830/**
1831 * Request that the ScrollPort be scrolled to the bottom.
1832 *
1833 * The scroll will happen asynchronously, soon after the call stack winds down.
1834 * Multiple calls will be coalesced into a single scroll.
1835 *
1836 * This affects the scrollbar position of the ScrollPort, and has nothing to
1837 * do with the VT scroll commands.
1838 */
1839hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1840 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001841 return;
rginda8ba33642011-12-14 12:31:31 -08001842
1843 var self = this;
1844 this.timeouts_.scrollDown = setTimeout(function() {
1845 delete self.timeouts_.scrollDown;
1846 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1847 }, 10);
1848};
1849
1850/**
1851 * Move the cursor up a specified number of rows.
1852 *
1853 * @param {integer} count The number of rows to move the cursor.
1854 */
1855hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001856 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001857};
1858
1859/**
1860 * Move the cursor down a specified number of rows.
1861 *
1862 * @param {integer} count The number of rows to move the cursor.
1863 */
1864hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001865 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001866 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1867 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1868 this.screenSize.height - 1);
1869
rgindacbbd7482012-06-13 15:06:16 -07001870 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001871 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001872 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001873};
1874
1875/**
1876 * Move the cursor left a specified number of columns.
1877 *
1878 * @param {integer} count The number of columns to move the cursor.
1879 */
1880hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001881 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001882};
1883
1884/**
1885 * Move the cursor right a specified number of columns.
1886 *
1887 * @param {integer} count The number of columns to move the cursor.
1888 */
1889hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001890 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001891 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001892 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001893 this.setCursorColumn(column);
1894};
1895
1896/**
1897 * Reverse the foreground and background colors of the terminal.
1898 *
1899 * This only affects text that was drawn with no attributes.
1900 *
1901 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1902 * been drawn with attributes that happen to coincide with the default
1903 * 'no-attribute' colors. My guess is probably not.
1904 */
1905hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001906 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001907 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001908 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1909 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001910 } else {
rginda9f5222b2012-03-05 11:53:28 -08001911 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1912 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001913 }
1914};
1915
1916/**
rginda87b86462011-12-14 13:48:03 -08001917 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001918 */
1919hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001920 if (this.bellAudio_.getAttribute('src'))
1921 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001922
rginda6d397402012-01-17 10:58:29 -08001923 this.cursorNode_.style.backgroundColor =
1924 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001925
1926 var self = this;
1927 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001928 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001929 }, 200);
rginda87b86462011-12-14 13:48:03 -08001930};
1931
1932/**
rginda8ba33642011-12-14 12:31:31 -08001933 * Set the origin mode bit.
1934 *
1935 * If origin mode is on, certain VT cursor and scrolling commands measure their
1936 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1937 * to the top of the addressable screen.
1938 *
1939 * Defaults to off.
1940 *
1941 * @param {boolean} state True to set origin mode, false to unset.
1942 */
1943hterm.Terminal.prototype.setOriginMode = function(state) {
1944 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001945 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001946};
1947
1948/**
1949 * Set the insert mode bit.
1950 *
1951 * If insert mode is on, existing text beyond the cursor position will be
1952 * shifted right to make room for new text. Otherwise, new text overwrites
1953 * any existing text.
1954 *
1955 * Defaults to off.
1956 *
1957 * @param {boolean} state True to set insert mode, false to unset.
1958 */
1959hterm.Terminal.prototype.setInsertMode = function(state) {
1960 this.options_.insertMode = state;
1961};
1962
1963/**
rginda87b86462011-12-14 13:48:03 -08001964 * Set the auto carriage return bit.
1965 *
1966 * If auto carriage return is on then a formfeed character is interpreted
1967 * as a newline, otherwise it's the same as a linefeed. The difference boils
1968 * down to whether or not the cursor column is reset.
1969 */
1970hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1971 this.options_.autoCarriageReturn = state;
1972};
1973
1974/**
rginda8ba33642011-12-14 12:31:31 -08001975 * Set the wraparound mode bit.
1976 *
1977 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1978 * to the start of the following row. Otherwise, the cursor is clamped to the
1979 * end of the screen and attempts to write past it are ignored.
1980 *
1981 * Defaults to on.
1982 *
1983 * @param {boolean} state True to set wraparound mode, false to unset.
1984 */
1985hterm.Terminal.prototype.setWraparound = function(state) {
1986 this.options_.wraparound = state;
1987};
1988
1989/**
1990 * Set the reverse-wraparound mode bit.
1991 *
1992 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1993 * to the end of the previous row. Otherwise, the cursor is clamped to column
1994 * 0.
1995 *
1996 * Defaults to off.
1997 *
1998 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1999 */
2000hterm.Terminal.prototype.setReverseWraparound = function(state) {
2001 this.options_.reverseWraparound = state;
2002};
2003
2004/**
2005 * Selects between the primary and alternate screens.
2006 *
2007 * If alternate mode is on, the alternate screen is active. Otherwise the
2008 * primary screen is active.
2009 *
2010 * Swapping screens has no effect on the scrollback buffer.
2011 *
2012 * Each screen maintains its own cursor position.
2013 *
2014 * Defaults to off.
2015 *
2016 * @param {boolean} state True to set alternate mode, false to unset.
2017 */
2018hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002019 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002020 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2021
rginda35c456b2012-02-09 17:29:05 -08002022 if (this.screen_.rowsArray.length &&
2023 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2024 // If the screen changed sizes while we were away, our rowIndexes may
2025 // be incorrect.
2026 var offset = this.scrollbackRows_.length;
2027 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002028 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002029 ary[i].rowIndex = offset + i;
2030 }
2031 }
rginda8ba33642011-12-14 12:31:31 -08002032
rginda35c456b2012-02-09 17:29:05 -08002033 this.realizeWidth_(this.screenSize.width);
2034 this.realizeHeight_(this.screenSize.height);
2035 this.scrollPort_.syncScrollHeight();
2036 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002037
rginda6d397402012-01-17 10:58:29 -08002038 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002039 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002040};
2041
2042/**
2043 * Set the cursor-blink mode bit.
2044 *
2045 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2046 * a visible cursor does not blink.
2047 *
2048 * You should make sure to turn blinking off if you're going to dispose of a
2049 * terminal, otherwise you'll leak a timeout.
2050 *
2051 * Defaults to on.
2052 *
2053 * @param {boolean} state True to set cursor-blink mode, false to unset.
2054 */
2055hterm.Terminal.prototype.setCursorBlink = function(state) {
2056 this.options_.cursorBlink = state;
2057
2058 if (!state && this.timeouts_.cursorBlink) {
2059 clearTimeout(this.timeouts_.cursorBlink);
2060 delete this.timeouts_.cursorBlink;
2061 }
2062
2063 if (this.options_.cursorVisible)
2064 this.setCursorVisible(true);
2065};
2066
2067/**
2068 * Set the cursor-visible mode bit.
2069 *
2070 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2071 *
2072 * Defaults to on.
2073 *
2074 * @param {boolean} state True to set cursor-visible mode, false to unset.
2075 */
2076hterm.Terminal.prototype.setCursorVisible = function(state) {
2077 this.options_.cursorVisible = state;
2078
2079 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002080 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002081 return;
2082 }
2083
rginda87b86462011-12-14 13:48:03 -08002084 this.syncCursorPosition_();
2085
2086 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002087
2088 if (this.options_.cursorBlink) {
2089 if (this.timeouts_.cursorBlink)
2090 return;
2091
2092 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2093 500);
2094 } else {
2095 if (this.timeouts_.cursorBlink) {
2096 clearTimeout(this.timeouts_.cursorBlink);
2097 delete this.timeouts_.cursorBlink;
2098 }
2099 }
2100};
2101
2102/**
rginda87b86462011-12-14 13:48:03 -08002103 * Synchronizes the visible cursor and document selection with the current
2104 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002105 */
2106hterm.Terminal.prototype.syncCursorPosition_ = function() {
2107 var topRowIndex = this.scrollPort_.getTopRowIndex();
2108 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2109 var cursorRowIndex = this.scrollbackRows_.length +
2110 this.screen_.cursorPosition.row;
2111
2112 if (cursorRowIndex > bottomRowIndex) {
2113 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002114 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002115 return;
2116 }
2117
rginda35c456b2012-02-09 17:29:05 -08002118 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
2119 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
2120
rginda8ba33642011-12-14 12:31:31 -08002121 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002122 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2123 'px';
2124 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2125 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002126
2127 this.cursorNode_.setAttribute('title',
2128 '(' + this.screen_.cursorPosition.row +
2129 ', ' + this.screen_.cursorPosition.column +
2130 ')');
2131
2132 // Update the caret for a11y purposes.
2133 var selection = this.document_.getSelection();
2134 if (selection && selection.isCollapsed)
2135 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002136};
2137
2138/**
2139 * Synchronizes the visible cursor with the current cursor coordinates.
2140 *
2141 * The sync will happen asynchronously, soon after the call stack winds down.
2142 * Multiple calls will be coalesced into a single sync.
2143 */
2144hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2145 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002146 return;
rginda8ba33642011-12-14 12:31:31 -08002147
2148 var self = this;
2149 this.timeouts_.syncCursor = setTimeout(function() {
2150 self.syncCursorPosition_();
2151 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002152 }, 0);
2153};
2154
rgindacc2996c2012-02-24 14:59:31 -08002155/**
rgindaf522ce02012-04-17 17:49:17 -07002156 * Show or hide the zoom warning.
2157 *
2158 * The zoom warning is a message warning the user that their browser zoom must
2159 * be set to 100% in order for hterm to function properly.
2160 *
2161 * @param {boolean} state True to show the message, false to hide it.
2162 */
2163hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2164 if (!this.zoomWarningNode_) {
2165 if (!state)
2166 return;
2167
2168 this.zoomWarningNode_ = this.document_.createElement('div');
2169 this.zoomWarningNode_.style.cssText = (
2170 'color: black;' +
2171 'background-color: #ff2222;' +
2172 'font-size: large;' +
2173 'border-radius: 8px;' +
2174 'opacity: 0.75;' +
2175 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2176 'top: 0.5em;' +
2177 'right: 1.2em;' +
2178 'position: absolute;' +
2179 '-webkit-text-size-adjust: none;' +
2180 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002181 }
2182
rgindade84e382012-04-20 15:39:31 -07002183 this.zoomWarningNode_.textContent = hterm.msg('ZOOM_WARNING') ||
2184 ('!! ' + parseInt(this.scrollPort_.characterSize.zoomFactor * 100) +
2185 '% !!');
rgindaf522ce02012-04-17 17:49:17 -07002186 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2187
2188 if (state) {
2189 if (!this.zoomWarningNode_.parentNode)
2190 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2191 } else if (this.zoomWarningNode_.parentNode) {
2192 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2193 }
2194};
2195
2196/**
rgindacc2996c2012-02-24 14:59:31 -08002197 * Show the terminal overlay for a given amount of time.
2198 *
2199 * The terminal overlay appears in inverse video in a large font, centered
2200 * over the terminal. You should probably keep the overlay message brief,
2201 * since it's in a large font and you probably aren't going to check the size
2202 * of the terminal first.
2203 *
2204 * @param {string} msg The text (not HTML) message to display in the overlay.
2205 * @param {number} opt_timeout The amount of time to wait before fading out
2206 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2207 * stay up forever (or until the next overlay).
2208 */
2209hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002210 if (!this.overlayNode_) {
2211 if (!this.div_)
2212 return;
2213
2214 this.overlayNode_ = this.document_.createElement('div');
2215 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002216 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002217 'font-size: xx-large;' +
2218 'opacity: 0.75;' +
2219 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2220 'position: absolute;' +
2221 '-webkit-user-select: none;' +
2222 '-webkit-transition: opacity 180ms ease-in;');
2223 }
2224
rginda9f5222b2012-03-05 11:53:28 -08002225 this.overlayNode_.style.color = this.prefs_.get('background-color');
2226 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2227 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2228
rgindaf0090c92012-02-10 14:58:52 -08002229 this.overlayNode_.textContent = msg;
2230 this.overlayNode_.style.opacity = '0.75';
2231
2232 if (!this.overlayNode_.parentNode)
2233 this.div_.appendChild(this.overlayNode_);
2234
2235 this.overlayNode_.style.top = (
2236 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
2237 this.overlayNode_.style.left = (
2238 this.div_.clientWidth - this.overlayNode_.clientWidth -
2239 this.scrollbarWidthPx) / 2;
2240
2241 var self = this;
2242
2243 if (this.overlayTimeout_)
2244 clearTimeout(this.overlayTimeout_);
2245
rgindacc2996c2012-02-24 14:59:31 -08002246 if (opt_timeout === null)
2247 return;
2248
rgindaf0090c92012-02-10 14:58:52 -08002249 this.overlayTimeout_ = setTimeout(function() {
2250 self.overlayNode_.style.opacity = '0';
2251 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002252 if (self.overlayNode_.parentNode)
2253 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002254 self.overlayTimeout_ = null;
2255 self.overlayNode_.style.opacity = '0.75';
2256 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002257 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002258};
2259
rginda4bba5e12012-06-20 16:15:30 -07002260/**
2261 * Paste from the system clipboard to the terminal.
2262 */
2263hterm.Terminal.prototype.paste = function() {
2264 hterm.pasteFromClipboard(this.document_);
2265};
2266
2267/**
2268 * Copy a string to the system clipboard.
2269 *
2270 * Note: If there is a selected range in the terminal, it'll be cleared.
2271 */
2272hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda9fb38222012-09-11 14:19:12 -07002273 if (this.prefs_.get('enable-clipboard-notice'))
2274 setTimeout(this.showOverlay.bind(this, hterm.msg('NOTIFY_COPY'), 500), 200);
rgindaa09e7332012-08-17 12:49:51 -07002275
2276 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002277 copySource.textContent = str;
2278 copySource.style.cssText = (
2279 '-webkit-user-select: text;' +
2280 'position: absolute;' +
2281 'top: -99px');
2282
2283 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002284
rginda4bba5e12012-06-20 16:15:30 -07002285 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002286 var anchorNode = selection.anchorNode;
2287 var anchorOffset = selection.anchorOffset;
2288 var focusNode = selection.focusNode;
2289 var focusOffset = selection.focusOffset;
2290
rginda4bba5e12012-06-20 16:15:30 -07002291 selection.selectAllChildren(copySource);
2292
rgindaa09e7332012-08-17 12:49:51 -07002293 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002294
rgindafaa74742012-08-21 13:34:03 -07002295 selection.collapse(anchorNode, anchorOffset);
2296 selection.extend(focusNode, focusOffset);
2297
rginda4bba5e12012-06-20 16:15:30 -07002298 copySource.parentNode.removeChild(copySource);
2299};
2300
rgindaa09e7332012-08-17 12:49:51 -07002301hterm.Terminal.prototype.getSelectionText = function() {
2302 var selection = this.scrollPort_.selection;
2303 selection.sync();
2304
2305 if (selection.isCollapsed)
2306 return null;
2307
2308
2309 // Start offset measures from the beginning of the line.
2310 var startOffset = selection.startOffset;
2311 var node = selection.startNode;
Robert Gindafdbb3f22012-09-06 20:23:06 -07002312 if (node.nodeName != 'X-ROW') {
2313 // If the selection doesn't start on an x-row node, then it must be
2314 // somewhere inside the x-row. Add any characters from previous siblings
2315 // into the start offset.
2316 while (node.previousSibling) {
2317 node = node.previousSibling;
2318 startOffset += node.textContent.length;
2319 }
rgindaa09e7332012-08-17 12:49:51 -07002320 }
2321
2322 // End offset measures from the end of the line.
2323 var endOffset = selection.endNode.textContent.length - selection.endOffset;
2324 var node = selection.endNode;
Robert Gindafdbb3f22012-09-06 20:23:06 -07002325 if (node.nodeName != 'X-ROW') {
2326 // If the selection doesn't end on an x-row node, then it must be
2327 // somewhere inside the x-row. Add any characters from following siblings
2328 // into the end offset.
2329 while (node.nextSibling) {
2330 node = node.nextSibling;
2331 endOffset += node.textContent.length;
2332 }
rgindaa09e7332012-08-17 12:49:51 -07002333 }
2334
2335 var rv = this.getRowsText(selection.startRow.rowIndex,
2336 selection.endRow.rowIndex + 1);
2337 return rv.substring(startOffset, rv.length - endOffset);
2338};
2339
rginda4bba5e12012-06-20 16:15:30 -07002340/**
2341 * Copy the current selection to the system clipboard, then clear it after a
2342 * short delay.
2343 */
2344hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002345 var text = this.getSelectionText();
2346 if (text != null)
2347 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002348};
2349
rgindaf0090c92012-02-10 14:58:52 -08002350hterm.Terminal.prototype.overlaySize = function() {
2351 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2352};
2353
rginda87b86462011-12-14 13:48:03 -08002354/**
2355 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2356 *
2357 * @param {string} string The VT string representing the keystroke.
2358 */
2359hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002360 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002361 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2362
2363 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08002364};
2365
2366/**
rgindad5613292012-06-19 15:40:37 -07002367 * Add the terminalRow and terminalColumn properties to mouse events and
2368 * then forward on to onMouse().
2369 *
2370 * The terminalRow and terminalColumn properties contain the (row, column)
2371 * coordinates for the mouse event.
2372 */
2373hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002374 if (e.processedByTerminalHandler_) {
2375 // We register our event handlers on the document, as well as the cursor
2376 // and the scroll blocker. Mouse events that occur on the cursor or
2377 // scroll blocker will also appear on the document, but we don't want to
2378 // process them twice.
2379 //
2380 // We can't just prevent bubbling because that has other side effects, so
2381 // we decorate the event object with this property instead.
2382 return;
2383 }
2384
2385 e.processedByTerminalHandler_ = true;
2386
rginda4bba5e12012-06-20 16:15:30 -07002387 if (e.type == 'mousedown' && e.which == this.mousePasteButton) {
2388 this.paste();
2389 return;
2390 }
2391
2392 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2393 !this.document_.getSelection().isCollapsed) {
rgindafaa74742012-08-21 13:34:03 -07002394 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002395 return;
2396 }
2397
rgindad5613292012-06-19 15:40:37 -07002398 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2399 this.scrollPort_.characterSize.height) + 1;
2400 e.terminalColumn = parseInt(e.clientX /
2401 this.scrollPort_.characterSize.width) + 1;
2402
2403 if (e.type == 'mousedown') {
2404 if (e.terminalColumn > this.screenSize.width) {
2405 // Mousedown in the scrollbar area.
2406 return;
2407 }
2408
2409 if (!this.enableMouseDragScroll) {
2410 // Move the scroll-blocker into place if we want to keep the scrollport
2411 // from scrolling.
2412 this.scrollBlockerNode_.engaged = true;
2413 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2414 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2415 }
2416 } else if (this.scrollBlockerNode_.engaged &&
2417 (e.type == 'mousemove' || e.type == 'mouseup')) {
2418 // Disengage the scroll-blocker after one of these events.
2419 this.scrollBlockerNode_.engaged = false;
2420 this.scrollBlockerNode_.style.top = '-99px';
2421 }
2422
rgindafaa74742012-08-21 13:34:03 -07002423 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002424};
2425
2426/**
2427 * Clients should override this if they care to know about mouse events.
2428 *
2429 * The event parameter will be a normal DOM mouse click event with additional
2430 * 'terminalRow' and 'terminalColumn' properties.
2431 */
2432hterm.Terminal.prototype.onMouse = function(e) { };
2433
2434/**
rginda8e92a692012-05-20 19:37:20 -07002435 * React when focus changes.
2436 */
2437hterm.Terminal.prototype.onFocusChange_ = function(state) {
2438 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
2439};
2440
2441/**
rginda8ba33642011-12-14 12:31:31 -08002442 * React when the ScrollPort is scrolled.
2443 */
2444hterm.Terminal.prototype.onScroll_ = function() {
2445 this.scheduleSyncCursorPosition_();
2446};
2447
2448/**
rginda9846e2f2012-01-27 13:53:33 -08002449 * React when text is pasted into the scrollPort.
2450 */
2451hterm.Terminal.prototype.onPaste_ = function(e) {
David Benjamin8f962172012-07-17 07:38:43 -04002452 this.io.onVTKeystroke(this.vt.encodeUTF8(e.text));
rginda9846e2f2012-01-27 13:53:33 -08002453};
2454
2455/**
rgindaa09e7332012-08-17 12:49:51 -07002456 * React when the user tries to copy from the scrollPort.
2457 */
2458hterm.Terminal.prototype.onCopy_ = function(e) {
2459 e.preventDefault();
rgindafaa74742012-08-21 13:34:03 -07002460 this.copySelectionToClipboard();
rgindaa09e7332012-08-17 12:49:51 -07002461};
2462
2463/**
rginda8ba33642011-12-14 12:31:31 -08002464 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002465 *
2466 * Note: This function should not directly contain code that alters the internal
2467 * state of the terminal. That kind of code belongs in realizeWidth or
2468 * realizeHeight, so that it can be executed synchronously in the case of a
2469 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002470 */
2471hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002472 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002473 this.scrollPort_.characterSize.width);
2474 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2475 this.scrollPort_.characterSize.height);
2476
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002477 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002478 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002479 // gets removed from the document or during the initial load, and we can't
2480 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002481 return;
2482 }
2483
rgindaa8ba17d2012-08-15 14:41:10 -07002484 var isNewSize = (columnCount != this.screenSize.width ||
2485 rowCount != this.screenSize.height);
2486
2487 // We do this even if the size didn't change, just to be sure everything is
2488 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002489 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002490 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002491
2492 if (isNewSize)
2493 this.overlaySize();
2494
2495 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002496};
2497
2498/**
2499 * Service the cursor blink timeout.
2500 */
2501hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002502 if (this.cursorNode_.style.opacity == '0') {
2503 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002504 } else {
rginda87b86462011-12-14 13:48:03 -08002505 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002506 }
2507};
David Reveman8f552492012-03-28 12:18:41 -04002508
2509/**
2510 * Set the scrollbar-visible mode bit.
2511 *
2512 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2513 * Otherwise it will not.
2514 *
2515 * Defaults to on.
2516 *
2517 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2518 */
2519hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2520 this.scrollPort_.setScrollbarVisible(state);
2521};