blob: 35f96eabe795e995fa538ff19fcfcbaa163f3b1d [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
Masaya Suzuki273aa982014-05-31 07:25:55 +09007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource', 'lib.wc',
Robert Ginda57f03b42012-09-13 11:02:48 -07008 'hterm.Keyboard', 'hterm.Options', 'hterm.PreferenceManager',
Ricky Liang48f05cb2013-12-31 23:35:29 +08009 'hterm.Screen', 'hterm.ScrollPort', 'hterm.Size',
10 'hterm.TextAttributes', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
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 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070053 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080054
rginda87b86462011-12-14 13:48:03 -080055 // The div that contains this terminal.
56 this.div_ = null;
57
rgindac9bc5502012-01-18 11:48:44 -080058 // The document that contains the scrollPort. Defaulted to the global
59 // document here so that the terminal is functional even if it hasn't been
60 // inserted into a document yet, but re-set in decorate().
61 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080062
rginda8ba33642011-12-14 12:31:31 -080063 // The rows that have scrolled off screen and are no longer addressable.
64 this.scrollbackRows_ = [];
65
rgindac9bc5502012-01-18 11:48:44 -080066 // Saved tab stops.
67 this.tabStops_ = [];
68
David Benjamin66e954d2012-05-05 21:08:12 -040069 // Keep track of whether default tab stops have been erased; after a TBC
70 // clears all tab stops, defaults aren't restored on resize until a reset.
71 this.defaultTabStops = true;
72
rginda8ba33642011-12-14 12:31:31 -080073 // The VT's notion of the top and bottom rows. Used during some VT
74 // cursor positioning and scrolling commands.
75 this.vtScrollTop_ = null;
76 this.vtScrollBottom_ = null;
77
78 // The DIV element for the visible cursor.
79 this.cursorNode_ = null;
80
Robert Ginda830583c2013-08-07 13:20:46 -070081 // The current cursor shape of the terminal.
82 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
83
84 // The current color of the cursor.
85 this.cursorColor_ = null;
86
rginda9f5222b2012-03-05 11:53:28 -080087 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070088 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070089 this.backgroundColor_ = null;
90 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070091 this.scrollOnOutput_ = null;
92 this.scrollOnKeystroke_ = null;
rginda9f5222b2012-03-05 11:53:28 -080093
Robert Ginda928cf632014-03-05 15:07:41 -080094 // True if we should send mouse events to the vt, false if we want them
95 // to manage the local text selection.
96 this.reportMouseEvents_ = false;
97
rgindaf0090c92012-02-10 14:58:52 -080098 // Terminal bell sound.
99 this.bellAudio_ = this.document_.createElement('audio');
rgindaf0090c92012-02-10 14:58:52 -0800100 this.bellAudio_.setAttribute('preload', 'auto');
101
Michael Kelly485ecd12014-06-09 11:41:56 -0400102 // All terminal bell notifications that have been generated (not necessarily
103 // shown).
104 this.bellNotificationList_ = [];
105
106 // Whether we have permission to display notifications.
107 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400108
rginda6d397402012-01-17 10:58:29 -0800109 // Cursor position and attributes saved with DECSC.
110 this.savedOptions_ = {};
111
rginda8ba33642011-12-14 12:31:31 -0800112 // The current mode bits for the terminal.
113 this.options_ = new hterm.Options();
114
115 // Timeouts we might need to clear.
116 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800117
118 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800119 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800120
rgindafeaf3142012-01-31 15:14:20 -0800121 // The keyboard hander.
122 this.keyboard = new hterm.Keyboard(this);
123
rginda87b86462011-12-14 13:48:03 -0800124 // General IO interface that can be given to third parties without exposing
125 // the entire terminal object.
126 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800127
rgindad5613292012-06-19 15:40:37 -0700128 // True if mouse-click-drag should scroll the terminal.
129 this.enableMouseDragScroll = true;
130
Robert Ginda57f03b42012-09-13 11:02:48 -0700131 this.copyOnSelect = null;
rginda4bba5e12012-06-20 16:15:30 -0700132 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700133
Rob Spies0bec09b2014-06-06 15:58:09 -0700134 // Whether to use the default window copy behaviour.
135 this.useDefaultWindowCopy = false;
136
137 this.clearSelectionAfterCopy = true;
138
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400139 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800140 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700141
142 this.setProfile(opt_profileId || 'default',
143 function() { this.onTerminalReady() }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800144};
145
146/**
Robert Ginda830583c2013-08-07 13:20:46 -0700147 * Possible cursor shapes.
148 */
149hterm.Terminal.cursorShape = {
150 BLOCK: 'BLOCK',
151 BEAM: 'BEAM',
152 UNDERLINE: 'UNDERLINE'
153};
154
155/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700156 * Clients should override this to be notified when the terminal is ready
157 * for use.
158 *
159 * The terminal initialization is asynchronous, and shouldn't be used before
160 * this method is called.
161 */
162hterm.Terminal.prototype.onTerminalReady = function() { };
163
164/**
rginda35c456b2012-02-09 17:29:05 -0800165 * Default tab with of 8 to match xterm.
166 */
167hterm.Terminal.prototype.tabWidth = 8;
168
169/**
rginda9f5222b2012-03-05 11:53:28 -0800170 * Select a preference profile.
171 *
172 * This will load the terminal preferences for the given profile name and
173 * associate subsequent preference changes with the new preference profile.
174 *
175 * @param {string} newName The name of the preference profile. Forward slash
176 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700177 * @param {function} opt_callback Optional callback to invoke when the profile
178 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800179 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700180hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
181 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800182
Robert Ginda57f03b42012-09-13 11:02:48 -0700183 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800184
Robert Ginda57f03b42012-09-13 11:02:48 -0700185 if (this.prefs_)
186 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800187
Robert Ginda57f03b42012-09-13 11:02:48 -0700188 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
189 this.prefs_.addObservers(null, {
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700190 'alt-backspace-is-meta-backspace': function(v) {
191 terminal.keyboard.altBackspaceIsMetaBackspace = v;
192 },
193
Robert Ginda57f03b42012-09-13 11:02:48 -0700194 'alt-is-meta': function(v) {
195 terminal.keyboard.altIsMeta = v;
196 },
197
198 'alt-sends-what': function(v) {
199 if (!/^(escape|8-bit|browser-key)$/.test(v))
200 v = 'escape';
201
202 terminal.keyboard.altSendsWhat = v;
203 },
204
205 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800206 var ary = v.match(/^lib-resource:(\S+)/);
207 if (ary) {
208 terminal.bellAudio_.setAttribute('src',
209 lib.resource.getDataUrl(ary[1]));
210 } else {
211 terminal.bellAudio_.setAttribute('src', v);
212 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700213 },
214
Michael Kelly485ecd12014-06-09 11:41:56 -0400215 'desktop-notification-bell': function(v) {
216 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700217 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400218 Notification.permission === 'granted';
219 if (!terminal.desktopNotificationBell_) {
220 // Note: We don't call Notification.requestPermission here because
221 // Chrome requires the call be the result of a user action (such as an
222 // onclick handler), and pref listeners are run asynchronously.
223 //
224 // A way of working around this would be to display a dialog in the
225 // terminal with a "click-to-request-permission" button.
226 console.warn('desktop-notification-bell is true but we do not have ' +
227 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400228 }
229 } else {
230 terminal.desktopNotificationBell_ = false;
231 }
232 },
233
Robert Ginda57f03b42012-09-13 11:02:48 -0700234 'background-color': function(v) {
235 terminal.setBackgroundColor(v);
236 },
237
238 'background-image': function(v) {
239 terminal.scrollPort_.setBackgroundImage(v);
240 },
241
242 'background-size': function(v) {
243 terminal.scrollPort_.setBackgroundSize(v);
244 },
245
246 'background-position': function(v) {
247 terminal.scrollPort_.setBackgroundPosition(v);
248 },
249
250 'backspace-sends-backspace': function(v) {
251 terminal.keyboard.backspaceSendsBackspace = v;
252 },
253
254 'cursor-blink': function(v) {
255 terminal.setCursorBlink(!!v);
256 },
257
258 'cursor-color': function(v) {
259 terminal.setCursorColor(v);
260 },
261
262 'color-palette-overrides': function(v) {
263 if (!(v == null || v instanceof Object || v instanceof Array)) {
264 console.warn('Preference color-palette-overrides is not an array or ' +
265 'object: ' + v);
266 return;
rginda9f5222b2012-03-05 11:53:28 -0800267 }
rginda9f5222b2012-03-05 11:53:28 -0800268
Robert Ginda57f03b42012-09-13 11:02:48 -0700269 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700270
Robert Ginda57f03b42012-09-13 11:02:48 -0700271 if (v) {
272 for (var key in v) {
273 var i = parseInt(key);
274 if (isNaN(i) || i < 0 || i > 255) {
275 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
276 continue;
277 }
278
279 if (v[i]) {
280 var rgb = lib.colors.normalizeCSS(v[i]);
281 if (rgb)
282 lib.colors.colorPalette[i] = rgb;
283 }
284 }
rginda30f20f62012-04-05 16:36:19 -0700285 }
rginda30f20f62012-04-05 16:36:19 -0700286
Robert Ginda57f03b42012-09-13 11:02:48 -0700287 terminal.primaryScreen_.textAttributes.resetColorPalette()
288 terminal.alternateScreen_.textAttributes.resetColorPalette();
289 },
rginda30f20f62012-04-05 16:36:19 -0700290
Robert Ginda57f03b42012-09-13 11:02:48 -0700291 'copy-on-select': function(v) {
292 terminal.copyOnSelect = !!v;
293 },
rginda9f5222b2012-03-05 11:53:28 -0800294
Rob Spies0bec09b2014-06-06 15:58:09 -0700295 'use-default-window-copy': function(v) {
296 terminal.useDefaultWindowCopy = !!v;
297 },
298
299 'clear-selection-after-copy': function(v) {
300 terminal.clearSelectionAfterCopy = !!v;
301 },
302
Robert Ginda7e5e9522014-03-14 12:23:58 -0700303 'ctrl-plus-minus-zero-zoom': function(v) {
304 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
305 },
306
Robert Gindafb5a3f92014-05-13 14:12:00 -0700307 'ctrl-c-copy': function(v) {
308 terminal.keyboard.ctrlCCopy = v;
309 },
310
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100311 'ctrl-v-paste': function(v) {
312 terminal.keyboard.ctrlVPaste = v;
313 },
314
Masaya Suzuki273aa982014-05-31 07:25:55 +0900315 'east-asian-ambiguous-as-two-column': function(v) {
316 lib.wc.regardCjkAmbiguous = v;
317 },
318
Robert Ginda57f03b42012-09-13 11:02:48 -0700319 'enable-8-bit-control': function(v) {
320 terminal.vt.enable8BitControl = !!v;
321 },
rginda30f20f62012-04-05 16:36:19 -0700322
Robert Ginda57f03b42012-09-13 11:02:48 -0700323 'enable-bold': function(v) {
324 terminal.syncBoldSafeState();
325 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400326
Robert Ginda3e278d72014-03-25 13:18:51 -0700327 'enable-bold-as-bright': function(v) {
328 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
329 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
330 },
331
Robert Ginda57f03b42012-09-13 11:02:48 -0700332 'enable-clipboard-write': function(v) {
333 terminal.vt.enableClipboardWrite = !!v;
334 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400335
Robert Ginda3755e752013-05-31 13:34:09 -0700336 'enable-dec12': function(v) {
337 terminal.vt.enableDec12 = !!v;
338 },
339
Robert Ginda57f03b42012-09-13 11:02:48 -0700340 'font-family': function(v) {
341 terminal.syncFontFamily();
342 },
rginda30f20f62012-04-05 16:36:19 -0700343
Robert Ginda57f03b42012-09-13 11:02:48 -0700344 'font-size': function(v) {
345 terminal.setFontSize(v);
346 },
rginda9875d902012-08-20 16:21:57 -0700347
Robert Ginda57f03b42012-09-13 11:02:48 -0700348 'font-smoothing': function(v) {
349 terminal.syncFontFamily();
350 },
rgindade84e382012-04-20 15:39:31 -0700351
Robert Ginda57f03b42012-09-13 11:02:48 -0700352 'foreground-color': function(v) {
353 terminal.setForegroundColor(v);
354 },
rginda30f20f62012-04-05 16:36:19 -0700355
Robert Ginda57f03b42012-09-13 11:02:48 -0700356 'home-keys-scroll': function(v) {
357 terminal.keyboard.homeKeysScroll = v;
358 },
rginda4bba5e12012-06-20 16:15:30 -0700359
Robert Ginda57f03b42012-09-13 11:02:48 -0700360 'max-string-sequence': function(v) {
361 terminal.vt.maxStringSequence = v;
362 },
rginda11057d52012-04-25 12:29:56 -0700363
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700364 'media-keys-are-fkeys': function(v) {
365 terminal.keyboard.mediaKeysAreFKeys = v;
366 },
367
Robert Ginda57f03b42012-09-13 11:02:48 -0700368 'meta-sends-escape': function(v) {
369 terminal.keyboard.metaSendsEscape = v;
370 },
rginda30f20f62012-04-05 16:36:19 -0700371
Robert Ginda57f03b42012-09-13 11:02:48 -0700372 'mouse-paste-button': function(v) {
373 terminal.syncMousePasteButton();
374 },
rgindaa8ba17d2012-08-15 14:41:10 -0700375
Robert Gindae76aa9f2014-03-14 12:29:12 -0700376 'page-keys-scroll': function(v) {
377 terminal.keyboard.pageKeysScroll = v;
378 },
379
Robert Ginda40932892012-12-10 17:26:40 -0800380 'pass-alt-number': function(v) {
381 if (v == null) {
382 var osx = window.navigator.userAgent.match(/Mac OS X/);
383
384 // Let Alt-1..9 pass to the browser (to control tab switching) on
385 // non-OS X systems, or if hterm is not opened in an app window.
386 v = (!osx && hterm.windowType != 'popup');
387 }
388
389 terminal.passAltNumber = v;
390 },
391
392 'pass-ctrl-number': function(v) {
393 if (v == null) {
394 var osx = window.navigator.userAgent.match(/Mac OS X/);
395
396 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
397 // non-OS X systems, or if hterm is not opened in an app window.
398 v = (!osx && hterm.windowType != 'popup');
399 }
400
401 terminal.passCtrlNumber = v;
402 },
403
404 'pass-meta-number': function(v) {
405 if (v == null) {
406 var osx = window.navigator.userAgent.match(/Mac OS X/);
407
408 // Let Meta-1..9 pass to the browser (to control tab switching) on
409 // OS X systems, or if hterm is not opened in an app window.
410 v = (osx && hterm.windowType != 'popup');
411 }
412
413 terminal.passMetaNumber = v;
414 },
415
Marius Schilder77857b32014-05-14 16:21:26 -0700416 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700417 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700418 },
419
Robert Ginda8cb7d902013-06-20 14:37:18 -0700420 'receive-encoding': function(v) {
421 if (!(/^(utf-8|raw)$/).test(v)) {
422 console.warn('Invalid value for "receive-encoding": ' + v);
423 v = 'utf-8';
424 }
425
426 terminal.vt.characterEncoding = v;
427 },
428
Robert Ginda57f03b42012-09-13 11:02:48 -0700429 'scroll-on-keystroke': function(v) {
430 terminal.scrollOnKeystroke_ = v;
431 },
rginda9f5222b2012-03-05 11:53:28 -0800432
Robert Ginda57f03b42012-09-13 11:02:48 -0700433 'scroll-on-output': function(v) {
434 terminal.scrollOnOutput_ = v;
435 },
rginda30f20f62012-04-05 16:36:19 -0700436
Robert Ginda57f03b42012-09-13 11:02:48 -0700437 'scrollbar-visible': function(v) {
438 terminal.setScrollbarVisible(v);
439 },
rginda9f5222b2012-03-05 11:53:28 -0800440
Robert Ginda8cb7d902013-06-20 14:37:18 -0700441 'send-encoding': function(v) {
442 if (!(/^(utf-8|raw)$/).test(v)) {
443 console.warn('Invalid value for "send-encoding": ' + v);
444 v = 'utf-8';
445 }
446
447 terminal.keyboard.characterEncoding = v;
448 },
449
Robert Ginda57f03b42012-09-13 11:02:48 -0700450 'shift-insert-paste': function(v) {
451 terminal.keyboard.shiftInsertPaste = v;
452 },
rginda9f5222b2012-03-05 11:53:28 -0800453
Robert Gindae76aa9f2014-03-14 12:29:12 -0700454 'user-css': function(v) {
455 terminal.scrollPort_.setUserCss(v);
Robert Ginda57f03b42012-09-13 11:02:48 -0700456 }
457 });
rginda30f20f62012-04-05 16:36:19 -0700458
Robert Ginda57f03b42012-09-13 11:02:48 -0700459 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800460 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700461
462 if (opt_callback)
463 opt_callback();
464 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800465};
466
Rob Spies56953412014-04-28 14:09:47 -0700467
468/**
469 * Returns the preferences manager used for configuring this terminal.
470 */
471hterm.Terminal.prototype.getPrefs = function() {
472 return this.prefs_;
473};
474
475
rginda8e92a692012-05-20 19:37:20 -0700476/**
477 * Set the color for the cursor.
478 *
479 * If you want this setting to persist, set it through prefs_, rather than
480 * with this method.
481 */
482hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700483 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700484 this.cursorNode_.style.backgroundColor = color;
485 this.cursorNode_.style.borderColor = color;
486};
487
488/**
489 * Return the current cursor color as a string.
490 */
491hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700492 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700493};
494
495/**
rgindad5613292012-06-19 15:40:37 -0700496 * Enable or disable mouse based text selection in the terminal.
497 */
498hterm.Terminal.prototype.setSelectionEnabled = function(state) {
499 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700500};
501
502/**
rginda8e92a692012-05-20 19:37:20 -0700503 * Set the background color.
504 *
505 * If you want this setting to persist, set it through prefs_, rather than
506 * with this method.
507 */
508hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700509 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700510 this.primaryScreen_.textAttributes.setDefaults(
511 this.foregroundColor_, this.backgroundColor_);
512 this.alternateScreen_.textAttributes.setDefaults(
513 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700514 this.scrollPort_.setBackgroundColor(color);
515};
516
rginda9f5222b2012-03-05 11:53:28 -0800517/**
518 * Return the current terminal background color.
519 *
520 * Intended for use by other classes, so we don't have to expose the entire
521 * prefs_ object.
522 */
523hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700524 return this.backgroundColor_;
525};
526
527/**
528 * Set the foreground color.
529 *
530 * If you want this setting to persist, set it through prefs_, rather than
531 * with this method.
532 */
533hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700534 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700535 this.primaryScreen_.textAttributes.setDefaults(
536 this.foregroundColor_, this.backgroundColor_);
537 this.alternateScreen_.textAttributes.setDefaults(
538 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700539 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800540};
541
542/**
543 * Return the current terminal foreground color.
544 *
545 * Intended for use by other classes, so we don't have to expose the entire
546 * prefs_ object.
547 */
548hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700549 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800550};
551
552/**
rginda87b86462011-12-14 13:48:03 -0800553 * Create a new instance of a terminal command and run it with a given
554 * argument string.
555 *
556 * @param {function} commandClass The constructor for a terminal command.
557 * @param {string} argString The argument string to pass to the command.
558 */
559hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700560 var environment = this.prefs_.get('environment');
561 if (typeof environment != 'object' || environment == null)
562 environment = {};
563
rginda87b86462011-12-14 13:48:03 -0800564 var self = this;
565 this.command = new commandClass(
566 { argString: argString || '',
567 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700568 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800569 onExit: function(code) {
570 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800571 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700572 if (self.prefs_.get('close-on-exit'))
573 window.close();
rginda87b86462011-12-14 13:48:03 -0800574 }
575 });
576
rgindafeaf3142012-01-31 15:14:20 -0800577 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800578 this.command.run();
579};
580
581/**
rgindafeaf3142012-01-31 15:14:20 -0800582 * Returns true if the current screen is the primary screen, false otherwise.
583 */
584hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700585 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800586};
587
588/**
589 * Install the keyboard handler for this terminal.
590 *
591 * This will prevent the browser from seeing any keystrokes sent to the
592 * terminal.
593 */
594hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700595 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800596}
597
598/**
599 * Uninstall the keyboard handler for this terminal.
600 */
601hterm.Terminal.prototype.uninstallKeyboard = function() {
602 this.keyboard.installKeyboard(null);
603}
604
605/**
rginda35c456b2012-02-09 17:29:05 -0800606 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800607 *
608 * Call setFontSize(0) to reset to the default font size.
609 *
610 * This function does not modify the font-size preference.
611 *
612 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800613 */
614hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800615 if (px === 0)
616 px = this.prefs_.get('font-size');
617
rginda35c456b2012-02-09 17:29:05 -0800618 this.scrollPort_.setFontSize(px);
Ricky Liang48f05cb2013-12-31 23:35:29 +0800619 if (this.wcCssRule_) {
620 this.wcCssRule_.style.width = this.scrollPort_.characterSize.width * 2 +
621 'px';
622 }
rginda35c456b2012-02-09 17:29:05 -0800623};
624
625/**
626 * Get the current font size.
627 */
628hterm.Terminal.prototype.getFontSize = function() {
629 return this.scrollPort_.getFontSize();
630};
631
632/**
rginda8e92a692012-05-20 19:37:20 -0700633 * Get the current font family.
634 */
635hterm.Terminal.prototype.getFontFamily = function() {
636 return this.scrollPort_.getFontFamily();
637};
638
639/**
rginda35c456b2012-02-09 17:29:05 -0800640 * Set the CSS "font-family" for this terminal.
641 */
rginda9f5222b2012-03-05 11:53:28 -0800642hterm.Terminal.prototype.syncFontFamily = function() {
643 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
644 this.prefs_.get('font-smoothing'));
645 this.syncBoldSafeState();
646};
647
rginda4bba5e12012-06-20 16:15:30 -0700648/**
649 * Set this.mousePasteButton based on the mouse-paste-button pref,
650 * autodetecting if necessary.
651 */
652hterm.Terminal.prototype.syncMousePasteButton = function() {
653 var button = this.prefs_.get('mouse-paste-button');
654 if (typeof button == 'number') {
655 this.mousePasteButton = button;
656 return;
657 }
658
659 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
660 if (!ary || ary[2] == 'CrOS') {
661 this.mousePasteButton = 2;
662 } else {
663 this.mousePasteButton = 3;
664 }
665};
666
667/**
668 * Enable or disable bold based on the enable-bold pref, autodetecting if
669 * necessary.
670 */
rginda9f5222b2012-03-05 11:53:28 -0800671hterm.Terminal.prototype.syncBoldSafeState = function() {
672 var enableBold = this.prefs_.get('enable-bold');
673 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700674 this.primaryScreen_.textAttributes.enableBold = enableBold;
675 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800676 return;
677 }
678
rgindaf7521392012-02-28 17:20:34 -0800679 var normalSize = this.scrollPort_.measureCharacterSize();
680 var boldSize = this.scrollPort_.measureCharacterSize('bold');
681
682 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800683 if (!isBoldSafe) {
684 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700685 'from normal. Font family is: ' +
686 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800687 }
rginda9f5222b2012-03-05 11:53:28 -0800688
Robert Gindaed016262012-10-26 16:27:09 -0700689 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
690 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800691};
692
693/**
rginda87b86462011-12-14 13:48:03 -0800694 * Return a copy of the current cursor position.
695 *
696 * @return {hterm.RowCol} The RowCol object representing the current position.
697 */
698hterm.Terminal.prototype.saveCursor = function() {
699 return this.screen_.cursorPosition.clone();
700};
701
rgindaa19afe22012-01-25 15:40:22 -0800702hterm.Terminal.prototype.getTextAttributes = function() {
703 return this.screen_.textAttributes;
704};
705
rginda1a09aa02012-06-18 21:11:25 -0700706hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
707 this.screen_.textAttributes = textAttributes;
708};
709
rginda87b86462011-12-14 13:48:03 -0800710/**
rgindaf522ce02012-04-17 17:49:17 -0700711 * Return the current browser zoom factor applied to the terminal.
712 *
713 * @return {number} The current browser zoom factor.
714 */
715hterm.Terminal.prototype.getZoomFactor = function() {
716 return this.scrollPort_.characterSize.zoomFactor;
717};
718
719/**
rginda9846e2f2012-01-27 13:53:33 -0800720 * Change the title of this terminal's window.
721 */
722hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800723 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800724};
725
726/**
rginda87b86462011-12-14 13:48:03 -0800727 * Restore a previously saved cursor position.
728 *
729 * @param {hterm.RowCol} cursor The position to restore.
730 */
731hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700732 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
733 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800734 this.screen_.setCursorPosition(row, column);
735 if (cursor.column > column ||
736 cursor.column == column && cursor.overflow) {
737 this.screen_.cursorPosition.overflow = true;
738 }
rginda87b86462011-12-14 13:48:03 -0800739};
740
741/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400742 * Clear the cursor's overflow flag.
743 */
744hterm.Terminal.prototype.clearCursorOverflow = function() {
745 this.screen_.cursorPosition.overflow = false;
746};
747
748/**
Robert Ginda830583c2013-08-07 13:20:46 -0700749 * Sets the cursor shape
750 */
751hterm.Terminal.prototype.setCursorShape = function(shape) {
752 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800753 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700754}
755
756/**
757 * Get the cursor shape
758 */
759hterm.Terminal.prototype.getCursorShape = function() {
760 return this.cursorShape_;
761}
762
763/**
rginda87b86462011-12-14 13:48:03 -0800764 * Set the width of the terminal, resizing the UI to match.
765 */
766hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800767 if (columnCount == null) {
768 this.div_.style.width = '100%';
769 return;
770 }
771
rginda35c456b2012-02-09 17:29:05 -0800772 this.div_.style.width = this.scrollPort_.characterSize.width *
Robert Ginda97769282013-02-01 15:30:30 -0800773 columnCount + this.scrollPort_.currentScrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400774 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800775 this.scheduleSyncCursorPosition_();
776};
rginda87b86462011-12-14 13:48:03 -0800777
rgindac9bc5502012-01-18 11:48:44 -0800778/**
rginda35c456b2012-02-09 17:29:05 -0800779 * Set the height of the terminal, resizing the UI to match.
780 */
781hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800782 if (rowCount == null) {
783 this.div_.style.height = '100%';
784 return;
785 }
786
rginda35c456b2012-02-09 17:29:05 -0800787 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700788 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800789 this.realizeSize_(this.screenSize.width, rowCount);
790 this.scheduleSyncCursorPosition_();
791};
792
793/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400794 * Deal with terminal size changes.
795 *
796 */
797hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
798 if (columnCount != this.screenSize.width)
799 this.realizeWidth_(columnCount);
800
801 if (rowCount != this.screenSize.height)
802 this.realizeHeight_(rowCount);
803
804 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700805 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400806};
807
808/**
rgindac9bc5502012-01-18 11:48:44 -0800809 * Deal with terminal width changes.
810 *
811 * This function does what needs to be done when the terminal width changes
812 * out from under us. It happens here rather than in onResize_() because this
813 * code may need to run synchronously to handle programmatic changes of
814 * terminal width.
815 *
816 * Relying on the browser to send us an async resize event means we may not be
817 * in the correct state yet when the next escape sequence hits.
818 */
819hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700820 if (columnCount <= 0)
821 throw new Error('Attempt to realize bad width: ' + columnCount);
822
rgindac9bc5502012-01-18 11:48:44 -0800823 var deltaColumns = columnCount - this.screen_.getWidth();
824
rginda87b86462011-12-14 13:48:03 -0800825 this.screenSize.width = columnCount;
826 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800827
828 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400829 if (this.defaultTabStops)
830 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800831 } else {
832 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400833 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800834 break;
835
836 this.tabStops_.pop();
837 }
838 }
839
840 this.screen_.setColumnCount(this.screenSize.width);
841};
842
843/**
844 * Deal with terminal height changes.
845 *
846 * This function does what needs to be done when the terminal height changes
847 * out from under us. It happens here rather than in onResize_() because this
848 * code may need to run synchronously to handle programmatic changes of
849 * terminal height.
850 *
851 * Relying on the browser to send us an async resize event means we may not be
852 * in the correct state yet when the next escape sequence hits.
853 */
854hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700855 if (rowCount <= 0)
856 throw new Error('Attempt to realize bad height: ' + rowCount);
857
rgindac9bc5502012-01-18 11:48:44 -0800858 var deltaRows = rowCount - this.screen_.getHeight();
859
860 this.screenSize.height = rowCount;
861
862 var cursor = this.saveCursor();
863
864 if (deltaRows < 0) {
865 // Screen got smaller.
866 deltaRows *= -1;
867 while (deltaRows) {
868 var lastRow = this.getRowCount() - 1;
869 if (lastRow - this.scrollbackRows_.length == cursor.row)
870 break;
871
872 if (this.getRowText(lastRow))
873 break;
874
875 this.screen_.popRow();
876 deltaRows--;
877 }
878
879 var ary = this.screen_.shiftRows(deltaRows);
880 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
881
882 // We just removed rows from the top of the screen, we need to update
883 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800884 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800885 } else if (deltaRows > 0) {
886 // Screen got larger.
887
888 if (deltaRows <= this.scrollbackRows_.length) {
889 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
890 var rows = this.scrollbackRows_.splice(
891 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
892 this.screen_.unshiftRows(rows);
893 deltaRows -= scrollbackCount;
894 cursor.row += scrollbackCount;
895 }
896
897 if (deltaRows)
898 this.appendRows_(deltaRows);
899 }
900
rginda35c456b2012-02-09 17:29:05 -0800901 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800902 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800903};
904
905/**
906 * Scroll the terminal to the top of the scrollback buffer.
907 */
908hterm.Terminal.prototype.scrollHome = function() {
909 this.scrollPort_.scrollRowToTop(0);
910};
911
912/**
913 * Scroll the terminal to the end.
914 */
915hterm.Terminal.prototype.scrollEnd = function() {
916 this.scrollPort_.scrollRowToBottom(this.getRowCount());
917};
918
919/**
920 * Scroll the terminal one page up (minus one line) relative to the current
921 * position.
922 */
923hterm.Terminal.prototype.scrollPageUp = function() {
924 var i = this.scrollPort_.getTopRowIndex();
925 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
926};
927
928/**
929 * Scroll the terminal one page down (minus one line) relative to the current
930 * position.
931 */
932hterm.Terminal.prototype.scrollPageDown = function() {
933 var i = this.scrollPort_.getTopRowIndex();
934 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800935};
936
rgindac9bc5502012-01-18 11:48:44 -0800937/**
Robert Ginda40932892012-12-10 17:26:40 -0800938 * Clear primary screen, secondary screen, and the scrollback buffer.
939 */
940hterm.Terminal.prototype.wipeContents = function() {
941 this.scrollbackRows_.length = 0;
942 this.scrollPort_.resetCache();
943
944 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
945 var bottom = screen.getHeight();
946 if (bottom > 0) {
947 this.renumberRows_(0, bottom);
948 this.clearHome(screen);
949 }
950 }.bind(this));
951
952 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -0700953 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -0800954};
955
956/**
rgindac9bc5502012-01-18 11:48:44 -0800957 * Full terminal reset.
958 */
rginda87b86462011-12-14 13:48:03 -0800959hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800960 this.clearAllTabStops();
961 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700962
963 this.clearHome(this.primaryScreen_);
964 this.primaryScreen_.textAttributes.reset();
965
966 this.clearHome(this.alternateScreen_);
967 this.alternateScreen_.textAttributes.reset();
968
rgindab8bc8932012-04-27 12:45:03 -0700969 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
970
Robert Ginda92e18102013-03-14 13:56:37 -0700971 this.vt.reset();
972
rgindac9bc5502012-01-18 11:48:44 -0800973 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800974};
975
rgindac9bc5502012-01-18 11:48:44 -0800976/**
977 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700978 *
979 * Perform a soft reset to the default values listed in
980 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800981 */
rginda0f5c0292012-01-13 11:00:13 -0800982hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700983 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800984 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700985
rgindab8bc8932012-04-27 12:45:03 -0700986 // Xterm also resets the color palette on soft reset, even though it doesn't
987 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700988 this.primaryScreen_.textAttributes.resetColorPalette();
989 this.alternateScreen_.textAttributes.resetColorPalette();
990
rgindab8bc8932012-04-27 12:45:03 -0700991 // The xterm man page explicitly says this will happen on soft reset.
992 this.setVTScrollRegion(null, null);
993
994 // Xterm also shows the cursor on soft reset, but does not alter the blink
995 // state.
rgindaa19afe22012-01-25 15:40:22 -0800996 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800997};
998
rgindac9bc5502012-01-18 11:48:44 -0800999/**
1000 * Move the cursor forward to the next tab stop, or to the last column
1001 * if no more tab stops are set.
1002 */
1003hterm.Terminal.prototype.forwardTabStop = function() {
1004 var column = this.screen_.cursorPosition.column;
1005
1006 for (var i = 0; i < this.tabStops_.length; i++) {
1007 if (this.tabStops_[i] > column) {
1008 this.setCursorColumn(this.tabStops_[i]);
1009 return;
1010 }
1011 }
1012
David Benjamin66e954d2012-05-05 21:08:12 -04001013 // xterm does not clear the overflow flag on HT or CHT.
1014 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001015 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001016 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001017};
1018
rgindac9bc5502012-01-18 11:48:44 -08001019/**
1020 * Move the cursor backward to the previous tab stop, or to the first column
1021 * if no previous tab stops are set.
1022 */
1023hterm.Terminal.prototype.backwardTabStop = function() {
1024 var column = this.screen_.cursorPosition.column;
1025
1026 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1027 if (this.tabStops_[i] < column) {
1028 this.setCursorColumn(this.tabStops_[i]);
1029 return;
1030 }
1031 }
1032
1033 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001034};
1035
rgindac9bc5502012-01-18 11:48:44 -08001036/**
1037 * Set a tab stop at the given column.
1038 *
1039 * @param {int} column Zero based column.
1040 */
1041hterm.Terminal.prototype.setTabStop = function(column) {
1042 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1043 if (this.tabStops_[i] == column)
1044 return;
1045
1046 if (this.tabStops_[i] < column) {
1047 this.tabStops_.splice(i + 1, 0, column);
1048 return;
1049 }
1050 }
1051
1052 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001053};
1054
rgindac9bc5502012-01-18 11:48:44 -08001055/**
1056 * Clear the tab stop at the current cursor position.
1057 *
1058 * No effect if there is no tab stop at the current cursor position.
1059 */
1060hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1061 var column = this.screen_.cursorPosition.column;
1062
1063 var i = this.tabStops_.indexOf(column);
1064 if (i == -1)
1065 return;
1066
1067 this.tabStops_.splice(i, 1);
1068};
1069
1070/**
1071 * Clear all tab stops.
1072 */
1073hterm.Terminal.prototype.clearAllTabStops = function() {
1074 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001075 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001076};
1077
1078/**
1079 * Set up the default tab stops, starting from a given column.
1080 *
1081 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001082 * from the specified column, or 0 if no column is provided. It also flags
1083 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001084 *
1085 * This does not clear the existing tab stops first, use clearAllTabStops
1086 * for that.
1087 *
1088 * @param {int} opt_start Optional starting zero based starting column, useful
1089 * for filling out missing tab stops when the terminal is resized.
1090 */
1091hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1092 var start = opt_start || 0;
1093 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001094 // Round start up to a default tab stop.
1095 start = start - 1 - ((start - 1) % w) + w;
1096 for (var i = start; i < this.screenSize.width; i += w) {
1097 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001098 }
David Benjamin66e954d2012-05-05 21:08:12 -04001099
1100 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001101};
1102
rginda6d397402012-01-17 10:58:29 -08001103/**
rginda8ba33642011-12-14 12:31:31 -08001104 * Interpret a sequence of characters.
1105 *
1106 * Incomplete escape sequences are buffered until the next call.
1107 *
1108 * @param {string} str Sequence of characters to interpret or pass through.
1109 */
1110hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001111 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001112 this.scheduleSyncCursorPosition_();
1113};
1114
1115/**
1116 * Take over the given DIV for use as the terminal display.
1117 *
1118 * @param {HTMLDivElement} div The div to use as the terminal display.
1119 */
1120hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001121 this.div_ = div;
1122
rginda8ba33642011-12-14 12:31:31 -08001123 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001124 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001125 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1126 this.scrollPort_.setBackgroundPosition(
1127 this.prefs_.get('background-position'));
Robert Gindae76aa9f2014-03-14 12:29:12 -07001128 this.scrollPort_.setUserCss(this.prefs_.get('user-css'));
rginda30f20f62012-04-05 16:36:19 -07001129
rginda0918b652012-04-04 11:26:24 -07001130 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001131
rginda9f5222b2012-03-05 11:53:28 -08001132 this.setFontSize(this.prefs_.get('font-size'));
1133 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001134
David Reveman8f552492012-03-28 12:18:41 -04001135 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1136
rginda8ba33642011-12-14 12:31:31 -08001137 this.document_ = this.scrollPort_.getDocument();
1138
rginda4bba5e12012-06-20 16:15:30 -07001139 this.document_.body.oncontextmenu = function() { return false };
1140
1141 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001142 var screenNode = this.scrollPort_.getScreenNode();
1143 screenNode.addEventListener('mousedown', onMouse);
1144 screenNode.addEventListener('mouseup', onMouse);
1145 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001146 this.scrollPort_.onScrollWheel = onMouse;
1147
Toni Barzic0bfa8922013-11-22 11:18:35 -08001148 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001149 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001150 // Listen for mousedown events on the screenNode as in FF the focus
1151 // events don't bubble.
1152 screenNode.addEventListener('mousedown', function() {
1153 setTimeout(this.onFocusChange_.bind(this, true));
1154 }.bind(this));
1155
Toni Barzic0bfa8922013-11-22 11:18:35 -08001156 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001157 'blur', this.onFocusChange_.bind(this, false));
1158
1159 var style = this.document_.createElement('style');
1160 style.textContent =
1161 ('.cursor-node[focus="false"] {' +
1162 ' box-sizing: border-box;' +
1163 ' background-color: transparent !important;' +
1164 ' border-width: 2px;' +
1165 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001166 '}' +
1167 '.wc-node {' +
1168 ' display: inline-block;' +
1169 ' text-align: center;' +
1170 ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001171 '}');
1172 this.document_.head.appendChild(style);
1173
Ricky Liang48f05cb2013-12-31 23:35:29 +08001174 var styleSheets = this.document_.styleSheets;
1175 var cssRules = styleSheets[styleSheets.length - 1].cssRules;
1176 this.wcCssRule_ = cssRules[cssRules.length - 1];
1177
rginda8ba33642011-12-14 12:31:31 -08001178 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001179 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001180 this.cursorNode_.style.cssText =
1181 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001182 'top: -99px;' +
1183 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001184 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1185 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
Rob Spies06533ba2014-04-24 11:20:37 -07001186 '-webkit-transition: opacity, background-color 100ms linear;' +
1187 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001188
rginda8e92a692012-05-20 19:37:20 -07001189 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001190 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1191 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001192
rginda8ba33642011-12-14 12:31:31 -08001193 this.document_.body.appendChild(this.cursorNode_);
1194
rgindad5613292012-06-19 15:40:37 -07001195 // When 'enableMouseDragScroll' is off we reposition this element directly
1196 // under the mouse cursor after a click. This makes Chrome associate
1197 // subsequent mousemove events with the scroll-blocker. Since the
1198 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1199 // events do not cause the scrollport to scroll.
1200 //
1201 // It's a hack, but it's the cleanest way I could find.
1202 this.scrollBlockerNode_ = this.document_.createElement('div');
1203 this.scrollBlockerNode_.style.cssText =
1204 ('position: absolute;' +
1205 'top: -99px;' +
1206 'display: block;' +
1207 'width: 10px;' +
1208 'height: 10px;');
1209 this.document_.body.appendChild(this.scrollBlockerNode_);
1210
1211 var onMouse = this.onMouse_.bind(this);
1212 this.scrollPort_.onScrollWheel = onMouse;
1213 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1214 ].forEach(function(event) {
1215 this.scrollBlockerNode_.addEventListener(event, onMouse);
1216 this.cursorNode_.addEventListener(event, onMouse);
1217 this.document_.addEventListener(event, onMouse);
1218 }.bind(this));
1219
1220 this.cursorNode_.addEventListener('mousedown', function() {
1221 setTimeout(this.focus.bind(this));
1222 }.bind(this));
1223
rginda8ba33642011-12-14 12:31:31 -08001224 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001225
rginda87b86462011-12-14 13:48:03 -08001226 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001227 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001228};
1229
rginda0918b652012-04-04 11:26:24 -07001230/**
1231 * Return the HTML document that contains the terminal DOM nodes.
1232 */
rginda87b86462011-12-14 13:48:03 -08001233hterm.Terminal.prototype.getDocument = function() {
1234 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001235};
1236
1237/**
rginda0918b652012-04-04 11:26:24 -07001238 * Focus the terminal.
1239 */
1240hterm.Terminal.prototype.focus = function() {
1241 this.scrollPort_.focus();
1242};
1243
1244/**
rginda8ba33642011-12-14 12:31:31 -08001245 * Return the HTML Element for a given row index.
1246 *
1247 * This is a method from the RowProvider interface. The ScrollPort uses
1248 * it to fetch rows on demand as they are scrolled into view.
1249 *
1250 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1251 * pairs to conserve memory.
1252 *
1253 * @param {integer} index The zero-based row index, measured relative to the
1254 * start of the scrollback buffer. On-screen rows will always have the
1255 * largest indicies.
1256 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1257 */
1258hterm.Terminal.prototype.getRowNode = function(index) {
1259 if (index < this.scrollbackRows_.length)
1260 return this.scrollbackRows_[index];
1261
1262 var screenIndex = index - this.scrollbackRows_.length;
1263 return this.screen_.rowsArray[screenIndex];
1264};
1265
1266/**
1267 * Return the text content for a given range of rows.
1268 *
1269 * This is a method from the RowProvider interface. The ScrollPort uses
1270 * it to fetch text content on demand when the user attempts to copy their
1271 * selection to the clipboard.
1272 *
1273 * @param {integer} start The zero-based row index to start from, measured
1274 * relative to the start of the scrollback buffer. On-screen rows will
1275 * always have the largest indicies.
1276 * @param {integer} end The zero-based row index to end on, measured
1277 * relative to the start of the scrollback buffer.
1278 * @return {string} A single string containing the text value of the range of
1279 * rows. Lines will be newline delimited, with no trailing newline.
1280 */
1281hterm.Terminal.prototype.getRowsText = function(start, end) {
1282 var ary = [];
1283 for (var i = start; i < end; i++) {
1284 var node = this.getRowNode(i);
1285 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001286 if (i < end - 1 && !node.getAttribute('line-overflow'))
1287 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001288 }
1289
rgindaa09e7332012-08-17 12:49:51 -07001290 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001291};
1292
1293/**
1294 * Return the text content for a given row.
1295 *
1296 * This is a method from the RowProvider interface. The ScrollPort uses
1297 * it to fetch text content on demand when the user attempts to copy their
1298 * selection to the clipboard.
1299 *
1300 * @param {integer} index The zero-based row index to return, measured
1301 * relative to the start of the scrollback buffer. On-screen rows will
1302 * always have the largest indicies.
1303 * @return {string} A string containing the text value of the selected row.
1304 */
1305hterm.Terminal.prototype.getRowText = function(index) {
1306 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001307 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001308};
1309
1310/**
1311 * Return the total number of rows in the addressable screen and in the
1312 * scrollback buffer of this terminal.
1313 *
1314 * This is a method from the RowProvider interface. The ScrollPort uses
1315 * it to compute the size of the scrollbar.
1316 *
1317 * @return {integer} The number of rows in this terminal.
1318 */
1319hterm.Terminal.prototype.getRowCount = function() {
1320 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1321};
1322
1323/**
1324 * Create DOM nodes for new rows and append them to the end of the terminal.
1325 *
1326 * This is the only correct way to add a new DOM node for a row. Notice that
1327 * the new row is appended to the bottom of the list of rows, and does not
1328 * require renumbering (of the rowIndex property) of previous rows.
1329 *
1330 * If you think you want a new blank row somewhere in the middle of the
1331 * terminal, look into moveRows_().
1332 *
1333 * This method does not pay attention to vtScrollTop/Bottom, since you should
1334 * be using moveRows() in cases where they would matter.
1335 *
1336 * The cursor will be positioned at column 0 of the first inserted line.
1337 */
1338hterm.Terminal.prototype.appendRows_ = function(count) {
1339 var cursorRow = this.screen_.rowsArray.length;
1340 var offset = this.scrollbackRows_.length + cursorRow;
1341 for (var i = 0; i < count; i++) {
1342 var row = this.document_.createElement('x-row');
1343 row.appendChild(this.document_.createTextNode(''));
1344 row.rowIndex = offset + i;
1345 this.screen_.pushRow(row);
1346 }
1347
1348 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1349 if (extraRows > 0) {
1350 var ary = this.screen_.shiftRows(extraRows);
1351 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001352 if (this.scrollPort_.isScrolledEnd)
1353 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001354 }
1355
1356 if (cursorRow >= this.screen_.rowsArray.length)
1357 cursorRow = this.screen_.rowsArray.length - 1;
1358
rginda87b86462011-12-14 13:48:03 -08001359 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001360};
1361
1362/**
1363 * Relocate rows from one part of the addressable screen to another.
1364 *
1365 * This is used to recycle rows during VT scrolls (those which are driven
1366 * by VT commands, rather than by the user manipulating the scrollbar.)
1367 *
1368 * In this case, the blank lines scrolled into the scroll region are made of
1369 * the nodes we scrolled off. These have their rowIndex properties carefully
1370 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001371 */
1372hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1373 var ary = this.screen_.removeRows(fromIndex, count);
1374 this.screen_.insertRows(toIndex, ary);
1375
1376 var start, end;
1377 if (fromIndex < toIndex) {
1378 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001379 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001380 } else {
1381 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001382 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001383 }
1384
1385 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001386 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001387};
1388
1389/**
1390 * Renumber the rowIndex property of the given range of rows.
1391 *
1392 * The start and end indicies are relative to the screen, not the scrollback.
1393 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001394 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001395 * no need to renumber scrollback rows.
1396 */
Robert Ginda40932892012-12-10 17:26:40 -08001397hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1398 var screen = opt_screen || this.screen_;
1399
rginda8ba33642011-12-14 12:31:31 -08001400 var offset = this.scrollbackRows_.length;
1401 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001402 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001403 }
1404};
1405
1406/**
1407 * Print a string to the terminal.
1408 *
1409 * This respects the current insert and wraparound modes. It will add new lines
1410 * to the end of the terminal, scrolling off the top into the scrollback buffer
1411 * if necessary.
1412 *
1413 * The string is *not* parsed for escape codes. Use the interpret() method if
1414 * that's what you're after.
1415 *
1416 * @param{string} str The string to print.
1417 */
1418hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001419 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001420
Ricky Liang48f05cb2013-12-31 23:35:29 +08001421 var strWidth = lib.wc.strWidth(str);
1422
1423 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001424 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1425 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001426 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001427 }
rgindaa19afe22012-01-25 15:40:22 -08001428
Ricky Liang48f05cb2013-12-31 23:35:29 +08001429 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001430 var didOverflow = false;
1431 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001432
rgindaa9abdd82012-08-06 18:05:09 -07001433 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1434 didOverflow = true;
1435 count = this.screenSize.width - this.screen_.cursorPosition.column;
1436 }
rgindaa19afe22012-01-25 15:40:22 -08001437
rgindaa9abdd82012-08-06 18:05:09 -07001438 if (didOverflow && !this.options_.wraparound) {
1439 // If the string overflowed the line but wraparound is off, then the
1440 // last printed character should be the last of the string.
1441 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001442 substr = lib.wc.substr(str, startOffset, count - 1) +
1443 lib.wc.substr(str, strWidth - 1);
1444 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001445 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001446 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001447 }
rgindaa19afe22012-01-25 15:40:22 -08001448
Ricky Liang48f05cb2013-12-31 23:35:29 +08001449 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1450 for (var i = 0; i < tokens.length; i++) {
1451 if (tokens[i].wcNode)
1452 this.screen_.textAttributes.wcNode = true;
1453
1454 if (this.options_.insertMode) {
1455 this.screen_.insertString(tokens[i].str);
1456 } else {
1457 this.screen_.overwriteString(tokens[i].str);
1458 }
1459 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001460 }
1461
1462 this.screen_.maybeClipCurrentRow();
1463 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001464 }
rginda8ba33642011-12-14 12:31:31 -08001465
1466 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001467
rginda9f5222b2012-03-05 11:53:28 -08001468 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001469 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001470};
1471
1472/**
rginda87b86462011-12-14 13:48:03 -08001473 * Set the VT scroll region.
1474 *
rginda87b86462011-12-14 13:48:03 -08001475 * This also resets the cursor position to the absolute (0, 0) position, since
1476 * that's what xterm appears to do.
1477 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001478 * Setting the scroll region to the full height of the terminal will clear
1479 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1480 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1481 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1482 * continue to work as most users would expect.
1483 *
rginda87b86462011-12-14 13:48:03 -08001484 * @param {integer} scrollTop The zero-based top of the scroll region.
1485 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1486 * inclusive.
1487 */
1488hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001489 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001490 this.vtScrollTop_ = null;
1491 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001492 } else {
1493 this.vtScrollTop_ = scrollTop;
1494 this.vtScrollBottom_ = scrollBottom;
1495 }
rginda87b86462011-12-14 13:48:03 -08001496};
1497
1498/**
rginda8ba33642011-12-14 12:31:31 -08001499 * Return the top row index according to the VT.
1500 *
1501 * This will return 0 unless the terminal has been told to restrict scrolling
1502 * to some lower row. It is used for some VT cursor positioning and scrolling
1503 * commands.
1504 *
1505 * @return {integer} The topmost row in the terminal's scroll region.
1506 */
1507hterm.Terminal.prototype.getVTScrollTop = function() {
1508 if (this.vtScrollTop_ != null)
1509 return this.vtScrollTop_;
1510
1511 return 0;
rginda87b86462011-12-14 13:48:03 -08001512};
rginda8ba33642011-12-14 12:31:31 -08001513
1514/**
1515 * Return the bottom row index according to the VT.
1516 *
1517 * This will return the height of the terminal unless the it has been told to
1518 * restrict scrolling to some higher row. It is used for some VT cursor
1519 * positioning and scrolling commands.
1520 *
1521 * @return {integer} The bottommost row in the terminal's scroll region.
1522 */
1523hterm.Terminal.prototype.getVTScrollBottom = function() {
1524 if (this.vtScrollBottom_ != null)
1525 return this.vtScrollBottom_;
1526
rginda87b86462011-12-14 13:48:03 -08001527 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001528}
1529
1530/**
1531 * Process a '\n' character.
1532 *
1533 * If the cursor is on the final row of the terminal this will append a new
1534 * blank row to the screen and scroll the topmost row into the scrollback
1535 * buffer.
1536 *
1537 * Otherwise, this moves the cursor to column zero of the next row.
1538 */
1539hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001540 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1541 this.screen_.rowsArray.length - 1);
1542
1543 if (this.vtScrollBottom_ != null) {
1544 // A VT Scroll region is active, we never append new rows.
1545 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1546 // We're at the end of the VT Scroll Region, perform a VT scroll.
1547 this.vtScrollUp(1);
1548 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1549 } else if (cursorAtEndOfScreen) {
1550 // We're at the end of the screen, the only thing to do is put the
1551 // cursor to column 0.
1552 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1553 } else {
1554 // Anywhere else, advance the cursor row, and reset the column.
1555 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1556 }
1557 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001558 // We're at the end of the screen. Append a new row to the terminal,
1559 // shifting the top row into the scrollback.
1560 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001561 } else {
rginda87b86462011-12-14 13:48:03 -08001562 // Anywhere else in the screen just moves the cursor.
1563 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001564 }
1565};
1566
1567/**
1568 * Like newLine(), except maintain the cursor column.
1569 */
1570hterm.Terminal.prototype.lineFeed = function() {
1571 var column = this.screen_.cursorPosition.column;
1572 this.newLine();
1573 this.setCursorColumn(column);
1574};
1575
1576/**
rginda87b86462011-12-14 13:48:03 -08001577 * If autoCarriageReturn is set then newLine(), else lineFeed().
1578 */
1579hterm.Terminal.prototype.formFeed = function() {
1580 if (this.options_.autoCarriageReturn) {
1581 this.newLine();
1582 } else {
1583 this.lineFeed();
1584 }
1585};
1586
1587/**
1588 * Move the cursor up one row, possibly inserting a blank line.
1589 *
1590 * The cursor column is not changed.
1591 */
1592hterm.Terminal.prototype.reverseLineFeed = function() {
1593 var scrollTop = this.getVTScrollTop();
1594 var currentRow = this.screen_.cursorPosition.row;
1595
1596 if (currentRow == scrollTop) {
1597 this.insertLines(1);
1598 } else {
1599 this.setAbsoluteCursorRow(currentRow - 1);
1600 }
1601};
1602
1603/**
rginda8ba33642011-12-14 12:31:31 -08001604 * Replace all characters to the left of the current cursor with the space
1605 * character.
1606 *
1607 * TODO(rginda): This should probably *remove* the characters (not just replace
1608 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001609 * position.
rginda8ba33642011-12-14 12:31:31 -08001610 */
1611hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001612 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001613 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001614 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001615 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001616};
1617
1618/**
David Benjamin684a9b72012-05-01 17:19:58 -04001619 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001620 *
1621 * The cursor position is unchanged.
1622 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001623 * If the current background color is not the default background color this
1624 * will insert spaces rather than delete. This is unfortunate because the
1625 * trailing space will affect text selection, but it's difficult to come up
1626 * with a way to style empty space that wouldn't trip up the hterm.Screen
1627 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001628 *
1629 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1630 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1631 * crbug.com/232390 for details.
rginda8ba33642011-12-14 12:31:31 -08001632 */
1633hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001634 if (this.screen_.cursorPosition.overflow)
1635 return;
1636
Robert Ginda7fd57082012-09-25 14:41:47 -07001637 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1638 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001639
1640 if (this.screen_.textAttributes.background ===
1641 this.screen_.textAttributes.DEFAULT_COLOR) {
1642 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001643 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001644 this.screen_.cursorPosition.column + count) {
1645 this.screen_.deleteChars(count);
1646 this.clearCursorOverflow();
1647 return;
1648 }
1649 }
1650
rginda87b86462011-12-14 13:48:03 -08001651 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001652 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001653 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001654 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001655};
1656
1657/**
1658 * Erase the current line.
1659 *
1660 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001661 */
1662hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001663 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001664 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001665 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001666 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001667};
1668
1669/**
David Benjamina08d78f2012-05-05 00:28:49 -04001670 * Erase all characters from the start of the screen to the current cursor
1671 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001672 *
1673 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001674 */
1675hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001676 var cursor = this.saveCursor();
1677
1678 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001679
David Benjamina08d78f2012-05-05 00:28:49 -04001680 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001681 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001682 this.screen_.clearCursorRow();
1683 }
1684
rginda87b86462011-12-14 13:48:03 -08001685 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001686 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001687};
1688
1689/**
1690 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001691 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001692 *
1693 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001694 */
1695hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001696 var cursor = this.saveCursor();
1697
1698 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001699
David Benjamina08d78f2012-05-05 00:28:49 -04001700 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001701 for (var i = cursor.row + 1; i <= bottom; i++) {
1702 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001703 this.screen_.clearCursorRow();
1704 }
1705
rginda87b86462011-12-14 13:48:03 -08001706 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001707 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001708};
1709
1710/**
1711 * Fill the terminal with a given character.
1712 *
1713 * This methods does not respect the VT scroll region.
1714 *
1715 * @param {string} ch The character to use for the fill.
1716 */
1717hterm.Terminal.prototype.fill = function(ch) {
1718 var cursor = this.saveCursor();
1719
1720 this.setAbsoluteCursorPosition(0, 0);
1721 for (var row = 0; row < this.screenSize.height; row++) {
1722 for (var col = 0; col < this.screenSize.width; col++) {
1723 this.setAbsoluteCursorPosition(row, col);
1724 this.screen_.overwriteString(ch);
1725 }
1726 }
1727
1728 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001729};
1730
1731/**
rginda9ea433c2012-03-16 11:57:00 -07001732 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001733 *
rginda9ea433c2012-03-16 11:57:00 -07001734 * This does not respect the scroll region.
1735 *
1736 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1737 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001738 */
rginda9ea433c2012-03-16 11:57:00 -07001739hterm.Terminal.prototype.clearHome = function(opt_screen) {
1740 var screen = opt_screen || this.screen_;
1741 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001742
rginda11057d52012-04-25 12:29:56 -07001743 if (bottom == 0) {
1744 // Empty screen, nothing to do.
1745 return;
1746 }
1747
rgindae4d29232012-01-19 10:47:13 -08001748 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001749 screen.setCursorPosition(i, 0);
1750 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001751 }
1752
rginda9ea433c2012-03-16 11:57:00 -07001753 screen.setCursorPosition(0, 0);
1754};
1755
1756/**
1757 * Erase the entire display without changing the cursor position.
1758 *
1759 * The cursor position is unchanged. This does not respect the scroll
1760 * region.
1761 *
1762 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1763 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001764 */
1765hterm.Terminal.prototype.clear = function(opt_screen) {
1766 var screen = opt_screen || this.screen_;
1767 var cursor = screen.cursorPosition.clone();
1768 this.clearHome(screen);
1769 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001770};
1771
1772/**
1773 * VT command to insert lines at the current cursor row.
1774 *
1775 * This respects the current scroll region. Rows pushed off the bottom are
1776 * lost (they won't show up in the scrollback buffer).
1777 *
rginda8ba33642011-12-14 12:31:31 -08001778 * @param {integer} count The number of lines to insert.
1779 */
1780hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001781 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001782
1783 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001784 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001785
Robert Ginda579186b2012-09-26 11:40:04 -07001786 // The moveCount is the number of rows we need to relocate to make room for
1787 // the new row(s). The count is the distance to move them.
1788 var moveCount = bottom - cursorRow - count + 1;
1789 if (moveCount)
1790 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001791
Robert Ginda579186b2012-09-26 11:40:04 -07001792 for (var i = count - 1; i >= 0; i--) {
1793 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001794 this.screen_.clearCursorRow();
1795 }
rginda8ba33642011-12-14 12:31:31 -08001796};
1797
1798/**
1799 * VT command to delete lines at the current cursor row.
1800 *
1801 * New rows are added to the bottom of scroll region to take their place. New
1802 * rows are strictly there to take up space and have no content or style.
1803 */
1804hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001805 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001806
rginda87b86462011-12-14 13:48:03 -08001807 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001808 var bottom = this.getVTScrollBottom();
1809
rginda87b86462011-12-14 13:48:03 -08001810 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001811 count = Math.min(count, maxCount);
1812
rginda87b86462011-12-14 13:48:03 -08001813 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001814 if (count != maxCount)
1815 this.moveRows_(top, count, moveStart);
1816
1817 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001818 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001819 this.screen_.clearCursorRow();
1820 }
1821
rginda87b86462011-12-14 13:48:03 -08001822 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001823 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001824};
1825
1826/**
1827 * Inserts the given number of spaces at the current cursor position.
1828 *
rginda87b86462011-12-14 13:48:03 -08001829 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001830 */
1831hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001832 var cursor = this.saveCursor();
1833
rgindacbbd7482012-06-13 15:06:16 -07001834 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001835 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001836 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001837
1838 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001839 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001840};
1841
1842/**
1843 * Forward-delete the specified number of characters starting at the cursor
1844 * position.
1845 *
1846 * @param {integer} count The number of characters to delete.
1847 */
1848hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001849 var deleted = this.screen_.deleteChars(count);
1850 if (deleted && !this.screen_.textAttributes.isDefault()) {
1851 var cursor = this.saveCursor();
1852 this.setCursorColumn(this.screenSize.width - deleted);
1853 this.screen_.insertString(lib.f.getWhitespace(deleted));
1854 this.restoreCursor(cursor);
1855 }
1856
David Benjamin54e8bf62012-06-01 22:31:40 -04001857 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001858};
1859
1860/**
1861 * Shift rows in the scroll region upwards by a given number of lines.
1862 *
1863 * New rows are inserted at the bottom of the scroll region to fill the
1864 * vacated rows. The new rows not filled out with the current text attributes.
1865 *
1866 * This function does not affect the scrollback rows at all. Rows shifted
1867 * off the top are lost.
1868 *
rginda87b86462011-12-14 13:48:03 -08001869 * The cursor position is not altered.
1870 *
rginda8ba33642011-12-14 12:31:31 -08001871 * @param {integer} count The number of rows to scroll.
1872 */
1873hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001874 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001875
rginda87b86462011-12-14 13:48:03 -08001876 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001877 this.deleteLines(count);
1878
rginda87b86462011-12-14 13:48:03 -08001879 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001880};
1881
1882/**
1883 * Shift rows below the cursor down by a given number of lines.
1884 *
1885 * This function respects the current scroll region.
1886 *
1887 * New rows are inserted at the top of the scroll region to fill the
1888 * vacated rows. The new rows not filled out with the current text attributes.
1889 *
1890 * This function does not affect the scrollback rows at all. Rows shifted
1891 * off the bottom are lost.
1892 *
1893 * @param {integer} count The number of rows to scroll.
1894 */
1895hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001896 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001897
rginda87b86462011-12-14 13:48:03 -08001898 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001899 this.insertLines(opt_count);
1900
rginda87b86462011-12-14 13:48:03 -08001901 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001902};
1903
rginda87b86462011-12-14 13:48:03 -08001904
rginda8ba33642011-12-14 12:31:31 -08001905/**
1906 * Set the cursor position.
1907 *
1908 * The cursor row is relative to the scroll region if the terminal has
1909 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1910 *
1911 * @param {integer} row The new zero-based cursor row.
1912 * @param {integer} row The new zero-based cursor column.
1913 */
1914hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1915 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001916 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001917 } else {
rginda87b86462011-12-14 13:48:03 -08001918 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001919 }
rginda87b86462011-12-14 13:48:03 -08001920};
rginda8ba33642011-12-14 12:31:31 -08001921
rginda87b86462011-12-14 13:48:03 -08001922hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1923 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001924 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1925 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001926 this.screen_.setCursorPosition(row, column);
1927};
1928
1929hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001930 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1931 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001932 this.screen_.setCursorPosition(row, column);
1933};
1934
1935/**
1936 * Set the cursor column.
1937 *
1938 * @param {integer} column The new zero-based cursor column.
1939 */
1940hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001941 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001942};
1943
1944/**
1945 * Return the cursor column.
1946 *
1947 * @return {integer} The zero-based cursor column.
1948 */
1949hterm.Terminal.prototype.getCursorColumn = function() {
1950 return this.screen_.cursorPosition.column;
1951};
1952
1953/**
1954 * Set the cursor row.
1955 *
1956 * The cursor row is relative to the scroll region if the terminal has
1957 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1958 *
1959 * @param {integer} row The new cursor row.
1960 */
rginda87b86462011-12-14 13:48:03 -08001961hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1962 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001963};
1964
1965/**
1966 * Return the cursor row.
1967 *
1968 * @return {integer} The zero-based cursor row.
1969 */
1970hterm.Terminal.prototype.getCursorRow = function(row) {
1971 return this.screen_.cursorPosition.row;
1972};
1973
1974/**
1975 * Request that the ScrollPort redraw itself soon.
1976 *
1977 * The redraw will happen asynchronously, soon after the call stack winds down.
1978 * Multiple calls will be coalesced into a single redraw.
1979 */
1980hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001981 if (this.timeouts_.redraw)
1982 return;
rginda8ba33642011-12-14 12:31:31 -08001983
1984 var self = this;
rginda87b86462011-12-14 13:48:03 -08001985 this.timeouts_.redraw = setTimeout(function() {
1986 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001987 self.scrollPort_.redraw_();
1988 }, 0);
1989};
1990
1991/**
1992 * Request that the ScrollPort be scrolled to the bottom.
1993 *
1994 * The scroll will happen asynchronously, soon after the call stack winds down.
1995 * Multiple calls will be coalesced into a single scroll.
1996 *
1997 * This affects the scrollbar position of the ScrollPort, and has nothing to
1998 * do with the VT scroll commands.
1999 */
2000hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2001 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002002 return;
rginda8ba33642011-12-14 12:31:31 -08002003
2004 var self = this;
2005 this.timeouts_.scrollDown = setTimeout(function() {
2006 delete self.timeouts_.scrollDown;
2007 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2008 }, 10);
2009};
2010
2011/**
2012 * Move the cursor up a specified number of rows.
2013 *
2014 * @param {integer} count The number of rows to move the cursor.
2015 */
2016hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002017 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002018};
2019
2020/**
2021 * Move the cursor down a specified number of rows.
2022 *
2023 * @param {integer} count The number of rows to move the cursor.
2024 */
2025hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002026 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002027 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2028 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2029 this.screenSize.height - 1);
2030
rgindacbbd7482012-06-13 15:06:16 -07002031 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002032 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002033 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002034};
2035
2036/**
2037 * Move the cursor left a specified number of columns.
2038 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002039 * If reverse wraparound mode is enabled and the previous row wrapped into
2040 * the current row then we back up through the wraparound as well.
2041 *
rginda8ba33642011-12-14 12:31:31 -08002042 * @param {integer} count The number of columns to move the cursor.
2043 */
2044hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002045 count = count || 1;
2046
2047 if (count < 1)
2048 return;
2049
2050 var currentColumn = this.screen_.cursorPosition.column;
2051 var newColumn = currentColumn - count;
2052 if (newColumn < 0) {
2053 if (this.options_.reverseWraparound) {
2054 var currentRow = this.screen_.cursorPosition.row;
2055 if (currentRow > 0 &&
2056 this.screen_.rowsArray[currentRow - 1].getAttribute(
2057 'line-overflow')) {
2058 this.setCursorPosition(currentRow - 1, this.screenSize.width - 1);
2059 if (count - currentColumn - 1)
2060 this.cursorLeft(count - currentColumn - 1);
2061 return;
2062 }
2063 }
2064
2065 newColumn = 0;
2066 }
2067
2068 this.setCursorColumn(newColumn);
2069 return;
rginda8ba33642011-12-14 12:31:31 -08002070};
2071
2072/**
2073 * Move the cursor right a specified number of columns.
2074 *
2075 * @param {integer} count The number of columns to move the cursor.
2076 */
2077hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002078 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002079
2080 if (count < 1)
2081 return;
2082
rgindacbbd7482012-06-13 15:06:16 -07002083 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002084 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002085 this.setCursorColumn(column);
2086};
2087
2088/**
2089 * Reverse the foreground and background colors of the terminal.
2090 *
2091 * This only affects text that was drawn with no attributes.
2092 *
2093 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2094 * been drawn with attributes that happen to coincide with the default
2095 * 'no-attribute' colors. My guess is probably not.
2096 */
2097hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002098 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002099 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002100 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2101 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002102 } else {
rginda9f5222b2012-03-05 11:53:28 -08002103 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2104 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002105 }
2106};
2107
2108/**
rginda87b86462011-12-14 13:48:03 -08002109 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002110 *
2111 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002112 */
2113hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002114 this.cursorNode_.style.backgroundColor =
2115 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002116
2117 var self = this;
2118 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002119 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002120 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002121
Michael Kelly485ecd12014-06-09 11:41:56 -04002122 // bellSquelchTimeout_ affects both audio and notification bells.
2123 if (this.bellSquelchTimeout_)
2124 return;
2125
Robert Ginda92e18102013-03-14 13:56:37 -07002126 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002127 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002128 this.bellSequelchTimeout_ = setTimeout(function() {
2129 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002130 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002131 } else {
2132 delete this.bellSquelchTimeout_;
2133 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002134
2135 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
2136 var n = new Notification(
2137 lib.f.replaceVars(hterm.desktopNotificationTitle,
Robert Ginda348dc2b2014-06-24 14:42:23 -07002138 {'title': window.document.title || 'hterm'}));
Michael Kelly485ecd12014-06-09 11:41:56 -04002139 this.bellNotificationList_.push(n);
2140 // TODO: Should we try to raise the window here?
2141 n.onclick = function() { self.closeBellNotifications_(); };
2142 }
rginda87b86462011-12-14 13:48:03 -08002143};
2144
2145/**
rginda8ba33642011-12-14 12:31:31 -08002146 * Set the origin mode bit.
2147 *
2148 * If origin mode is on, certain VT cursor and scrolling commands measure their
2149 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2150 * to the top of the addressable screen.
2151 *
2152 * Defaults to off.
2153 *
2154 * @param {boolean} state True to set origin mode, false to unset.
2155 */
2156hterm.Terminal.prototype.setOriginMode = function(state) {
2157 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002158 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002159};
2160
2161/**
2162 * Set the insert mode bit.
2163 *
2164 * If insert mode is on, existing text beyond the cursor position will be
2165 * shifted right to make room for new text. Otherwise, new text overwrites
2166 * any existing text.
2167 *
2168 * Defaults to off.
2169 *
2170 * @param {boolean} state True to set insert mode, false to unset.
2171 */
2172hterm.Terminal.prototype.setInsertMode = function(state) {
2173 this.options_.insertMode = state;
2174};
2175
2176/**
rginda87b86462011-12-14 13:48:03 -08002177 * Set the auto carriage return bit.
2178 *
2179 * If auto carriage return is on then a formfeed character is interpreted
2180 * as a newline, otherwise it's the same as a linefeed. The difference boils
2181 * down to whether or not the cursor column is reset.
2182 */
2183hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2184 this.options_.autoCarriageReturn = state;
2185};
2186
2187/**
rginda8ba33642011-12-14 12:31:31 -08002188 * Set the wraparound mode bit.
2189 *
2190 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2191 * to the start of the following row. Otherwise, the cursor is clamped to the
2192 * end of the screen and attempts to write past it are ignored.
2193 *
2194 * Defaults to on.
2195 *
2196 * @param {boolean} state True to set wraparound mode, false to unset.
2197 */
2198hterm.Terminal.prototype.setWraparound = function(state) {
2199 this.options_.wraparound = state;
2200};
2201
2202/**
2203 * Set the reverse-wraparound mode bit.
2204 *
2205 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2206 * to the end of the previous row. Otherwise, the cursor is clamped to column
2207 * 0.
2208 *
2209 * Defaults to off.
2210 *
2211 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2212 */
2213hterm.Terminal.prototype.setReverseWraparound = function(state) {
2214 this.options_.reverseWraparound = state;
2215};
2216
2217/**
2218 * Selects between the primary and alternate screens.
2219 *
2220 * If alternate mode is on, the alternate screen is active. Otherwise the
2221 * primary screen is active.
2222 *
2223 * Swapping screens has no effect on the scrollback buffer.
2224 *
2225 * Each screen maintains its own cursor position.
2226 *
2227 * Defaults to off.
2228 *
2229 * @param {boolean} state True to set alternate mode, false to unset.
2230 */
2231hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002232 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002233 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2234
rginda35c456b2012-02-09 17:29:05 -08002235 if (this.screen_.rowsArray.length &&
2236 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2237 // If the screen changed sizes while we were away, our rowIndexes may
2238 // be incorrect.
2239 var offset = this.scrollbackRows_.length;
2240 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002241 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002242 ary[i].rowIndex = offset + i;
2243 }
2244 }
rginda8ba33642011-12-14 12:31:31 -08002245
rginda35c456b2012-02-09 17:29:05 -08002246 this.realizeWidth_(this.screenSize.width);
2247 this.realizeHeight_(this.screenSize.height);
2248 this.scrollPort_.syncScrollHeight();
2249 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002250
rginda6d397402012-01-17 10:58:29 -08002251 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002252 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002253};
2254
2255/**
2256 * Set the cursor-blink mode bit.
2257 *
2258 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2259 * a visible cursor does not blink.
2260 *
2261 * You should make sure to turn blinking off if you're going to dispose of a
2262 * terminal, otherwise you'll leak a timeout.
2263 *
2264 * Defaults to on.
2265 *
2266 * @param {boolean} state True to set cursor-blink mode, false to unset.
2267 */
2268hterm.Terminal.prototype.setCursorBlink = function(state) {
2269 this.options_.cursorBlink = state;
2270
2271 if (!state && this.timeouts_.cursorBlink) {
2272 clearTimeout(this.timeouts_.cursorBlink);
2273 delete this.timeouts_.cursorBlink;
2274 }
2275
2276 if (this.options_.cursorVisible)
2277 this.setCursorVisible(true);
2278};
2279
2280/**
2281 * Set the cursor-visible mode bit.
2282 *
2283 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2284 *
2285 * Defaults to on.
2286 *
2287 * @param {boolean} state True to set cursor-visible mode, false to unset.
2288 */
2289hterm.Terminal.prototype.setCursorVisible = function(state) {
2290 this.options_.cursorVisible = state;
2291
2292 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002293 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002294 return;
2295 }
2296
rginda87b86462011-12-14 13:48:03 -08002297 this.syncCursorPosition_();
2298
2299 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002300
2301 if (this.options_.cursorBlink) {
2302 if (this.timeouts_.cursorBlink)
2303 return;
2304
2305 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2306 500);
2307 } else {
2308 if (this.timeouts_.cursorBlink) {
2309 clearTimeout(this.timeouts_.cursorBlink);
2310 delete this.timeouts_.cursorBlink;
2311 }
2312 }
2313};
2314
2315/**
rginda87b86462011-12-14 13:48:03 -08002316 * Synchronizes the visible cursor and document selection with the current
2317 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002318 */
2319hterm.Terminal.prototype.syncCursorPosition_ = function() {
2320 var topRowIndex = this.scrollPort_.getTopRowIndex();
2321 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2322 var cursorRowIndex = this.scrollbackRows_.length +
2323 this.screen_.cursorPosition.row;
2324
2325 if (cursorRowIndex > bottomRowIndex) {
2326 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002327 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002328 return;
2329 }
2330
2331 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002332 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2333 'px';
2334 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2335 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002336
2337 this.cursorNode_.setAttribute('title',
2338 '(' + this.screen_.cursorPosition.row +
2339 ', ' + this.screen_.cursorPosition.column +
2340 ')');
2341
2342 // Update the caret for a11y purposes.
2343 var selection = this.document_.getSelection();
2344 if (selection && selection.isCollapsed)
2345 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002346};
2347
Robert Gindafb1be6a2013-12-11 11:56:22 -08002348/**
2349 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2350 * and character cell dimensions.
2351 */
Robert Ginda830583c2013-08-07 13:20:46 -07002352hterm.Terminal.prototype.restyleCursor_ = function() {
2353 var shape = this.cursorShape_;
2354
2355 if (this.cursorNode_.getAttribute('focus') == 'false') {
2356 // Always show a block cursor when unfocused.
2357 shape = hterm.Terminal.cursorShape.BLOCK;
2358 }
2359
2360 var style = this.cursorNode_.style;
2361
Robert Gindafb1be6a2013-12-11 11:56:22 -08002362 style.width = this.scrollPort_.characterSize.width + 'px';
2363
Robert Ginda830583c2013-08-07 13:20:46 -07002364 switch (shape) {
2365 case hterm.Terminal.cursorShape.BEAM:
2366 style.height = this.scrollPort_.characterSize.height + 'px';
2367 style.backgroundColor = 'transparent';
2368 style.borderBottomStyle = null;
2369 style.borderLeftStyle = 'solid';
2370 break;
2371
2372 case hterm.Terminal.cursorShape.UNDERLINE:
2373 style.height = this.scrollPort_.characterSize.baseline + 'px';
2374 style.backgroundColor = 'transparent';
2375 style.borderBottomStyle = 'solid';
2376 // correct the size to put it exactly at the baseline
2377 style.borderLeftStyle = null;
2378 break;
2379
2380 default:
2381 style.height = this.scrollPort_.characterSize.height + 'px';
2382 style.backgroundColor = this.cursorColor_;
2383 style.borderBottomStyle = null;
2384 style.borderLeftStyle = null;
2385 break;
2386 }
2387};
2388
rginda8ba33642011-12-14 12:31:31 -08002389/**
2390 * Synchronizes the visible cursor with the current cursor coordinates.
2391 *
2392 * The sync will happen asynchronously, soon after the call stack winds down.
2393 * Multiple calls will be coalesced into a single sync.
2394 */
2395hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2396 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002397 return;
rginda8ba33642011-12-14 12:31:31 -08002398
2399 var self = this;
2400 this.timeouts_.syncCursor = setTimeout(function() {
2401 self.syncCursorPosition_();
2402 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002403 }, 0);
2404};
2405
rgindacc2996c2012-02-24 14:59:31 -08002406/**
rgindaf522ce02012-04-17 17:49:17 -07002407 * Show or hide the zoom warning.
2408 *
2409 * The zoom warning is a message warning the user that their browser zoom must
2410 * be set to 100% in order for hterm to function properly.
2411 *
2412 * @param {boolean} state True to show the message, false to hide it.
2413 */
2414hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2415 if (!this.zoomWarningNode_) {
2416 if (!state)
2417 return;
2418
2419 this.zoomWarningNode_ = this.document_.createElement('div');
2420 this.zoomWarningNode_.style.cssText = (
2421 'color: black;' +
2422 'background-color: #ff2222;' +
2423 'font-size: large;' +
2424 'border-radius: 8px;' +
2425 'opacity: 0.75;' +
2426 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2427 'top: 0.5em;' +
2428 'right: 1.2em;' +
2429 'position: absolute;' +
2430 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002431 '-webkit-user-select: none;' +
2432 '-moz-text-size-adjust: none;' +
2433 '-moz-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002434 }
2435
Robert Gindab4839c22013-02-28 16:52:10 -08002436 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2437 hterm.zoomWarningMessage,
2438 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2439
rgindaf522ce02012-04-17 17:49:17 -07002440 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2441
2442 if (state) {
2443 if (!this.zoomWarningNode_.parentNode)
2444 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2445 } else if (this.zoomWarningNode_.parentNode) {
2446 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2447 }
2448};
2449
2450/**
rgindacc2996c2012-02-24 14:59:31 -08002451 * Show the terminal overlay for a given amount of time.
2452 *
2453 * The terminal overlay appears in inverse video in a large font, centered
2454 * over the terminal. You should probably keep the overlay message brief,
2455 * since it's in a large font and you probably aren't going to check the size
2456 * of the terminal first.
2457 *
2458 * @param {string} msg The text (not HTML) message to display in the overlay.
2459 * @param {number} opt_timeout The amount of time to wait before fading out
2460 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2461 * stay up forever (or until the next overlay).
2462 */
2463hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002464 if (!this.overlayNode_) {
2465 if (!this.div_)
2466 return;
2467
2468 this.overlayNode_ = this.document_.createElement('div');
2469 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002470 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002471 'font-size: xx-large;' +
2472 'opacity: 0.75;' +
2473 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2474 'position: absolute;' +
2475 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002476 '-webkit-transition: opacity 180ms ease-in;' +
2477 '-moz-user-select: none;' +
2478 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002479
2480 this.overlayNode_.addEventListener('mousedown', function(e) {
2481 e.preventDefault();
2482 e.stopPropagation();
2483 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002484 }
2485
rginda9f5222b2012-03-05 11:53:28 -08002486 this.overlayNode_.style.color = this.prefs_.get('background-color');
2487 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2488 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2489
rgindaf0090c92012-02-10 14:58:52 -08002490 this.overlayNode_.textContent = msg;
2491 this.overlayNode_.style.opacity = '0.75';
2492
2493 if (!this.overlayNode_.parentNode)
2494 this.div_.appendChild(this.overlayNode_);
2495
Robert Ginda97769282013-02-01 15:30:30 -08002496 var divSize = hterm.getClientSize(this.div_);
2497 var overlaySize = hterm.getClientSize(this.overlayNode_);
2498
2499 this.overlayNode_.style.top = (divSize.height - overlaySize.height) / 2;
2500 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
2501 this.scrollPort_.currentScrollbarWidthPx) / 2;
rgindaf0090c92012-02-10 14:58:52 -08002502
2503 var self = this;
2504
2505 if (this.overlayTimeout_)
2506 clearTimeout(this.overlayTimeout_);
2507
rgindacc2996c2012-02-24 14:59:31 -08002508 if (opt_timeout === null)
2509 return;
2510
rgindaf0090c92012-02-10 14:58:52 -08002511 this.overlayTimeout_ = setTimeout(function() {
2512 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002513 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002514 if (self.overlayNode_.parentNode)
2515 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002516 self.overlayTimeout_ = null;
2517 self.overlayNode_.style.opacity = '0.75';
2518 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002519 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002520};
2521
rginda4bba5e12012-06-20 16:15:30 -07002522/**
2523 * Paste from the system clipboard to the terminal.
2524 */
2525hterm.Terminal.prototype.paste = function() {
2526 hterm.pasteFromClipboard(this.document_);
2527};
2528
2529/**
2530 * Copy a string to the system clipboard.
2531 *
2532 * Note: If there is a selected range in the terminal, it'll be cleared.
2533 */
2534hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002535 if (this.prefs_.get('enable-clipboard-notice'))
2536 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2537
rgindaa09e7332012-08-17 12:49:51 -07002538 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002539 copySource.textContent = str;
2540 copySource.style.cssText = (
2541 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002542 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002543 'position: absolute;' +
2544 'top: -99px');
2545
2546 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002547
rginda4bba5e12012-06-20 16:15:30 -07002548 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002549 var anchorNode = selection.anchorNode;
2550 var anchorOffset = selection.anchorOffset;
2551 var focusNode = selection.focusNode;
2552 var focusOffset = selection.focusOffset;
2553
rginda4bba5e12012-06-20 16:15:30 -07002554 selection.selectAllChildren(copySource);
2555
rgindaa09e7332012-08-17 12:49:51 -07002556 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002557
Rob Spies56953412014-04-28 14:09:47 -07002558 // IE doesn't support selection.extend. This means that the selection
2559 // won't return on IE.
Rob Spies0bec09b2014-06-06 15:58:09 -07002560 if (this.clearSelectionAfterCopy && selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002561 selection.collapse(anchorNode, anchorOffset);
2562 selection.extend(focusNode, focusOffset);
2563 }
rgindafaa74742012-08-21 13:34:03 -07002564
rginda4bba5e12012-06-20 16:15:30 -07002565 copySource.parentNode.removeChild(copySource);
2566};
2567
rgindaa09e7332012-08-17 12:49:51 -07002568hterm.Terminal.prototype.getSelectionText = function() {
2569 var selection = this.scrollPort_.selection;
2570 selection.sync();
2571
2572 if (selection.isCollapsed)
2573 return null;
2574
2575
2576 // Start offset measures from the beginning of the line.
2577 var startOffset = selection.startOffset;
2578 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002579
Robert Gindafdbb3f22012-09-06 20:23:06 -07002580 if (node.nodeName != 'X-ROW') {
2581 // If the selection doesn't start on an x-row node, then it must be
2582 // somewhere inside the x-row. Add any characters from previous siblings
2583 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002584
2585 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2586 // If node is the text node in a styled span, move up to the span node.
2587 node = node.parentNode;
2588 }
2589
Robert Gindafdbb3f22012-09-06 20:23:06 -07002590 while (node.previousSibling) {
2591 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002592 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002593 }
rgindaa09e7332012-08-17 12:49:51 -07002594 }
2595
2596 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002597 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2598 selection.endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002599 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002600
Robert Gindafdbb3f22012-09-06 20:23:06 -07002601 if (node.nodeName != 'X-ROW') {
2602 // If the selection doesn't end on an x-row node, then it must be
2603 // somewhere inside the x-row. Add any characters from following siblings
2604 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002605
2606 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2607 // If node is the text node in a styled span, move up to the span node.
2608 node = node.parentNode;
2609 }
2610
Robert Gindafdbb3f22012-09-06 20:23:06 -07002611 while (node.nextSibling) {
2612 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002613 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002614 }
rgindaa09e7332012-08-17 12:49:51 -07002615 }
2616
2617 var rv = this.getRowsText(selection.startRow.rowIndex,
2618 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002619 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002620};
2621
rginda4bba5e12012-06-20 16:15:30 -07002622/**
2623 * Copy the current selection to the system clipboard, then clear it after a
2624 * short delay.
2625 */
2626hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002627 var text = this.getSelectionText();
2628 if (text != null)
2629 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002630};
2631
rgindaf0090c92012-02-10 14:58:52 -08002632hterm.Terminal.prototype.overlaySize = function() {
2633 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2634};
2635
rginda87b86462011-12-14 13:48:03 -08002636/**
2637 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2638 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002639 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002640 */
2641hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002642 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002643 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2644
Robert Ginda8cb7d902013-06-20 14:37:18 -07002645 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002646};
2647
2648/**
rgindad5613292012-06-19 15:40:37 -07002649 * Add the terminalRow and terminalColumn properties to mouse events and
2650 * then forward on to onMouse().
2651 *
2652 * The terminalRow and terminalColumn properties contain the (row, column)
2653 * coordinates for the mouse event.
2654 */
2655hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002656 if (e.processedByTerminalHandler_) {
2657 // We register our event handlers on the document, as well as the cursor
2658 // and the scroll blocker. Mouse events that occur on the cursor or
2659 // scroll blocker will also appear on the document, but we don't want to
2660 // process them twice.
2661 //
2662 // We can't just prevent bubbling because that has other side effects, so
2663 // we decorate the event object with this property instead.
2664 return;
2665 }
2666
2667 e.processedByTerminalHandler_ = true;
2668
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002669 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2670 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002671 return;
2672 }
2673
rgindad5613292012-06-19 15:40:37 -07002674 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2675 this.scrollPort_.characterSize.height) + 1;
2676 e.terminalColumn = parseInt(e.clientX /
2677 this.scrollPort_.characterSize.width) + 1;
2678
Robert Ginda928cf632014-03-05 15:07:41 -08002679 if (e.type == 'mousedown') {
2680 if (e.altKey || this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED) {
2681 // If VT mouse reporting is disabled, or has been defeated with
2682 // alt-mousedown, then the mouse will act on the local selection.
2683 this.reportMouseEvents_ = false;
2684 this.setSelectionEnabled(true);
2685 } else {
2686 // Otherwise we defer ownership of the mouse to the VT.
2687 this.reportMouseEvents_ = true;
Robert Ginda3ae37822014-05-15 13:05:35 -07002688 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08002689 this.setSelectionEnabled(false);
2690 e.preventDefault();
2691 }
2692 }
2693
2694 if (!this.reportMouseEvents_) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002695 if (e.type == 'dblclick') {
2696 this.screen_.expandSelection(this.document_.getSelection());
2697 hterm.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07002698 }
2699
Robert Ginda928cf632014-03-05 15:07:41 -08002700 if (e.type == 'mousedown' && e.which == this.mousePasteButton)
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002701 this.paste();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002702
2703 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2704 !this.document_.getSelection().isCollapsed) {
2705 hterm.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002706 }
2707
2708 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
2709 this.scrollBlockerNode_.engaged) {
2710 // Disengage the scroll-blocker after one of these events.
2711 this.scrollBlockerNode_.engaged = false;
2712 this.scrollBlockerNode_.style.top = '-99px';
2713 }
2714
Robert Ginda928cf632014-03-05 15:07:41 -08002715 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002716 if (!this.scrollBlockerNode_.engaged) {
2717 if (e.type == 'mousedown') {
2718 // Move the scroll-blocker into place if we want to keep the scrollport
2719 // from scrolling.
2720 this.scrollBlockerNode_.engaged = true;
2721 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2722 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2723 } else if (e.type == 'mousemove') {
2724 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
2725 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07002726 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002727 e.preventDefault();
2728 }
2729 }
Robert Ginda928cf632014-03-05 15:07:41 -08002730
2731 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002732 }
2733
Robert Ginda928cf632014-03-05 15:07:41 -08002734 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
2735 // Restore this on mouseup in case it was temporarily defeated with a
2736 // alt-mousedown. Only do this when the selection is empty so that
2737 // we don't immediately kill the users selection.
2738 this.reportMouseEvents_ = (this.vt.mouseReport !=
2739 this.vt.MOUSE_REPORT_DISABLED);
2740 }
rgindad5613292012-06-19 15:40:37 -07002741};
2742
2743/**
2744 * Clients should override this if they care to know about mouse events.
2745 *
2746 * The event parameter will be a normal DOM mouse click event with additional
2747 * 'terminalRow' and 'terminalColumn' properties.
2748 */
2749hterm.Terminal.prototype.onMouse = function(e) { };
2750
2751/**
rginda8e92a692012-05-20 19:37:20 -07002752 * React when focus changes.
2753 */
Rob Spies06533ba2014-04-24 11:20:37 -07002754hterm.Terminal.prototype.onFocusChange_ = function(focused) {
2755 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07002756 this.restyleCursor_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002757 if (focused === true)
2758 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07002759};
2760
2761/**
rginda8ba33642011-12-14 12:31:31 -08002762 * React when the ScrollPort is scrolled.
2763 */
2764hterm.Terminal.prototype.onScroll_ = function() {
2765 this.scheduleSyncCursorPosition_();
2766};
2767
2768/**
rginda9846e2f2012-01-27 13:53:33 -08002769 * React when text is pasted into the scrollPort.
2770 */
2771hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Ginda8cb7d902013-06-20 14:37:18 -07002772 this.onVTKeystroke(e.text.replace(/\n/mg, '\r'));
rginda9846e2f2012-01-27 13:53:33 -08002773};
2774
2775/**
rgindaa09e7332012-08-17 12:49:51 -07002776 * React when the user tries to copy from the scrollPort.
2777 */
2778hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07002779 if (!this.useDefaultWindowCopy) {
2780 e.preventDefault();
2781 setTimeout(this.copySelectionToClipboard.bind(this), 0);
2782 }
rgindaa09e7332012-08-17 12:49:51 -07002783};
2784
2785/**
rginda8ba33642011-12-14 12:31:31 -08002786 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002787 *
2788 * Note: This function should not directly contain code that alters the internal
2789 * state of the terminal. That kind of code belongs in realizeWidth or
2790 * realizeHeight, so that it can be executed synchronously in the case of a
2791 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002792 */
2793hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08002794 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002795 this.scrollPort_.characterSize.width);
Robert Ginda19f61292014-03-04 14:07:57 -08002796 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
rginda35c456b2012-02-09 17:29:05 -08002797 this.scrollPort_.characterSize.height);
2798
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002799 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002800 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002801 // gets removed from the document or during the initial load, and we can't
2802 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002803 return;
2804 }
2805
rgindaa8ba17d2012-08-15 14:41:10 -07002806 var isNewSize = (columnCount != this.screenSize.width ||
2807 rowCount != this.screenSize.height);
2808
2809 // We do this even if the size didn't change, just to be sure everything is
2810 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002811 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002812 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002813
2814 if (isNewSize)
2815 this.overlaySize();
2816
Robert Gindafb1be6a2013-12-11 11:56:22 -08002817 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07002818 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002819};
2820
2821/**
2822 * Service the cursor blink timeout.
2823 */
2824hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Ginda830583c2013-08-07 13:20:46 -07002825 if (this.cursorNode_.getAttribute('focus') == 'false' ||
2826 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08002827 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002828 } else {
rginda87b86462011-12-14 13:48:03 -08002829 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002830 }
2831};
David Reveman8f552492012-03-28 12:18:41 -04002832
2833/**
2834 * Set the scrollbar-visible mode bit.
2835 *
2836 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2837 * Otherwise it will not.
2838 *
2839 * Defaults to on.
2840 *
2841 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2842 */
2843hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2844 this.scrollPort_.setScrollbarVisible(state);
2845};
Michael Kelly485ecd12014-06-09 11:41:56 -04002846
2847/**
2848 * Close all web notifications created by terminal bells.
2849 */
2850hterm.Terminal.prototype.closeBellNotifications_ = function() {
2851 this.bellNotificationList_.forEach(function(n) {
2852 n.close();
2853 });
2854 this.bellNotificationList_.length = 0;
2855};