blob: 65abab6cd5612d40612f05b24330e299e6cdc6d5 [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
Robert Ginda26806d12014-07-24 13:44:07 -0700799 this.div_.style.width = Math.ceil(
800 this.scrollPort_.characterSize.width *
801 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400802 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800803 this.scheduleSyncCursorPosition_();
804};
rginda87b86462011-12-14 13:48:03 -0800805
rgindac9bc5502012-01-18 11:48:44 -0800806/**
rginda35c456b2012-02-09 17:29:05 -0800807 * Set the height of the terminal, resizing the UI to match.
808 */
809hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800810 if (rowCount == null) {
811 this.div_.style.height = '100%';
812 return;
813 }
814
rginda35c456b2012-02-09 17:29:05 -0800815 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700816 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800817 this.realizeSize_(this.screenSize.width, rowCount);
818 this.scheduleSyncCursorPosition_();
819};
820
821/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400822 * Deal with terminal size changes.
823 *
824 */
825hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
826 if (columnCount != this.screenSize.width)
827 this.realizeWidth_(columnCount);
828
829 if (rowCount != this.screenSize.height)
830 this.realizeHeight_(rowCount);
831
832 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700833 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400834};
835
836/**
rgindac9bc5502012-01-18 11:48:44 -0800837 * Deal with terminal width changes.
838 *
839 * This function does what needs to be done when the terminal width changes
840 * out from under us. It happens here rather than in onResize_() because this
841 * code may need to run synchronously to handle programmatic changes of
842 * terminal width.
843 *
844 * Relying on the browser to send us an async resize event means we may not be
845 * in the correct state yet when the next escape sequence hits.
846 */
847hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700848 if (columnCount <= 0)
849 throw new Error('Attempt to realize bad width: ' + columnCount);
850
rgindac9bc5502012-01-18 11:48:44 -0800851 var deltaColumns = columnCount - this.screen_.getWidth();
852
rginda87b86462011-12-14 13:48:03 -0800853 this.screenSize.width = columnCount;
854 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800855
856 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400857 if (this.defaultTabStops)
858 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800859 } else {
860 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400861 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800862 break;
863
864 this.tabStops_.pop();
865 }
866 }
867
868 this.screen_.setColumnCount(this.screenSize.width);
869};
870
871/**
872 * Deal with terminal height changes.
873 *
874 * This function does what needs to be done when the terminal height changes
875 * out from under us. It happens here rather than in onResize_() because this
876 * code may need to run synchronously to handle programmatic changes of
877 * terminal height.
878 *
879 * Relying on the browser to send us an async resize event means we may not be
880 * in the correct state yet when the next escape sequence hits.
881 */
882hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700883 if (rowCount <= 0)
884 throw new Error('Attempt to realize bad height: ' + rowCount);
885
rgindac9bc5502012-01-18 11:48:44 -0800886 var deltaRows = rowCount - this.screen_.getHeight();
887
888 this.screenSize.height = rowCount;
889
890 var cursor = this.saveCursor();
891
892 if (deltaRows < 0) {
893 // Screen got smaller.
894 deltaRows *= -1;
895 while (deltaRows) {
896 var lastRow = this.getRowCount() - 1;
897 if (lastRow - this.scrollbackRows_.length == cursor.row)
898 break;
899
900 if (this.getRowText(lastRow))
901 break;
902
903 this.screen_.popRow();
904 deltaRows--;
905 }
906
907 var ary = this.screen_.shiftRows(deltaRows);
908 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
909
910 // We just removed rows from the top of the screen, we need to update
911 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800912 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800913 } else if (deltaRows > 0) {
914 // Screen got larger.
915
916 if (deltaRows <= this.scrollbackRows_.length) {
917 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
918 var rows = this.scrollbackRows_.splice(
919 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
920 this.screen_.unshiftRows(rows);
921 deltaRows -= scrollbackCount;
922 cursor.row += scrollbackCount;
923 }
924
925 if (deltaRows)
926 this.appendRows_(deltaRows);
927 }
928
rginda35c456b2012-02-09 17:29:05 -0800929 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800930 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800931};
932
933/**
934 * Scroll the terminal to the top of the scrollback buffer.
935 */
936hterm.Terminal.prototype.scrollHome = function() {
937 this.scrollPort_.scrollRowToTop(0);
938};
939
940/**
941 * Scroll the terminal to the end.
942 */
943hterm.Terminal.prototype.scrollEnd = function() {
944 this.scrollPort_.scrollRowToBottom(this.getRowCount());
945};
946
947/**
948 * Scroll the terminal one page up (minus one line) relative to the current
949 * position.
950 */
951hterm.Terminal.prototype.scrollPageUp = function() {
952 var i = this.scrollPort_.getTopRowIndex();
953 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
954};
955
956/**
957 * Scroll the terminal one page down (minus one line) relative to the current
958 * position.
959 */
960hterm.Terminal.prototype.scrollPageDown = function() {
961 var i = this.scrollPort_.getTopRowIndex();
962 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800963};
964
rgindac9bc5502012-01-18 11:48:44 -0800965/**
Robert Ginda40932892012-12-10 17:26:40 -0800966 * Clear primary screen, secondary screen, and the scrollback buffer.
967 */
968hterm.Terminal.prototype.wipeContents = function() {
969 this.scrollbackRows_.length = 0;
970 this.scrollPort_.resetCache();
971
972 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
973 var bottom = screen.getHeight();
974 if (bottom > 0) {
975 this.renumberRows_(0, bottom);
976 this.clearHome(screen);
977 }
978 }.bind(this));
979
980 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -0700981 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -0800982};
983
984/**
rgindac9bc5502012-01-18 11:48:44 -0800985 * Full terminal reset.
986 */
rginda87b86462011-12-14 13:48:03 -0800987hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800988 this.clearAllTabStops();
989 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700990
991 this.clearHome(this.primaryScreen_);
992 this.primaryScreen_.textAttributes.reset();
993
994 this.clearHome(this.alternateScreen_);
995 this.alternateScreen_.textAttributes.reset();
996
rgindab8bc8932012-04-27 12:45:03 -0700997 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
998
Robert Ginda92e18102013-03-14 13:56:37 -0700999 this.vt.reset();
1000
rgindac9bc5502012-01-18 11:48:44 -08001001 this.softReset();
rginda87b86462011-12-14 13:48:03 -08001002};
1003
rgindac9bc5502012-01-18 11:48:44 -08001004/**
1005 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001006 *
1007 * Perform a soft reset to the default values listed in
1008 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001009 */
rginda0f5c0292012-01-13 11:00:13 -08001010hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -07001011 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001012 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001013
rgindab8bc8932012-04-27 12:45:03 -07001014 // Xterm also resets the color palette on soft reset, even though it doesn't
1015 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -07001016 this.primaryScreen_.textAttributes.resetColorPalette();
1017 this.alternateScreen_.textAttributes.resetColorPalette();
1018
rgindab8bc8932012-04-27 12:45:03 -07001019 // The xterm man page explicitly says this will happen on soft reset.
1020 this.setVTScrollRegion(null, null);
1021
1022 // Xterm also shows the cursor on soft reset, but does not alter the blink
1023 // state.
rgindaa19afe22012-01-25 15:40:22 -08001024 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001025};
1026
rgindac9bc5502012-01-18 11:48:44 -08001027/**
1028 * Move the cursor forward to the next tab stop, or to the last column
1029 * if no more tab stops are set.
1030 */
1031hterm.Terminal.prototype.forwardTabStop = function() {
1032 var column = this.screen_.cursorPosition.column;
1033
1034 for (var i = 0; i < this.tabStops_.length; i++) {
1035 if (this.tabStops_[i] > column) {
1036 this.setCursorColumn(this.tabStops_[i]);
1037 return;
1038 }
1039 }
1040
David Benjamin66e954d2012-05-05 21:08:12 -04001041 // xterm does not clear the overflow flag on HT or CHT.
1042 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001043 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001044 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001045};
1046
rgindac9bc5502012-01-18 11:48:44 -08001047/**
1048 * Move the cursor backward to the previous tab stop, or to the first column
1049 * if no previous tab stops are set.
1050 */
1051hterm.Terminal.prototype.backwardTabStop = function() {
1052 var column = this.screen_.cursorPosition.column;
1053
1054 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1055 if (this.tabStops_[i] < column) {
1056 this.setCursorColumn(this.tabStops_[i]);
1057 return;
1058 }
1059 }
1060
1061 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001062};
1063
rgindac9bc5502012-01-18 11:48:44 -08001064/**
1065 * Set a tab stop at the given column.
1066 *
1067 * @param {int} column Zero based column.
1068 */
1069hterm.Terminal.prototype.setTabStop = function(column) {
1070 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1071 if (this.tabStops_[i] == column)
1072 return;
1073
1074 if (this.tabStops_[i] < column) {
1075 this.tabStops_.splice(i + 1, 0, column);
1076 return;
1077 }
1078 }
1079
1080 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001081};
1082
rgindac9bc5502012-01-18 11:48:44 -08001083/**
1084 * Clear the tab stop at the current cursor position.
1085 *
1086 * No effect if there is no tab stop at the current cursor position.
1087 */
1088hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1089 var column = this.screen_.cursorPosition.column;
1090
1091 var i = this.tabStops_.indexOf(column);
1092 if (i == -1)
1093 return;
1094
1095 this.tabStops_.splice(i, 1);
1096};
1097
1098/**
1099 * Clear all tab stops.
1100 */
1101hterm.Terminal.prototype.clearAllTabStops = function() {
1102 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001103 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001104};
1105
1106/**
1107 * Set up the default tab stops, starting from a given column.
1108 *
1109 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001110 * from the specified column, or 0 if no column is provided. It also flags
1111 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001112 *
1113 * This does not clear the existing tab stops first, use clearAllTabStops
1114 * for that.
1115 *
1116 * @param {int} opt_start Optional starting zero based starting column, useful
1117 * for filling out missing tab stops when the terminal is resized.
1118 */
1119hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1120 var start = opt_start || 0;
1121 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001122 // Round start up to a default tab stop.
1123 start = start - 1 - ((start - 1) % w) + w;
1124 for (var i = start; i < this.screenSize.width; i += w) {
1125 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001126 }
David Benjamin66e954d2012-05-05 21:08:12 -04001127
1128 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001129};
1130
rginda6d397402012-01-17 10:58:29 -08001131/**
rginda8ba33642011-12-14 12:31:31 -08001132 * Interpret a sequence of characters.
1133 *
1134 * Incomplete escape sequences are buffered until the next call.
1135 *
1136 * @param {string} str Sequence of characters to interpret or pass through.
1137 */
1138hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001139 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001140 this.scheduleSyncCursorPosition_();
1141};
1142
1143/**
1144 * Take over the given DIV for use as the terminal display.
1145 *
1146 * @param {HTMLDivElement} div The div to use as the terminal display.
1147 */
1148hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001149 this.div_ = div;
1150
rginda8ba33642011-12-14 12:31:31 -08001151 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001152 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001153 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1154 this.scrollPort_.setBackgroundPosition(
1155 this.prefs_.get('background-position'));
Robert Gindae76aa9f2014-03-14 12:29:12 -07001156 this.scrollPort_.setUserCss(this.prefs_.get('user-css'));
rginda30f20f62012-04-05 16:36:19 -07001157
rginda0918b652012-04-04 11:26:24 -07001158 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001159
rginda9f5222b2012-03-05 11:53:28 -08001160 this.setFontSize(this.prefs_.get('font-size'));
1161 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001162
David Reveman8f552492012-03-28 12:18:41 -04001163 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1164
rginda8ba33642011-12-14 12:31:31 -08001165 this.document_ = this.scrollPort_.getDocument();
1166
rginda4bba5e12012-06-20 16:15:30 -07001167 this.document_.body.oncontextmenu = function() { return false };
1168
1169 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001170 var screenNode = this.scrollPort_.getScreenNode();
1171 screenNode.addEventListener('mousedown', onMouse);
1172 screenNode.addEventListener('mouseup', onMouse);
1173 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001174 this.scrollPort_.onScrollWheel = onMouse;
1175
Toni Barzic0bfa8922013-11-22 11:18:35 -08001176 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001177 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001178 // Listen for mousedown events on the screenNode as in FF the focus
1179 // events don't bubble.
1180 screenNode.addEventListener('mousedown', function() {
1181 setTimeout(this.onFocusChange_.bind(this, true));
1182 }.bind(this));
1183
Toni Barzic0bfa8922013-11-22 11:18:35 -08001184 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001185 'blur', this.onFocusChange_.bind(this, false));
1186
1187 var style = this.document_.createElement('style');
1188 style.textContent =
1189 ('.cursor-node[focus="false"] {' +
1190 ' box-sizing: border-box;' +
1191 ' background-color: transparent !important;' +
1192 ' border-width: 2px;' +
1193 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001194 '}' +
1195 '.wc-node {' +
1196 ' display: inline-block;' +
1197 ' text-align: center;' +
1198 ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001199 '}');
1200 this.document_.head.appendChild(style);
1201
Ricky Liang48f05cb2013-12-31 23:35:29 +08001202 var styleSheets = this.document_.styleSheets;
1203 var cssRules = styleSheets[styleSheets.length - 1].cssRules;
1204 this.wcCssRule_ = cssRules[cssRules.length - 1];
1205
rginda8ba33642011-12-14 12:31:31 -08001206 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001207 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001208 this.cursorNode_.style.cssText =
1209 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001210 'top: -99px;' +
1211 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001212 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1213 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
Rob Spies06533ba2014-04-24 11:20:37 -07001214 '-webkit-transition: opacity, background-color 100ms linear;' +
1215 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001216
rginda8e92a692012-05-20 19:37:20 -07001217 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001218 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1219 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001220
rginda8ba33642011-12-14 12:31:31 -08001221 this.document_.body.appendChild(this.cursorNode_);
1222
rgindad5613292012-06-19 15:40:37 -07001223 // When 'enableMouseDragScroll' is off we reposition this element directly
1224 // under the mouse cursor after a click. This makes Chrome associate
1225 // subsequent mousemove events with the scroll-blocker. Since the
1226 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1227 // events do not cause the scrollport to scroll.
1228 //
1229 // It's a hack, but it's the cleanest way I could find.
1230 this.scrollBlockerNode_ = this.document_.createElement('div');
1231 this.scrollBlockerNode_.style.cssText =
1232 ('position: absolute;' +
1233 'top: -99px;' +
1234 'display: block;' +
1235 'width: 10px;' +
1236 'height: 10px;');
1237 this.document_.body.appendChild(this.scrollBlockerNode_);
1238
1239 var onMouse = this.onMouse_.bind(this);
1240 this.scrollPort_.onScrollWheel = onMouse;
1241 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1242 ].forEach(function(event) {
1243 this.scrollBlockerNode_.addEventListener(event, onMouse);
1244 this.cursorNode_.addEventListener(event, onMouse);
1245 this.document_.addEventListener(event, onMouse);
1246 }.bind(this));
1247
1248 this.cursorNode_.addEventListener('mousedown', function() {
1249 setTimeout(this.focus.bind(this));
1250 }.bind(this));
1251
rginda8ba33642011-12-14 12:31:31 -08001252 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001253
rginda87b86462011-12-14 13:48:03 -08001254 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001255 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001256};
1257
rginda0918b652012-04-04 11:26:24 -07001258/**
1259 * Return the HTML document that contains the terminal DOM nodes.
1260 */
rginda87b86462011-12-14 13:48:03 -08001261hterm.Terminal.prototype.getDocument = function() {
1262 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001263};
1264
1265/**
rginda0918b652012-04-04 11:26:24 -07001266 * Focus the terminal.
1267 */
1268hterm.Terminal.prototype.focus = function() {
1269 this.scrollPort_.focus();
1270};
1271
1272/**
rginda8ba33642011-12-14 12:31:31 -08001273 * Return the HTML Element for a given row index.
1274 *
1275 * This is a method from the RowProvider interface. The ScrollPort uses
1276 * it to fetch rows on demand as they are scrolled into view.
1277 *
1278 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1279 * pairs to conserve memory.
1280 *
1281 * @param {integer} index The zero-based row index, measured relative to the
1282 * start of the scrollback buffer. On-screen rows will always have the
1283 * largest indicies.
1284 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1285 */
1286hterm.Terminal.prototype.getRowNode = function(index) {
1287 if (index < this.scrollbackRows_.length)
1288 return this.scrollbackRows_[index];
1289
1290 var screenIndex = index - this.scrollbackRows_.length;
1291 return this.screen_.rowsArray[screenIndex];
1292};
1293
1294/**
1295 * Return the text content for a given range of rows.
1296 *
1297 * This is a method from the RowProvider interface. The ScrollPort uses
1298 * it to fetch text content on demand when the user attempts to copy their
1299 * selection to the clipboard.
1300 *
1301 * @param {integer} start The zero-based row index to start from, measured
1302 * relative to the start of the scrollback buffer. On-screen rows will
1303 * always have the largest indicies.
1304 * @param {integer} end The zero-based row index to end on, measured
1305 * relative to the start of the scrollback buffer.
1306 * @return {string} A single string containing the text value of the range of
1307 * rows. Lines will be newline delimited, with no trailing newline.
1308 */
1309hterm.Terminal.prototype.getRowsText = function(start, end) {
1310 var ary = [];
1311 for (var i = start; i < end; i++) {
1312 var node = this.getRowNode(i);
1313 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001314 if (i < end - 1 && !node.getAttribute('line-overflow'))
1315 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001316 }
1317
rgindaa09e7332012-08-17 12:49:51 -07001318 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001319};
1320
1321/**
1322 * Return the text content for a given row.
1323 *
1324 * This is a method from the RowProvider interface. The ScrollPort uses
1325 * it to fetch text content on demand when the user attempts to copy their
1326 * selection to the clipboard.
1327 *
1328 * @param {integer} index The zero-based row index to return, measured
1329 * relative to the start of the scrollback buffer. On-screen rows will
1330 * always have the largest indicies.
1331 * @return {string} A string containing the text value of the selected row.
1332 */
1333hterm.Terminal.prototype.getRowText = function(index) {
1334 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001335 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001336};
1337
1338/**
1339 * Return the total number of rows in the addressable screen and in the
1340 * scrollback buffer of this terminal.
1341 *
1342 * This is a method from the RowProvider interface. The ScrollPort uses
1343 * it to compute the size of the scrollbar.
1344 *
1345 * @return {integer} The number of rows in this terminal.
1346 */
1347hterm.Terminal.prototype.getRowCount = function() {
1348 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1349};
1350
1351/**
1352 * Create DOM nodes for new rows and append them to the end of the terminal.
1353 *
1354 * This is the only correct way to add a new DOM node for a row. Notice that
1355 * the new row is appended to the bottom of the list of rows, and does not
1356 * require renumbering (of the rowIndex property) of previous rows.
1357 *
1358 * If you think you want a new blank row somewhere in the middle of the
1359 * terminal, look into moveRows_().
1360 *
1361 * This method does not pay attention to vtScrollTop/Bottom, since you should
1362 * be using moveRows() in cases where they would matter.
1363 *
1364 * The cursor will be positioned at column 0 of the first inserted line.
1365 */
1366hterm.Terminal.prototype.appendRows_ = function(count) {
1367 var cursorRow = this.screen_.rowsArray.length;
1368 var offset = this.scrollbackRows_.length + cursorRow;
1369 for (var i = 0; i < count; i++) {
1370 var row = this.document_.createElement('x-row');
1371 row.appendChild(this.document_.createTextNode(''));
1372 row.rowIndex = offset + i;
1373 this.screen_.pushRow(row);
1374 }
1375
1376 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1377 if (extraRows > 0) {
1378 var ary = this.screen_.shiftRows(extraRows);
1379 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001380 if (this.scrollPort_.isScrolledEnd)
1381 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001382 }
1383
1384 if (cursorRow >= this.screen_.rowsArray.length)
1385 cursorRow = this.screen_.rowsArray.length - 1;
1386
rginda87b86462011-12-14 13:48:03 -08001387 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001388};
1389
1390/**
1391 * Relocate rows from one part of the addressable screen to another.
1392 *
1393 * This is used to recycle rows during VT scrolls (those which are driven
1394 * by VT commands, rather than by the user manipulating the scrollbar.)
1395 *
1396 * In this case, the blank lines scrolled into the scroll region are made of
1397 * the nodes we scrolled off. These have their rowIndex properties carefully
1398 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001399 */
1400hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1401 var ary = this.screen_.removeRows(fromIndex, count);
1402 this.screen_.insertRows(toIndex, ary);
1403
1404 var start, end;
1405 if (fromIndex < toIndex) {
1406 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001407 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001408 } else {
1409 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001410 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001411 }
1412
1413 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001414 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001415};
1416
1417/**
1418 * Renumber the rowIndex property of the given range of rows.
1419 *
1420 * The start and end indicies are relative to the screen, not the scrollback.
1421 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001422 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001423 * no need to renumber scrollback rows.
1424 */
Robert Ginda40932892012-12-10 17:26:40 -08001425hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1426 var screen = opt_screen || this.screen_;
1427
rginda8ba33642011-12-14 12:31:31 -08001428 var offset = this.scrollbackRows_.length;
1429 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001430 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001431 }
1432};
1433
1434/**
1435 * Print a string to the terminal.
1436 *
1437 * This respects the current insert and wraparound modes. It will add new lines
1438 * to the end of the terminal, scrolling off the top into the scrollback buffer
1439 * if necessary.
1440 *
1441 * The string is *not* parsed for escape codes. Use the interpret() method if
1442 * that's what you're after.
1443 *
1444 * @param{string} str The string to print.
1445 */
1446hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001447 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001448
Ricky Liang48f05cb2013-12-31 23:35:29 +08001449 var strWidth = lib.wc.strWidth(str);
1450
1451 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001452 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1453 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001454 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001455 }
rgindaa19afe22012-01-25 15:40:22 -08001456
Ricky Liang48f05cb2013-12-31 23:35:29 +08001457 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001458 var didOverflow = false;
1459 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001460
rgindaa9abdd82012-08-06 18:05:09 -07001461 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1462 didOverflow = true;
1463 count = this.screenSize.width - this.screen_.cursorPosition.column;
1464 }
rgindaa19afe22012-01-25 15:40:22 -08001465
rgindaa9abdd82012-08-06 18:05:09 -07001466 if (didOverflow && !this.options_.wraparound) {
1467 // If the string overflowed the line but wraparound is off, then the
1468 // last printed character should be the last of the string.
1469 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001470 substr = lib.wc.substr(str, startOffset, count - 1) +
1471 lib.wc.substr(str, strWidth - 1);
1472 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001473 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001474 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001475 }
rgindaa19afe22012-01-25 15:40:22 -08001476
Ricky Liang48f05cb2013-12-31 23:35:29 +08001477 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1478 for (var i = 0; i < tokens.length; i++) {
1479 if (tokens[i].wcNode)
1480 this.screen_.textAttributes.wcNode = true;
1481
1482 if (this.options_.insertMode) {
1483 this.screen_.insertString(tokens[i].str);
1484 } else {
1485 this.screen_.overwriteString(tokens[i].str);
1486 }
1487 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001488 }
1489
1490 this.screen_.maybeClipCurrentRow();
1491 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001492 }
rginda8ba33642011-12-14 12:31:31 -08001493
1494 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001495
rginda9f5222b2012-03-05 11:53:28 -08001496 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001497 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001498};
1499
1500/**
rginda87b86462011-12-14 13:48:03 -08001501 * Set the VT scroll region.
1502 *
rginda87b86462011-12-14 13:48:03 -08001503 * This also resets the cursor position to the absolute (0, 0) position, since
1504 * that's what xterm appears to do.
1505 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001506 * Setting the scroll region to the full height of the terminal will clear
1507 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1508 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1509 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1510 * continue to work as most users would expect.
1511 *
rginda87b86462011-12-14 13:48:03 -08001512 * @param {integer} scrollTop The zero-based top of the scroll region.
1513 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1514 * inclusive.
1515 */
1516hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001517 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001518 this.vtScrollTop_ = null;
1519 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001520 } else {
1521 this.vtScrollTop_ = scrollTop;
1522 this.vtScrollBottom_ = scrollBottom;
1523 }
rginda87b86462011-12-14 13:48:03 -08001524};
1525
1526/**
rginda8ba33642011-12-14 12:31:31 -08001527 * Return the top row index according to the VT.
1528 *
1529 * This will return 0 unless the terminal has been told to restrict scrolling
1530 * to some lower row. It is used for some VT cursor positioning and scrolling
1531 * commands.
1532 *
1533 * @return {integer} The topmost row in the terminal's scroll region.
1534 */
1535hterm.Terminal.prototype.getVTScrollTop = function() {
1536 if (this.vtScrollTop_ != null)
1537 return this.vtScrollTop_;
1538
1539 return 0;
rginda87b86462011-12-14 13:48:03 -08001540};
rginda8ba33642011-12-14 12:31:31 -08001541
1542/**
1543 * Return the bottom row index according to the VT.
1544 *
1545 * This will return the height of the terminal unless the it has been told to
1546 * restrict scrolling to some higher row. It is used for some VT cursor
1547 * positioning and scrolling commands.
1548 *
1549 * @return {integer} The bottommost row in the terminal's scroll region.
1550 */
1551hterm.Terminal.prototype.getVTScrollBottom = function() {
1552 if (this.vtScrollBottom_ != null)
1553 return this.vtScrollBottom_;
1554
rginda87b86462011-12-14 13:48:03 -08001555 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001556}
1557
1558/**
1559 * Process a '\n' character.
1560 *
1561 * If the cursor is on the final row of the terminal this will append a new
1562 * blank row to the screen and scroll the topmost row into the scrollback
1563 * buffer.
1564 *
1565 * Otherwise, this moves the cursor to column zero of the next row.
1566 */
1567hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001568 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1569 this.screen_.rowsArray.length - 1);
1570
1571 if (this.vtScrollBottom_ != null) {
1572 // A VT Scroll region is active, we never append new rows.
1573 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1574 // We're at the end of the VT Scroll Region, perform a VT scroll.
1575 this.vtScrollUp(1);
1576 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1577 } else if (cursorAtEndOfScreen) {
1578 // We're at the end of the screen, the only thing to do is put the
1579 // cursor to column 0.
1580 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1581 } else {
1582 // Anywhere else, advance the cursor row, and reset the column.
1583 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1584 }
1585 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001586 // We're at the end of the screen. Append a new row to the terminal,
1587 // shifting the top row into the scrollback.
1588 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001589 } else {
rginda87b86462011-12-14 13:48:03 -08001590 // Anywhere else in the screen just moves the cursor.
1591 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001592 }
1593};
1594
1595/**
1596 * Like newLine(), except maintain the cursor column.
1597 */
1598hterm.Terminal.prototype.lineFeed = function() {
1599 var column = this.screen_.cursorPosition.column;
1600 this.newLine();
1601 this.setCursorColumn(column);
1602};
1603
1604/**
rginda87b86462011-12-14 13:48:03 -08001605 * If autoCarriageReturn is set then newLine(), else lineFeed().
1606 */
1607hterm.Terminal.prototype.formFeed = function() {
1608 if (this.options_.autoCarriageReturn) {
1609 this.newLine();
1610 } else {
1611 this.lineFeed();
1612 }
1613};
1614
1615/**
1616 * Move the cursor up one row, possibly inserting a blank line.
1617 *
1618 * The cursor column is not changed.
1619 */
1620hterm.Terminal.prototype.reverseLineFeed = function() {
1621 var scrollTop = this.getVTScrollTop();
1622 var currentRow = this.screen_.cursorPosition.row;
1623
1624 if (currentRow == scrollTop) {
1625 this.insertLines(1);
1626 } else {
1627 this.setAbsoluteCursorRow(currentRow - 1);
1628 }
1629};
1630
1631/**
rginda8ba33642011-12-14 12:31:31 -08001632 * Replace all characters to the left of the current cursor with the space
1633 * character.
1634 *
1635 * TODO(rginda): This should probably *remove* the characters (not just replace
1636 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001637 * position.
rginda8ba33642011-12-14 12:31:31 -08001638 */
1639hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001640 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001641 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001642 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001643 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001644};
1645
1646/**
David Benjamin684a9b72012-05-01 17:19:58 -04001647 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001648 *
1649 * The cursor position is unchanged.
1650 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001651 * If the current background color is not the default background color this
1652 * will insert spaces rather than delete. This is unfortunate because the
1653 * trailing space will affect text selection, but it's difficult to come up
1654 * with a way to style empty space that wouldn't trip up the hterm.Screen
1655 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001656 *
1657 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1658 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1659 * crbug.com/232390 for details.
rginda8ba33642011-12-14 12:31:31 -08001660 */
1661hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001662 if (this.screen_.cursorPosition.overflow)
1663 return;
1664
Robert Ginda7fd57082012-09-25 14:41:47 -07001665 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1666 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001667
1668 if (this.screen_.textAttributes.background ===
1669 this.screen_.textAttributes.DEFAULT_COLOR) {
1670 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001671 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001672 this.screen_.cursorPosition.column + count) {
1673 this.screen_.deleteChars(count);
1674 this.clearCursorOverflow();
1675 return;
1676 }
1677 }
1678
rginda87b86462011-12-14 13:48:03 -08001679 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001680 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001681 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001682 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001683};
1684
1685/**
1686 * Erase the current line.
1687 *
1688 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001689 */
1690hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001691 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001692 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001693 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001694 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001695};
1696
1697/**
David Benjamina08d78f2012-05-05 00:28:49 -04001698 * Erase all characters from the start of the screen to the current cursor
1699 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001700 *
1701 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001702 */
1703hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001704 var cursor = this.saveCursor();
1705
1706 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001707
David Benjamina08d78f2012-05-05 00:28:49 -04001708 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001709 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001710 this.screen_.clearCursorRow();
1711 }
1712
rginda87b86462011-12-14 13:48:03 -08001713 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001714 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001715};
1716
1717/**
1718 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001719 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001720 *
1721 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001722 */
1723hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001724 var cursor = this.saveCursor();
1725
1726 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001727
David Benjamina08d78f2012-05-05 00:28:49 -04001728 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001729 for (var i = cursor.row + 1; i <= bottom; i++) {
1730 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001731 this.screen_.clearCursorRow();
1732 }
1733
rginda87b86462011-12-14 13:48:03 -08001734 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001735 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001736};
1737
1738/**
1739 * Fill the terminal with a given character.
1740 *
1741 * This methods does not respect the VT scroll region.
1742 *
1743 * @param {string} ch The character to use for the fill.
1744 */
1745hterm.Terminal.prototype.fill = function(ch) {
1746 var cursor = this.saveCursor();
1747
1748 this.setAbsoluteCursorPosition(0, 0);
1749 for (var row = 0; row < this.screenSize.height; row++) {
1750 for (var col = 0; col < this.screenSize.width; col++) {
1751 this.setAbsoluteCursorPosition(row, col);
1752 this.screen_.overwriteString(ch);
1753 }
1754 }
1755
1756 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001757};
1758
1759/**
rginda9ea433c2012-03-16 11:57:00 -07001760 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001761 *
rginda9ea433c2012-03-16 11:57:00 -07001762 * This does not respect the scroll region.
1763 *
1764 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1765 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001766 */
rginda9ea433c2012-03-16 11:57:00 -07001767hterm.Terminal.prototype.clearHome = function(opt_screen) {
1768 var screen = opt_screen || this.screen_;
1769 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001770
rginda11057d52012-04-25 12:29:56 -07001771 if (bottom == 0) {
1772 // Empty screen, nothing to do.
1773 return;
1774 }
1775
rgindae4d29232012-01-19 10:47:13 -08001776 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001777 screen.setCursorPosition(i, 0);
1778 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001779 }
1780
rginda9ea433c2012-03-16 11:57:00 -07001781 screen.setCursorPosition(0, 0);
1782};
1783
1784/**
1785 * Erase the entire display without changing the cursor position.
1786 *
1787 * The cursor position is unchanged. This does not respect the scroll
1788 * region.
1789 *
1790 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1791 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001792 */
1793hterm.Terminal.prototype.clear = function(opt_screen) {
1794 var screen = opt_screen || this.screen_;
1795 var cursor = screen.cursorPosition.clone();
1796 this.clearHome(screen);
1797 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001798};
1799
1800/**
1801 * VT command to insert lines at the current cursor row.
1802 *
1803 * This respects the current scroll region. Rows pushed off the bottom are
1804 * lost (they won't show up in the scrollback buffer).
1805 *
rginda8ba33642011-12-14 12:31:31 -08001806 * @param {integer} count The number of lines to insert.
1807 */
1808hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001809 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001810
1811 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001812 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001813
Robert Ginda579186b2012-09-26 11:40:04 -07001814 // The moveCount is the number of rows we need to relocate to make room for
1815 // the new row(s). The count is the distance to move them.
1816 var moveCount = bottom - cursorRow - count + 1;
1817 if (moveCount)
1818 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001819
Robert Ginda579186b2012-09-26 11:40:04 -07001820 for (var i = count - 1; i >= 0; i--) {
1821 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001822 this.screen_.clearCursorRow();
1823 }
rginda8ba33642011-12-14 12:31:31 -08001824};
1825
1826/**
1827 * VT command to delete lines at the current cursor row.
1828 *
1829 * New rows are added to the bottom of scroll region to take their place. New
1830 * rows are strictly there to take up space and have no content or style.
1831 */
1832hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001833 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001834
rginda87b86462011-12-14 13:48:03 -08001835 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001836 var bottom = this.getVTScrollBottom();
1837
rginda87b86462011-12-14 13:48:03 -08001838 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001839 count = Math.min(count, maxCount);
1840
rginda87b86462011-12-14 13:48:03 -08001841 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001842 if (count != maxCount)
1843 this.moveRows_(top, count, moveStart);
1844
1845 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001846 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001847 this.screen_.clearCursorRow();
1848 }
1849
rginda87b86462011-12-14 13:48:03 -08001850 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001851 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001852};
1853
1854/**
1855 * Inserts the given number of spaces at the current cursor position.
1856 *
rginda87b86462011-12-14 13:48:03 -08001857 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001858 */
1859hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001860 var cursor = this.saveCursor();
1861
rgindacbbd7482012-06-13 15:06:16 -07001862 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001863 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001864 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001865
1866 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001867 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001868};
1869
1870/**
1871 * Forward-delete the specified number of characters starting at the cursor
1872 * position.
1873 *
1874 * @param {integer} count The number of characters to delete.
1875 */
1876hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001877 var deleted = this.screen_.deleteChars(count);
1878 if (deleted && !this.screen_.textAttributes.isDefault()) {
1879 var cursor = this.saveCursor();
1880 this.setCursorColumn(this.screenSize.width - deleted);
1881 this.screen_.insertString(lib.f.getWhitespace(deleted));
1882 this.restoreCursor(cursor);
1883 }
1884
David Benjamin54e8bf62012-06-01 22:31:40 -04001885 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001886};
1887
1888/**
1889 * Shift rows in the scroll region upwards by a given number of lines.
1890 *
1891 * New rows are inserted at the bottom of the scroll region to fill the
1892 * vacated rows. The new rows not filled out with the current text attributes.
1893 *
1894 * This function does not affect the scrollback rows at all. Rows shifted
1895 * off the top are lost.
1896 *
rginda87b86462011-12-14 13:48:03 -08001897 * The cursor position is not altered.
1898 *
rginda8ba33642011-12-14 12:31:31 -08001899 * @param {integer} count The number of rows to scroll.
1900 */
1901hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001902 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001903
rginda87b86462011-12-14 13:48:03 -08001904 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001905 this.deleteLines(count);
1906
rginda87b86462011-12-14 13:48:03 -08001907 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001908};
1909
1910/**
1911 * Shift rows below the cursor down by a given number of lines.
1912 *
1913 * This function respects the current scroll region.
1914 *
1915 * New rows are inserted at the top of the scroll region to fill the
1916 * vacated rows. The new rows not filled out with the current text attributes.
1917 *
1918 * This function does not affect the scrollback rows at all. Rows shifted
1919 * off the bottom are lost.
1920 *
1921 * @param {integer} count The number of rows to scroll.
1922 */
1923hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001924 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001925
rginda87b86462011-12-14 13:48:03 -08001926 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001927 this.insertLines(opt_count);
1928
rginda87b86462011-12-14 13:48:03 -08001929 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001930};
1931
rginda87b86462011-12-14 13:48:03 -08001932
rginda8ba33642011-12-14 12:31:31 -08001933/**
1934 * Set the cursor position.
1935 *
1936 * The cursor row is relative to the scroll region if the terminal has
1937 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1938 *
1939 * @param {integer} row The new zero-based cursor row.
1940 * @param {integer} row The new zero-based cursor column.
1941 */
1942hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1943 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001944 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001945 } else {
rginda87b86462011-12-14 13:48:03 -08001946 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001947 }
rginda87b86462011-12-14 13:48:03 -08001948};
rginda8ba33642011-12-14 12:31:31 -08001949
rginda87b86462011-12-14 13:48:03 -08001950hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1951 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001952 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1953 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001954 this.screen_.setCursorPosition(row, column);
1955};
1956
1957hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001958 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1959 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001960 this.screen_.setCursorPosition(row, column);
1961};
1962
1963/**
1964 * Set the cursor column.
1965 *
1966 * @param {integer} column The new zero-based cursor column.
1967 */
1968hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001969 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001970};
1971
1972/**
1973 * Return the cursor column.
1974 *
1975 * @return {integer} The zero-based cursor column.
1976 */
1977hterm.Terminal.prototype.getCursorColumn = function() {
1978 return this.screen_.cursorPosition.column;
1979};
1980
1981/**
1982 * Set the cursor row.
1983 *
1984 * The cursor row is relative to the scroll region if the terminal has
1985 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1986 *
1987 * @param {integer} row The new cursor row.
1988 */
rginda87b86462011-12-14 13:48:03 -08001989hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1990 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001991};
1992
1993/**
1994 * Return the cursor row.
1995 *
1996 * @return {integer} The zero-based cursor row.
1997 */
1998hterm.Terminal.prototype.getCursorRow = function(row) {
1999 return this.screen_.cursorPosition.row;
2000};
2001
2002/**
2003 * Request that the ScrollPort redraw itself soon.
2004 *
2005 * The redraw will happen asynchronously, soon after the call stack winds down.
2006 * Multiple calls will be coalesced into a single redraw.
2007 */
2008hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002009 if (this.timeouts_.redraw)
2010 return;
rginda8ba33642011-12-14 12:31:31 -08002011
2012 var self = this;
rginda87b86462011-12-14 13:48:03 -08002013 this.timeouts_.redraw = setTimeout(function() {
2014 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002015 self.scrollPort_.redraw_();
2016 }, 0);
2017};
2018
2019/**
2020 * Request that the ScrollPort be scrolled to the bottom.
2021 *
2022 * The scroll will happen asynchronously, soon after the call stack winds down.
2023 * Multiple calls will be coalesced into a single scroll.
2024 *
2025 * This affects the scrollbar position of the ScrollPort, and has nothing to
2026 * do with the VT scroll commands.
2027 */
2028hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2029 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002030 return;
rginda8ba33642011-12-14 12:31:31 -08002031
2032 var self = this;
2033 this.timeouts_.scrollDown = setTimeout(function() {
2034 delete self.timeouts_.scrollDown;
2035 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2036 }, 10);
2037};
2038
2039/**
2040 * Move the cursor up a specified number of rows.
2041 *
2042 * @param {integer} count The number of rows to move the cursor.
2043 */
2044hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002045 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002046};
2047
2048/**
2049 * Move the cursor down a specified number of rows.
2050 *
2051 * @param {integer} count The number of rows to move the cursor.
2052 */
2053hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002054 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002055 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2056 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2057 this.screenSize.height - 1);
2058
rgindacbbd7482012-06-13 15:06:16 -07002059 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002060 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002061 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002062};
2063
2064/**
2065 * Move the cursor left a specified number of columns.
2066 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002067 * If reverse wraparound mode is enabled and the previous row wrapped into
2068 * the current row then we back up through the wraparound as well.
2069 *
rginda8ba33642011-12-14 12:31:31 -08002070 * @param {integer} count The number of columns to move the cursor.
2071 */
2072hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002073 count = count || 1;
2074
2075 if (count < 1)
2076 return;
2077
2078 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002079 if (this.options_.reverseWraparound) {
2080 if (this.screen_.cursorPosition.overflow) {
2081 // If this cursor is in the right margin, consume one count to get it
2082 // back to the last column. This only applies when we're in reverse
2083 // wraparound mode.
2084 count--;
2085 this.clearCursorOverflow();
2086
2087 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002088 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002089 }
2090
Robert Gindabfb32622014-07-17 13:20:27 -07002091 var newRow = this.screen_.cursorPosition.row;
2092 var newColumn = currentColumn - count;
2093 if (newColumn < 0) {
2094 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2095 if (newRow < 0) {
2096 // xterm also wraps from row 0 to the last row.
2097 newRow = this.screenSize.height + newRow % this.screenSize.height;
2098 }
2099 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2100 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002101
Robert Gindabfb32622014-07-17 13:20:27 -07002102 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2103
2104 } else {
2105 var newColumn = Math.max(currentColumn - count, 0);
2106 this.setCursorColumn(newColumn);
2107 }
rginda8ba33642011-12-14 12:31:31 -08002108};
2109
2110/**
2111 * Move the cursor right a specified number of columns.
2112 *
2113 * @param {integer} count The number of columns to move the cursor.
2114 */
2115hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002116 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002117
2118 if (count < 1)
2119 return;
2120
rgindacbbd7482012-06-13 15:06:16 -07002121 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002122 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002123 this.setCursorColumn(column);
2124};
2125
2126/**
2127 * Reverse the foreground and background colors of the terminal.
2128 *
2129 * This only affects text that was drawn with no attributes.
2130 *
2131 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2132 * been drawn with attributes that happen to coincide with the default
2133 * 'no-attribute' colors. My guess is probably not.
2134 */
2135hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002136 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002137 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002138 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2139 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002140 } else {
rginda9f5222b2012-03-05 11:53:28 -08002141 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2142 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002143 }
2144};
2145
2146/**
rginda87b86462011-12-14 13:48:03 -08002147 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002148 *
2149 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002150 */
2151hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002152 this.cursorNode_.style.backgroundColor =
2153 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002154
2155 var self = this;
2156 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002157 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002158 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002159
Michael Kelly485ecd12014-06-09 11:41:56 -04002160 // bellSquelchTimeout_ affects both audio and notification bells.
2161 if (this.bellSquelchTimeout_)
2162 return;
2163
Robert Ginda92e18102013-03-14 13:56:37 -07002164 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002165 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002166 this.bellSequelchTimeout_ = setTimeout(function() {
2167 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002168 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002169 } else {
2170 delete this.bellSquelchTimeout_;
2171 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002172
2173 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
2174 var n = new Notification(
2175 lib.f.replaceVars(hterm.desktopNotificationTitle,
Robert Ginda348dc2b2014-06-24 14:42:23 -07002176 {'title': window.document.title || 'hterm'}));
Michael Kelly485ecd12014-06-09 11:41:56 -04002177 this.bellNotificationList_.push(n);
2178 // TODO: Should we try to raise the window here?
2179 n.onclick = function() { self.closeBellNotifications_(); };
2180 }
rginda87b86462011-12-14 13:48:03 -08002181};
2182
2183/**
rginda8ba33642011-12-14 12:31:31 -08002184 * Set the origin mode bit.
2185 *
2186 * If origin mode is on, certain VT cursor and scrolling commands measure their
2187 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2188 * to the top of the addressable screen.
2189 *
2190 * Defaults to off.
2191 *
2192 * @param {boolean} state True to set origin mode, false to unset.
2193 */
2194hterm.Terminal.prototype.setOriginMode = function(state) {
2195 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002196 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002197};
2198
2199/**
2200 * Set the insert mode bit.
2201 *
2202 * If insert mode is on, existing text beyond the cursor position will be
2203 * shifted right to make room for new text. Otherwise, new text overwrites
2204 * any existing text.
2205 *
2206 * Defaults to off.
2207 *
2208 * @param {boolean} state True to set insert mode, false to unset.
2209 */
2210hterm.Terminal.prototype.setInsertMode = function(state) {
2211 this.options_.insertMode = state;
2212};
2213
2214/**
rginda87b86462011-12-14 13:48:03 -08002215 * Set the auto carriage return bit.
2216 *
2217 * If auto carriage return is on then a formfeed character is interpreted
2218 * as a newline, otherwise it's the same as a linefeed. The difference boils
2219 * down to whether or not the cursor column is reset.
2220 */
2221hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2222 this.options_.autoCarriageReturn = state;
2223};
2224
2225/**
rginda8ba33642011-12-14 12:31:31 -08002226 * Set the wraparound mode bit.
2227 *
2228 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2229 * to the start of the following row. Otherwise, the cursor is clamped to the
2230 * end of the screen and attempts to write past it are ignored.
2231 *
2232 * Defaults to on.
2233 *
2234 * @param {boolean} state True to set wraparound mode, false to unset.
2235 */
2236hterm.Terminal.prototype.setWraparound = function(state) {
2237 this.options_.wraparound = state;
2238};
2239
2240/**
2241 * Set the reverse-wraparound mode bit.
2242 *
2243 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2244 * to the end of the previous row. Otherwise, the cursor is clamped to column
2245 * 0.
2246 *
2247 * Defaults to off.
2248 *
2249 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2250 */
2251hterm.Terminal.prototype.setReverseWraparound = function(state) {
2252 this.options_.reverseWraparound = state;
2253};
2254
2255/**
2256 * Selects between the primary and alternate screens.
2257 *
2258 * If alternate mode is on, the alternate screen is active. Otherwise the
2259 * primary screen is active.
2260 *
2261 * Swapping screens has no effect on the scrollback buffer.
2262 *
2263 * Each screen maintains its own cursor position.
2264 *
2265 * Defaults to off.
2266 *
2267 * @param {boolean} state True to set alternate mode, false to unset.
2268 */
2269hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002270 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002271 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2272
rginda35c456b2012-02-09 17:29:05 -08002273 if (this.screen_.rowsArray.length &&
2274 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2275 // If the screen changed sizes while we were away, our rowIndexes may
2276 // be incorrect.
2277 var offset = this.scrollbackRows_.length;
2278 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002279 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002280 ary[i].rowIndex = offset + i;
2281 }
2282 }
rginda8ba33642011-12-14 12:31:31 -08002283
rginda35c456b2012-02-09 17:29:05 -08002284 this.realizeWidth_(this.screenSize.width);
2285 this.realizeHeight_(this.screenSize.height);
2286 this.scrollPort_.syncScrollHeight();
2287 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002288
rginda6d397402012-01-17 10:58:29 -08002289 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002290 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002291};
2292
2293/**
2294 * Set the cursor-blink mode bit.
2295 *
2296 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2297 * a visible cursor does not blink.
2298 *
2299 * You should make sure to turn blinking off if you're going to dispose of a
2300 * terminal, otherwise you'll leak a timeout.
2301 *
2302 * Defaults to on.
2303 *
2304 * @param {boolean} state True to set cursor-blink mode, false to unset.
2305 */
2306hterm.Terminal.prototype.setCursorBlink = function(state) {
2307 this.options_.cursorBlink = state;
2308
2309 if (!state && this.timeouts_.cursorBlink) {
2310 clearTimeout(this.timeouts_.cursorBlink);
2311 delete this.timeouts_.cursorBlink;
2312 }
2313
2314 if (this.options_.cursorVisible)
2315 this.setCursorVisible(true);
2316};
2317
2318/**
2319 * Set the cursor-visible mode bit.
2320 *
2321 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2322 *
2323 * Defaults to on.
2324 *
2325 * @param {boolean} state True to set cursor-visible mode, false to unset.
2326 */
2327hterm.Terminal.prototype.setCursorVisible = function(state) {
2328 this.options_.cursorVisible = state;
2329
2330 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002331 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002332 return;
2333 }
2334
rginda87b86462011-12-14 13:48:03 -08002335 this.syncCursorPosition_();
2336
2337 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002338
2339 if (this.options_.cursorBlink) {
2340 if (this.timeouts_.cursorBlink)
2341 return;
2342
Robert Gindaea2183e2014-07-17 09:51:51 -07002343 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002344 } else {
2345 if (this.timeouts_.cursorBlink) {
2346 clearTimeout(this.timeouts_.cursorBlink);
2347 delete this.timeouts_.cursorBlink;
2348 }
2349 }
2350};
2351
2352/**
rginda87b86462011-12-14 13:48:03 -08002353 * Synchronizes the visible cursor and document selection with the current
2354 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002355 */
2356hterm.Terminal.prototype.syncCursorPosition_ = function() {
2357 var topRowIndex = this.scrollPort_.getTopRowIndex();
2358 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2359 var cursorRowIndex = this.scrollbackRows_.length +
2360 this.screen_.cursorPosition.row;
2361
2362 if (cursorRowIndex > bottomRowIndex) {
2363 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002364 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002365 return;
2366 }
2367
Robert Gindab837c052014-08-11 11:17:51 -07002368 if (this.options_.cursorVisible &&
2369 this.cursorNode_.style.display == 'none') {
2370 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2371 this.cursorNode_.style.display = '';
2372 }
2373
2374
rginda8ba33642011-12-14 12:31:31 -08002375 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002376 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2377 'px';
2378 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2379 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002380
2381 this.cursorNode_.setAttribute('title',
2382 '(' + this.screen_.cursorPosition.row +
2383 ', ' + this.screen_.cursorPosition.column +
2384 ')');
2385
2386 // Update the caret for a11y purposes.
2387 var selection = this.document_.getSelection();
2388 if (selection && selection.isCollapsed)
2389 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002390};
2391
Robert Gindafb1be6a2013-12-11 11:56:22 -08002392/**
2393 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2394 * and character cell dimensions.
2395 */
Robert Ginda830583c2013-08-07 13:20:46 -07002396hterm.Terminal.prototype.restyleCursor_ = function() {
2397 var shape = this.cursorShape_;
2398
2399 if (this.cursorNode_.getAttribute('focus') == 'false') {
2400 // Always show a block cursor when unfocused.
2401 shape = hterm.Terminal.cursorShape.BLOCK;
2402 }
2403
2404 var style = this.cursorNode_.style;
2405
Robert Gindafb1be6a2013-12-11 11:56:22 -08002406 style.width = this.scrollPort_.characterSize.width + 'px';
2407
Robert Ginda830583c2013-08-07 13:20:46 -07002408 switch (shape) {
2409 case hterm.Terminal.cursorShape.BEAM:
2410 style.height = this.scrollPort_.characterSize.height + 'px';
2411 style.backgroundColor = 'transparent';
2412 style.borderBottomStyle = null;
2413 style.borderLeftStyle = 'solid';
2414 break;
2415
2416 case hterm.Terminal.cursorShape.UNDERLINE:
2417 style.height = this.scrollPort_.characterSize.baseline + 'px';
2418 style.backgroundColor = 'transparent';
2419 style.borderBottomStyle = 'solid';
2420 // correct the size to put it exactly at the baseline
2421 style.borderLeftStyle = null;
2422 break;
2423
2424 default:
2425 style.height = this.scrollPort_.characterSize.height + 'px';
2426 style.backgroundColor = this.cursorColor_;
2427 style.borderBottomStyle = null;
2428 style.borderLeftStyle = null;
2429 break;
2430 }
2431};
2432
rginda8ba33642011-12-14 12:31:31 -08002433/**
2434 * Synchronizes the visible cursor with the current cursor coordinates.
2435 *
2436 * The sync will happen asynchronously, soon after the call stack winds down.
2437 * Multiple calls will be coalesced into a single sync.
2438 */
2439hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2440 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002441 return;
rginda8ba33642011-12-14 12:31:31 -08002442
2443 var self = this;
2444 this.timeouts_.syncCursor = setTimeout(function() {
2445 self.syncCursorPosition_();
2446 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002447 }, 0);
2448};
2449
rgindacc2996c2012-02-24 14:59:31 -08002450/**
rgindaf522ce02012-04-17 17:49:17 -07002451 * Show or hide the zoom warning.
2452 *
2453 * The zoom warning is a message warning the user that their browser zoom must
2454 * be set to 100% in order for hterm to function properly.
2455 *
2456 * @param {boolean} state True to show the message, false to hide it.
2457 */
2458hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2459 if (!this.zoomWarningNode_) {
2460 if (!state)
2461 return;
2462
2463 this.zoomWarningNode_ = this.document_.createElement('div');
2464 this.zoomWarningNode_.style.cssText = (
2465 'color: black;' +
2466 'background-color: #ff2222;' +
2467 'font-size: large;' +
2468 'border-radius: 8px;' +
2469 'opacity: 0.75;' +
2470 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2471 'top: 0.5em;' +
2472 'right: 1.2em;' +
2473 'position: absolute;' +
2474 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002475 '-webkit-user-select: none;' +
2476 '-moz-text-size-adjust: none;' +
2477 '-moz-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002478 }
2479
Robert Gindab4839c22013-02-28 16:52:10 -08002480 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2481 hterm.zoomWarningMessage,
2482 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2483
rgindaf522ce02012-04-17 17:49:17 -07002484 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2485
2486 if (state) {
2487 if (!this.zoomWarningNode_.parentNode)
2488 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2489 } else if (this.zoomWarningNode_.parentNode) {
2490 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2491 }
2492};
2493
2494/**
rgindacc2996c2012-02-24 14:59:31 -08002495 * Show the terminal overlay for a given amount of time.
2496 *
2497 * The terminal overlay appears in inverse video in a large font, centered
2498 * over the terminal. You should probably keep the overlay message brief,
2499 * since it's in a large font and you probably aren't going to check the size
2500 * of the terminal first.
2501 *
2502 * @param {string} msg The text (not HTML) message to display in the overlay.
2503 * @param {number} opt_timeout The amount of time to wait before fading out
2504 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2505 * stay up forever (or until the next overlay).
2506 */
2507hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002508 if (!this.overlayNode_) {
2509 if (!this.div_)
2510 return;
2511
2512 this.overlayNode_ = this.document_.createElement('div');
2513 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002514 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002515 'font-size: xx-large;' +
2516 'opacity: 0.75;' +
2517 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2518 'position: absolute;' +
2519 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002520 '-webkit-transition: opacity 180ms ease-in;' +
2521 '-moz-user-select: none;' +
2522 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002523
2524 this.overlayNode_.addEventListener('mousedown', function(e) {
2525 e.preventDefault();
2526 e.stopPropagation();
2527 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002528 }
2529
rginda9f5222b2012-03-05 11:53:28 -08002530 this.overlayNode_.style.color = this.prefs_.get('background-color');
2531 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2532 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2533
rgindaf0090c92012-02-10 14:58:52 -08002534 this.overlayNode_.textContent = msg;
2535 this.overlayNode_.style.opacity = '0.75';
2536
2537 if (!this.overlayNode_.parentNode)
2538 this.div_.appendChild(this.overlayNode_);
2539
Robert Ginda97769282013-02-01 15:30:30 -08002540 var divSize = hterm.getClientSize(this.div_);
2541 var overlaySize = hterm.getClientSize(this.overlayNode_);
2542
Robert Ginda8a59f762014-07-23 11:29:55 -07002543 this.overlayNode_.style.top =
2544 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002545 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002546 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002547
2548 var self = this;
2549
2550 if (this.overlayTimeout_)
2551 clearTimeout(this.overlayTimeout_);
2552
rgindacc2996c2012-02-24 14:59:31 -08002553 if (opt_timeout === null)
2554 return;
2555
rgindaf0090c92012-02-10 14:58:52 -08002556 this.overlayTimeout_ = setTimeout(function() {
2557 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002558 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002559 if (self.overlayNode_.parentNode)
2560 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002561 self.overlayTimeout_ = null;
2562 self.overlayNode_.style.opacity = '0.75';
2563 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002564 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002565};
2566
rginda4bba5e12012-06-20 16:15:30 -07002567/**
2568 * Paste from the system clipboard to the terminal.
2569 */
2570hterm.Terminal.prototype.paste = function() {
2571 hterm.pasteFromClipboard(this.document_);
2572};
2573
2574/**
2575 * Copy a string to the system clipboard.
2576 *
2577 * Note: If there is a selected range in the terminal, it'll be cleared.
2578 */
2579hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002580 if (this.prefs_.get('enable-clipboard-notice'))
2581 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2582
rgindaa09e7332012-08-17 12:49:51 -07002583 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002584 copySource.textContent = str;
2585 copySource.style.cssText = (
2586 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002587 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002588 'position: absolute;' +
2589 'top: -99px');
2590
2591 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002592
rginda4bba5e12012-06-20 16:15:30 -07002593 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002594 var anchorNode = selection.anchorNode;
2595 var anchorOffset = selection.anchorOffset;
2596 var focusNode = selection.focusNode;
2597 var focusOffset = selection.focusOffset;
2598
rginda4bba5e12012-06-20 16:15:30 -07002599 selection.selectAllChildren(copySource);
2600
rgindaa09e7332012-08-17 12:49:51 -07002601 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002602
Rob Spies56953412014-04-28 14:09:47 -07002603 // IE doesn't support selection.extend. This means that the selection
2604 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002605 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002606 selection.collapse(anchorNode, anchorOffset);
2607 selection.extend(focusNode, focusOffset);
2608 }
rgindafaa74742012-08-21 13:34:03 -07002609
rginda4bba5e12012-06-20 16:15:30 -07002610 copySource.parentNode.removeChild(copySource);
2611};
2612
rgindaa09e7332012-08-17 12:49:51 -07002613hterm.Terminal.prototype.getSelectionText = function() {
2614 var selection = this.scrollPort_.selection;
2615 selection.sync();
2616
2617 if (selection.isCollapsed)
2618 return null;
2619
2620
2621 // Start offset measures from the beginning of the line.
2622 var startOffset = selection.startOffset;
2623 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002624
Robert Gindafdbb3f22012-09-06 20:23:06 -07002625 if (node.nodeName != 'X-ROW') {
2626 // If the selection doesn't start on an x-row node, then it must be
2627 // somewhere inside the x-row. Add any characters from previous siblings
2628 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002629
2630 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2631 // If node is the text node in a styled span, move up to the span node.
2632 node = node.parentNode;
2633 }
2634
Robert Gindafdbb3f22012-09-06 20:23:06 -07002635 while (node.previousSibling) {
2636 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002637 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002638 }
rgindaa09e7332012-08-17 12:49:51 -07002639 }
2640
2641 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002642 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2643 selection.endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002644 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002645
Robert Gindafdbb3f22012-09-06 20:23:06 -07002646 if (node.nodeName != 'X-ROW') {
2647 // If the selection doesn't end on an x-row node, then it must be
2648 // somewhere inside the x-row. Add any characters from following siblings
2649 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002650
2651 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2652 // If node is the text node in a styled span, move up to the span node.
2653 node = node.parentNode;
2654 }
2655
Robert Gindafdbb3f22012-09-06 20:23:06 -07002656 while (node.nextSibling) {
2657 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002658 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002659 }
rgindaa09e7332012-08-17 12:49:51 -07002660 }
2661
2662 var rv = this.getRowsText(selection.startRow.rowIndex,
2663 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002664 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002665};
2666
rginda4bba5e12012-06-20 16:15:30 -07002667/**
2668 * Copy the current selection to the system clipboard, then clear it after a
2669 * short delay.
2670 */
2671hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002672 var text = this.getSelectionText();
2673 if (text != null)
2674 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002675};
2676
rgindaf0090c92012-02-10 14:58:52 -08002677hterm.Terminal.prototype.overlaySize = function() {
2678 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2679};
2680
rginda87b86462011-12-14 13:48:03 -08002681/**
2682 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2683 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002684 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002685 */
2686hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002687 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002688 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2689
Robert Ginda8cb7d902013-06-20 14:37:18 -07002690 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002691};
2692
2693/**
rgindad5613292012-06-19 15:40:37 -07002694 * Add the terminalRow and terminalColumn properties to mouse events and
2695 * then forward on to onMouse().
2696 *
2697 * The terminalRow and terminalColumn properties contain the (row, column)
2698 * coordinates for the mouse event.
2699 */
2700hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002701 if (e.processedByTerminalHandler_) {
2702 // We register our event handlers on the document, as well as the cursor
2703 // and the scroll blocker. Mouse events that occur on the cursor or
2704 // scroll blocker will also appear on the document, but we don't want to
2705 // process them twice.
2706 //
2707 // We can't just prevent bubbling because that has other side effects, so
2708 // we decorate the event object with this property instead.
2709 return;
2710 }
2711
2712 e.processedByTerminalHandler_ = true;
2713
Robert Gindaeda48db2014-07-17 09:25:30 -07002714 // One based row/column stored on the mouse event.
2715 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2716 this.scrollPort_.characterSize.height) + 1;
2717 e.terminalColumn = parseInt(e.clientX /
2718 this.scrollPort_.characterSize.width) + 1;
2719
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002720 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2721 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002722 return;
2723 }
2724
Robert Gindab837c052014-08-11 11:17:51 -07002725 if (this.options_.cursorVisible &&
2726 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED) {
2727 // If the cursor is visible and we're not sending mouse events to the
2728 // host app, then we want to hide the terminal cursor when the mouse
2729 // cursor is over top. This keeps the terminal cursor from interfering
2730 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07002731 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
2732 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
2733 this.cursorNode_.style.display = 'none';
2734 } else if (this.cursorNode_.style.display == 'none') {
2735 this.cursorNode_.style.display = '';
2736 }
2737 }
rgindad5613292012-06-19 15:40:37 -07002738
Robert Ginda928cf632014-03-05 15:07:41 -08002739 if (e.type == 'mousedown') {
2740 if (e.altKey || this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED) {
2741 // If VT mouse reporting is disabled, or has been defeated with
2742 // alt-mousedown, then the mouse will act on the local selection.
2743 this.reportMouseEvents_ = false;
2744 this.setSelectionEnabled(true);
2745 } else {
2746 // Otherwise we defer ownership of the mouse to the VT.
2747 this.reportMouseEvents_ = true;
Robert Ginda3ae37822014-05-15 13:05:35 -07002748 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08002749 this.setSelectionEnabled(false);
2750 e.preventDefault();
2751 }
2752 }
2753
2754 if (!this.reportMouseEvents_) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002755 if (e.type == 'dblclick') {
2756 this.screen_.expandSelection(this.document_.getSelection());
2757 hterm.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07002758 }
2759
Robert Ginda928cf632014-03-05 15:07:41 -08002760 if (e.type == 'mousedown' && e.which == this.mousePasteButton)
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002761 this.paste();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002762
2763 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2764 !this.document_.getSelection().isCollapsed) {
2765 hterm.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002766 }
2767
2768 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
2769 this.scrollBlockerNode_.engaged) {
2770 // Disengage the scroll-blocker after one of these events.
2771 this.scrollBlockerNode_.engaged = false;
2772 this.scrollBlockerNode_.style.top = '-99px';
2773 }
2774
Robert Ginda928cf632014-03-05 15:07:41 -08002775 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002776 if (!this.scrollBlockerNode_.engaged) {
2777 if (e.type == 'mousedown') {
2778 // Move the scroll-blocker into place if we want to keep the scrollport
2779 // from scrolling.
2780 this.scrollBlockerNode_.engaged = true;
2781 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2782 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2783 } else if (e.type == 'mousemove') {
2784 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
2785 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07002786 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002787 e.preventDefault();
2788 }
2789 }
Robert Ginda928cf632014-03-05 15:07:41 -08002790
2791 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002792 }
2793
Robert Ginda928cf632014-03-05 15:07:41 -08002794 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
2795 // Restore this on mouseup in case it was temporarily defeated with a
2796 // alt-mousedown. Only do this when the selection is empty so that
2797 // we don't immediately kill the users selection.
2798 this.reportMouseEvents_ = (this.vt.mouseReport !=
2799 this.vt.MOUSE_REPORT_DISABLED);
2800 }
rgindad5613292012-06-19 15:40:37 -07002801};
2802
2803/**
2804 * Clients should override this if they care to know about mouse events.
2805 *
2806 * The event parameter will be a normal DOM mouse click event with additional
2807 * 'terminalRow' and 'terminalColumn' properties.
2808 */
2809hterm.Terminal.prototype.onMouse = function(e) { };
2810
2811/**
rginda8e92a692012-05-20 19:37:20 -07002812 * React when focus changes.
2813 */
Rob Spies06533ba2014-04-24 11:20:37 -07002814hterm.Terminal.prototype.onFocusChange_ = function(focused) {
2815 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07002816 this.restyleCursor_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002817 if (focused === true)
2818 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07002819};
2820
2821/**
rginda8ba33642011-12-14 12:31:31 -08002822 * React when the ScrollPort is scrolled.
2823 */
2824hterm.Terminal.prototype.onScroll_ = function() {
2825 this.scheduleSyncCursorPosition_();
2826};
2827
2828/**
rginda9846e2f2012-01-27 13:53:33 -08002829 * React when text is pasted into the scrollPort.
2830 */
2831hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07002832 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07002833 data = this.keyboard.encode(data);
Robert Gindaa063b202014-07-21 11:08:25 -07002834 if (this.options_.bracketedPaste)
2835 data = '\x1b[200~' + data + '\x1b[201~';
2836
2837 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08002838};
2839
2840/**
rgindaa09e7332012-08-17 12:49:51 -07002841 * React when the user tries to copy from the scrollPort.
2842 */
2843hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07002844 if (!this.useDefaultWindowCopy) {
2845 e.preventDefault();
2846 setTimeout(this.copySelectionToClipboard.bind(this), 0);
2847 }
rgindaa09e7332012-08-17 12:49:51 -07002848};
2849
2850/**
rginda8ba33642011-12-14 12:31:31 -08002851 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002852 *
2853 * Note: This function should not directly contain code that alters the internal
2854 * state of the terminal. That kind of code belongs in realizeWidth or
2855 * realizeHeight, so that it can be executed synchronously in the case of a
2856 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002857 */
2858hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08002859 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002860 this.scrollPort_.characterSize.width);
Robert Ginda19f61292014-03-04 14:07:57 -08002861 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
rginda35c456b2012-02-09 17:29:05 -08002862 this.scrollPort_.characterSize.height);
2863
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002864 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002865 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002866 // gets removed from the document or during the initial load, and we can't
2867 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002868 return;
2869 }
2870
rgindaa8ba17d2012-08-15 14:41:10 -07002871 var isNewSize = (columnCount != this.screenSize.width ||
2872 rowCount != this.screenSize.height);
2873
2874 // We do this even if the size didn't change, just to be sure everything is
2875 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002876 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002877 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002878
2879 if (isNewSize)
2880 this.overlaySize();
2881
Robert Gindafb1be6a2013-12-11 11:56:22 -08002882 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07002883 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002884};
2885
2886/**
2887 * Service the cursor blink timeout.
2888 */
2889hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07002890 if (!this.options_.cursorBlink) {
2891 delete this.timeouts_.cursorBlink;
2892 return;
2893 }
2894
Robert Ginda830583c2013-08-07 13:20:46 -07002895 if (this.cursorNode_.getAttribute('focus') == 'false' ||
2896 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08002897 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07002898 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
2899 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08002900 } else {
rginda87b86462011-12-14 13:48:03 -08002901 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07002902 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
2903 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08002904 }
2905};
David Reveman8f552492012-03-28 12:18:41 -04002906
2907/**
2908 * Set the scrollbar-visible mode bit.
2909 *
2910 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2911 * Otherwise it will not.
2912 *
2913 * Defaults to on.
2914 *
2915 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2916 */
2917hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2918 this.scrollPort_.setScrollbarVisible(state);
2919};
Michael Kelly485ecd12014-06-09 11:41:56 -04002920
2921/**
2922 * Close all web notifications created by terminal bells.
2923 */
2924hterm.Terminal.prototype.closeBellNotifications_ = function() {
2925 this.bellNotificationList_.forEach(function(n) {
2926 n.close();
2927 });
2928 this.bellNotificationList_.length = 0;
2929};