blob: 02d92e5b31626fce03cc69903653bb2ee168a5a3 [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
Robert Gindaea2183e2014-07-17 09:51:51 -070087 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
88 this.cursorBlinkCycle_ = [100, 100];
89
90 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
91 // cursor on/off servicing.
92 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
93
rginda9f5222b2012-03-05 11:53:28 -080094 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070095 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070096 this.backgroundColor_ = null;
97 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070098 this.scrollOnOutput_ = null;
99 this.scrollOnKeystroke_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800100
Robert Ginda928cf632014-03-05 15:07:41 -0800101 // True if we should send mouse events to the vt, false if we want them
102 // to manage the local text selection.
103 this.reportMouseEvents_ = false;
104
rgindaf0090c92012-02-10 14:58:52 -0800105 // Terminal bell sound.
106 this.bellAudio_ = this.document_.createElement('audio');
rgindaf0090c92012-02-10 14:58:52 -0800107 this.bellAudio_.setAttribute('preload', 'auto');
108
Michael Kelly485ecd12014-06-09 11:41:56 -0400109 // All terminal bell notifications that have been generated (not necessarily
110 // shown).
111 this.bellNotificationList_ = [];
112
113 // Whether we have permission to display notifications.
114 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400115
rginda6d397402012-01-17 10:58:29 -0800116 // Cursor position and attributes saved with DECSC.
117 this.savedOptions_ = {};
118
rginda8ba33642011-12-14 12:31:31 -0800119 // The current mode bits for the terminal.
120 this.options_ = new hterm.Options();
121
122 // Timeouts we might need to clear.
123 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800124
125 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800126 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800127
rgindafeaf3142012-01-31 15:14:20 -0800128 // The keyboard hander.
129 this.keyboard = new hterm.Keyboard(this);
130
rginda87b86462011-12-14 13:48:03 -0800131 // General IO interface that can be given to third parties without exposing
132 // the entire terminal object.
133 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800134
rgindad5613292012-06-19 15:40:37 -0700135 // True if mouse-click-drag should scroll the terminal.
136 this.enableMouseDragScroll = true;
137
Robert Ginda57f03b42012-09-13 11:02:48 -0700138 this.copyOnSelect = null;
rginda4bba5e12012-06-20 16:15:30 -0700139 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700140
Rob Spies0bec09b2014-06-06 15:58:09 -0700141 // Whether to use the default window copy behaviour.
142 this.useDefaultWindowCopy = false;
143
144 this.clearSelectionAfterCopy = true;
145
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400146 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800147 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700148
149 this.setProfile(opt_profileId || 'default',
150 function() { this.onTerminalReady() }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800151};
152
153/**
Robert Ginda830583c2013-08-07 13:20:46 -0700154 * Possible cursor shapes.
155 */
156hterm.Terminal.cursorShape = {
157 BLOCK: 'BLOCK',
158 BEAM: 'BEAM',
159 UNDERLINE: 'UNDERLINE'
160};
161
162/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700163 * Clients should override this to be notified when the terminal is ready
164 * for use.
165 *
166 * The terminal initialization is asynchronous, and shouldn't be used before
167 * this method is called.
168 */
169hterm.Terminal.prototype.onTerminalReady = function() { };
170
171/**
rginda35c456b2012-02-09 17:29:05 -0800172 * Default tab with of 8 to match xterm.
173 */
174hterm.Terminal.prototype.tabWidth = 8;
175
176/**
rginda9f5222b2012-03-05 11:53:28 -0800177 * Select a preference profile.
178 *
179 * This will load the terminal preferences for the given profile name and
180 * associate subsequent preference changes with the new preference profile.
181 *
182 * @param {string} newName The name of the preference profile. Forward slash
183 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700184 * @param {function} opt_callback Optional callback to invoke when the profile
185 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800186 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700187hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
188 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800189
Robert Ginda57f03b42012-09-13 11:02:48 -0700190 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800191
Robert Ginda57f03b42012-09-13 11:02:48 -0700192 if (this.prefs_)
193 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800194
Robert Ginda57f03b42012-09-13 11:02:48 -0700195 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
196 this.prefs_.addObservers(null, {
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700197 'alt-backspace-is-meta-backspace': function(v) {
198 terminal.keyboard.altBackspaceIsMetaBackspace = v;
199 },
200
Robert Ginda57f03b42012-09-13 11:02:48 -0700201 'alt-is-meta': function(v) {
202 terminal.keyboard.altIsMeta = v;
203 },
204
205 'alt-sends-what': function(v) {
206 if (!/^(escape|8-bit|browser-key)$/.test(v))
207 v = 'escape';
208
209 terminal.keyboard.altSendsWhat = v;
210 },
211
212 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800213 var ary = v.match(/^lib-resource:(\S+)/);
214 if (ary) {
215 terminal.bellAudio_.setAttribute('src',
216 lib.resource.getDataUrl(ary[1]));
217 } else {
218 terminal.bellAudio_.setAttribute('src', v);
219 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700220 },
221
Michael Kelly485ecd12014-06-09 11:41:56 -0400222 'desktop-notification-bell': function(v) {
223 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700224 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400225 Notification.permission === 'granted';
226 if (!terminal.desktopNotificationBell_) {
227 // Note: We don't call Notification.requestPermission here because
228 // Chrome requires the call be the result of a user action (such as an
229 // onclick handler), and pref listeners are run asynchronously.
230 //
231 // A way of working around this would be to display a dialog in the
232 // terminal with a "click-to-request-permission" button.
233 console.warn('desktop-notification-bell is true but we do not have ' +
234 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400235 }
236 } else {
237 terminal.desktopNotificationBell_ = false;
238 }
239 },
240
Robert Ginda57f03b42012-09-13 11:02:48 -0700241 'background-color': function(v) {
242 terminal.setBackgroundColor(v);
243 },
244
245 'background-image': function(v) {
246 terminal.scrollPort_.setBackgroundImage(v);
247 },
248
249 'background-size': function(v) {
250 terminal.scrollPort_.setBackgroundSize(v);
251 },
252
253 'background-position': function(v) {
254 terminal.scrollPort_.setBackgroundPosition(v);
255 },
256
257 'backspace-sends-backspace': function(v) {
258 terminal.keyboard.backspaceSendsBackspace = v;
259 },
260
261 'cursor-blink': function(v) {
262 terminal.setCursorBlink(!!v);
263 },
264
Robert Gindaea2183e2014-07-17 09:51:51 -0700265 'cursor-blink-cycle': function(v) {
266 if (v instanceof Array &&
267 typeof v[0] == 'number' &&
268 typeof v[1] == 'number') {
269 terminal.cursorBlinkCycle_ = v;
270 } else if (typeof v == 'number') {
271 terminal.cursorBlinkCycle_ = [v, v];
272 } else {
273 // Fast blink indicates an error.
274 terminal.cursorBlinkCycle_ = [100, 100];
275 }
276 },
277
Robert Ginda57f03b42012-09-13 11:02:48 -0700278 'cursor-color': function(v) {
279 terminal.setCursorColor(v);
280 },
281
282 'color-palette-overrides': function(v) {
283 if (!(v == null || v instanceof Object || v instanceof Array)) {
284 console.warn('Preference color-palette-overrides is not an array or ' +
285 'object: ' + v);
286 return;
rginda9f5222b2012-03-05 11:53:28 -0800287 }
rginda9f5222b2012-03-05 11:53:28 -0800288
Robert Ginda57f03b42012-09-13 11:02:48 -0700289 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700290
Robert Ginda57f03b42012-09-13 11:02:48 -0700291 if (v) {
292 for (var key in v) {
293 var i = parseInt(key);
294 if (isNaN(i) || i < 0 || i > 255) {
295 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
296 continue;
297 }
298
299 if (v[i]) {
300 var rgb = lib.colors.normalizeCSS(v[i]);
301 if (rgb)
302 lib.colors.colorPalette[i] = rgb;
303 }
304 }
rginda30f20f62012-04-05 16:36:19 -0700305 }
rginda30f20f62012-04-05 16:36:19 -0700306
Robert Ginda57f03b42012-09-13 11:02:48 -0700307 terminal.primaryScreen_.textAttributes.resetColorPalette()
308 terminal.alternateScreen_.textAttributes.resetColorPalette();
309 },
rginda30f20f62012-04-05 16:36:19 -0700310
Robert Ginda57f03b42012-09-13 11:02:48 -0700311 'copy-on-select': function(v) {
312 terminal.copyOnSelect = !!v;
313 },
rginda9f5222b2012-03-05 11:53:28 -0800314
Rob Spies0bec09b2014-06-06 15:58:09 -0700315 'use-default-window-copy': function(v) {
316 terminal.useDefaultWindowCopy = !!v;
317 },
318
319 'clear-selection-after-copy': function(v) {
320 terminal.clearSelectionAfterCopy = !!v;
321 },
322
Robert Ginda7e5e9522014-03-14 12:23:58 -0700323 'ctrl-plus-minus-zero-zoom': function(v) {
324 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
325 },
326
Robert Gindafb5a3f92014-05-13 14:12:00 -0700327 'ctrl-c-copy': function(v) {
328 terminal.keyboard.ctrlCCopy = v;
329 },
330
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100331 'ctrl-v-paste': function(v) {
332 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700333 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100334 },
335
Masaya Suzuki273aa982014-05-31 07:25:55 +0900336 'east-asian-ambiguous-as-two-column': function(v) {
337 lib.wc.regardCjkAmbiguous = v;
338 },
339
Robert Ginda57f03b42012-09-13 11:02:48 -0700340 'enable-8-bit-control': function(v) {
341 terminal.vt.enable8BitControl = !!v;
342 },
rginda30f20f62012-04-05 16:36:19 -0700343
Robert Ginda57f03b42012-09-13 11:02:48 -0700344 'enable-bold': function(v) {
345 terminal.syncBoldSafeState();
346 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400347
Robert Ginda3e278d72014-03-25 13:18:51 -0700348 'enable-bold-as-bright': function(v) {
349 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
350 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
351 },
352
Robert Ginda57f03b42012-09-13 11:02:48 -0700353 'enable-clipboard-write': function(v) {
354 terminal.vt.enableClipboardWrite = !!v;
355 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400356
Robert Ginda3755e752013-05-31 13:34:09 -0700357 'enable-dec12': function(v) {
358 terminal.vt.enableDec12 = !!v;
359 },
360
Robert Ginda57f03b42012-09-13 11:02:48 -0700361 'font-family': function(v) {
362 terminal.syncFontFamily();
363 },
rginda30f20f62012-04-05 16:36:19 -0700364
Robert Ginda57f03b42012-09-13 11:02:48 -0700365 'font-size': function(v) {
366 terminal.setFontSize(v);
367 },
rginda9875d902012-08-20 16:21:57 -0700368
Robert Ginda57f03b42012-09-13 11:02:48 -0700369 'font-smoothing': function(v) {
370 terminal.syncFontFamily();
371 },
rgindade84e382012-04-20 15:39:31 -0700372
Robert Ginda57f03b42012-09-13 11:02:48 -0700373 'foreground-color': function(v) {
374 terminal.setForegroundColor(v);
375 },
rginda30f20f62012-04-05 16:36:19 -0700376
Robert Ginda57f03b42012-09-13 11:02:48 -0700377 'home-keys-scroll': function(v) {
378 terminal.keyboard.homeKeysScroll = v;
379 },
rginda4bba5e12012-06-20 16:15:30 -0700380
Robert Ginda57f03b42012-09-13 11:02:48 -0700381 'max-string-sequence': function(v) {
382 terminal.vt.maxStringSequence = v;
383 },
rginda11057d52012-04-25 12:29:56 -0700384
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700385 'media-keys-are-fkeys': function(v) {
386 terminal.keyboard.mediaKeysAreFKeys = v;
387 },
388
Robert Ginda57f03b42012-09-13 11:02:48 -0700389 'meta-sends-escape': function(v) {
390 terminal.keyboard.metaSendsEscape = v;
391 },
rginda30f20f62012-04-05 16:36:19 -0700392
Robert Ginda57f03b42012-09-13 11:02:48 -0700393 'mouse-paste-button': function(v) {
394 terminal.syncMousePasteButton();
395 },
rgindaa8ba17d2012-08-15 14:41:10 -0700396
Robert Gindae76aa9f2014-03-14 12:29:12 -0700397 'page-keys-scroll': function(v) {
398 terminal.keyboard.pageKeysScroll = v;
399 },
400
Robert Ginda40932892012-12-10 17:26:40 -0800401 'pass-alt-number': function(v) {
402 if (v == null) {
403 var osx = window.navigator.userAgent.match(/Mac OS X/);
404
405 // Let Alt-1..9 pass to the browser (to control tab switching) on
406 // non-OS X systems, or if hterm is not opened in an app window.
407 v = (!osx && hterm.windowType != 'popup');
408 }
409
410 terminal.passAltNumber = v;
411 },
412
413 'pass-ctrl-number': function(v) {
414 if (v == null) {
415 var osx = window.navigator.userAgent.match(/Mac OS X/);
416
417 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
418 // non-OS X systems, or if hterm is not opened in an app window.
419 v = (!osx && hterm.windowType != 'popup');
420 }
421
422 terminal.passCtrlNumber = v;
423 },
424
425 'pass-meta-number': function(v) {
426 if (v == null) {
427 var osx = window.navigator.userAgent.match(/Mac OS X/);
428
429 // Let Meta-1..9 pass to the browser (to control tab switching) on
430 // OS X systems, or if hterm is not opened in an app window.
431 v = (osx && hterm.windowType != 'popup');
432 }
433
434 terminal.passMetaNumber = v;
435 },
436
Marius Schilder77857b32014-05-14 16:21:26 -0700437 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700438 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700439 },
440
Robert Ginda8cb7d902013-06-20 14:37:18 -0700441 'receive-encoding': function(v) {
442 if (!(/^(utf-8|raw)$/).test(v)) {
443 console.warn('Invalid value for "receive-encoding": ' + v);
444 v = 'utf-8';
445 }
446
447 terminal.vt.characterEncoding = v;
448 },
449
Robert Ginda57f03b42012-09-13 11:02:48 -0700450 'scroll-on-keystroke': function(v) {
451 terminal.scrollOnKeystroke_ = v;
452 },
rginda9f5222b2012-03-05 11:53:28 -0800453
Robert Ginda57f03b42012-09-13 11:02:48 -0700454 'scroll-on-output': function(v) {
455 terminal.scrollOnOutput_ = v;
456 },
rginda30f20f62012-04-05 16:36:19 -0700457
Robert Ginda57f03b42012-09-13 11:02:48 -0700458 'scrollbar-visible': function(v) {
459 terminal.setScrollbarVisible(v);
460 },
rginda9f5222b2012-03-05 11:53:28 -0800461
Robert Ginda8cb7d902013-06-20 14:37:18 -0700462 'send-encoding': function(v) {
463 if (!(/^(utf-8|raw)$/).test(v)) {
464 console.warn('Invalid value for "send-encoding": ' + v);
465 v = 'utf-8';
466 }
467
468 terminal.keyboard.characterEncoding = v;
469 },
470
Robert Ginda57f03b42012-09-13 11:02:48 -0700471 'shift-insert-paste': function(v) {
472 terminal.keyboard.shiftInsertPaste = v;
473 },
rginda9f5222b2012-03-05 11:53:28 -0800474
Robert Gindae76aa9f2014-03-14 12:29:12 -0700475 'user-css': function(v) {
476 terminal.scrollPort_.setUserCss(v);
Robert Ginda57f03b42012-09-13 11:02:48 -0700477 }
478 });
rginda30f20f62012-04-05 16:36:19 -0700479
Robert Ginda57f03b42012-09-13 11:02:48 -0700480 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800481 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700482
483 if (opt_callback)
484 opt_callback();
485 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800486};
487
Rob Spies56953412014-04-28 14:09:47 -0700488
489/**
490 * Returns the preferences manager used for configuring this terminal.
491 */
492hterm.Terminal.prototype.getPrefs = function() {
493 return this.prefs_;
494};
495
496
rginda8e92a692012-05-20 19:37:20 -0700497/**
498 * Set the color for the cursor.
499 *
500 * If you want this setting to persist, set it through prefs_, rather than
501 * with this method.
502 */
503hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700504 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700505 this.cursorNode_.style.backgroundColor = color;
506 this.cursorNode_.style.borderColor = color;
507};
508
509/**
510 * Return the current cursor color as a string.
511 */
512hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700513 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700514};
515
516/**
rgindad5613292012-06-19 15:40:37 -0700517 * Enable or disable mouse based text selection in the terminal.
518 */
519hterm.Terminal.prototype.setSelectionEnabled = function(state) {
520 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700521};
522
523/**
rginda8e92a692012-05-20 19:37:20 -0700524 * Set the background color.
525 *
526 * If you want this setting to persist, set it through prefs_, rather than
527 * with this method.
528 */
529hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700530 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700531 this.primaryScreen_.textAttributes.setDefaults(
532 this.foregroundColor_, this.backgroundColor_);
533 this.alternateScreen_.textAttributes.setDefaults(
534 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700535 this.scrollPort_.setBackgroundColor(color);
536};
537
rginda9f5222b2012-03-05 11:53:28 -0800538/**
539 * Return the current terminal background color.
540 *
541 * Intended for use by other classes, so we don't have to expose the entire
542 * prefs_ object.
543 */
544hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700545 return this.backgroundColor_;
546};
547
548/**
549 * Set the foreground color.
550 *
551 * If you want this setting to persist, set it through prefs_, rather than
552 * with this method.
553 */
554hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700555 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700556 this.primaryScreen_.textAttributes.setDefaults(
557 this.foregroundColor_, this.backgroundColor_);
558 this.alternateScreen_.textAttributes.setDefaults(
559 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700560 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800561};
562
563/**
564 * Return the current terminal foreground color.
565 *
566 * Intended for use by other classes, so we don't have to expose the entire
567 * prefs_ object.
568 */
569hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700570 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800571};
572
573/**
rginda87b86462011-12-14 13:48:03 -0800574 * Create a new instance of a terminal command and run it with a given
575 * argument string.
576 *
577 * @param {function} commandClass The constructor for a terminal command.
578 * @param {string} argString The argument string to pass to the command.
579 */
580hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700581 var environment = this.prefs_.get('environment');
582 if (typeof environment != 'object' || environment == null)
583 environment = {};
584
rginda87b86462011-12-14 13:48:03 -0800585 var self = this;
586 this.command = new commandClass(
587 { argString: argString || '',
588 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700589 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800590 onExit: function(code) {
591 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800592 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700593 if (self.prefs_.get('close-on-exit'))
594 window.close();
rginda87b86462011-12-14 13:48:03 -0800595 }
596 });
597
rgindafeaf3142012-01-31 15:14:20 -0800598 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800599 this.command.run();
600};
601
602/**
rgindafeaf3142012-01-31 15:14:20 -0800603 * Returns true if the current screen is the primary screen, false otherwise.
604 */
605hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700606 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800607};
608
609/**
610 * Install the keyboard handler for this terminal.
611 *
612 * This will prevent the browser from seeing any keystrokes sent to the
613 * terminal.
614 */
615hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700616 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800617}
618
619/**
620 * Uninstall the keyboard handler for this terminal.
621 */
622hterm.Terminal.prototype.uninstallKeyboard = function() {
623 this.keyboard.installKeyboard(null);
624}
625
626/**
rginda35c456b2012-02-09 17:29:05 -0800627 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800628 *
629 * Call setFontSize(0) to reset to the default font size.
630 *
631 * This function does not modify the font-size preference.
632 *
633 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800634 */
635hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800636 if (px === 0)
637 px = this.prefs_.get('font-size');
638
rginda35c456b2012-02-09 17:29:05 -0800639 this.scrollPort_.setFontSize(px);
Ricky Liang48f05cb2013-12-31 23:35:29 +0800640 if (this.wcCssRule_) {
641 this.wcCssRule_.style.width = this.scrollPort_.characterSize.width * 2 +
642 'px';
643 }
rginda35c456b2012-02-09 17:29:05 -0800644};
645
646/**
647 * Get the current font size.
648 */
649hterm.Terminal.prototype.getFontSize = function() {
650 return this.scrollPort_.getFontSize();
651};
652
653/**
rginda8e92a692012-05-20 19:37:20 -0700654 * Get the current font family.
655 */
656hterm.Terminal.prototype.getFontFamily = function() {
657 return this.scrollPort_.getFontFamily();
658};
659
660/**
rginda35c456b2012-02-09 17:29:05 -0800661 * Set the CSS "font-family" for this terminal.
662 */
rginda9f5222b2012-03-05 11:53:28 -0800663hterm.Terminal.prototype.syncFontFamily = function() {
664 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
665 this.prefs_.get('font-smoothing'));
666 this.syncBoldSafeState();
667};
668
rginda4bba5e12012-06-20 16:15:30 -0700669/**
670 * Set this.mousePasteButton based on the mouse-paste-button pref,
671 * autodetecting if necessary.
672 */
673hterm.Terminal.prototype.syncMousePasteButton = function() {
674 var button = this.prefs_.get('mouse-paste-button');
675 if (typeof button == 'number') {
676 this.mousePasteButton = button;
677 return;
678 }
679
680 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
681 if (!ary || ary[2] == 'CrOS') {
682 this.mousePasteButton = 2;
683 } else {
684 this.mousePasteButton = 3;
685 }
686};
687
688/**
689 * Enable or disable bold based on the enable-bold pref, autodetecting if
690 * necessary.
691 */
rginda9f5222b2012-03-05 11:53:28 -0800692hterm.Terminal.prototype.syncBoldSafeState = function() {
693 var enableBold = this.prefs_.get('enable-bold');
694 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700695 this.primaryScreen_.textAttributes.enableBold = enableBold;
696 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800697 return;
698 }
699
rgindaf7521392012-02-28 17:20:34 -0800700 var normalSize = this.scrollPort_.measureCharacterSize();
701 var boldSize = this.scrollPort_.measureCharacterSize('bold');
702
703 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800704 if (!isBoldSafe) {
705 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700706 'from normal. Font family is: ' +
707 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800708 }
rginda9f5222b2012-03-05 11:53:28 -0800709
Robert Gindaed016262012-10-26 16:27:09 -0700710 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
711 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800712};
713
714/**
rginda87b86462011-12-14 13:48:03 -0800715 * Return a copy of the current cursor position.
716 *
717 * @return {hterm.RowCol} The RowCol object representing the current position.
718 */
719hterm.Terminal.prototype.saveCursor = function() {
720 return this.screen_.cursorPosition.clone();
721};
722
rgindaa19afe22012-01-25 15:40:22 -0800723hterm.Terminal.prototype.getTextAttributes = function() {
724 return this.screen_.textAttributes;
725};
726
rginda1a09aa02012-06-18 21:11:25 -0700727hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
728 this.screen_.textAttributes = textAttributes;
729};
730
rginda87b86462011-12-14 13:48:03 -0800731/**
rgindaf522ce02012-04-17 17:49:17 -0700732 * Return the current browser zoom factor applied to the terminal.
733 *
734 * @return {number} The current browser zoom factor.
735 */
736hterm.Terminal.prototype.getZoomFactor = function() {
737 return this.scrollPort_.characterSize.zoomFactor;
738};
739
740/**
rginda9846e2f2012-01-27 13:53:33 -0800741 * Change the title of this terminal's window.
742 */
743hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800744 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800745};
746
747/**
rginda87b86462011-12-14 13:48:03 -0800748 * Restore a previously saved cursor position.
749 *
750 * @param {hterm.RowCol} cursor The position to restore.
751 */
752hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700753 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
754 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800755 this.screen_.setCursorPosition(row, column);
756 if (cursor.column > column ||
757 cursor.column == column && cursor.overflow) {
758 this.screen_.cursorPosition.overflow = true;
759 }
rginda87b86462011-12-14 13:48:03 -0800760};
761
762/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400763 * Clear the cursor's overflow flag.
764 */
765hterm.Terminal.prototype.clearCursorOverflow = function() {
766 this.screen_.cursorPosition.overflow = false;
767};
768
769/**
Robert Ginda830583c2013-08-07 13:20:46 -0700770 * Sets the cursor shape
771 */
772hterm.Terminal.prototype.setCursorShape = function(shape) {
773 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800774 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700775}
776
777/**
778 * Get the cursor shape
779 */
780hterm.Terminal.prototype.getCursorShape = function() {
781 return this.cursorShape_;
782}
783
784/**
rginda87b86462011-12-14 13:48:03 -0800785 * Set the width of the terminal, resizing the UI to match.
786 */
787hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800788 if (columnCount == null) {
789 this.div_.style.width = '100%';
790 return;
791 }
792
rginda35c456b2012-02-09 17:29:05 -0800793 this.div_.style.width = this.scrollPort_.characterSize.width *
Robert Ginda97769282013-02-01 15:30:30 -0800794 columnCount + this.scrollPort_.currentScrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400795 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800796 this.scheduleSyncCursorPosition_();
797};
rginda87b86462011-12-14 13:48:03 -0800798
rgindac9bc5502012-01-18 11:48:44 -0800799/**
rginda35c456b2012-02-09 17:29:05 -0800800 * Set the height of the terminal, resizing the UI to match.
801 */
802hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800803 if (rowCount == null) {
804 this.div_.style.height = '100%';
805 return;
806 }
807
rginda35c456b2012-02-09 17:29:05 -0800808 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700809 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800810 this.realizeSize_(this.screenSize.width, rowCount);
811 this.scheduleSyncCursorPosition_();
812};
813
814/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400815 * Deal with terminal size changes.
816 *
817 */
818hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
819 if (columnCount != this.screenSize.width)
820 this.realizeWidth_(columnCount);
821
822 if (rowCount != this.screenSize.height)
823 this.realizeHeight_(rowCount);
824
825 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700826 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400827};
828
829/**
rgindac9bc5502012-01-18 11:48:44 -0800830 * Deal with terminal width changes.
831 *
832 * This function does what needs to be done when the terminal width changes
833 * out from under us. It happens here rather than in onResize_() because this
834 * code may need to run synchronously to handle programmatic changes of
835 * terminal width.
836 *
837 * Relying on the browser to send us an async resize event means we may not be
838 * in the correct state yet when the next escape sequence hits.
839 */
840hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700841 if (columnCount <= 0)
842 throw new Error('Attempt to realize bad width: ' + columnCount);
843
rgindac9bc5502012-01-18 11:48:44 -0800844 var deltaColumns = columnCount - this.screen_.getWidth();
845
rginda87b86462011-12-14 13:48:03 -0800846 this.screenSize.width = columnCount;
847 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800848
849 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400850 if (this.defaultTabStops)
851 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800852 } else {
853 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400854 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800855 break;
856
857 this.tabStops_.pop();
858 }
859 }
860
861 this.screen_.setColumnCount(this.screenSize.width);
862};
863
864/**
865 * Deal with terminal height changes.
866 *
867 * This function does what needs to be done when the terminal height changes
868 * out from under us. It happens here rather than in onResize_() because this
869 * code may need to run synchronously to handle programmatic changes of
870 * terminal height.
871 *
872 * Relying on the browser to send us an async resize event means we may not be
873 * in the correct state yet when the next escape sequence hits.
874 */
875hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700876 if (rowCount <= 0)
877 throw new Error('Attempt to realize bad height: ' + rowCount);
878
rgindac9bc5502012-01-18 11:48:44 -0800879 var deltaRows = rowCount - this.screen_.getHeight();
880
881 this.screenSize.height = rowCount;
882
883 var cursor = this.saveCursor();
884
885 if (deltaRows < 0) {
886 // Screen got smaller.
887 deltaRows *= -1;
888 while (deltaRows) {
889 var lastRow = this.getRowCount() - 1;
890 if (lastRow - this.scrollbackRows_.length == cursor.row)
891 break;
892
893 if (this.getRowText(lastRow))
894 break;
895
896 this.screen_.popRow();
897 deltaRows--;
898 }
899
900 var ary = this.screen_.shiftRows(deltaRows);
901 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
902
903 // We just removed rows from the top of the screen, we need to update
904 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800905 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800906 } else if (deltaRows > 0) {
907 // Screen got larger.
908
909 if (deltaRows <= this.scrollbackRows_.length) {
910 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
911 var rows = this.scrollbackRows_.splice(
912 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
913 this.screen_.unshiftRows(rows);
914 deltaRows -= scrollbackCount;
915 cursor.row += scrollbackCount;
916 }
917
918 if (deltaRows)
919 this.appendRows_(deltaRows);
920 }
921
rginda35c456b2012-02-09 17:29:05 -0800922 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800923 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800924};
925
926/**
927 * Scroll the terminal to the top of the scrollback buffer.
928 */
929hterm.Terminal.prototype.scrollHome = function() {
930 this.scrollPort_.scrollRowToTop(0);
931};
932
933/**
934 * Scroll the terminal to the end.
935 */
936hterm.Terminal.prototype.scrollEnd = function() {
937 this.scrollPort_.scrollRowToBottom(this.getRowCount());
938};
939
940/**
941 * Scroll the terminal one page up (minus one line) relative to the current
942 * position.
943 */
944hterm.Terminal.prototype.scrollPageUp = function() {
945 var i = this.scrollPort_.getTopRowIndex();
946 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
947};
948
949/**
950 * Scroll the terminal one page down (minus one line) relative to the current
951 * position.
952 */
953hterm.Terminal.prototype.scrollPageDown = function() {
954 var i = this.scrollPort_.getTopRowIndex();
955 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800956};
957
rgindac9bc5502012-01-18 11:48:44 -0800958/**
Robert Ginda40932892012-12-10 17:26:40 -0800959 * Clear primary screen, secondary screen, and the scrollback buffer.
960 */
961hterm.Terminal.prototype.wipeContents = function() {
962 this.scrollbackRows_.length = 0;
963 this.scrollPort_.resetCache();
964
965 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
966 var bottom = screen.getHeight();
967 if (bottom > 0) {
968 this.renumberRows_(0, bottom);
969 this.clearHome(screen);
970 }
971 }.bind(this));
972
973 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -0700974 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -0800975};
976
977/**
rgindac9bc5502012-01-18 11:48:44 -0800978 * Full terminal reset.
979 */
rginda87b86462011-12-14 13:48:03 -0800980hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800981 this.clearAllTabStops();
982 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700983
984 this.clearHome(this.primaryScreen_);
985 this.primaryScreen_.textAttributes.reset();
986
987 this.clearHome(this.alternateScreen_);
988 this.alternateScreen_.textAttributes.reset();
989
rgindab8bc8932012-04-27 12:45:03 -0700990 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
991
Robert Ginda92e18102013-03-14 13:56:37 -0700992 this.vt.reset();
993
rgindac9bc5502012-01-18 11:48:44 -0800994 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800995};
996
rgindac9bc5502012-01-18 11:48:44 -0800997/**
998 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700999 *
1000 * Perform a soft reset to the default values listed in
1001 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001002 */
rginda0f5c0292012-01-13 11:00:13 -08001003hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -07001004 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001005 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001006
rgindab8bc8932012-04-27 12:45:03 -07001007 // Xterm also resets the color palette on soft reset, even though it doesn't
1008 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -07001009 this.primaryScreen_.textAttributes.resetColorPalette();
1010 this.alternateScreen_.textAttributes.resetColorPalette();
1011
rgindab8bc8932012-04-27 12:45:03 -07001012 // The xterm man page explicitly says this will happen on soft reset.
1013 this.setVTScrollRegion(null, null);
1014
1015 // Xterm also shows the cursor on soft reset, but does not alter the blink
1016 // state.
rgindaa19afe22012-01-25 15:40:22 -08001017 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001018};
1019
rgindac9bc5502012-01-18 11:48:44 -08001020/**
1021 * Move the cursor forward to the next tab stop, or to the last column
1022 * if no more tab stops are set.
1023 */
1024hterm.Terminal.prototype.forwardTabStop = function() {
1025 var column = this.screen_.cursorPosition.column;
1026
1027 for (var i = 0; i < this.tabStops_.length; i++) {
1028 if (this.tabStops_[i] > column) {
1029 this.setCursorColumn(this.tabStops_[i]);
1030 return;
1031 }
1032 }
1033
David Benjamin66e954d2012-05-05 21:08:12 -04001034 // xterm does not clear the overflow flag on HT or CHT.
1035 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001036 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001037 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001038};
1039
rgindac9bc5502012-01-18 11:48:44 -08001040/**
1041 * Move the cursor backward to the previous tab stop, or to the first column
1042 * if no previous tab stops are set.
1043 */
1044hterm.Terminal.prototype.backwardTabStop = function() {
1045 var column = this.screen_.cursorPosition.column;
1046
1047 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1048 if (this.tabStops_[i] < column) {
1049 this.setCursorColumn(this.tabStops_[i]);
1050 return;
1051 }
1052 }
1053
1054 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001055};
1056
rgindac9bc5502012-01-18 11:48:44 -08001057/**
1058 * Set a tab stop at the given column.
1059 *
1060 * @param {int} column Zero based column.
1061 */
1062hterm.Terminal.prototype.setTabStop = function(column) {
1063 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1064 if (this.tabStops_[i] == column)
1065 return;
1066
1067 if (this.tabStops_[i] < column) {
1068 this.tabStops_.splice(i + 1, 0, column);
1069 return;
1070 }
1071 }
1072
1073 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001074};
1075
rgindac9bc5502012-01-18 11:48:44 -08001076/**
1077 * Clear the tab stop at the current cursor position.
1078 *
1079 * No effect if there is no tab stop at the current cursor position.
1080 */
1081hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1082 var column = this.screen_.cursorPosition.column;
1083
1084 var i = this.tabStops_.indexOf(column);
1085 if (i == -1)
1086 return;
1087
1088 this.tabStops_.splice(i, 1);
1089};
1090
1091/**
1092 * Clear all tab stops.
1093 */
1094hterm.Terminal.prototype.clearAllTabStops = function() {
1095 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001096 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001097};
1098
1099/**
1100 * Set up the default tab stops, starting from a given column.
1101 *
1102 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001103 * from the specified column, or 0 if no column is provided. It also flags
1104 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001105 *
1106 * This does not clear the existing tab stops first, use clearAllTabStops
1107 * for that.
1108 *
1109 * @param {int} opt_start Optional starting zero based starting column, useful
1110 * for filling out missing tab stops when the terminal is resized.
1111 */
1112hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1113 var start = opt_start || 0;
1114 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001115 // Round start up to a default tab stop.
1116 start = start - 1 - ((start - 1) % w) + w;
1117 for (var i = start; i < this.screenSize.width; i += w) {
1118 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001119 }
David Benjamin66e954d2012-05-05 21:08:12 -04001120
1121 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001122};
1123
rginda6d397402012-01-17 10:58:29 -08001124/**
rginda8ba33642011-12-14 12:31:31 -08001125 * Interpret a sequence of characters.
1126 *
1127 * Incomplete escape sequences are buffered until the next call.
1128 *
1129 * @param {string} str Sequence of characters to interpret or pass through.
1130 */
1131hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001132 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001133 this.scheduleSyncCursorPosition_();
1134};
1135
1136/**
1137 * Take over the given DIV for use as the terminal display.
1138 *
1139 * @param {HTMLDivElement} div The div to use as the terminal display.
1140 */
1141hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001142 this.div_ = div;
1143
rginda8ba33642011-12-14 12:31:31 -08001144 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001145 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001146 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1147 this.scrollPort_.setBackgroundPosition(
1148 this.prefs_.get('background-position'));
Robert Gindae76aa9f2014-03-14 12:29:12 -07001149 this.scrollPort_.setUserCss(this.prefs_.get('user-css'));
rginda30f20f62012-04-05 16:36:19 -07001150
rginda0918b652012-04-04 11:26:24 -07001151 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001152
rginda9f5222b2012-03-05 11:53:28 -08001153 this.setFontSize(this.prefs_.get('font-size'));
1154 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001155
David Reveman8f552492012-03-28 12:18:41 -04001156 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1157
rginda8ba33642011-12-14 12:31:31 -08001158 this.document_ = this.scrollPort_.getDocument();
1159
rginda4bba5e12012-06-20 16:15:30 -07001160 this.document_.body.oncontextmenu = function() { return false };
1161
1162 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001163 var screenNode = this.scrollPort_.getScreenNode();
1164 screenNode.addEventListener('mousedown', onMouse);
1165 screenNode.addEventListener('mouseup', onMouse);
1166 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001167 this.scrollPort_.onScrollWheel = onMouse;
1168
Toni Barzic0bfa8922013-11-22 11:18:35 -08001169 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001170 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001171 // Listen for mousedown events on the screenNode as in FF the focus
1172 // events don't bubble.
1173 screenNode.addEventListener('mousedown', function() {
1174 setTimeout(this.onFocusChange_.bind(this, true));
1175 }.bind(this));
1176
Toni Barzic0bfa8922013-11-22 11:18:35 -08001177 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001178 'blur', this.onFocusChange_.bind(this, false));
1179
1180 var style = this.document_.createElement('style');
1181 style.textContent =
1182 ('.cursor-node[focus="false"] {' +
1183 ' box-sizing: border-box;' +
1184 ' background-color: transparent !important;' +
1185 ' border-width: 2px;' +
1186 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001187 '}' +
1188 '.wc-node {' +
1189 ' display: inline-block;' +
1190 ' text-align: center;' +
1191 ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001192 '}');
1193 this.document_.head.appendChild(style);
1194
Ricky Liang48f05cb2013-12-31 23:35:29 +08001195 var styleSheets = this.document_.styleSheets;
1196 var cssRules = styleSheets[styleSheets.length - 1].cssRules;
1197 this.wcCssRule_ = cssRules[cssRules.length - 1];
1198
rginda8ba33642011-12-14 12:31:31 -08001199 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001200 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001201 this.cursorNode_.style.cssText =
1202 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001203 'top: -99px;' +
1204 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001205 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1206 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
Rob Spies06533ba2014-04-24 11:20:37 -07001207 '-webkit-transition: opacity, background-color 100ms linear;' +
1208 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001209
rginda8e92a692012-05-20 19:37:20 -07001210 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001211 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1212 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001213
rginda8ba33642011-12-14 12:31:31 -08001214 this.document_.body.appendChild(this.cursorNode_);
1215
rgindad5613292012-06-19 15:40:37 -07001216 // When 'enableMouseDragScroll' is off we reposition this element directly
1217 // under the mouse cursor after a click. This makes Chrome associate
1218 // subsequent mousemove events with the scroll-blocker. Since the
1219 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1220 // events do not cause the scrollport to scroll.
1221 //
1222 // It's a hack, but it's the cleanest way I could find.
1223 this.scrollBlockerNode_ = this.document_.createElement('div');
1224 this.scrollBlockerNode_.style.cssText =
1225 ('position: absolute;' +
1226 'top: -99px;' +
1227 'display: block;' +
1228 'width: 10px;' +
1229 'height: 10px;');
1230 this.document_.body.appendChild(this.scrollBlockerNode_);
1231
1232 var onMouse = this.onMouse_.bind(this);
1233 this.scrollPort_.onScrollWheel = onMouse;
1234 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1235 ].forEach(function(event) {
1236 this.scrollBlockerNode_.addEventListener(event, onMouse);
1237 this.cursorNode_.addEventListener(event, onMouse);
1238 this.document_.addEventListener(event, onMouse);
1239 }.bind(this));
1240
1241 this.cursorNode_.addEventListener('mousedown', function() {
1242 setTimeout(this.focus.bind(this));
1243 }.bind(this));
1244
rginda8ba33642011-12-14 12:31:31 -08001245 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001246
rginda87b86462011-12-14 13:48:03 -08001247 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001248 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001249};
1250
rginda0918b652012-04-04 11:26:24 -07001251/**
1252 * Return the HTML document that contains the terminal DOM nodes.
1253 */
rginda87b86462011-12-14 13:48:03 -08001254hterm.Terminal.prototype.getDocument = function() {
1255 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001256};
1257
1258/**
rginda0918b652012-04-04 11:26:24 -07001259 * Focus the terminal.
1260 */
1261hterm.Terminal.prototype.focus = function() {
1262 this.scrollPort_.focus();
1263};
1264
1265/**
rginda8ba33642011-12-14 12:31:31 -08001266 * Return the HTML Element for a given row index.
1267 *
1268 * This is a method from the RowProvider interface. The ScrollPort uses
1269 * it to fetch rows on demand as they are scrolled into view.
1270 *
1271 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1272 * pairs to conserve memory.
1273 *
1274 * @param {integer} index The zero-based row index, measured relative to the
1275 * start of the scrollback buffer. On-screen rows will always have the
1276 * largest indicies.
1277 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1278 */
1279hterm.Terminal.prototype.getRowNode = function(index) {
1280 if (index < this.scrollbackRows_.length)
1281 return this.scrollbackRows_[index];
1282
1283 var screenIndex = index - this.scrollbackRows_.length;
1284 return this.screen_.rowsArray[screenIndex];
1285};
1286
1287/**
1288 * Return the text content for a given range of rows.
1289 *
1290 * This is a method from the RowProvider interface. The ScrollPort uses
1291 * it to fetch text content on demand when the user attempts to copy their
1292 * selection to the clipboard.
1293 *
1294 * @param {integer} start The zero-based row index to start from, measured
1295 * relative to the start of the scrollback buffer. On-screen rows will
1296 * always have the largest indicies.
1297 * @param {integer} end The zero-based row index to end on, measured
1298 * relative to the start of the scrollback buffer.
1299 * @return {string} A single string containing the text value of the range of
1300 * rows. Lines will be newline delimited, with no trailing newline.
1301 */
1302hterm.Terminal.prototype.getRowsText = function(start, end) {
1303 var ary = [];
1304 for (var i = start; i < end; i++) {
1305 var node = this.getRowNode(i);
1306 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001307 if (i < end - 1 && !node.getAttribute('line-overflow'))
1308 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001309 }
1310
rgindaa09e7332012-08-17 12:49:51 -07001311 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001312};
1313
1314/**
1315 * Return the text content for a given row.
1316 *
1317 * This is a method from the RowProvider interface. The ScrollPort uses
1318 * it to fetch text content on demand when the user attempts to copy their
1319 * selection to the clipboard.
1320 *
1321 * @param {integer} index The zero-based row index to return, measured
1322 * relative to the start of the scrollback buffer. On-screen rows will
1323 * always have the largest indicies.
1324 * @return {string} A string containing the text value of the selected row.
1325 */
1326hterm.Terminal.prototype.getRowText = function(index) {
1327 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001328 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001329};
1330
1331/**
1332 * Return the total number of rows in the addressable screen and in the
1333 * scrollback buffer of this terminal.
1334 *
1335 * This is a method from the RowProvider interface. The ScrollPort uses
1336 * it to compute the size of the scrollbar.
1337 *
1338 * @return {integer} The number of rows in this terminal.
1339 */
1340hterm.Terminal.prototype.getRowCount = function() {
1341 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1342};
1343
1344/**
1345 * Create DOM nodes for new rows and append them to the end of the terminal.
1346 *
1347 * This is the only correct way to add a new DOM node for a row. Notice that
1348 * the new row is appended to the bottom of the list of rows, and does not
1349 * require renumbering (of the rowIndex property) of previous rows.
1350 *
1351 * If you think you want a new blank row somewhere in the middle of the
1352 * terminal, look into moveRows_().
1353 *
1354 * This method does not pay attention to vtScrollTop/Bottom, since you should
1355 * be using moveRows() in cases where they would matter.
1356 *
1357 * The cursor will be positioned at column 0 of the first inserted line.
1358 */
1359hterm.Terminal.prototype.appendRows_ = function(count) {
1360 var cursorRow = this.screen_.rowsArray.length;
1361 var offset = this.scrollbackRows_.length + cursorRow;
1362 for (var i = 0; i < count; i++) {
1363 var row = this.document_.createElement('x-row');
1364 row.appendChild(this.document_.createTextNode(''));
1365 row.rowIndex = offset + i;
1366 this.screen_.pushRow(row);
1367 }
1368
1369 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1370 if (extraRows > 0) {
1371 var ary = this.screen_.shiftRows(extraRows);
1372 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001373 if (this.scrollPort_.isScrolledEnd)
1374 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001375 }
1376
1377 if (cursorRow >= this.screen_.rowsArray.length)
1378 cursorRow = this.screen_.rowsArray.length - 1;
1379
rginda87b86462011-12-14 13:48:03 -08001380 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001381};
1382
1383/**
1384 * Relocate rows from one part of the addressable screen to another.
1385 *
1386 * This is used to recycle rows during VT scrolls (those which are driven
1387 * by VT commands, rather than by the user manipulating the scrollbar.)
1388 *
1389 * In this case, the blank lines scrolled into the scroll region are made of
1390 * the nodes we scrolled off. These have their rowIndex properties carefully
1391 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001392 */
1393hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1394 var ary = this.screen_.removeRows(fromIndex, count);
1395 this.screen_.insertRows(toIndex, ary);
1396
1397 var start, end;
1398 if (fromIndex < toIndex) {
1399 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001400 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001401 } else {
1402 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001403 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001404 }
1405
1406 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001407 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001408};
1409
1410/**
1411 * Renumber the rowIndex property of the given range of rows.
1412 *
1413 * The start and end indicies are relative to the screen, not the scrollback.
1414 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001415 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001416 * no need to renumber scrollback rows.
1417 */
Robert Ginda40932892012-12-10 17:26:40 -08001418hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1419 var screen = opt_screen || this.screen_;
1420
rginda8ba33642011-12-14 12:31:31 -08001421 var offset = this.scrollbackRows_.length;
1422 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001423 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001424 }
1425};
1426
1427/**
1428 * Print a string to the terminal.
1429 *
1430 * This respects the current insert and wraparound modes. It will add new lines
1431 * to the end of the terminal, scrolling off the top into the scrollback buffer
1432 * if necessary.
1433 *
1434 * The string is *not* parsed for escape codes. Use the interpret() method if
1435 * that's what you're after.
1436 *
1437 * @param{string} str The string to print.
1438 */
1439hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001440 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001441
Ricky Liang48f05cb2013-12-31 23:35:29 +08001442 var strWidth = lib.wc.strWidth(str);
1443
1444 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001445 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1446 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001447 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001448 }
rgindaa19afe22012-01-25 15:40:22 -08001449
Ricky Liang48f05cb2013-12-31 23:35:29 +08001450 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001451 var didOverflow = false;
1452 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001453
rgindaa9abdd82012-08-06 18:05:09 -07001454 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1455 didOverflow = true;
1456 count = this.screenSize.width - this.screen_.cursorPosition.column;
1457 }
rgindaa19afe22012-01-25 15:40:22 -08001458
rgindaa9abdd82012-08-06 18:05:09 -07001459 if (didOverflow && !this.options_.wraparound) {
1460 // If the string overflowed the line but wraparound is off, then the
1461 // last printed character should be the last of the string.
1462 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001463 substr = lib.wc.substr(str, startOffset, count - 1) +
1464 lib.wc.substr(str, strWidth - 1);
1465 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001466 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001467 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001468 }
rgindaa19afe22012-01-25 15:40:22 -08001469
Ricky Liang48f05cb2013-12-31 23:35:29 +08001470 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1471 for (var i = 0; i < tokens.length; i++) {
1472 if (tokens[i].wcNode)
1473 this.screen_.textAttributes.wcNode = true;
1474
1475 if (this.options_.insertMode) {
1476 this.screen_.insertString(tokens[i].str);
1477 } else {
1478 this.screen_.overwriteString(tokens[i].str);
1479 }
1480 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001481 }
1482
1483 this.screen_.maybeClipCurrentRow();
1484 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001485 }
rginda8ba33642011-12-14 12:31:31 -08001486
1487 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001488
rginda9f5222b2012-03-05 11:53:28 -08001489 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001490 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001491};
1492
1493/**
rginda87b86462011-12-14 13:48:03 -08001494 * Set the VT scroll region.
1495 *
rginda87b86462011-12-14 13:48:03 -08001496 * This also resets the cursor position to the absolute (0, 0) position, since
1497 * that's what xterm appears to do.
1498 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001499 * Setting the scroll region to the full height of the terminal will clear
1500 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1501 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1502 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1503 * continue to work as most users would expect.
1504 *
rginda87b86462011-12-14 13:48:03 -08001505 * @param {integer} scrollTop The zero-based top of the scroll region.
1506 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1507 * inclusive.
1508 */
1509hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001510 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001511 this.vtScrollTop_ = null;
1512 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001513 } else {
1514 this.vtScrollTop_ = scrollTop;
1515 this.vtScrollBottom_ = scrollBottom;
1516 }
rginda87b86462011-12-14 13:48:03 -08001517};
1518
1519/**
rginda8ba33642011-12-14 12:31:31 -08001520 * Return the top row index according to the VT.
1521 *
1522 * This will return 0 unless the terminal has been told to restrict scrolling
1523 * to some lower row. It is used for some VT cursor positioning and scrolling
1524 * commands.
1525 *
1526 * @return {integer} The topmost row in the terminal's scroll region.
1527 */
1528hterm.Terminal.prototype.getVTScrollTop = function() {
1529 if (this.vtScrollTop_ != null)
1530 return this.vtScrollTop_;
1531
1532 return 0;
rginda87b86462011-12-14 13:48:03 -08001533};
rginda8ba33642011-12-14 12:31:31 -08001534
1535/**
1536 * Return the bottom row index according to the VT.
1537 *
1538 * This will return the height of the terminal unless the it has been told to
1539 * restrict scrolling to some higher row. It is used for some VT cursor
1540 * positioning and scrolling commands.
1541 *
1542 * @return {integer} The bottommost row in the terminal's scroll region.
1543 */
1544hterm.Terminal.prototype.getVTScrollBottom = function() {
1545 if (this.vtScrollBottom_ != null)
1546 return this.vtScrollBottom_;
1547
rginda87b86462011-12-14 13:48:03 -08001548 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001549}
1550
1551/**
1552 * Process a '\n' character.
1553 *
1554 * If the cursor is on the final row of the terminal this will append a new
1555 * blank row to the screen and scroll the topmost row into the scrollback
1556 * buffer.
1557 *
1558 * Otherwise, this moves the cursor to column zero of the next row.
1559 */
1560hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001561 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1562 this.screen_.rowsArray.length - 1);
1563
1564 if (this.vtScrollBottom_ != null) {
1565 // A VT Scroll region is active, we never append new rows.
1566 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1567 // We're at the end of the VT Scroll Region, perform a VT scroll.
1568 this.vtScrollUp(1);
1569 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1570 } else if (cursorAtEndOfScreen) {
1571 // We're at the end of the screen, the only thing to do is put the
1572 // cursor to column 0.
1573 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1574 } else {
1575 // Anywhere else, advance the cursor row, and reset the column.
1576 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1577 }
1578 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001579 // We're at the end of the screen. Append a new row to the terminal,
1580 // shifting the top row into the scrollback.
1581 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001582 } else {
rginda87b86462011-12-14 13:48:03 -08001583 // Anywhere else in the screen just moves the cursor.
1584 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001585 }
1586};
1587
1588/**
1589 * Like newLine(), except maintain the cursor column.
1590 */
1591hterm.Terminal.prototype.lineFeed = function() {
1592 var column = this.screen_.cursorPosition.column;
1593 this.newLine();
1594 this.setCursorColumn(column);
1595};
1596
1597/**
rginda87b86462011-12-14 13:48:03 -08001598 * If autoCarriageReturn is set then newLine(), else lineFeed().
1599 */
1600hterm.Terminal.prototype.formFeed = function() {
1601 if (this.options_.autoCarriageReturn) {
1602 this.newLine();
1603 } else {
1604 this.lineFeed();
1605 }
1606};
1607
1608/**
1609 * Move the cursor up one row, possibly inserting a blank line.
1610 *
1611 * The cursor column is not changed.
1612 */
1613hterm.Terminal.prototype.reverseLineFeed = function() {
1614 var scrollTop = this.getVTScrollTop();
1615 var currentRow = this.screen_.cursorPosition.row;
1616
1617 if (currentRow == scrollTop) {
1618 this.insertLines(1);
1619 } else {
1620 this.setAbsoluteCursorRow(currentRow - 1);
1621 }
1622};
1623
1624/**
rginda8ba33642011-12-14 12:31:31 -08001625 * Replace all characters to the left of the current cursor with the space
1626 * character.
1627 *
1628 * TODO(rginda): This should probably *remove* the characters (not just replace
1629 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001630 * position.
rginda8ba33642011-12-14 12:31:31 -08001631 */
1632hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001633 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001634 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001635 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001636 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001637};
1638
1639/**
David Benjamin684a9b72012-05-01 17:19:58 -04001640 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001641 *
1642 * The cursor position is unchanged.
1643 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001644 * If the current background color is not the default background color this
1645 * will insert spaces rather than delete. This is unfortunate because the
1646 * trailing space will affect text selection, but it's difficult to come up
1647 * with a way to style empty space that wouldn't trip up the hterm.Screen
1648 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001649 *
1650 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1651 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1652 * crbug.com/232390 for details.
rginda8ba33642011-12-14 12:31:31 -08001653 */
1654hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001655 if (this.screen_.cursorPosition.overflow)
1656 return;
1657
Robert Ginda7fd57082012-09-25 14:41:47 -07001658 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1659 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001660
1661 if (this.screen_.textAttributes.background ===
1662 this.screen_.textAttributes.DEFAULT_COLOR) {
1663 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001664 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001665 this.screen_.cursorPosition.column + count) {
1666 this.screen_.deleteChars(count);
1667 this.clearCursorOverflow();
1668 return;
1669 }
1670 }
1671
rginda87b86462011-12-14 13:48:03 -08001672 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001673 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001674 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001675 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001676};
1677
1678/**
1679 * Erase the current line.
1680 *
1681 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001682 */
1683hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001684 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001685 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001686 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001687 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001688};
1689
1690/**
David Benjamina08d78f2012-05-05 00:28:49 -04001691 * Erase all characters from the start of the screen to the current cursor
1692 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001693 *
1694 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001695 */
1696hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001697 var cursor = this.saveCursor();
1698
1699 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001700
David Benjamina08d78f2012-05-05 00:28:49 -04001701 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001702 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();
rginda8ba33642011-12-14 12:31:31 -08001708};
1709
1710/**
1711 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001712 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001713 *
1714 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001715 */
1716hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001717 var cursor = this.saveCursor();
1718
1719 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001720
David Benjamina08d78f2012-05-05 00:28:49 -04001721 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001722 for (var i = cursor.row + 1; i <= bottom; i++) {
1723 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001724 this.screen_.clearCursorRow();
1725 }
1726
rginda87b86462011-12-14 13:48:03 -08001727 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001728 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001729};
1730
1731/**
1732 * Fill the terminal with a given character.
1733 *
1734 * This methods does not respect the VT scroll region.
1735 *
1736 * @param {string} ch The character to use for the fill.
1737 */
1738hterm.Terminal.prototype.fill = function(ch) {
1739 var cursor = this.saveCursor();
1740
1741 this.setAbsoluteCursorPosition(0, 0);
1742 for (var row = 0; row < this.screenSize.height; row++) {
1743 for (var col = 0; col < this.screenSize.width; col++) {
1744 this.setAbsoluteCursorPosition(row, col);
1745 this.screen_.overwriteString(ch);
1746 }
1747 }
1748
1749 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001750};
1751
1752/**
rginda9ea433c2012-03-16 11:57:00 -07001753 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001754 *
rginda9ea433c2012-03-16 11:57:00 -07001755 * This does not respect the scroll region.
1756 *
1757 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1758 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001759 */
rginda9ea433c2012-03-16 11:57:00 -07001760hterm.Terminal.prototype.clearHome = function(opt_screen) {
1761 var screen = opt_screen || this.screen_;
1762 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001763
rginda11057d52012-04-25 12:29:56 -07001764 if (bottom == 0) {
1765 // Empty screen, nothing to do.
1766 return;
1767 }
1768
rgindae4d29232012-01-19 10:47:13 -08001769 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001770 screen.setCursorPosition(i, 0);
1771 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001772 }
1773
rginda9ea433c2012-03-16 11:57:00 -07001774 screen.setCursorPosition(0, 0);
1775};
1776
1777/**
1778 * Erase the entire display without changing the cursor position.
1779 *
1780 * The cursor position is unchanged. This does not respect the scroll
1781 * region.
1782 *
1783 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1784 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001785 */
1786hterm.Terminal.prototype.clear = function(opt_screen) {
1787 var screen = opt_screen || this.screen_;
1788 var cursor = screen.cursorPosition.clone();
1789 this.clearHome(screen);
1790 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001791};
1792
1793/**
1794 * VT command to insert lines at the current cursor row.
1795 *
1796 * This respects the current scroll region. Rows pushed off the bottom are
1797 * lost (they won't show up in the scrollback buffer).
1798 *
rginda8ba33642011-12-14 12:31:31 -08001799 * @param {integer} count The number of lines to insert.
1800 */
1801hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001802 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001803
1804 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001805 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001806
Robert Ginda579186b2012-09-26 11:40:04 -07001807 // The moveCount is the number of rows we need to relocate to make room for
1808 // the new row(s). The count is the distance to move them.
1809 var moveCount = bottom - cursorRow - count + 1;
1810 if (moveCount)
1811 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001812
Robert Ginda579186b2012-09-26 11:40:04 -07001813 for (var i = count - 1; i >= 0; i--) {
1814 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001815 this.screen_.clearCursorRow();
1816 }
rginda8ba33642011-12-14 12:31:31 -08001817};
1818
1819/**
1820 * VT command to delete lines at the current cursor row.
1821 *
1822 * New rows are added to the bottom of scroll region to take their place. New
1823 * rows are strictly there to take up space and have no content or style.
1824 */
1825hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001826 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001827
rginda87b86462011-12-14 13:48:03 -08001828 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001829 var bottom = this.getVTScrollBottom();
1830
rginda87b86462011-12-14 13:48:03 -08001831 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001832 count = Math.min(count, maxCount);
1833
rginda87b86462011-12-14 13:48:03 -08001834 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001835 if (count != maxCount)
1836 this.moveRows_(top, count, moveStart);
1837
1838 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001839 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001840 this.screen_.clearCursorRow();
1841 }
1842
rginda87b86462011-12-14 13:48:03 -08001843 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001844 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001845};
1846
1847/**
1848 * Inserts the given number of spaces at the current cursor position.
1849 *
rginda87b86462011-12-14 13:48:03 -08001850 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001851 */
1852hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001853 var cursor = this.saveCursor();
1854
rgindacbbd7482012-06-13 15:06:16 -07001855 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001856 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001857 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001858
1859 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001860 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001861};
1862
1863/**
1864 * Forward-delete the specified number of characters starting at the cursor
1865 * position.
1866 *
1867 * @param {integer} count The number of characters to delete.
1868 */
1869hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001870 var deleted = this.screen_.deleteChars(count);
1871 if (deleted && !this.screen_.textAttributes.isDefault()) {
1872 var cursor = this.saveCursor();
1873 this.setCursorColumn(this.screenSize.width - deleted);
1874 this.screen_.insertString(lib.f.getWhitespace(deleted));
1875 this.restoreCursor(cursor);
1876 }
1877
David Benjamin54e8bf62012-06-01 22:31:40 -04001878 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001879};
1880
1881/**
1882 * Shift rows in the scroll region upwards by a given number of lines.
1883 *
1884 * New rows are inserted at the bottom of the scroll region to fill the
1885 * vacated rows. The new rows not filled out with the current text attributes.
1886 *
1887 * This function does not affect the scrollback rows at all. Rows shifted
1888 * off the top are lost.
1889 *
rginda87b86462011-12-14 13:48:03 -08001890 * The cursor position is not altered.
1891 *
rginda8ba33642011-12-14 12:31:31 -08001892 * @param {integer} count The number of rows to scroll.
1893 */
1894hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001895 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001896
rginda87b86462011-12-14 13:48:03 -08001897 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001898 this.deleteLines(count);
1899
rginda87b86462011-12-14 13:48:03 -08001900 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001901};
1902
1903/**
1904 * Shift rows below the cursor down by a given number of lines.
1905 *
1906 * This function respects the current scroll region.
1907 *
1908 * New rows are inserted at the top of the scroll region to fill the
1909 * vacated rows. The new rows not filled out with the current text attributes.
1910 *
1911 * This function does not affect the scrollback rows at all. Rows shifted
1912 * off the bottom are lost.
1913 *
1914 * @param {integer} count The number of rows to scroll.
1915 */
1916hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001917 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001918
rginda87b86462011-12-14 13:48:03 -08001919 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001920 this.insertLines(opt_count);
1921
rginda87b86462011-12-14 13:48:03 -08001922 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001923};
1924
rginda87b86462011-12-14 13:48:03 -08001925
rginda8ba33642011-12-14 12:31:31 -08001926/**
1927 * Set the cursor position.
1928 *
1929 * The cursor row is relative to the scroll region if the terminal has
1930 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1931 *
1932 * @param {integer} row The new zero-based cursor row.
1933 * @param {integer} row The new zero-based cursor column.
1934 */
1935hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1936 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001937 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001938 } else {
rginda87b86462011-12-14 13:48:03 -08001939 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001940 }
rginda87b86462011-12-14 13:48:03 -08001941};
rginda8ba33642011-12-14 12:31:31 -08001942
rginda87b86462011-12-14 13:48:03 -08001943hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1944 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001945 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1946 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001947 this.screen_.setCursorPosition(row, column);
1948};
1949
1950hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001951 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1952 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001953 this.screen_.setCursorPosition(row, column);
1954};
1955
1956/**
1957 * Set the cursor column.
1958 *
1959 * @param {integer} column The new zero-based cursor column.
1960 */
1961hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001962 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001963};
1964
1965/**
1966 * Return the cursor column.
1967 *
1968 * @return {integer} The zero-based cursor column.
1969 */
1970hterm.Terminal.prototype.getCursorColumn = function() {
1971 return this.screen_.cursorPosition.column;
1972};
1973
1974/**
1975 * Set the cursor row.
1976 *
1977 * The cursor row is relative to the scroll region if the terminal has
1978 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1979 *
1980 * @param {integer} row The new cursor row.
1981 */
rginda87b86462011-12-14 13:48:03 -08001982hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1983 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001984};
1985
1986/**
1987 * Return the cursor row.
1988 *
1989 * @return {integer} The zero-based cursor row.
1990 */
1991hterm.Terminal.prototype.getCursorRow = function(row) {
1992 return this.screen_.cursorPosition.row;
1993};
1994
1995/**
1996 * Request that the ScrollPort redraw itself soon.
1997 *
1998 * The redraw will happen asynchronously, soon after the call stack winds down.
1999 * Multiple calls will be coalesced into a single redraw.
2000 */
2001hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002002 if (this.timeouts_.redraw)
2003 return;
rginda8ba33642011-12-14 12:31:31 -08002004
2005 var self = this;
rginda87b86462011-12-14 13:48:03 -08002006 this.timeouts_.redraw = setTimeout(function() {
2007 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002008 self.scrollPort_.redraw_();
2009 }, 0);
2010};
2011
2012/**
2013 * Request that the ScrollPort be scrolled to the bottom.
2014 *
2015 * The scroll will happen asynchronously, soon after the call stack winds down.
2016 * Multiple calls will be coalesced into a single scroll.
2017 *
2018 * This affects the scrollbar position of the ScrollPort, and has nothing to
2019 * do with the VT scroll commands.
2020 */
2021hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2022 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002023 return;
rginda8ba33642011-12-14 12:31:31 -08002024
2025 var self = this;
2026 this.timeouts_.scrollDown = setTimeout(function() {
2027 delete self.timeouts_.scrollDown;
2028 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2029 }, 10);
2030};
2031
2032/**
2033 * Move the cursor up a specified number of rows.
2034 *
2035 * @param {integer} count The number of rows to move the cursor.
2036 */
2037hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002038 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002039};
2040
2041/**
2042 * Move the cursor down a specified number of rows.
2043 *
2044 * @param {integer} count The number of rows to move the cursor.
2045 */
2046hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002047 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002048 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2049 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2050 this.screenSize.height - 1);
2051
rgindacbbd7482012-06-13 15:06:16 -07002052 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002053 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002054 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002055};
2056
2057/**
2058 * Move the cursor left a specified number of columns.
2059 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002060 * If reverse wraparound mode is enabled and the previous row wrapped into
2061 * the current row then we back up through the wraparound as well.
2062 *
rginda8ba33642011-12-14 12:31:31 -08002063 * @param {integer} count The number of columns to move the cursor.
2064 */
2065hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002066 count = count || 1;
2067
2068 if (count < 1)
2069 return;
2070
2071 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002072 if (this.options_.reverseWraparound) {
2073 if (this.screen_.cursorPosition.overflow) {
2074 // If this cursor is in the right margin, consume one count to get it
2075 // back to the last column. This only applies when we're in reverse
2076 // wraparound mode.
2077 count--;
2078 this.clearCursorOverflow();
2079
2080 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002081 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002082 }
2083
Robert Gindabfb32622014-07-17 13:20:27 -07002084 var newRow = this.screen_.cursorPosition.row;
2085 var newColumn = currentColumn - count;
2086 if (newColumn < 0) {
2087 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2088 if (newRow < 0) {
2089 // xterm also wraps from row 0 to the last row.
2090 newRow = this.screenSize.height + newRow % this.screenSize.height;
2091 }
2092 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2093 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002094
Robert Gindabfb32622014-07-17 13:20:27 -07002095 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2096
2097 } else {
2098 var newColumn = Math.max(currentColumn - count, 0);
2099 this.setCursorColumn(newColumn);
2100 }
rginda8ba33642011-12-14 12:31:31 -08002101};
2102
2103/**
2104 * Move the cursor right a specified number of columns.
2105 *
2106 * @param {integer} count The number of columns to move the cursor.
2107 */
2108hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002109 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002110
2111 if (count < 1)
2112 return;
2113
rgindacbbd7482012-06-13 15:06:16 -07002114 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002115 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002116 this.setCursorColumn(column);
2117};
2118
2119/**
2120 * Reverse the foreground and background colors of the terminal.
2121 *
2122 * This only affects text that was drawn with no attributes.
2123 *
2124 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2125 * been drawn with attributes that happen to coincide with the default
2126 * 'no-attribute' colors. My guess is probably not.
2127 */
2128hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002129 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002130 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002131 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2132 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002133 } else {
rginda9f5222b2012-03-05 11:53:28 -08002134 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2135 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002136 }
2137};
2138
2139/**
rginda87b86462011-12-14 13:48:03 -08002140 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002141 *
2142 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002143 */
2144hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002145 this.cursorNode_.style.backgroundColor =
2146 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002147
2148 var self = this;
2149 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002150 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002151 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002152
Michael Kelly485ecd12014-06-09 11:41:56 -04002153 // bellSquelchTimeout_ affects both audio and notification bells.
2154 if (this.bellSquelchTimeout_)
2155 return;
2156
Robert Ginda92e18102013-03-14 13:56:37 -07002157 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002158 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002159 this.bellSequelchTimeout_ = setTimeout(function() {
2160 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002161 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002162 } else {
2163 delete this.bellSquelchTimeout_;
2164 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002165
2166 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
2167 var n = new Notification(
2168 lib.f.replaceVars(hterm.desktopNotificationTitle,
Robert Ginda348dc2b2014-06-24 14:42:23 -07002169 {'title': window.document.title || 'hterm'}));
Michael Kelly485ecd12014-06-09 11:41:56 -04002170 this.bellNotificationList_.push(n);
2171 // TODO: Should we try to raise the window here?
2172 n.onclick = function() { self.closeBellNotifications_(); };
2173 }
rginda87b86462011-12-14 13:48:03 -08002174};
2175
2176/**
rginda8ba33642011-12-14 12:31:31 -08002177 * Set the origin mode bit.
2178 *
2179 * If origin mode is on, certain VT cursor and scrolling commands measure their
2180 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2181 * to the top of the addressable screen.
2182 *
2183 * Defaults to off.
2184 *
2185 * @param {boolean} state True to set origin mode, false to unset.
2186 */
2187hterm.Terminal.prototype.setOriginMode = function(state) {
2188 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002189 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002190};
2191
2192/**
2193 * Set the insert mode bit.
2194 *
2195 * If insert mode is on, existing text beyond the cursor position will be
2196 * shifted right to make room for new text. Otherwise, new text overwrites
2197 * any existing text.
2198 *
2199 * Defaults to off.
2200 *
2201 * @param {boolean} state True to set insert mode, false to unset.
2202 */
2203hterm.Terminal.prototype.setInsertMode = function(state) {
2204 this.options_.insertMode = state;
2205};
2206
2207/**
rginda87b86462011-12-14 13:48:03 -08002208 * Set the auto carriage return bit.
2209 *
2210 * If auto carriage return is on then a formfeed character is interpreted
2211 * as a newline, otherwise it's the same as a linefeed. The difference boils
2212 * down to whether or not the cursor column is reset.
2213 */
2214hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2215 this.options_.autoCarriageReturn = state;
2216};
2217
2218/**
rginda8ba33642011-12-14 12:31:31 -08002219 * Set the wraparound mode bit.
2220 *
2221 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2222 * to the start of the following row. Otherwise, the cursor is clamped to the
2223 * end of the screen and attempts to write past it are ignored.
2224 *
2225 * Defaults to on.
2226 *
2227 * @param {boolean} state True to set wraparound mode, false to unset.
2228 */
2229hterm.Terminal.prototype.setWraparound = function(state) {
2230 this.options_.wraparound = state;
2231};
2232
2233/**
2234 * Set the reverse-wraparound mode bit.
2235 *
2236 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2237 * to the end of the previous row. Otherwise, the cursor is clamped to column
2238 * 0.
2239 *
2240 * Defaults to off.
2241 *
2242 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2243 */
2244hterm.Terminal.prototype.setReverseWraparound = function(state) {
2245 this.options_.reverseWraparound = state;
2246};
2247
2248/**
2249 * Selects between the primary and alternate screens.
2250 *
2251 * If alternate mode is on, the alternate screen is active. Otherwise the
2252 * primary screen is active.
2253 *
2254 * Swapping screens has no effect on the scrollback buffer.
2255 *
2256 * Each screen maintains its own cursor position.
2257 *
2258 * Defaults to off.
2259 *
2260 * @param {boolean} state True to set alternate mode, false to unset.
2261 */
2262hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002263 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002264 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2265
rginda35c456b2012-02-09 17:29:05 -08002266 if (this.screen_.rowsArray.length &&
2267 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2268 // If the screen changed sizes while we were away, our rowIndexes may
2269 // be incorrect.
2270 var offset = this.scrollbackRows_.length;
2271 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002272 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002273 ary[i].rowIndex = offset + i;
2274 }
2275 }
rginda8ba33642011-12-14 12:31:31 -08002276
rginda35c456b2012-02-09 17:29:05 -08002277 this.realizeWidth_(this.screenSize.width);
2278 this.realizeHeight_(this.screenSize.height);
2279 this.scrollPort_.syncScrollHeight();
2280 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002281
rginda6d397402012-01-17 10:58:29 -08002282 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002283 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002284};
2285
2286/**
2287 * Set the cursor-blink mode bit.
2288 *
2289 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2290 * a visible cursor does not blink.
2291 *
2292 * You should make sure to turn blinking off if you're going to dispose of a
2293 * terminal, otherwise you'll leak a timeout.
2294 *
2295 * Defaults to on.
2296 *
2297 * @param {boolean} state True to set cursor-blink mode, false to unset.
2298 */
2299hterm.Terminal.prototype.setCursorBlink = function(state) {
2300 this.options_.cursorBlink = state;
2301
2302 if (!state && this.timeouts_.cursorBlink) {
2303 clearTimeout(this.timeouts_.cursorBlink);
2304 delete this.timeouts_.cursorBlink;
2305 }
2306
2307 if (this.options_.cursorVisible)
2308 this.setCursorVisible(true);
2309};
2310
2311/**
2312 * Set the cursor-visible mode bit.
2313 *
2314 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2315 *
2316 * Defaults to on.
2317 *
2318 * @param {boolean} state True to set cursor-visible mode, false to unset.
2319 */
2320hterm.Terminal.prototype.setCursorVisible = function(state) {
2321 this.options_.cursorVisible = state;
2322
2323 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002324 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002325 return;
2326 }
2327
rginda87b86462011-12-14 13:48:03 -08002328 this.syncCursorPosition_();
2329
2330 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002331
2332 if (this.options_.cursorBlink) {
2333 if (this.timeouts_.cursorBlink)
2334 return;
2335
Robert Gindaea2183e2014-07-17 09:51:51 -07002336 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002337 } else {
2338 if (this.timeouts_.cursorBlink) {
2339 clearTimeout(this.timeouts_.cursorBlink);
2340 delete this.timeouts_.cursorBlink;
2341 }
2342 }
2343};
2344
2345/**
rginda87b86462011-12-14 13:48:03 -08002346 * Synchronizes the visible cursor and document selection with the current
2347 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002348 */
2349hterm.Terminal.prototype.syncCursorPosition_ = function() {
2350 var topRowIndex = this.scrollPort_.getTopRowIndex();
2351 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2352 var cursorRowIndex = this.scrollbackRows_.length +
2353 this.screen_.cursorPosition.row;
2354
2355 if (cursorRowIndex > bottomRowIndex) {
2356 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002357 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002358 return;
2359 }
2360
2361 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002362 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2363 'px';
2364 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2365 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002366
2367 this.cursorNode_.setAttribute('title',
2368 '(' + this.screen_.cursorPosition.row +
2369 ', ' + this.screen_.cursorPosition.column +
2370 ')');
2371
2372 // Update the caret for a11y purposes.
2373 var selection = this.document_.getSelection();
2374 if (selection && selection.isCollapsed)
2375 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002376};
2377
Robert Gindafb1be6a2013-12-11 11:56:22 -08002378/**
2379 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2380 * and character cell dimensions.
2381 */
Robert Ginda830583c2013-08-07 13:20:46 -07002382hterm.Terminal.prototype.restyleCursor_ = function() {
2383 var shape = this.cursorShape_;
2384
2385 if (this.cursorNode_.getAttribute('focus') == 'false') {
2386 // Always show a block cursor when unfocused.
2387 shape = hterm.Terminal.cursorShape.BLOCK;
2388 }
2389
2390 var style = this.cursorNode_.style;
2391
Robert Gindafb1be6a2013-12-11 11:56:22 -08002392 style.width = this.scrollPort_.characterSize.width + 'px';
2393
Robert Ginda830583c2013-08-07 13:20:46 -07002394 switch (shape) {
2395 case hterm.Terminal.cursorShape.BEAM:
2396 style.height = this.scrollPort_.characterSize.height + 'px';
2397 style.backgroundColor = 'transparent';
2398 style.borderBottomStyle = null;
2399 style.borderLeftStyle = 'solid';
2400 break;
2401
2402 case hterm.Terminal.cursorShape.UNDERLINE:
2403 style.height = this.scrollPort_.characterSize.baseline + 'px';
2404 style.backgroundColor = 'transparent';
2405 style.borderBottomStyle = 'solid';
2406 // correct the size to put it exactly at the baseline
2407 style.borderLeftStyle = null;
2408 break;
2409
2410 default:
2411 style.height = this.scrollPort_.characterSize.height + 'px';
2412 style.backgroundColor = this.cursorColor_;
2413 style.borderBottomStyle = null;
2414 style.borderLeftStyle = null;
2415 break;
2416 }
2417};
2418
rginda8ba33642011-12-14 12:31:31 -08002419/**
2420 * Synchronizes the visible cursor with the current cursor coordinates.
2421 *
2422 * The sync will happen asynchronously, soon after the call stack winds down.
2423 * Multiple calls will be coalesced into a single sync.
2424 */
2425hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2426 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002427 return;
rginda8ba33642011-12-14 12:31:31 -08002428
2429 var self = this;
2430 this.timeouts_.syncCursor = setTimeout(function() {
2431 self.syncCursorPosition_();
2432 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002433 }, 0);
2434};
2435
rgindacc2996c2012-02-24 14:59:31 -08002436/**
rgindaf522ce02012-04-17 17:49:17 -07002437 * Show or hide the zoom warning.
2438 *
2439 * The zoom warning is a message warning the user that their browser zoom must
2440 * be set to 100% in order for hterm to function properly.
2441 *
2442 * @param {boolean} state True to show the message, false to hide it.
2443 */
2444hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2445 if (!this.zoomWarningNode_) {
2446 if (!state)
2447 return;
2448
2449 this.zoomWarningNode_ = this.document_.createElement('div');
2450 this.zoomWarningNode_.style.cssText = (
2451 'color: black;' +
2452 'background-color: #ff2222;' +
2453 'font-size: large;' +
2454 'border-radius: 8px;' +
2455 'opacity: 0.75;' +
2456 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2457 'top: 0.5em;' +
2458 'right: 1.2em;' +
2459 'position: absolute;' +
2460 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002461 '-webkit-user-select: none;' +
2462 '-moz-text-size-adjust: none;' +
2463 '-moz-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002464 }
2465
Robert Gindab4839c22013-02-28 16:52:10 -08002466 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2467 hterm.zoomWarningMessage,
2468 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2469
rgindaf522ce02012-04-17 17:49:17 -07002470 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2471
2472 if (state) {
2473 if (!this.zoomWarningNode_.parentNode)
2474 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2475 } else if (this.zoomWarningNode_.parentNode) {
2476 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2477 }
2478};
2479
2480/**
rgindacc2996c2012-02-24 14:59:31 -08002481 * Show the terminal overlay for a given amount of time.
2482 *
2483 * The terminal overlay appears in inverse video in a large font, centered
2484 * over the terminal. You should probably keep the overlay message brief,
2485 * since it's in a large font and you probably aren't going to check the size
2486 * of the terminal first.
2487 *
2488 * @param {string} msg The text (not HTML) message to display in the overlay.
2489 * @param {number} opt_timeout The amount of time to wait before fading out
2490 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2491 * stay up forever (or until the next overlay).
2492 */
2493hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002494 if (!this.overlayNode_) {
2495 if (!this.div_)
2496 return;
2497
2498 this.overlayNode_ = this.document_.createElement('div');
2499 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002500 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002501 'font-size: xx-large;' +
2502 'opacity: 0.75;' +
2503 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2504 'position: absolute;' +
2505 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002506 '-webkit-transition: opacity 180ms ease-in;' +
2507 '-moz-user-select: none;' +
2508 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002509
2510 this.overlayNode_.addEventListener('mousedown', function(e) {
2511 e.preventDefault();
2512 e.stopPropagation();
2513 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002514 }
2515
rginda9f5222b2012-03-05 11:53:28 -08002516 this.overlayNode_.style.color = this.prefs_.get('background-color');
2517 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2518 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2519
rgindaf0090c92012-02-10 14:58:52 -08002520 this.overlayNode_.textContent = msg;
2521 this.overlayNode_.style.opacity = '0.75';
2522
2523 if (!this.overlayNode_.parentNode)
2524 this.div_.appendChild(this.overlayNode_);
2525
Robert Ginda97769282013-02-01 15:30:30 -08002526 var divSize = hterm.getClientSize(this.div_);
2527 var overlaySize = hterm.getClientSize(this.overlayNode_);
2528
2529 this.overlayNode_.style.top = (divSize.height - overlaySize.height) / 2;
2530 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
2531 this.scrollPort_.currentScrollbarWidthPx) / 2;
rgindaf0090c92012-02-10 14:58:52 -08002532
2533 var self = this;
2534
2535 if (this.overlayTimeout_)
2536 clearTimeout(this.overlayTimeout_);
2537
rgindacc2996c2012-02-24 14:59:31 -08002538 if (opt_timeout === null)
2539 return;
2540
rgindaf0090c92012-02-10 14:58:52 -08002541 this.overlayTimeout_ = setTimeout(function() {
2542 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002543 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002544 if (self.overlayNode_.parentNode)
2545 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002546 self.overlayTimeout_ = null;
2547 self.overlayNode_.style.opacity = '0.75';
2548 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002549 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002550};
2551
rginda4bba5e12012-06-20 16:15:30 -07002552/**
2553 * Paste from the system clipboard to the terminal.
2554 */
2555hterm.Terminal.prototype.paste = function() {
2556 hterm.pasteFromClipboard(this.document_);
2557};
2558
2559/**
2560 * Copy a string to the system clipboard.
2561 *
2562 * Note: If there is a selected range in the terminal, it'll be cleared.
2563 */
2564hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002565 if (this.prefs_.get('enable-clipboard-notice'))
2566 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2567
rgindaa09e7332012-08-17 12:49:51 -07002568 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002569 copySource.textContent = str;
2570 copySource.style.cssText = (
2571 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002572 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002573 'position: absolute;' +
2574 'top: -99px');
2575
2576 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002577
rginda4bba5e12012-06-20 16:15:30 -07002578 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002579 var anchorNode = selection.anchorNode;
2580 var anchorOffset = selection.anchorOffset;
2581 var focusNode = selection.focusNode;
2582 var focusOffset = selection.focusOffset;
2583
rginda4bba5e12012-06-20 16:15:30 -07002584 selection.selectAllChildren(copySource);
2585
rgindaa09e7332012-08-17 12:49:51 -07002586 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002587
Rob Spies56953412014-04-28 14:09:47 -07002588 // IE doesn't support selection.extend. This means that the selection
2589 // won't return on IE.
Rob Spies0bec09b2014-06-06 15:58:09 -07002590 if (this.clearSelectionAfterCopy && selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002591 selection.collapse(anchorNode, anchorOffset);
2592 selection.extend(focusNode, focusOffset);
2593 }
rgindafaa74742012-08-21 13:34:03 -07002594
rginda4bba5e12012-06-20 16:15:30 -07002595 copySource.parentNode.removeChild(copySource);
2596};
2597
rgindaa09e7332012-08-17 12:49:51 -07002598hterm.Terminal.prototype.getSelectionText = function() {
2599 var selection = this.scrollPort_.selection;
2600 selection.sync();
2601
2602 if (selection.isCollapsed)
2603 return null;
2604
2605
2606 // Start offset measures from the beginning of the line.
2607 var startOffset = selection.startOffset;
2608 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002609
Robert Gindafdbb3f22012-09-06 20:23:06 -07002610 if (node.nodeName != 'X-ROW') {
2611 // If the selection doesn't start on an x-row node, then it must be
2612 // somewhere inside the x-row. Add any characters from previous siblings
2613 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002614
2615 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2616 // If node is the text node in a styled span, move up to the span node.
2617 node = node.parentNode;
2618 }
2619
Robert Gindafdbb3f22012-09-06 20:23:06 -07002620 while (node.previousSibling) {
2621 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002622 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002623 }
rgindaa09e7332012-08-17 12:49:51 -07002624 }
2625
2626 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002627 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2628 selection.endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002629 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002630
Robert Gindafdbb3f22012-09-06 20:23:06 -07002631 if (node.nodeName != 'X-ROW') {
2632 // If the selection doesn't end on an x-row node, then it must be
2633 // somewhere inside the x-row. Add any characters from following siblings
2634 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002635
2636 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2637 // If node is the text node in a styled span, move up to the span node.
2638 node = node.parentNode;
2639 }
2640
Robert Gindafdbb3f22012-09-06 20:23:06 -07002641 while (node.nextSibling) {
2642 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002643 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002644 }
rgindaa09e7332012-08-17 12:49:51 -07002645 }
2646
2647 var rv = this.getRowsText(selection.startRow.rowIndex,
2648 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002649 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002650};
2651
rginda4bba5e12012-06-20 16:15:30 -07002652/**
2653 * Copy the current selection to the system clipboard, then clear it after a
2654 * short delay.
2655 */
2656hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002657 var text = this.getSelectionText();
2658 if (text != null)
2659 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002660};
2661
rgindaf0090c92012-02-10 14:58:52 -08002662hterm.Terminal.prototype.overlaySize = function() {
2663 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2664};
2665
rginda87b86462011-12-14 13:48:03 -08002666/**
2667 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2668 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002669 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002670 */
2671hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002672 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002673 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2674
Robert Ginda8cb7d902013-06-20 14:37:18 -07002675 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002676};
2677
2678/**
rgindad5613292012-06-19 15:40:37 -07002679 * Add the terminalRow and terminalColumn properties to mouse events and
2680 * then forward on to onMouse().
2681 *
2682 * The terminalRow and terminalColumn properties contain the (row, column)
2683 * coordinates for the mouse event.
2684 */
2685hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002686 if (e.processedByTerminalHandler_) {
2687 // We register our event handlers on the document, as well as the cursor
2688 // and the scroll blocker. Mouse events that occur on the cursor or
2689 // scroll blocker will also appear on the document, but we don't want to
2690 // process them twice.
2691 //
2692 // We can't just prevent bubbling because that has other side effects, so
2693 // we decorate the event object with this property instead.
2694 return;
2695 }
2696
2697 e.processedByTerminalHandler_ = true;
2698
Robert Gindaeda48db2014-07-17 09:25:30 -07002699 // One based row/column stored on the mouse event.
2700 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2701 this.scrollPort_.characterSize.height) + 1;
2702 e.terminalColumn = parseInt(e.clientX /
2703 this.scrollPort_.characterSize.width) + 1;
2704
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002705 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2706 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002707 return;
2708 }
2709
Robert Gindaeda48db2014-07-17 09:25:30 -07002710 if (this.options_.cursorVisible) {
2711 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
2712 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
2713 this.cursorNode_.style.display = 'none';
2714 } else if (this.cursorNode_.style.display == 'none') {
2715 this.cursorNode_.style.display = '';
2716 }
2717 }
rgindad5613292012-06-19 15:40:37 -07002718
Robert Ginda928cf632014-03-05 15:07:41 -08002719 if (e.type == 'mousedown') {
2720 if (e.altKey || this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED) {
2721 // If VT mouse reporting is disabled, or has been defeated with
2722 // alt-mousedown, then the mouse will act on the local selection.
2723 this.reportMouseEvents_ = false;
2724 this.setSelectionEnabled(true);
2725 } else {
2726 // Otherwise we defer ownership of the mouse to the VT.
2727 this.reportMouseEvents_ = true;
Robert Ginda3ae37822014-05-15 13:05:35 -07002728 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08002729 this.setSelectionEnabled(false);
2730 e.preventDefault();
2731 }
2732 }
2733
2734 if (!this.reportMouseEvents_) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002735 if (e.type == 'dblclick') {
2736 this.screen_.expandSelection(this.document_.getSelection());
2737 hterm.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07002738 }
2739
Robert Ginda928cf632014-03-05 15:07:41 -08002740 if (e.type == 'mousedown' && e.which == this.mousePasteButton)
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002741 this.paste();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002742
2743 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2744 !this.document_.getSelection().isCollapsed) {
2745 hterm.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002746 }
2747
2748 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
2749 this.scrollBlockerNode_.engaged) {
2750 // Disengage the scroll-blocker after one of these events.
2751 this.scrollBlockerNode_.engaged = false;
2752 this.scrollBlockerNode_.style.top = '-99px';
2753 }
2754
Robert Ginda928cf632014-03-05 15:07:41 -08002755 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002756 if (!this.scrollBlockerNode_.engaged) {
2757 if (e.type == 'mousedown') {
2758 // Move the scroll-blocker into place if we want to keep the scrollport
2759 // from scrolling.
2760 this.scrollBlockerNode_.engaged = true;
2761 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2762 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2763 } else if (e.type == 'mousemove') {
2764 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
2765 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07002766 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002767 e.preventDefault();
2768 }
2769 }
Robert Ginda928cf632014-03-05 15:07:41 -08002770
2771 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002772 }
2773
Robert Ginda928cf632014-03-05 15:07:41 -08002774 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
2775 // Restore this on mouseup in case it was temporarily defeated with a
2776 // alt-mousedown. Only do this when the selection is empty so that
2777 // we don't immediately kill the users selection.
2778 this.reportMouseEvents_ = (this.vt.mouseReport !=
2779 this.vt.MOUSE_REPORT_DISABLED);
2780 }
rgindad5613292012-06-19 15:40:37 -07002781};
2782
2783/**
2784 * Clients should override this if they care to know about mouse events.
2785 *
2786 * The event parameter will be a normal DOM mouse click event with additional
2787 * 'terminalRow' and 'terminalColumn' properties.
2788 */
2789hterm.Terminal.prototype.onMouse = function(e) { };
2790
2791/**
rginda8e92a692012-05-20 19:37:20 -07002792 * React when focus changes.
2793 */
Rob Spies06533ba2014-04-24 11:20:37 -07002794hterm.Terminal.prototype.onFocusChange_ = function(focused) {
2795 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07002796 this.restyleCursor_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002797 if (focused === true)
2798 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07002799};
2800
2801/**
rginda8ba33642011-12-14 12:31:31 -08002802 * React when the ScrollPort is scrolled.
2803 */
2804hterm.Terminal.prototype.onScroll_ = function() {
2805 this.scheduleSyncCursorPosition_();
2806};
2807
2808/**
rginda9846e2f2012-01-27 13:53:33 -08002809 * React when text is pasted into the scrollPort.
2810 */
2811hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Ginda8cb7d902013-06-20 14:37:18 -07002812 this.onVTKeystroke(e.text.replace(/\n/mg, '\r'));
rginda9846e2f2012-01-27 13:53:33 -08002813};
2814
2815/**
rgindaa09e7332012-08-17 12:49:51 -07002816 * React when the user tries to copy from the scrollPort.
2817 */
2818hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07002819 if (!this.useDefaultWindowCopy) {
2820 e.preventDefault();
2821 setTimeout(this.copySelectionToClipboard.bind(this), 0);
2822 }
rgindaa09e7332012-08-17 12:49:51 -07002823};
2824
2825/**
rginda8ba33642011-12-14 12:31:31 -08002826 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002827 *
2828 * Note: This function should not directly contain code that alters the internal
2829 * state of the terminal. That kind of code belongs in realizeWidth or
2830 * realizeHeight, so that it can be executed synchronously in the case of a
2831 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002832 */
2833hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08002834 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002835 this.scrollPort_.characterSize.width);
Robert Ginda19f61292014-03-04 14:07:57 -08002836 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
rginda35c456b2012-02-09 17:29:05 -08002837 this.scrollPort_.characterSize.height);
2838
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002839 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002840 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002841 // gets removed from the document or during the initial load, and we can't
2842 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002843 return;
2844 }
2845
rgindaa8ba17d2012-08-15 14:41:10 -07002846 var isNewSize = (columnCount != this.screenSize.width ||
2847 rowCount != this.screenSize.height);
2848
2849 // We do this even if the size didn't change, just to be sure everything is
2850 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002851 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002852 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002853
2854 if (isNewSize)
2855 this.overlaySize();
2856
Robert Gindafb1be6a2013-12-11 11:56:22 -08002857 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07002858 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002859};
2860
2861/**
2862 * Service the cursor blink timeout.
2863 */
2864hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07002865 if (!this.options_.cursorBlink) {
2866 delete this.timeouts_.cursorBlink;
2867 return;
2868 }
2869
Robert Ginda830583c2013-08-07 13:20:46 -07002870 if (this.cursorNode_.getAttribute('focus') == 'false' ||
2871 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08002872 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07002873 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
2874 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08002875 } else {
rginda87b86462011-12-14 13:48:03 -08002876 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07002877 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
2878 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08002879 }
2880};
David Reveman8f552492012-03-28 12:18:41 -04002881
2882/**
2883 * Set the scrollbar-visible mode bit.
2884 *
2885 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2886 * Otherwise it will not.
2887 *
2888 * Defaults to on.
2889 *
2890 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2891 */
2892hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2893 this.scrollPort_.setScrollbarVisible(state);
2894};
Michael Kelly485ecd12014-06-09 11:41:56 -04002895
2896/**
2897 * Close all web notifications created by terminal bells.
2898 */
2899hterm.Terminal.prototype.closeBellNotifications_ = function() {
2900 this.bellNotificationList_.forEach(function(n) {
2901 n.close();
2902 });
2903 this.bellNotificationList_.length = 0;
2904};