blob: f6ca50fe34f8f7ccd260133fc5cc4477f116dcf5 [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
Robert Gindaa063b202014-07-21 11:08:25 -0700496/**
497 * Enable or disable bracketed paste mode.
498 */
499hterm.Terminal.prototype.setBracketedPaste = function(state) {
500 this.options_.bracketedPaste = state;
501};
Rob Spies56953412014-04-28 14:09:47 -0700502
rginda8e92a692012-05-20 19:37:20 -0700503/**
504 * Set the color for the cursor.
505 *
506 * If you want this setting to persist, set it through prefs_, rather than
507 * with this method.
508 */
509hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700510 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700511 this.cursorNode_.style.backgroundColor = color;
512 this.cursorNode_.style.borderColor = color;
513};
514
515/**
516 * Return the current cursor color as a string.
517 */
518hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700519 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700520};
521
522/**
rgindad5613292012-06-19 15:40:37 -0700523 * Enable or disable mouse based text selection in the terminal.
524 */
525hterm.Terminal.prototype.setSelectionEnabled = function(state) {
526 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700527};
528
529/**
rginda8e92a692012-05-20 19:37:20 -0700530 * Set the background color.
531 *
532 * If you want this setting to persist, set it through prefs_, rather than
533 * with this method.
534 */
535hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700536 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700537 this.primaryScreen_.textAttributes.setDefaults(
538 this.foregroundColor_, this.backgroundColor_);
539 this.alternateScreen_.textAttributes.setDefaults(
540 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700541 this.scrollPort_.setBackgroundColor(color);
542};
543
rginda9f5222b2012-03-05 11:53:28 -0800544/**
545 * Return the current terminal background color.
546 *
547 * Intended for use by other classes, so we don't have to expose the entire
548 * prefs_ object.
549 */
550hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700551 return this.backgroundColor_;
552};
553
554/**
555 * Set the foreground color.
556 *
557 * If you want this setting to persist, set it through prefs_, rather than
558 * with this method.
559 */
560hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700561 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700562 this.primaryScreen_.textAttributes.setDefaults(
563 this.foregroundColor_, this.backgroundColor_);
564 this.alternateScreen_.textAttributes.setDefaults(
565 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700566 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800567};
568
569/**
570 * Return the current terminal foreground color.
571 *
572 * Intended for use by other classes, so we don't have to expose the entire
573 * prefs_ object.
574 */
575hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700576 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800577};
578
579/**
rginda87b86462011-12-14 13:48:03 -0800580 * Create a new instance of a terminal command and run it with a given
581 * argument string.
582 *
583 * @param {function} commandClass The constructor for a terminal command.
584 * @param {string} argString The argument string to pass to the command.
585 */
586hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700587 var environment = this.prefs_.get('environment');
588 if (typeof environment != 'object' || environment == null)
589 environment = {};
590
rginda87b86462011-12-14 13:48:03 -0800591 var self = this;
592 this.command = new commandClass(
593 { argString: argString || '',
594 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700595 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800596 onExit: function(code) {
597 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800598 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700599 if (self.prefs_.get('close-on-exit'))
600 window.close();
rginda87b86462011-12-14 13:48:03 -0800601 }
602 });
603
rgindafeaf3142012-01-31 15:14:20 -0800604 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800605 this.command.run();
606};
607
608/**
rgindafeaf3142012-01-31 15:14:20 -0800609 * Returns true if the current screen is the primary screen, false otherwise.
610 */
611hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700612 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800613};
614
615/**
616 * Install the keyboard handler for this terminal.
617 *
618 * This will prevent the browser from seeing any keystrokes sent to the
619 * terminal.
620 */
621hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700622 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800623}
624
625/**
626 * Uninstall the keyboard handler for this terminal.
627 */
628hterm.Terminal.prototype.uninstallKeyboard = function() {
629 this.keyboard.installKeyboard(null);
630}
631
632/**
rginda35c456b2012-02-09 17:29:05 -0800633 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800634 *
635 * Call setFontSize(0) to reset to the default font size.
636 *
637 * This function does not modify the font-size preference.
638 *
639 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800640 */
641hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800642 if (px === 0)
643 px = this.prefs_.get('font-size');
644
rginda35c456b2012-02-09 17:29:05 -0800645 this.scrollPort_.setFontSize(px);
Ricky Liang48f05cb2013-12-31 23:35:29 +0800646 if (this.wcCssRule_) {
647 this.wcCssRule_.style.width = this.scrollPort_.characterSize.width * 2 +
648 'px';
649 }
rginda35c456b2012-02-09 17:29:05 -0800650};
651
652/**
653 * Get the current font size.
654 */
655hterm.Terminal.prototype.getFontSize = function() {
656 return this.scrollPort_.getFontSize();
657};
658
659/**
rginda8e92a692012-05-20 19:37:20 -0700660 * Get the current font family.
661 */
662hterm.Terminal.prototype.getFontFamily = function() {
663 return this.scrollPort_.getFontFamily();
664};
665
666/**
rginda35c456b2012-02-09 17:29:05 -0800667 * Set the CSS "font-family" for this terminal.
668 */
rginda9f5222b2012-03-05 11:53:28 -0800669hterm.Terminal.prototype.syncFontFamily = function() {
670 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
671 this.prefs_.get('font-smoothing'));
672 this.syncBoldSafeState();
673};
674
rginda4bba5e12012-06-20 16:15:30 -0700675/**
676 * Set this.mousePasteButton based on the mouse-paste-button pref,
677 * autodetecting if necessary.
678 */
679hterm.Terminal.prototype.syncMousePasteButton = function() {
680 var button = this.prefs_.get('mouse-paste-button');
681 if (typeof button == 'number') {
682 this.mousePasteButton = button;
683 return;
684 }
685
686 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
687 if (!ary || ary[2] == 'CrOS') {
688 this.mousePasteButton = 2;
689 } else {
690 this.mousePasteButton = 3;
691 }
692};
693
694/**
695 * Enable or disable bold based on the enable-bold pref, autodetecting if
696 * necessary.
697 */
rginda9f5222b2012-03-05 11:53:28 -0800698hterm.Terminal.prototype.syncBoldSafeState = function() {
699 var enableBold = this.prefs_.get('enable-bold');
700 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700701 this.primaryScreen_.textAttributes.enableBold = enableBold;
702 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800703 return;
704 }
705
rgindaf7521392012-02-28 17:20:34 -0800706 var normalSize = this.scrollPort_.measureCharacterSize();
707 var boldSize = this.scrollPort_.measureCharacterSize('bold');
708
709 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800710 if (!isBoldSafe) {
711 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700712 'from normal. Font family is: ' +
713 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800714 }
rginda9f5222b2012-03-05 11:53:28 -0800715
Robert Gindaed016262012-10-26 16:27:09 -0700716 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
717 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800718};
719
720/**
rginda87b86462011-12-14 13:48:03 -0800721 * Return a copy of the current cursor position.
722 *
723 * @return {hterm.RowCol} The RowCol object representing the current position.
724 */
725hterm.Terminal.prototype.saveCursor = function() {
726 return this.screen_.cursorPosition.clone();
727};
728
rgindaa19afe22012-01-25 15:40:22 -0800729hterm.Terminal.prototype.getTextAttributes = function() {
730 return this.screen_.textAttributes;
731};
732
rginda1a09aa02012-06-18 21:11:25 -0700733hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
734 this.screen_.textAttributes = textAttributes;
735};
736
rginda87b86462011-12-14 13:48:03 -0800737/**
rgindaf522ce02012-04-17 17:49:17 -0700738 * Return the current browser zoom factor applied to the terminal.
739 *
740 * @return {number} The current browser zoom factor.
741 */
742hterm.Terminal.prototype.getZoomFactor = function() {
743 return this.scrollPort_.characterSize.zoomFactor;
744};
745
746/**
rginda9846e2f2012-01-27 13:53:33 -0800747 * Change the title of this terminal's window.
748 */
749hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800750 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800751};
752
753/**
rginda87b86462011-12-14 13:48:03 -0800754 * Restore a previously saved cursor position.
755 *
756 * @param {hterm.RowCol} cursor The position to restore.
757 */
758hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700759 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
760 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800761 this.screen_.setCursorPosition(row, column);
762 if (cursor.column > column ||
763 cursor.column == column && cursor.overflow) {
764 this.screen_.cursorPosition.overflow = true;
765 }
rginda87b86462011-12-14 13:48:03 -0800766};
767
768/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400769 * Clear the cursor's overflow flag.
770 */
771hterm.Terminal.prototype.clearCursorOverflow = function() {
772 this.screen_.cursorPosition.overflow = false;
773};
774
775/**
Robert Ginda830583c2013-08-07 13:20:46 -0700776 * Sets the cursor shape
777 */
778hterm.Terminal.prototype.setCursorShape = function(shape) {
779 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800780 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700781}
782
783/**
784 * Get the cursor shape
785 */
786hterm.Terminal.prototype.getCursorShape = function() {
787 return this.cursorShape_;
788}
789
790/**
rginda87b86462011-12-14 13:48:03 -0800791 * Set the width of the terminal, resizing the UI to match.
792 */
793hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800794 if (columnCount == null) {
795 this.div_.style.width = '100%';
796 return;
797 }
798
rginda35c456b2012-02-09 17:29:05 -0800799 this.div_.style.width = this.scrollPort_.characterSize.width *
Robert Ginda97769282013-02-01 15:30:30 -0800800 columnCount + this.scrollPort_.currentScrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400801 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800802 this.scheduleSyncCursorPosition_();
803};
rginda87b86462011-12-14 13:48:03 -0800804
rgindac9bc5502012-01-18 11:48:44 -0800805/**
rginda35c456b2012-02-09 17:29:05 -0800806 * Set the height of the terminal, resizing the UI to match.
807 */
808hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800809 if (rowCount == null) {
810 this.div_.style.height = '100%';
811 return;
812 }
813
rginda35c456b2012-02-09 17:29:05 -0800814 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700815 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800816 this.realizeSize_(this.screenSize.width, rowCount);
817 this.scheduleSyncCursorPosition_();
818};
819
820/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400821 * Deal with terminal size changes.
822 *
823 */
824hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
825 if (columnCount != this.screenSize.width)
826 this.realizeWidth_(columnCount);
827
828 if (rowCount != this.screenSize.height)
829 this.realizeHeight_(rowCount);
830
831 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700832 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400833};
834
835/**
rgindac9bc5502012-01-18 11:48:44 -0800836 * Deal with terminal width changes.
837 *
838 * This function does what needs to be done when the terminal width changes
839 * out from under us. It happens here rather than in onResize_() because this
840 * code may need to run synchronously to handle programmatic changes of
841 * terminal width.
842 *
843 * Relying on the browser to send us an async resize event means we may not be
844 * in the correct state yet when the next escape sequence hits.
845 */
846hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700847 if (columnCount <= 0)
848 throw new Error('Attempt to realize bad width: ' + columnCount);
849
rgindac9bc5502012-01-18 11:48:44 -0800850 var deltaColumns = columnCount - this.screen_.getWidth();
851
rginda87b86462011-12-14 13:48:03 -0800852 this.screenSize.width = columnCount;
853 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800854
855 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400856 if (this.defaultTabStops)
857 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800858 } else {
859 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400860 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800861 break;
862
863 this.tabStops_.pop();
864 }
865 }
866
867 this.screen_.setColumnCount(this.screenSize.width);
868};
869
870/**
871 * Deal with terminal height changes.
872 *
873 * This function does what needs to be done when the terminal height changes
874 * out from under us. It happens here rather than in onResize_() because this
875 * code may need to run synchronously to handle programmatic changes of
876 * terminal height.
877 *
878 * Relying on the browser to send us an async resize event means we may not be
879 * in the correct state yet when the next escape sequence hits.
880 */
881hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700882 if (rowCount <= 0)
883 throw new Error('Attempt to realize bad height: ' + rowCount);
884
rgindac9bc5502012-01-18 11:48:44 -0800885 var deltaRows = rowCount - this.screen_.getHeight();
886
887 this.screenSize.height = rowCount;
888
889 var cursor = this.saveCursor();
890
891 if (deltaRows < 0) {
892 // Screen got smaller.
893 deltaRows *= -1;
894 while (deltaRows) {
895 var lastRow = this.getRowCount() - 1;
896 if (lastRow - this.scrollbackRows_.length == cursor.row)
897 break;
898
899 if (this.getRowText(lastRow))
900 break;
901
902 this.screen_.popRow();
903 deltaRows--;
904 }
905
906 var ary = this.screen_.shiftRows(deltaRows);
907 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
908
909 // We just removed rows from the top of the screen, we need to update
910 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800911 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800912 } else if (deltaRows > 0) {
913 // Screen got larger.
914
915 if (deltaRows <= this.scrollbackRows_.length) {
916 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
917 var rows = this.scrollbackRows_.splice(
918 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
919 this.screen_.unshiftRows(rows);
920 deltaRows -= scrollbackCount;
921 cursor.row += scrollbackCount;
922 }
923
924 if (deltaRows)
925 this.appendRows_(deltaRows);
926 }
927
rginda35c456b2012-02-09 17:29:05 -0800928 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800929 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800930};
931
932/**
933 * Scroll the terminal to the top of the scrollback buffer.
934 */
935hterm.Terminal.prototype.scrollHome = function() {
936 this.scrollPort_.scrollRowToTop(0);
937};
938
939/**
940 * Scroll the terminal to the end.
941 */
942hterm.Terminal.prototype.scrollEnd = function() {
943 this.scrollPort_.scrollRowToBottom(this.getRowCount());
944};
945
946/**
947 * Scroll the terminal one page up (minus one line) relative to the current
948 * position.
949 */
950hterm.Terminal.prototype.scrollPageUp = function() {
951 var i = this.scrollPort_.getTopRowIndex();
952 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
953};
954
955/**
956 * Scroll the terminal one page down (minus one line) relative to the current
957 * position.
958 */
959hterm.Terminal.prototype.scrollPageDown = function() {
960 var i = this.scrollPort_.getTopRowIndex();
961 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800962};
963
rgindac9bc5502012-01-18 11:48:44 -0800964/**
Robert Ginda40932892012-12-10 17:26:40 -0800965 * Clear primary screen, secondary screen, and the scrollback buffer.
966 */
967hterm.Terminal.prototype.wipeContents = function() {
968 this.scrollbackRows_.length = 0;
969 this.scrollPort_.resetCache();
970
971 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
972 var bottom = screen.getHeight();
973 if (bottom > 0) {
974 this.renumberRows_(0, bottom);
975 this.clearHome(screen);
976 }
977 }.bind(this));
978
979 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -0700980 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -0800981};
982
983/**
rgindac9bc5502012-01-18 11:48:44 -0800984 * Full terminal reset.
985 */
rginda87b86462011-12-14 13:48:03 -0800986hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800987 this.clearAllTabStops();
988 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700989
990 this.clearHome(this.primaryScreen_);
991 this.primaryScreen_.textAttributes.reset();
992
993 this.clearHome(this.alternateScreen_);
994 this.alternateScreen_.textAttributes.reset();
995
rgindab8bc8932012-04-27 12:45:03 -0700996 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
997
Robert Ginda92e18102013-03-14 13:56:37 -0700998 this.vt.reset();
999
rgindac9bc5502012-01-18 11:48:44 -08001000 this.softReset();
rginda87b86462011-12-14 13:48:03 -08001001};
1002
rgindac9bc5502012-01-18 11:48:44 -08001003/**
1004 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001005 *
1006 * Perform a soft reset to the default values listed in
1007 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001008 */
rginda0f5c0292012-01-13 11:00:13 -08001009hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -07001010 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001011 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001012
rgindab8bc8932012-04-27 12:45:03 -07001013 // Xterm also resets the color palette on soft reset, even though it doesn't
1014 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -07001015 this.primaryScreen_.textAttributes.resetColorPalette();
1016 this.alternateScreen_.textAttributes.resetColorPalette();
1017
rgindab8bc8932012-04-27 12:45:03 -07001018 // The xterm man page explicitly says this will happen on soft reset.
1019 this.setVTScrollRegion(null, null);
1020
1021 // Xterm also shows the cursor on soft reset, but does not alter the blink
1022 // state.
rgindaa19afe22012-01-25 15:40:22 -08001023 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001024};
1025
rgindac9bc5502012-01-18 11:48:44 -08001026/**
1027 * Move the cursor forward to the next tab stop, or to the last column
1028 * if no more tab stops are set.
1029 */
1030hterm.Terminal.prototype.forwardTabStop = function() {
1031 var column = this.screen_.cursorPosition.column;
1032
1033 for (var i = 0; i < this.tabStops_.length; i++) {
1034 if (this.tabStops_[i] > column) {
1035 this.setCursorColumn(this.tabStops_[i]);
1036 return;
1037 }
1038 }
1039
David Benjamin66e954d2012-05-05 21:08:12 -04001040 // xterm does not clear the overflow flag on HT or CHT.
1041 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001042 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001043 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001044};
1045
rgindac9bc5502012-01-18 11:48:44 -08001046/**
1047 * Move the cursor backward to the previous tab stop, or to the first column
1048 * if no previous tab stops are set.
1049 */
1050hterm.Terminal.prototype.backwardTabStop = function() {
1051 var column = this.screen_.cursorPosition.column;
1052
1053 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1054 if (this.tabStops_[i] < column) {
1055 this.setCursorColumn(this.tabStops_[i]);
1056 return;
1057 }
1058 }
1059
1060 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001061};
1062
rgindac9bc5502012-01-18 11:48:44 -08001063/**
1064 * Set a tab stop at the given column.
1065 *
1066 * @param {int} column Zero based column.
1067 */
1068hterm.Terminal.prototype.setTabStop = function(column) {
1069 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1070 if (this.tabStops_[i] == column)
1071 return;
1072
1073 if (this.tabStops_[i] < column) {
1074 this.tabStops_.splice(i + 1, 0, column);
1075 return;
1076 }
1077 }
1078
1079 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001080};
1081
rgindac9bc5502012-01-18 11:48:44 -08001082/**
1083 * Clear the tab stop at the current cursor position.
1084 *
1085 * No effect if there is no tab stop at the current cursor position.
1086 */
1087hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1088 var column = this.screen_.cursorPosition.column;
1089
1090 var i = this.tabStops_.indexOf(column);
1091 if (i == -1)
1092 return;
1093
1094 this.tabStops_.splice(i, 1);
1095};
1096
1097/**
1098 * Clear all tab stops.
1099 */
1100hterm.Terminal.prototype.clearAllTabStops = function() {
1101 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001102 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001103};
1104
1105/**
1106 * Set up the default tab stops, starting from a given column.
1107 *
1108 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001109 * from the specified column, or 0 if no column is provided. It also flags
1110 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001111 *
1112 * This does not clear the existing tab stops first, use clearAllTabStops
1113 * for that.
1114 *
1115 * @param {int} opt_start Optional starting zero based starting column, useful
1116 * for filling out missing tab stops when the terminal is resized.
1117 */
1118hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1119 var start = opt_start || 0;
1120 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001121 // Round start up to a default tab stop.
1122 start = start - 1 - ((start - 1) % w) + w;
1123 for (var i = start; i < this.screenSize.width; i += w) {
1124 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001125 }
David Benjamin66e954d2012-05-05 21:08:12 -04001126
1127 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001128};
1129
rginda6d397402012-01-17 10:58:29 -08001130/**
rginda8ba33642011-12-14 12:31:31 -08001131 * Interpret a sequence of characters.
1132 *
1133 * Incomplete escape sequences are buffered until the next call.
1134 *
1135 * @param {string} str Sequence of characters to interpret or pass through.
1136 */
1137hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001138 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001139 this.scheduleSyncCursorPosition_();
1140};
1141
1142/**
1143 * Take over the given DIV for use as the terminal display.
1144 *
1145 * @param {HTMLDivElement} div The div to use as the terminal display.
1146 */
1147hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001148 this.div_ = div;
1149
rginda8ba33642011-12-14 12:31:31 -08001150 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001151 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001152 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1153 this.scrollPort_.setBackgroundPosition(
1154 this.prefs_.get('background-position'));
Robert Gindae76aa9f2014-03-14 12:29:12 -07001155 this.scrollPort_.setUserCss(this.prefs_.get('user-css'));
rginda30f20f62012-04-05 16:36:19 -07001156
rginda0918b652012-04-04 11:26:24 -07001157 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001158
rginda9f5222b2012-03-05 11:53:28 -08001159 this.setFontSize(this.prefs_.get('font-size'));
1160 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001161
David Reveman8f552492012-03-28 12:18:41 -04001162 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1163
rginda8ba33642011-12-14 12:31:31 -08001164 this.document_ = this.scrollPort_.getDocument();
1165
rginda4bba5e12012-06-20 16:15:30 -07001166 this.document_.body.oncontextmenu = function() { return false };
1167
1168 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001169 var screenNode = this.scrollPort_.getScreenNode();
1170 screenNode.addEventListener('mousedown', onMouse);
1171 screenNode.addEventListener('mouseup', onMouse);
1172 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001173 this.scrollPort_.onScrollWheel = onMouse;
1174
Toni Barzic0bfa8922013-11-22 11:18:35 -08001175 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001176 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001177 // Listen for mousedown events on the screenNode as in FF the focus
1178 // events don't bubble.
1179 screenNode.addEventListener('mousedown', function() {
1180 setTimeout(this.onFocusChange_.bind(this, true));
1181 }.bind(this));
1182
Toni Barzic0bfa8922013-11-22 11:18:35 -08001183 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001184 'blur', this.onFocusChange_.bind(this, false));
1185
1186 var style = this.document_.createElement('style');
1187 style.textContent =
1188 ('.cursor-node[focus="false"] {' +
1189 ' box-sizing: border-box;' +
1190 ' background-color: transparent !important;' +
1191 ' border-width: 2px;' +
1192 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001193 '}' +
1194 '.wc-node {' +
1195 ' display: inline-block;' +
1196 ' text-align: center;' +
1197 ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001198 '}');
1199 this.document_.head.appendChild(style);
1200
Ricky Liang48f05cb2013-12-31 23:35:29 +08001201 var styleSheets = this.document_.styleSheets;
1202 var cssRules = styleSheets[styleSheets.length - 1].cssRules;
1203 this.wcCssRule_ = cssRules[cssRules.length - 1];
1204
rginda8ba33642011-12-14 12:31:31 -08001205 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001206 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001207 this.cursorNode_.style.cssText =
1208 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001209 'top: -99px;' +
1210 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001211 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1212 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
Rob Spies06533ba2014-04-24 11:20:37 -07001213 '-webkit-transition: opacity, background-color 100ms linear;' +
1214 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001215
rginda8e92a692012-05-20 19:37:20 -07001216 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001217 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1218 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001219
rginda8ba33642011-12-14 12:31:31 -08001220 this.document_.body.appendChild(this.cursorNode_);
1221
rgindad5613292012-06-19 15:40:37 -07001222 // When 'enableMouseDragScroll' is off we reposition this element directly
1223 // under the mouse cursor after a click. This makes Chrome associate
1224 // subsequent mousemove events with the scroll-blocker. Since the
1225 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1226 // events do not cause the scrollport to scroll.
1227 //
1228 // It's a hack, but it's the cleanest way I could find.
1229 this.scrollBlockerNode_ = this.document_.createElement('div');
1230 this.scrollBlockerNode_.style.cssText =
1231 ('position: absolute;' +
1232 'top: -99px;' +
1233 'display: block;' +
1234 'width: 10px;' +
1235 'height: 10px;');
1236 this.document_.body.appendChild(this.scrollBlockerNode_);
1237
1238 var onMouse = this.onMouse_.bind(this);
1239 this.scrollPort_.onScrollWheel = onMouse;
1240 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1241 ].forEach(function(event) {
1242 this.scrollBlockerNode_.addEventListener(event, onMouse);
1243 this.cursorNode_.addEventListener(event, onMouse);
1244 this.document_.addEventListener(event, onMouse);
1245 }.bind(this));
1246
1247 this.cursorNode_.addEventListener('mousedown', function() {
1248 setTimeout(this.focus.bind(this));
1249 }.bind(this));
1250
rginda8ba33642011-12-14 12:31:31 -08001251 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001252
rginda87b86462011-12-14 13:48:03 -08001253 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001254 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001255};
1256
rginda0918b652012-04-04 11:26:24 -07001257/**
1258 * Return the HTML document that contains the terminal DOM nodes.
1259 */
rginda87b86462011-12-14 13:48:03 -08001260hterm.Terminal.prototype.getDocument = function() {
1261 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001262};
1263
1264/**
rginda0918b652012-04-04 11:26:24 -07001265 * Focus the terminal.
1266 */
1267hterm.Terminal.prototype.focus = function() {
1268 this.scrollPort_.focus();
1269};
1270
1271/**
rginda8ba33642011-12-14 12:31:31 -08001272 * Return the HTML Element for a given row index.
1273 *
1274 * This is a method from the RowProvider interface. The ScrollPort uses
1275 * it to fetch rows on demand as they are scrolled into view.
1276 *
1277 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1278 * pairs to conserve memory.
1279 *
1280 * @param {integer} index The zero-based row index, measured relative to the
1281 * start of the scrollback buffer. On-screen rows will always have the
1282 * largest indicies.
1283 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1284 */
1285hterm.Terminal.prototype.getRowNode = function(index) {
1286 if (index < this.scrollbackRows_.length)
1287 return this.scrollbackRows_[index];
1288
1289 var screenIndex = index - this.scrollbackRows_.length;
1290 return this.screen_.rowsArray[screenIndex];
1291};
1292
1293/**
1294 * Return the text content for a given range of rows.
1295 *
1296 * This is a method from the RowProvider interface. The ScrollPort uses
1297 * it to fetch text content on demand when the user attempts to copy their
1298 * selection to the clipboard.
1299 *
1300 * @param {integer} start The zero-based row index to start from, measured
1301 * relative to the start of the scrollback buffer. On-screen rows will
1302 * always have the largest indicies.
1303 * @param {integer} end The zero-based row index to end on, measured
1304 * relative to the start of the scrollback buffer.
1305 * @return {string} A single string containing the text value of the range of
1306 * rows. Lines will be newline delimited, with no trailing newline.
1307 */
1308hterm.Terminal.prototype.getRowsText = function(start, end) {
1309 var ary = [];
1310 for (var i = start; i < end; i++) {
1311 var node = this.getRowNode(i);
1312 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001313 if (i < end - 1 && !node.getAttribute('line-overflow'))
1314 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001315 }
1316
rgindaa09e7332012-08-17 12:49:51 -07001317 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001318};
1319
1320/**
1321 * Return the text content for a given row.
1322 *
1323 * This is a method from the RowProvider interface. The ScrollPort uses
1324 * it to fetch text content on demand when the user attempts to copy their
1325 * selection to the clipboard.
1326 *
1327 * @param {integer} index The zero-based row index to return, measured
1328 * relative to the start of the scrollback buffer. On-screen rows will
1329 * always have the largest indicies.
1330 * @return {string} A string containing the text value of the selected row.
1331 */
1332hterm.Terminal.prototype.getRowText = function(index) {
1333 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001334 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001335};
1336
1337/**
1338 * Return the total number of rows in the addressable screen and in the
1339 * scrollback buffer of this terminal.
1340 *
1341 * This is a method from the RowProvider interface. The ScrollPort uses
1342 * it to compute the size of the scrollbar.
1343 *
1344 * @return {integer} The number of rows in this terminal.
1345 */
1346hterm.Terminal.prototype.getRowCount = function() {
1347 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1348};
1349
1350/**
1351 * Create DOM nodes for new rows and append them to the end of the terminal.
1352 *
1353 * This is the only correct way to add a new DOM node for a row. Notice that
1354 * the new row is appended to the bottom of the list of rows, and does not
1355 * require renumbering (of the rowIndex property) of previous rows.
1356 *
1357 * If you think you want a new blank row somewhere in the middle of the
1358 * terminal, look into moveRows_().
1359 *
1360 * This method does not pay attention to vtScrollTop/Bottom, since you should
1361 * be using moveRows() in cases where they would matter.
1362 *
1363 * The cursor will be positioned at column 0 of the first inserted line.
1364 */
1365hterm.Terminal.prototype.appendRows_ = function(count) {
1366 var cursorRow = this.screen_.rowsArray.length;
1367 var offset = this.scrollbackRows_.length + cursorRow;
1368 for (var i = 0; i < count; i++) {
1369 var row = this.document_.createElement('x-row');
1370 row.appendChild(this.document_.createTextNode(''));
1371 row.rowIndex = offset + i;
1372 this.screen_.pushRow(row);
1373 }
1374
1375 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1376 if (extraRows > 0) {
1377 var ary = this.screen_.shiftRows(extraRows);
1378 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001379 if (this.scrollPort_.isScrolledEnd)
1380 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001381 }
1382
1383 if (cursorRow >= this.screen_.rowsArray.length)
1384 cursorRow = this.screen_.rowsArray.length - 1;
1385
rginda87b86462011-12-14 13:48:03 -08001386 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001387};
1388
1389/**
1390 * Relocate rows from one part of the addressable screen to another.
1391 *
1392 * This is used to recycle rows during VT scrolls (those which are driven
1393 * by VT commands, rather than by the user manipulating the scrollbar.)
1394 *
1395 * In this case, the blank lines scrolled into the scroll region are made of
1396 * the nodes we scrolled off. These have their rowIndex properties carefully
1397 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001398 */
1399hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1400 var ary = this.screen_.removeRows(fromIndex, count);
1401 this.screen_.insertRows(toIndex, ary);
1402
1403 var start, end;
1404 if (fromIndex < toIndex) {
1405 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001406 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001407 } else {
1408 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001409 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001410 }
1411
1412 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001413 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001414};
1415
1416/**
1417 * Renumber the rowIndex property of the given range of rows.
1418 *
1419 * The start and end indicies are relative to the screen, not the scrollback.
1420 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001421 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001422 * no need to renumber scrollback rows.
1423 */
Robert Ginda40932892012-12-10 17:26:40 -08001424hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1425 var screen = opt_screen || this.screen_;
1426
rginda8ba33642011-12-14 12:31:31 -08001427 var offset = this.scrollbackRows_.length;
1428 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001429 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001430 }
1431};
1432
1433/**
1434 * Print a string to the terminal.
1435 *
1436 * This respects the current insert and wraparound modes. It will add new lines
1437 * to the end of the terminal, scrolling off the top into the scrollback buffer
1438 * if necessary.
1439 *
1440 * The string is *not* parsed for escape codes. Use the interpret() method if
1441 * that's what you're after.
1442 *
1443 * @param{string} str The string to print.
1444 */
1445hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001446 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001447
Ricky Liang48f05cb2013-12-31 23:35:29 +08001448 var strWidth = lib.wc.strWidth(str);
1449
1450 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001451 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1452 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001453 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001454 }
rgindaa19afe22012-01-25 15:40:22 -08001455
Ricky Liang48f05cb2013-12-31 23:35:29 +08001456 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001457 var didOverflow = false;
1458 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001459
rgindaa9abdd82012-08-06 18:05:09 -07001460 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1461 didOverflow = true;
1462 count = this.screenSize.width - this.screen_.cursorPosition.column;
1463 }
rgindaa19afe22012-01-25 15:40:22 -08001464
rgindaa9abdd82012-08-06 18:05:09 -07001465 if (didOverflow && !this.options_.wraparound) {
1466 // If the string overflowed the line but wraparound is off, then the
1467 // last printed character should be the last of the string.
1468 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001469 substr = lib.wc.substr(str, startOffset, count - 1) +
1470 lib.wc.substr(str, strWidth - 1);
1471 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001472 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001473 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001474 }
rgindaa19afe22012-01-25 15:40:22 -08001475
Ricky Liang48f05cb2013-12-31 23:35:29 +08001476 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1477 for (var i = 0; i < tokens.length; i++) {
1478 if (tokens[i].wcNode)
1479 this.screen_.textAttributes.wcNode = true;
1480
1481 if (this.options_.insertMode) {
1482 this.screen_.insertString(tokens[i].str);
1483 } else {
1484 this.screen_.overwriteString(tokens[i].str);
1485 }
1486 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001487 }
1488
1489 this.screen_.maybeClipCurrentRow();
1490 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001491 }
rginda8ba33642011-12-14 12:31:31 -08001492
1493 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001494
rginda9f5222b2012-03-05 11:53:28 -08001495 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001496 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001497};
1498
1499/**
rginda87b86462011-12-14 13:48:03 -08001500 * Set the VT scroll region.
1501 *
rginda87b86462011-12-14 13:48:03 -08001502 * This also resets the cursor position to the absolute (0, 0) position, since
1503 * that's what xterm appears to do.
1504 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001505 * Setting the scroll region to the full height of the terminal will clear
1506 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1507 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1508 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1509 * continue to work as most users would expect.
1510 *
rginda87b86462011-12-14 13:48:03 -08001511 * @param {integer} scrollTop The zero-based top of the scroll region.
1512 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1513 * inclusive.
1514 */
1515hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001516 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001517 this.vtScrollTop_ = null;
1518 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001519 } else {
1520 this.vtScrollTop_ = scrollTop;
1521 this.vtScrollBottom_ = scrollBottom;
1522 }
rginda87b86462011-12-14 13:48:03 -08001523};
1524
1525/**
rginda8ba33642011-12-14 12:31:31 -08001526 * Return the top row index according to the VT.
1527 *
1528 * This will return 0 unless the terminal has been told to restrict scrolling
1529 * to some lower row. It is used for some VT cursor positioning and scrolling
1530 * commands.
1531 *
1532 * @return {integer} The topmost row in the terminal's scroll region.
1533 */
1534hterm.Terminal.prototype.getVTScrollTop = function() {
1535 if (this.vtScrollTop_ != null)
1536 return this.vtScrollTop_;
1537
1538 return 0;
rginda87b86462011-12-14 13:48:03 -08001539};
rginda8ba33642011-12-14 12:31:31 -08001540
1541/**
1542 * Return the bottom row index according to the VT.
1543 *
1544 * This will return the height of the terminal unless the it has been told to
1545 * restrict scrolling to some higher row. It is used for some VT cursor
1546 * positioning and scrolling commands.
1547 *
1548 * @return {integer} The bottommost row in the terminal's scroll region.
1549 */
1550hterm.Terminal.prototype.getVTScrollBottom = function() {
1551 if (this.vtScrollBottom_ != null)
1552 return this.vtScrollBottom_;
1553
rginda87b86462011-12-14 13:48:03 -08001554 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001555}
1556
1557/**
1558 * Process a '\n' character.
1559 *
1560 * If the cursor is on the final row of the terminal this will append a new
1561 * blank row to the screen and scroll the topmost row into the scrollback
1562 * buffer.
1563 *
1564 * Otherwise, this moves the cursor to column zero of the next row.
1565 */
1566hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001567 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1568 this.screen_.rowsArray.length - 1);
1569
1570 if (this.vtScrollBottom_ != null) {
1571 // A VT Scroll region is active, we never append new rows.
1572 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1573 // We're at the end of the VT Scroll Region, perform a VT scroll.
1574 this.vtScrollUp(1);
1575 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1576 } else if (cursorAtEndOfScreen) {
1577 // We're at the end of the screen, the only thing to do is put the
1578 // cursor to column 0.
1579 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1580 } else {
1581 // Anywhere else, advance the cursor row, and reset the column.
1582 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1583 }
1584 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001585 // We're at the end of the screen. Append a new row to the terminal,
1586 // shifting the top row into the scrollback.
1587 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001588 } else {
rginda87b86462011-12-14 13:48:03 -08001589 // Anywhere else in the screen just moves the cursor.
1590 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001591 }
1592};
1593
1594/**
1595 * Like newLine(), except maintain the cursor column.
1596 */
1597hterm.Terminal.prototype.lineFeed = function() {
1598 var column = this.screen_.cursorPosition.column;
1599 this.newLine();
1600 this.setCursorColumn(column);
1601};
1602
1603/**
rginda87b86462011-12-14 13:48:03 -08001604 * If autoCarriageReturn is set then newLine(), else lineFeed().
1605 */
1606hterm.Terminal.prototype.formFeed = function() {
1607 if (this.options_.autoCarriageReturn) {
1608 this.newLine();
1609 } else {
1610 this.lineFeed();
1611 }
1612};
1613
1614/**
1615 * Move the cursor up one row, possibly inserting a blank line.
1616 *
1617 * The cursor column is not changed.
1618 */
1619hterm.Terminal.prototype.reverseLineFeed = function() {
1620 var scrollTop = this.getVTScrollTop();
1621 var currentRow = this.screen_.cursorPosition.row;
1622
1623 if (currentRow == scrollTop) {
1624 this.insertLines(1);
1625 } else {
1626 this.setAbsoluteCursorRow(currentRow - 1);
1627 }
1628};
1629
1630/**
rginda8ba33642011-12-14 12:31:31 -08001631 * Replace all characters to the left of the current cursor with the space
1632 * character.
1633 *
1634 * TODO(rginda): This should probably *remove* the characters (not just replace
1635 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001636 * position.
rginda8ba33642011-12-14 12:31:31 -08001637 */
1638hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001639 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001640 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001641 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001642 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001643};
1644
1645/**
David Benjamin684a9b72012-05-01 17:19:58 -04001646 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001647 *
1648 * The cursor position is unchanged.
1649 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001650 * If the current background color is not the default background color this
1651 * will insert spaces rather than delete. This is unfortunate because the
1652 * trailing space will affect text selection, but it's difficult to come up
1653 * with a way to style empty space that wouldn't trip up the hterm.Screen
1654 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001655 *
1656 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1657 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1658 * crbug.com/232390 for details.
rginda8ba33642011-12-14 12:31:31 -08001659 */
1660hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001661 if (this.screen_.cursorPosition.overflow)
1662 return;
1663
Robert Ginda7fd57082012-09-25 14:41:47 -07001664 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1665 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001666
1667 if (this.screen_.textAttributes.background ===
1668 this.screen_.textAttributes.DEFAULT_COLOR) {
1669 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001670 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001671 this.screen_.cursorPosition.column + count) {
1672 this.screen_.deleteChars(count);
1673 this.clearCursorOverflow();
1674 return;
1675 }
1676 }
1677
rginda87b86462011-12-14 13:48:03 -08001678 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001679 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001680 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001681 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001682};
1683
1684/**
1685 * Erase the current line.
1686 *
1687 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001688 */
1689hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001690 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001691 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001692 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001693 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001694};
1695
1696/**
David Benjamina08d78f2012-05-05 00:28:49 -04001697 * Erase all characters from the start of the screen to the current cursor
1698 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001699 *
1700 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001701 */
1702hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001703 var cursor = this.saveCursor();
1704
1705 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001706
David Benjamina08d78f2012-05-05 00:28:49 -04001707 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001708 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001709 this.screen_.clearCursorRow();
1710 }
1711
rginda87b86462011-12-14 13:48:03 -08001712 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001713 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001714};
1715
1716/**
1717 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001718 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001719 *
1720 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001721 */
1722hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001723 var cursor = this.saveCursor();
1724
1725 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001726
David Benjamina08d78f2012-05-05 00:28:49 -04001727 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001728 for (var i = cursor.row + 1; i <= bottom; i++) {
1729 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001730 this.screen_.clearCursorRow();
1731 }
1732
rginda87b86462011-12-14 13:48:03 -08001733 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001734 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001735};
1736
1737/**
1738 * Fill the terminal with a given character.
1739 *
1740 * This methods does not respect the VT scroll region.
1741 *
1742 * @param {string} ch The character to use for the fill.
1743 */
1744hterm.Terminal.prototype.fill = function(ch) {
1745 var cursor = this.saveCursor();
1746
1747 this.setAbsoluteCursorPosition(0, 0);
1748 for (var row = 0; row < this.screenSize.height; row++) {
1749 for (var col = 0; col < this.screenSize.width; col++) {
1750 this.setAbsoluteCursorPosition(row, col);
1751 this.screen_.overwriteString(ch);
1752 }
1753 }
1754
1755 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001756};
1757
1758/**
rginda9ea433c2012-03-16 11:57:00 -07001759 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001760 *
rginda9ea433c2012-03-16 11:57:00 -07001761 * This does not respect the scroll region.
1762 *
1763 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1764 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001765 */
rginda9ea433c2012-03-16 11:57:00 -07001766hterm.Terminal.prototype.clearHome = function(opt_screen) {
1767 var screen = opt_screen || this.screen_;
1768 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001769
rginda11057d52012-04-25 12:29:56 -07001770 if (bottom == 0) {
1771 // Empty screen, nothing to do.
1772 return;
1773 }
1774
rgindae4d29232012-01-19 10:47:13 -08001775 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001776 screen.setCursorPosition(i, 0);
1777 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001778 }
1779
rginda9ea433c2012-03-16 11:57:00 -07001780 screen.setCursorPosition(0, 0);
1781};
1782
1783/**
1784 * Erase the entire display without changing the cursor position.
1785 *
1786 * The cursor position is unchanged. This does not respect the scroll
1787 * region.
1788 *
1789 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1790 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001791 */
1792hterm.Terminal.prototype.clear = function(opt_screen) {
1793 var screen = opt_screen || this.screen_;
1794 var cursor = screen.cursorPosition.clone();
1795 this.clearHome(screen);
1796 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001797};
1798
1799/**
1800 * VT command to insert lines at the current cursor row.
1801 *
1802 * This respects the current scroll region. Rows pushed off the bottom are
1803 * lost (they won't show up in the scrollback buffer).
1804 *
rginda8ba33642011-12-14 12:31:31 -08001805 * @param {integer} count The number of lines to insert.
1806 */
1807hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001808 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001809
1810 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001811 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001812
Robert Ginda579186b2012-09-26 11:40:04 -07001813 // The moveCount is the number of rows we need to relocate to make room for
1814 // the new row(s). The count is the distance to move them.
1815 var moveCount = bottom - cursorRow - count + 1;
1816 if (moveCount)
1817 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001818
Robert Ginda579186b2012-09-26 11:40:04 -07001819 for (var i = count - 1; i >= 0; i--) {
1820 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001821 this.screen_.clearCursorRow();
1822 }
rginda8ba33642011-12-14 12:31:31 -08001823};
1824
1825/**
1826 * VT command to delete lines at the current cursor row.
1827 *
1828 * New rows are added to the bottom of scroll region to take their place. New
1829 * rows are strictly there to take up space and have no content or style.
1830 */
1831hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001832 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001833
rginda87b86462011-12-14 13:48:03 -08001834 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001835 var bottom = this.getVTScrollBottom();
1836
rginda87b86462011-12-14 13:48:03 -08001837 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001838 count = Math.min(count, maxCount);
1839
rginda87b86462011-12-14 13:48:03 -08001840 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001841 if (count != maxCount)
1842 this.moveRows_(top, count, moveStart);
1843
1844 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001845 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001846 this.screen_.clearCursorRow();
1847 }
1848
rginda87b86462011-12-14 13:48:03 -08001849 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001850 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001851};
1852
1853/**
1854 * Inserts the given number of spaces at the current cursor position.
1855 *
rginda87b86462011-12-14 13:48:03 -08001856 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001857 */
1858hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001859 var cursor = this.saveCursor();
1860
rgindacbbd7482012-06-13 15:06:16 -07001861 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001862 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001863 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001864
1865 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001866 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001867};
1868
1869/**
1870 * Forward-delete the specified number of characters starting at the cursor
1871 * position.
1872 *
1873 * @param {integer} count The number of characters to delete.
1874 */
1875hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001876 var deleted = this.screen_.deleteChars(count);
1877 if (deleted && !this.screen_.textAttributes.isDefault()) {
1878 var cursor = this.saveCursor();
1879 this.setCursorColumn(this.screenSize.width - deleted);
1880 this.screen_.insertString(lib.f.getWhitespace(deleted));
1881 this.restoreCursor(cursor);
1882 }
1883
David Benjamin54e8bf62012-06-01 22:31:40 -04001884 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001885};
1886
1887/**
1888 * Shift rows in the scroll region upwards by a given number of lines.
1889 *
1890 * New rows are inserted at the bottom of the scroll region to fill the
1891 * vacated rows. The new rows not filled out with the current text attributes.
1892 *
1893 * This function does not affect the scrollback rows at all. Rows shifted
1894 * off the top are lost.
1895 *
rginda87b86462011-12-14 13:48:03 -08001896 * The cursor position is not altered.
1897 *
rginda8ba33642011-12-14 12:31:31 -08001898 * @param {integer} count The number of rows to scroll.
1899 */
1900hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001901 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001902
rginda87b86462011-12-14 13:48:03 -08001903 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001904 this.deleteLines(count);
1905
rginda87b86462011-12-14 13:48:03 -08001906 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001907};
1908
1909/**
1910 * Shift rows below the cursor down by a given number of lines.
1911 *
1912 * This function respects the current scroll region.
1913 *
1914 * New rows are inserted at the top of the scroll region to fill the
1915 * vacated rows. The new rows not filled out with the current text attributes.
1916 *
1917 * This function does not affect the scrollback rows at all. Rows shifted
1918 * off the bottom are lost.
1919 *
1920 * @param {integer} count The number of rows to scroll.
1921 */
1922hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001923 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001924
rginda87b86462011-12-14 13:48:03 -08001925 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001926 this.insertLines(opt_count);
1927
rginda87b86462011-12-14 13:48:03 -08001928 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001929};
1930
rginda87b86462011-12-14 13:48:03 -08001931
rginda8ba33642011-12-14 12:31:31 -08001932/**
1933 * Set the cursor position.
1934 *
1935 * The cursor row is relative to the scroll region if the terminal has
1936 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1937 *
1938 * @param {integer} row The new zero-based cursor row.
1939 * @param {integer} row The new zero-based cursor column.
1940 */
1941hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1942 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001943 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001944 } else {
rginda87b86462011-12-14 13:48:03 -08001945 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001946 }
rginda87b86462011-12-14 13:48:03 -08001947};
rginda8ba33642011-12-14 12:31:31 -08001948
rginda87b86462011-12-14 13:48:03 -08001949hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1950 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001951 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1952 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001953 this.screen_.setCursorPosition(row, column);
1954};
1955
1956hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001957 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1958 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001959 this.screen_.setCursorPosition(row, column);
1960};
1961
1962/**
1963 * Set the cursor column.
1964 *
1965 * @param {integer} column The new zero-based cursor column.
1966 */
1967hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001968 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001969};
1970
1971/**
1972 * Return the cursor column.
1973 *
1974 * @return {integer} The zero-based cursor column.
1975 */
1976hterm.Terminal.prototype.getCursorColumn = function() {
1977 return this.screen_.cursorPosition.column;
1978};
1979
1980/**
1981 * Set the cursor row.
1982 *
1983 * The cursor row is relative to the scroll region if the terminal has
1984 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1985 *
1986 * @param {integer} row The new cursor row.
1987 */
rginda87b86462011-12-14 13:48:03 -08001988hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1989 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001990};
1991
1992/**
1993 * Return the cursor row.
1994 *
1995 * @return {integer} The zero-based cursor row.
1996 */
1997hterm.Terminal.prototype.getCursorRow = function(row) {
1998 return this.screen_.cursorPosition.row;
1999};
2000
2001/**
2002 * Request that the ScrollPort redraw itself soon.
2003 *
2004 * The redraw will happen asynchronously, soon after the call stack winds down.
2005 * Multiple calls will be coalesced into a single redraw.
2006 */
2007hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002008 if (this.timeouts_.redraw)
2009 return;
rginda8ba33642011-12-14 12:31:31 -08002010
2011 var self = this;
rginda87b86462011-12-14 13:48:03 -08002012 this.timeouts_.redraw = setTimeout(function() {
2013 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002014 self.scrollPort_.redraw_();
2015 }, 0);
2016};
2017
2018/**
2019 * Request that the ScrollPort be scrolled to the bottom.
2020 *
2021 * The scroll will happen asynchronously, soon after the call stack winds down.
2022 * Multiple calls will be coalesced into a single scroll.
2023 *
2024 * This affects the scrollbar position of the ScrollPort, and has nothing to
2025 * do with the VT scroll commands.
2026 */
2027hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2028 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002029 return;
rginda8ba33642011-12-14 12:31:31 -08002030
2031 var self = this;
2032 this.timeouts_.scrollDown = setTimeout(function() {
2033 delete self.timeouts_.scrollDown;
2034 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2035 }, 10);
2036};
2037
2038/**
2039 * Move the cursor up a specified number of rows.
2040 *
2041 * @param {integer} count The number of rows to move the cursor.
2042 */
2043hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002044 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002045};
2046
2047/**
2048 * Move the cursor down a specified number of rows.
2049 *
2050 * @param {integer} count The number of rows to move the cursor.
2051 */
2052hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002053 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002054 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2055 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2056 this.screenSize.height - 1);
2057
rgindacbbd7482012-06-13 15:06:16 -07002058 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002059 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002060 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002061};
2062
2063/**
2064 * Move the cursor left a specified number of columns.
2065 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002066 * If reverse wraparound mode is enabled and the previous row wrapped into
2067 * the current row then we back up through the wraparound as well.
2068 *
rginda8ba33642011-12-14 12:31:31 -08002069 * @param {integer} count The number of columns to move the cursor.
2070 */
2071hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002072 count = count || 1;
2073
2074 if (count < 1)
2075 return;
2076
2077 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002078 if (this.options_.reverseWraparound) {
2079 if (this.screen_.cursorPosition.overflow) {
2080 // If this cursor is in the right margin, consume one count to get it
2081 // back to the last column. This only applies when we're in reverse
2082 // wraparound mode.
2083 count--;
2084 this.clearCursorOverflow();
2085
2086 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002087 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002088 }
2089
Robert Gindabfb32622014-07-17 13:20:27 -07002090 var newRow = this.screen_.cursorPosition.row;
2091 var newColumn = currentColumn - count;
2092 if (newColumn < 0) {
2093 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2094 if (newRow < 0) {
2095 // xterm also wraps from row 0 to the last row.
2096 newRow = this.screenSize.height + newRow % this.screenSize.height;
2097 }
2098 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2099 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002100
Robert Gindabfb32622014-07-17 13:20:27 -07002101 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2102
2103 } else {
2104 var newColumn = Math.max(currentColumn - count, 0);
2105 this.setCursorColumn(newColumn);
2106 }
rginda8ba33642011-12-14 12:31:31 -08002107};
2108
2109/**
2110 * Move the cursor right a specified number of columns.
2111 *
2112 * @param {integer} count The number of columns to move the cursor.
2113 */
2114hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002115 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002116
2117 if (count < 1)
2118 return;
2119
rgindacbbd7482012-06-13 15:06:16 -07002120 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002121 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002122 this.setCursorColumn(column);
2123};
2124
2125/**
2126 * Reverse the foreground and background colors of the terminal.
2127 *
2128 * This only affects text that was drawn with no attributes.
2129 *
2130 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2131 * been drawn with attributes that happen to coincide with the default
2132 * 'no-attribute' colors. My guess is probably not.
2133 */
2134hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002135 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002136 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002137 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2138 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002139 } else {
rginda9f5222b2012-03-05 11:53:28 -08002140 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2141 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002142 }
2143};
2144
2145/**
rginda87b86462011-12-14 13:48:03 -08002146 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002147 *
2148 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002149 */
2150hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002151 this.cursorNode_.style.backgroundColor =
2152 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002153
2154 var self = this;
2155 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002156 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002157 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002158
Michael Kelly485ecd12014-06-09 11:41:56 -04002159 // bellSquelchTimeout_ affects both audio and notification bells.
2160 if (this.bellSquelchTimeout_)
2161 return;
2162
Robert Ginda92e18102013-03-14 13:56:37 -07002163 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002164 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002165 this.bellSequelchTimeout_ = setTimeout(function() {
2166 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002167 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002168 } else {
2169 delete this.bellSquelchTimeout_;
2170 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002171
2172 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
2173 var n = new Notification(
2174 lib.f.replaceVars(hterm.desktopNotificationTitle,
Robert Ginda348dc2b2014-06-24 14:42:23 -07002175 {'title': window.document.title || 'hterm'}));
Michael Kelly485ecd12014-06-09 11:41:56 -04002176 this.bellNotificationList_.push(n);
2177 // TODO: Should we try to raise the window here?
2178 n.onclick = function() { self.closeBellNotifications_(); };
2179 }
rginda87b86462011-12-14 13:48:03 -08002180};
2181
2182/**
rginda8ba33642011-12-14 12:31:31 -08002183 * Set the origin mode bit.
2184 *
2185 * If origin mode is on, certain VT cursor and scrolling commands measure their
2186 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2187 * to the top of the addressable screen.
2188 *
2189 * Defaults to off.
2190 *
2191 * @param {boolean} state True to set origin mode, false to unset.
2192 */
2193hterm.Terminal.prototype.setOriginMode = function(state) {
2194 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002195 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002196};
2197
2198/**
2199 * Set the insert mode bit.
2200 *
2201 * If insert mode is on, existing text beyond the cursor position will be
2202 * shifted right to make room for new text. Otherwise, new text overwrites
2203 * any existing text.
2204 *
2205 * Defaults to off.
2206 *
2207 * @param {boolean} state True to set insert mode, false to unset.
2208 */
2209hterm.Terminal.prototype.setInsertMode = function(state) {
2210 this.options_.insertMode = state;
2211};
2212
2213/**
rginda87b86462011-12-14 13:48:03 -08002214 * Set the auto carriage return bit.
2215 *
2216 * If auto carriage return is on then a formfeed character is interpreted
2217 * as a newline, otherwise it's the same as a linefeed. The difference boils
2218 * down to whether or not the cursor column is reset.
2219 */
2220hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2221 this.options_.autoCarriageReturn = state;
2222};
2223
2224/**
rginda8ba33642011-12-14 12:31:31 -08002225 * Set the wraparound mode bit.
2226 *
2227 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2228 * to the start of the following row. Otherwise, the cursor is clamped to the
2229 * end of the screen and attempts to write past it are ignored.
2230 *
2231 * Defaults to on.
2232 *
2233 * @param {boolean} state True to set wraparound mode, false to unset.
2234 */
2235hterm.Terminal.prototype.setWraparound = function(state) {
2236 this.options_.wraparound = state;
2237};
2238
2239/**
2240 * Set the reverse-wraparound mode bit.
2241 *
2242 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2243 * to the end of the previous row. Otherwise, the cursor is clamped to column
2244 * 0.
2245 *
2246 * Defaults to off.
2247 *
2248 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2249 */
2250hterm.Terminal.prototype.setReverseWraparound = function(state) {
2251 this.options_.reverseWraparound = state;
2252};
2253
2254/**
2255 * Selects between the primary and alternate screens.
2256 *
2257 * If alternate mode is on, the alternate screen is active. Otherwise the
2258 * primary screen is active.
2259 *
2260 * Swapping screens has no effect on the scrollback buffer.
2261 *
2262 * Each screen maintains its own cursor position.
2263 *
2264 * Defaults to off.
2265 *
2266 * @param {boolean} state True to set alternate mode, false to unset.
2267 */
2268hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002269 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002270 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2271
rginda35c456b2012-02-09 17:29:05 -08002272 if (this.screen_.rowsArray.length &&
2273 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2274 // If the screen changed sizes while we were away, our rowIndexes may
2275 // be incorrect.
2276 var offset = this.scrollbackRows_.length;
2277 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002278 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002279 ary[i].rowIndex = offset + i;
2280 }
2281 }
rginda8ba33642011-12-14 12:31:31 -08002282
rginda35c456b2012-02-09 17:29:05 -08002283 this.realizeWidth_(this.screenSize.width);
2284 this.realizeHeight_(this.screenSize.height);
2285 this.scrollPort_.syncScrollHeight();
2286 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002287
rginda6d397402012-01-17 10:58:29 -08002288 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002289 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002290};
2291
2292/**
2293 * Set the cursor-blink mode bit.
2294 *
2295 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2296 * a visible cursor does not blink.
2297 *
2298 * You should make sure to turn blinking off if you're going to dispose of a
2299 * terminal, otherwise you'll leak a timeout.
2300 *
2301 * Defaults to on.
2302 *
2303 * @param {boolean} state True to set cursor-blink mode, false to unset.
2304 */
2305hterm.Terminal.prototype.setCursorBlink = function(state) {
2306 this.options_.cursorBlink = state;
2307
2308 if (!state && this.timeouts_.cursorBlink) {
2309 clearTimeout(this.timeouts_.cursorBlink);
2310 delete this.timeouts_.cursorBlink;
2311 }
2312
2313 if (this.options_.cursorVisible)
2314 this.setCursorVisible(true);
2315};
2316
2317/**
2318 * Set the cursor-visible mode bit.
2319 *
2320 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2321 *
2322 * Defaults to on.
2323 *
2324 * @param {boolean} state True to set cursor-visible mode, false to unset.
2325 */
2326hterm.Terminal.prototype.setCursorVisible = function(state) {
2327 this.options_.cursorVisible = state;
2328
2329 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002330 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002331 return;
2332 }
2333
rginda87b86462011-12-14 13:48:03 -08002334 this.syncCursorPosition_();
2335
2336 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002337
2338 if (this.options_.cursorBlink) {
2339 if (this.timeouts_.cursorBlink)
2340 return;
2341
Robert Gindaea2183e2014-07-17 09:51:51 -07002342 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002343 } else {
2344 if (this.timeouts_.cursorBlink) {
2345 clearTimeout(this.timeouts_.cursorBlink);
2346 delete this.timeouts_.cursorBlink;
2347 }
2348 }
2349};
2350
2351/**
rginda87b86462011-12-14 13:48:03 -08002352 * Synchronizes the visible cursor and document selection with the current
2353 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002354 */
2355hterm.Terminal.prototype.syncCursorPosition_ = function() {
2356 var topRowIndex = this.scrollPort_.getTopRowIndex();
2357 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2358 var cursorRowIndex = this.scrollbackRows_.length +
2359 this.screen_.cursorPosition.row;
2360
2361 if (cursorRowIndex > bottomRowIndex) {
2362 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002363 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002364 return;
2365 }
2366
2367 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002368 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2369 'px';
2370 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2371 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002372
2373 this.cursorNode_.setAttribute('title',
2374 '(' + this.screen_.cursorPosition.row +
2375 ', ' + this.screen_.cursorPosition.column +
2376 ')');
2377
2378 // Update the caret for a11y purposes.
2379 var selection = this.document_.getSelection();
2380 if (selection && selection.isCollapsed)
2381 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002382};
2383
Robert Gindafb1be6a2013-12-11 11:56:22 -08002384/**
2385 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2386 * and character cell dimensions.
2387 */
Robert Ginda830583c2013-08-07 13:20:46 -07002388hterm.Terminal.prototype.restyleCursor_ = function() {
2389 var shape = this.cursorShape_;
2390
2391 if (this.cursorNode_.getAttribute('focus') == 'false') {
2392 // Always show a block cursor when unfocused.
2393 shape = hterm.Terminal.cursorShape.BLOCK;
2394 }
2395
2396 var style = this.cursorNode_.style;
2397
Robert Gindafb1be6a2013-12-11 11:56:22 -08002398 style.width = this.scrollPort_.characterSize.width + 'px';
2399
Robert Ginda830583c2013-08-07 13:20:46 -07002400 switch (shape) {
2401 case hterm.Terminal.cursorShape.BEAM:
2402 style.height = this.scrollPort_.characterSize.height + 'px';
2403 style.backgroundColor = 'transparent';
2404 style.borderBottomStyle = null;
2405 style.borderLeftStyle = 'solid';
2406 break;
2407
2408 case hterm.Terminal.cursorShape.UNDERLINE:
2409 style.height = this.scrollPort_.characterSize.baseline + 'px';
2410 style.backgroundColor = 'transparent';
2411 style.borderBottomStyle = 'solid';
2412 // correct the size to put it exactly at the baseline
2413 style.borderLeftStyle = null;
2414 break;
2415
2416 default:
2417 style.height = this.scrollPort_.characterSize.height + 'px';
2418 style.backgroundColor = this.cursorColor_;
2419 style.borderBottomStyle = null;
2420 style.borderLeftStyle = null;
2421 break;
2422 }
2423};
2424
rginda8ba33642011-12-14 12:31:31 -08002425/**
2426 * Synchronizes the visible cursor with the current cursor coordinates.
2427 *
2428 * The sync will happen asynchronously, soon after the call stack winds down.
2429 * Multiple calls will be coalesced into a single sync.
2430 */
2431hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2432 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002433 return;
rginda8ba33642011-12-14 12:31:31 -08002434
2435 var self = this;
2436 this.timeouts_.syncCursor = setTimeout(function() {
2437 self.syncCursorPosition_();
2438 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002439 }, 0);
2440};
2441
rgindacc2996c2012-02-24 14:59:31 -08002442/**
rgindaf522ce02012-04-17 17:49:17 -07002443 * Show or hide the zoom warning.
2444 *
2445 * The zoom warning is a message warning the user that their browser zoom must
2446 * be set to 100% in order for hterm to function properly.
2447 *
2448 * @param {boolean} state True to show the message, false to hide it.
2449 */
2450hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2451 if (!this.zoomWarningNode_) {
2452 if (!state)
2453 return;
2454
2455 this.zoomWarningNode_ = this.document_.createElement('div');
2456 this.zoomWarningNode_.style.cssText = (
2457 'color: black;' +
2458 'background-color: #ff2222;' +
2459 'font-size: large;' +
2460 'border-radius: 8px;' +
2461 'opacity: 0.75;' +
2462 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2463 'top: 0.5em;' +
2464 'right: 1.2em;' +
2465 'position: absolute;' +
2466 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002467 '-webkit-user-select: none;' +
2468 '-moz-text-size-adjust: none;' +
2469 '-moz-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002470 }
2471
Robert Gindab4839c22013-02-28 16:52:10 -08002472 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2473 hterm.zoomWarningMessage,
2474 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2475
rgindaf522ce02012-04-17 17:49:17 -07002476 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2477
2478 if (state) {
2479 if (!this.zoomWarningNode_.parentNode)
2480 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2481 } else if (this.zoomWarningNode_.parentNode) {
2482 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2483 }
2484};
2485
2486/**
rgindacc2996c2012-02-24 14:59:31 -08002487 * Show the terminal overlay for a given amount of time.
2488 *
2489 * The terminal overlay appears in inverse video in a large font, centered
2490 * over the terminal. You should probably keep the overlay message brief,
2491 * since it's in a large font and you probably aren't going to check the size
2492 * of the terminal first.
2493 *
2494 * @param {string} msg The text (not HTML) message to display in the overlay.
2495 * @param {number} opt_timeout The amount of time to wait before fading out
2496 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2497 * stay up forever (or until the next overlay).
2498 */
2499hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002500 if (!this.overlayNode_) {
2501 if (!this.div_)
2502 return;
2503
2504 this.overlayNode_ = this.document_.createElement('div');
2505 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002506 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002507 'font-size: xx-large;' +
2508 'opacity: 0.75;' +
2509 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2510 'position: absolute;' +
2511 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002512 '-webkit-transition: opacity 180ms ease-in;' +
2513 '-moz-user-select: none;' +
2514 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002515
2516 this.overlayNode_.addEventListener('mousedown', function(e) {
2517 e.preventDefault();
2518 e.stopPropagation();
2519 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002520 }
2521
rginda9f5222b2012-03-05 11:53:28 -08002522 this.overlayNode_.style.color = this.prefs_.get('background-color');
2523 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2524 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2525
rgindaf0090c92012-02-10 14:58:52 -08002526 this.overlayNode_.textContent = msg;
2527 this.overlayNode_.style.opacity = '0.75';
2528
2529 if (!this.overlayNode_.parentNode)
2530 this.div_.appendChild(this.overlayNode_);
2531
Robert Ginda97769282013-02-01 15:30:30 -08002532 var divSize = hterm.getClientSize(this.div_);
2533 var overlaySize = hterm.getClientSize(this.overlayNode_);
2534
Robert Ginda8a59f762014-07-23 11:29:55 -07002535 this.overlayNode_.style.top =
2536 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002537 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002538 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002539
2540 var self = this;
2541
2542 if (this.overlayTimeout_)
2543 clearTimeout(this.overlayTimeout_);
2544
rgindacc2996c2012-02-24 14:59:31 -08002545 if (opt_timeout === null)
2546 return;
2547
rgindaf0090c92012-02-10 14:58:52 -08002548 this.overlayTimeout_ = setTimeout(function() {
2549 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002550 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002551 if (self.overlayNode_.parentNode)
2552 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002553 self.overlayTimeout_ = null;
2554 self.overlayNode_.style.opacity = '0.75';
2555 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002556 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002557};
2558
rginda4bba5e12012-06-20 16:15:30 -07002559/**
2560 * Paste from the system clipboard to the terminal.
2561 */
2562hterm.Terminal.prototype.paste = function() {
2563 hterm.pasteFromClipboard(this.document_);
2564};
2565
2566/**
2567 * Copy a string to the system clipboard.
2568 *
2569 * Note: If there is a selected range in the terminal, it'll be cleared.
2570 */
2571hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002572 if (this.prefs_.get('enable-clipboard-notice'))
2573 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2574
rgindaa09e7332012-08-17 12:49:51 -07002575 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002576 copySource.textContent = str;
2577 copySource.style.cssText = (
2578 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002579 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002580 'position: absolute;' +
2581 'top: -99px');
2582
2583 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002584
rginda4bba5e12012-06-20 16:15:30 -07002585 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002586 var anchorNode = selection.anchorNode;
2587 var anchorOffset = selection.anchorOffset;
2588 var focusNode = selection.focusNode;
2589 var focusOffset = selection.focusOffset;
2590
rginda4bba5e12012-06-20 16:15:30 -07002591 selection.selectAllChildren(copySource);
2592
rgindaa09e7332012-08-17 12:49:51 -07002593 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002594
Rob Spies56953412014-04-28 14:09:47 -07002595 // IE doesn't support selection.extend. This means that the selection
2596 // won't return on IE.
Rob Spies0bec09b2014-06-06 15:58:09 -07002597 if (this.clearSelectionAfterCopy && selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002598 selection.collapse(anchorNode, anchorOffset);
2599 selection.extend(focusNode, focusOffset);
2600 }
rgindafaa74742012-08-21 13:34:03 -07002601
rginda4bba5e12012-06-20 16:15:30 -07002602 copySource.parentNode.removeChild(copySource);
2603};
2604
rgindaa09e7332012-08-17 12:49:51 -07002605hterm.Terminal.prototype.getSelectionText = function() {
2606 var selection = this.scrollPort_.selection;
2607 selection.sync();
2608
2609 if (selection.isCollapsed)
2610 return null;
2611
2612
2613 // Start offset measures from the beginning of the line.
2614 var startOffset = selection.startOffset;
2615 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002616
Robert Gindafdbb3f22012-09-06 20:23:06 -07002617 if (node.nodeName != 'X-ROW') {
2618 // If the selection doesn't start on an x-row node, then it must be
2619 // somewhere inside the x-row. Add any characters from previous siblings
2620 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002621
2622 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2623 // If node is the text node in a styled span, move up to the span node.
2624 node = node.parentNode;
2625 }
2626
Robert Gindafdbb3f22012-09-06 20:23:06 -07002627 while (node.previousSibling) {
2628 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002629 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002630 }
rgindaa09e7332012-08-17 12:49:51 -07002631 }
2632
2633 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002634 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2635 selection.endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002636 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002637
Robert Gindafdbb3f22012-09-06 20:23:06 -07002638 if (node.nodeName != 'X-ROW') {
2639 // If the selection doesn't end on an x-row node, then it must be
2640 // somewhere inside the x-row. Add any characters from following siblings
2641 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002642
2643 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2644 // If node is the text node in a styled span, move up to the span node.
2645 node = node.parentNode;
2646 }
2647
Robert Gindafdbb3f22012-09-06 20:23:06 -07002648 while (node.nextSibling) {
2649 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002650 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002651 }
rgindaa09e7332012-08-17 12:49:51 -07002652 }
2653
2654 var rv = this.getRowsText(selection.startRow.rowIndex,
2655 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002656 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002657};
2658
rginda4bba5e12012-06-20 16:15:30 -07002659/**
2660 * Copy the current selection to the system clipboard, then clear it after a
2661 * short delay.
2662 */
2663hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002664 var text = this.getSelectionText();
2665 if (text != null)
2666 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002667};
2668
rgindaf0090c92012-02-10 14:58:52 -08002669hterm.Terminal.prototype.overlaySize = function() {
2670 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2671};
2672
rginda87b86462011-12-14 13:48:03 -08002673/**
2674 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2675 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002676 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002677 */
2678hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002679 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002680 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2681
Robert Ginda8cb7d902013-06-20 14:37:18 -07002682 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002683};
2684
2685/**
rgindad5613292012-06-19 15:40:37 -07002686 * Add the terminalRow and terminalColumn properties to mouse events and
2687 * then forward on to onMouse().
2688 *
2689 * The terminalRow and terminalColumn properties contain the (row, column)
2690 * coordinates for the mouse event.
2691 */
2692hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002693 if (e.processedByTerminalHandler_) {
2694 // We register our event handlers on the document, as well as the cursor
2695 // and the scroll blocker. Mouse events that occur on the cursor or
2696 // scroll blocker will also appear on the document, but we don't want to
2697 // process them twice.
2698 //
2699 // We can't just prevent bubbling because that has other side effects, so
2700 // we decorate the event object with this property instead.
2701 return;
2702 }
2703
2704 e.processedByTerminalHandler_ = true;
2705
Robert Gindaeda48db2014-07-17 09:25:30 -07002706 // One based row/column stored on the mouse event.
2707 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2708 this.scrollPort_.characterSize.height) + 1;
2709 e.terminalColumn = parseInt(e.clientX /
2710 this.scrollPort_.characterSize.width) + 1;
2711
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002712 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2713 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002714 return;
2715 }
2716
Robert Gindaeda48db2014-07-17 09:25:30 -07002717 if (this.options_.cursorVisible) {
2718 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
2719 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
2720 this.cursorNode_.style.display = 'none';
2721 } else if (this.cursorNode_.style.display == 'none') {
2722 this.cursorNode_.style.display = '';
2723 }
2724 }
rgindad5613292012-06-19 15:40:37 -07002725
Robert Ginda928cf632014-03-05 15:07:41 -08002726 if (e.type == 'mousedown') {
2727 if (e.altKey || this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED) {
2728 // If VT mouse reporting is disabled, or has been defeated with
2729 // alt-mousedown, then the mouse will act on the local selection.
2730 this.reportMouseEvents_ = false;
2731 this.setSelectionEnabled(true);
2732 } else {
2733 // Otherwise we defer ownership of the mouse to the VT.
2734 this.reportMouseEvents_ = true;
Robert Ginda3ae37822014-05-15 13:05:35 -07002735 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08002736 this.setSelectionEnabled(false);
2737 e.preventDefault();
2738 }
2739 }
2740
2741 if (!this.reportMouseEvents_) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002742 if (e.type == 'dblclick') {
2743 this.screen_.expandSelection(this.document_.getSelection());
2744 hterm.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07002745 }
2746
Robert Ginda928cf632014-03-05 15:07:41 -08002747 if (e.type == 'mousedown' && e.which == this.mousePasteButton)
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002748 this.paste();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002749
2750 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2751 !this.document_.getSelection().isCollapsed) {
2752 hterm.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002753 }
2754
2755 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
2756 this.scrollBlockerNode_.engaged) {
2757 // Disengage the scroll-blocker after one of these events.
2758 this.scrollBlockerNode_.engaged = false;
2759 this.scrollBlockerNode_.style.top = '-99px';
2760 }
2761
Robert Ginda928cf632014-03-05 15:07:41 -08002762 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002763 if (!this.scrollBlockerNode_.engaged) {
2764 if (e.type == 'mousedown') {
2765 // Move the scroll-blocker into place if we want to keep the scrollport
2766 // from scrolling.
2767 this.scrollBlockerNode_.engaged = true;
2768 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2769 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2770 } else if (e.type == 'mousemove') {
2771 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
2772 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07002773 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002774 e.preventDefault();
2775 }
2776 }
Robert Ginda928cf632014-03-05 15:07:41 -08002777
2778 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002779 }
2780
Robert Ginda928cf632014-03-05 15:07:41 -08002781 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
2782 // Restore this on mouseup in case it was temporarily defeated with a
2783 // alt-mousedown. Only do this when the selection is empty so that
2784 // we don't immediately kill the users selection.
2785 this.reportMouseEvents_ = (this.vt.mouseReport !=
2786 this.vt.MOUSE_REPORT_DISABLED);
2787 }
rgindad5613292012-06-19 15:40:37 -07002788};
2789
2790/**
2791 * Clients should override this if they care to know about mouse events.
2792 *
2793 * The event parameter will be a normal DOM mouse click event with additional
2794 * 'terminalRow' and 'terminalColumn' properties.
2795 */
2796hterm.Terminal.prototype.onMouse = function(e) { };
2797
2798/**
rginda8e92a692012-05-20 19:37:20 -07002799 * React when focus changes.
2800 */
Rob Spies06533ba2014-04-24 11:20:37 -07002801hterm.Terminal.prototype.onFocusChange_ = function(focused) {
2802 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07002803 this.restyleCursor_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002804 if (focused === true)
2805 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07002806};
2807
2808/**
rginda8ba33642011-12-14 12:31:31 -08002809 * React when the ScrollPort is scrolled.
2810 */
2811hterm.Terminal.prototype.onScroll_ = function() {
2812 this.scheduleSyncCursorPosition_();
2813};
2814
2815/**
rginda9846e2f2012-01-27 13:53:33 -08002816 * React when text is pasted into the scrollPort.
2817 */
2818hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07002819 var data = e.text.replace(/\n/mg, '\r');
2820 if (this.options_.bracketedPaste)
2821 data = '\x1b[200~' + data + '\x1b[201~';
2822
2823 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08002824};
2825
2826/**
rgindaa09e7332012-08-17 12:49:51 -07002827 * React when the user tries to copy from the scrollPort.
2828 */
2829hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07002830 if (!this.useDefaultWindowCopy) {
2831 e.preventDefault();
2832 setTimeout(this.copySelectionToClipboard.bind(this), 0);
2833 }
rgindaa09e7332012-08-17 12:49:51 -07002834};
2835
2836/**
rginda8ba33642011-12-14 12:31:31 -08002837 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002838 *
2839 * Note: This function should not directly contain code that alters the internal
2840 * state of the terminal. That kind of code belongs in realizeWidth or
2841 * realizeHeight, so that it can be executed synchronously in the case of a
2842 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002843 */
2844hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08002845 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002846 this.scrollPort_.characterSize.width);
Robert Ginda19f61292014-03-04 14:07:57 -08002847 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
rginda35c456b2012-02-09 17:29:05 -08002848 this.scrollPort_.characterSize.height);
2849
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002850 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002851 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002852 // gets removed from the document or during the initial load, and we can't
2853 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002854 return;
2855 }
2856
rgindaa8ba17d2012-08-15 14:41:10 -07002857 var isNewSize = (columnCount != this.screenSize.width ||
2858 rowCount != this.screenSize.height);
2859
2860 // We do this even if the size didn't change, just to be sure everything is
2861 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002862 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002863 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002864
2865 if (isNewSize)
2866 this.overlaySize();
2867
Robert Gindafb1be6a2013-12-11 11:56:22 -08002868 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07002869 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002870};
2871
2872/**
2873 * Service the cursor blink timeout.
2874 */
2875hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07002876 if (!this.options_.cursorBlink) {
2877 delete this.timeouts_.cursorBlink;
2878 return;
2879 }
2880
Robert Ginda830583c2013-08-07 13:20:46 -07002881 if (this.cursorNode_.getAttribute('focus') == 'false' ||
2882 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08002883 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07002884 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
2885 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08002886 } else {
rginda87b86462011-12-14 13:48:03 -08002887 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07002888 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
2889 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08002890 }
2891};
David Reveman8f552492012-03-28 12:18:41 -04002892
2893/**
2894 * Set the scrollbar-visible mode bit.
2895 *
2896 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2897 * Otherwise it will not.
2898 *
2899 * Defaults to on.
2900 *
2901 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2902 */
2903hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2904 this.scrollPort_.setScrollbarVisible(state);
2905};
Michael Kelly485ecd12014-06-09 11:41:56 -04002906
2907/**
2908 * Close all web notifications created by terminal bells.
2909 */
2910hterm.Terminal.prototype.closeBellNotifications_ = function() {
2911 this.bellNotificationList_.forEach(function(n) {
2912 n.close();
2913 });
2914 this.bellNotificationList_.length = 0;
2915};