blob: 3d0001a39f85add9208029d0b2ad6560ef14525b [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;
333 },
334
Masaya Suzuki273aa982014-05-31 07:25:55 +0900335 'east-asian-ambiguous-as-two-column': function(v) {
336 lib.wc.regardCjkAmbiguous = v;
337 },
338
Robert Ginda57f03b42012-09-13 11:02:48 -0700339 'enable-8-bit-control': function(v) {
340 terminal.vt.enable8BitControl = !!v;
341 },
rginda30f20f62012-04-05 16:36:19 -0700342
Robert Ginda57f03b42012-09-13 11:02:48 -0700343 'enable-bold': function(v) {
344 terminal.syncBoldSafeState();
345 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400346
Robert Ginda3e278d72014-03-25 13:18:51 -0700347 'enable-bold-as-bright': function(v) {
348 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
349 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
350 },
351
Robert Ginda57f03b42012-09-13 11:02:48 -0700352 'enable-clipboard-write': function(v) {
353 terminal.vt.enableClipboardWrite = !!v;
354 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400355
Robert Ginda3755e752013-05-31 13:34:09 -0700356 'enable-dec12': function(v) {
357 terminal.vt.enableDec12 = !!v;
358 },
359
Robert Ginda57f03b42012-09-13 11:02:48 -0700360 'font-family': function(v) {
361 terminal.syncFontFamily();
362 },
rginda30f20f62012-04-05 16:36:19 -0700363
Robert Ginda57f03b42012-09-13 11:02:48 -0700364 'font-size': function(v) {
365 terminal.setFontSize(v);
366 },
rginda9875d902012-08-20 16:21:57 -0700367
Robert Ginda57f03b42012-09-13 11:02:48 -0700368 'font-smoothing': function(v) {
369 terminal.syncFontFamily();
370 },
rgindade84e382012-04-20 15:39:31 -0700371
Robert Ginda57f03b42012-09-13 11:02:48 -0700372 'foreground-color': function(v) {
373 terminal.setForegroundColor(v);
374 },
rginda30f20f62012-04-05 16:36:19 -0700375
Robert Ginda57f03b42012-09-13 11:02:48 -0700376 'home-keys-scroll': function(v) {
377 terminal.keyboard.homeKeysScroll = v;
378 },
rginda4bba5e12012-06-20 16:15:30 -0700379
Robert Ginda57f03b42012-09-13 11:02:48 -0700380 'max-string-sequence': function(v) {
381 terminal.vt.maxStringSequence = v;
382 },
rginda11057d52012-04-25 12:29:56 -0700383
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700384 'media-keys-are-fkeys': function(v) {
385 terminal.keyboard.mediaKeysAreFKeys = v;
386 },
387
Robert Ginda57f03b42012-09-13 11:02:48 -0700388 'meta-sends-escape': function(v) {
389 terminal.keyboard.metaSendsEscape = v;
390 },
rginda30f20f62012-04-05 16:36:19 -0700391
Robert Ginda57f03b42012-09-13 11:02:48 -0700392 'mouse-paste-button': function(v) {
393 terminal.syncMousePasteButton();
394 },
rgindaa8ba17d2012-08-15 14:41:10 -0700395
Robert Gindae76aa9f2014-03-14 12:29:12 -0700396 'page-keys-scroll': function(v) {
397 terminal.keyboard.pageKeysScroll = v;
398 },
399
Robert Ginda40932892012-12-10 17:26:40 -0800400 'pass-alt-number': function(v) {
401 if (v == null) {
402 var osx = window.navigator.userAgent.match(/Mac OS X/);
403
404 // Let Alt-1..9 pass to the browser (to control tab switching) on
405 // non-OS X systems, or if hterm is not opened in an app window.
406 v = (!osx && hterm.windowType != 'popup');
407 }
408
409 terminal.passAltNumber = v;
410 },
411
412 'pass-ctrl-number': function(v) {
413 if (v == null) {
414 var osx = window.navigator.userAgent.match(/Mac OS X/);
415
416 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
417 // non-OS X systems, or if hterm is not opened in an app window.
418 v = (!osx && hterm.windowType != 'popup');
419 }
420
421 terminal.passCtrlNumber = v;
422 },
423
424 'pass-meta-number': function(v) {
425 if (v == null) {
426 var osx = window.navigator.userAgent.match(/Mac OS X/);
427
428 // Let Meta-1..9 pass to the browser (to control tab switching) on
429 // OS X systems, or if hterm is not opened in an app window.
430 v = (osx && hterm.windowType != 'popup');
431 }
432
433 terminal.passMetaNumber = v;
434 },
435
Marius Schilder77857b32014-05-14 16:21:26 -0700436 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700437 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700438 },
439
Robert Ginda8cb7d902013-06-20 14:37:18 -0700440 'receive-encoding': function(v) {
441 if (!(/^(utf-8|raw)$/).test(v)) {
442 console.warn('Invalid value for "receive-encoding": ' + v);
443 v = 'utf-8';
444 }
445
446 terminal.vt.characterEncoding = v;
447 },
448
Robert Ginda57f03b42012-09-13 11:02:48 -0700449 'scroll-on-keystroke': function(v) {
450 terminal.scrollOnKeystroke_ = v;
451 },
rginda9f5222b2012-03-05 11:53:28 -0800452
Robert Ginda57f03b42012-09-13 11:02:48 -0700453 'scroll-on-output': function(v) {
454 terminal.scrollOnOutput_ = v;
455 },
rginda30f20f62012-04-05 16:36:19 -0700456
Robert Ginda57f03b42012-09-13 11:02:48 -0700457 'scrollbar-visible': function(v) {
458 terminal.setScrollbarVisible(v);
459 },
rginda9f5222b2012-03-05 11:53:28 -0800460
Robert Ginda8cb7d902013-06-20 14:37:18 -0700461 'send-encoding': function(v) {
462 if (!(/^(utf-8|raw)$/).test(v)) {
463 console.warn('Invalid value for "send-encoding": ' + v);
464 v = 'utf-8';
465 }
466
467 terminal.keyboard.characterEncoding = v;
468 },
469
Robert Ginda57f03b42012-09-13 11:02:48 -0700470 'shift-insert-paste': function(v) {
471 terminal.keyboard.shiftInsertPaste = v;
472 },
rginda9f5222b2012-03-05 11:53:28 -0800473
Robert Gindae76aa9f2014-03-14 12:29:12 -0700474 'user-css': function(v) {
475 terminal.scrollPort_.setUserCss(v);
Robert Ginda57f03b42012-09-13 11:02:48 -0700476 }
477 });
rginda30f20f62012-04-05 16:36:19 -0700478
Robert Ginda57f03b42012-09-13 11:02:48 -0700479 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800480 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700481
482 if (opt_callback)
483 opt_callback();
484 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800485};
486
Rob Spies56953412014-04-28 14:09:47 -0700487
488/**
489 * Returns the preferences manager used for configuring this terminal.
490 */
491hterm.Terminal.prototype.getPrefs = function() {
492 return this.prefs_;
493};
494
495
rginda8e92a692012-05-20 19:37:20 -0700496/**
497 * Set the color for the cursor.
498 *
499 * If you want this setting to persist, set it through prefs_, rather than
500 * with this method.
501 */
502hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700503 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700504 this.cursorNode_.style.backgroundColor = color;
505 this.cursorNode_.style.borderColor = color;
506};
507
508/**
509 * Return the current cursor color as a string.
510 */
511hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700512 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700513};
514
515/**
rgindad5613292012-06-19 15:40:37 -0700516 * Enable or disable mouse based text selection in the terminal.
517 */
518hterm.Terminal.prototype.setSelectionEnabled = function(state) {
519 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700520};
521
522/**
rginda8e92a692012-05-20 19:37:20 -0700523 * Set the background color.
524 *
525 * If you want this setting to persist, set it through prefs_, rather than
526 * with this method.
527 */
528hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700529 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700530 this.primaryScreen_.textAttributes.setDefaults(
531 this.foregroundColor_, this.backgroundColor_);
532 this.alternateScreen_.textAttributes.setDefaults(
533 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700534 this.scrollPort_.setBackgroundColor(color);
535};
536
rginda9f5222b2012-03-05 11:53:28 -0800537/**
538 * Return the current terminal background color.
539 *
540 * Intended for use by other classes, so we don't have to expose the entire
541 * prefs_ object.
542 */
543hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700544 return this.backgroundColor_;
545};
546
547/**
548 * Set the foreground color.
549 *
550 * If you want this setting to persist, set it through prefs_, rather than
551 * with this method.
552 */
553hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700554 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700555 this.primaryScreen_.textAttributes.setDefaults(
556 this.foregroundColor_, this.backgroundColor_);
557 this.alternateScreen_.textAttributes.setDefaults(
558 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700559 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800560};
561
562/**
563 * Return the current terminal foreground color.
564 *
565 * Intended for use by other classes, so we don't have to expose the entire
566 * prefs_ object.
567 */
568hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700569 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800570};
571
572/**
rginda87b86462011-12-14 13:48:03 -0800573 * Create a new instance of a terminal command and run it with a given
574 * argument string.
575 *
576 * @param {function} commandClass The constructor for a terminal command.
577 * @param {string} argString The argument string to pass to the command.
578 */
579hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700580 var environment = this.prefs_.get('environment');
581 if (typeof environment != 'object' || environment == null)
582 environment = {};
583
rginda87b86462011-12-14 13:48:03 -0800584 var self = this;
585 this.command = new commandClass(
586 { argString: argString || '',
587 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700588 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800589 onExit: function(code) {
590 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800591 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700592 if (self.prefs_.get('close-on-exit'))
593 window.close();
rginda87b86462011-12-14 13:48:03 -0800594 }
595 });
596
rgindafeaf3142012-01-31 15:14:20 -0800597 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800598 this.command.run();
599};
600
601/**
rgindafeaf3142012-01-31 15:14:20 -0800602 * Returns true if the current screen is the primary screen, false otherwise.
603 */
604hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700605 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800606};
607
608/**
609 * Install the keyboard handler for this terminal.
610 *
611 * This will prevent the browser from seeing any keystrokes sent to the
612 * terminal.
613 */
614hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700615 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800616}
617
618/**
619 * Uninstall the keyboard handler for this terminal.
620 */
621hterm.Terminal.prototype.uninstallKeyboard = function() {
622 this.keyboard.installKeyboard(null);
623}
624
625/**
rginda35c456b2012-02-09 17:29:05 -0800626 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800627 *
628 * Call setFontSize(0) to reset to the default font size.
629 *
630 * This function does not modify the font-size preference.
631 *
632 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800633 */
634hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800635 if (px === 0)
636 px = this.prefs_.get('font-size');
637
rginda35c456b2012-02-09 17:29:05 -0800638 this.scrollPort_.setFontSize(px);
Ricky Liang48f05cb2013-12-31 23:35:29 +0800639 if (this.wcCssRule_) {
640 this.wcCssRule_.style.width = this.scrollPort_.characterSize.width * 2 +
641 'px';
642 }
rginda35c456b2012-02-09 17:29:05 -0800643};
644
645/**
646 * Get the current font size.
647 */
648hterm.Terminal.prototype.getFontSize = function() {
649 return this.scrollPort_.getFontSize();
650};
651
652/**
rginda8e92a692012-05-20 19:37:20 -0700653 * Get the current font family.
654 */
655hterm.Terminal.prototype.getFontFamily = function() {
656 return this.scrollPort_.getFontFamily();
657};
658
659/**
rginda35c456b2012-02-09 17:29:05 -0800660 * Set the CSS "font-family" for this terminal.
661 */
rginda9f5222b2012-03-05 11:53:28 -0800662hterm.Terminal.prototype.syncFontFamily = function() {
663 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
664 this.prefs_.get('font-smoothing'));
665 this.syncBoldSafeState();
666};
667
rginda4bba5e12012-06-20 16:15:30 -0700668/**
669 * Set this.mousePasteButton based on the mouse-paste-button pref,
670 * autodetecting if necessary.
671 */
672hterm.Terminal.prototype.syncMousePasteButton = function() {
673 var button = this.prefs_.get('mouse-paste-button');
674 if (typeof button == 'number') {
675 this.mousePasteButton = button;
676 return;
677 }
678
679 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
680 if (!ary || ary[2] == 'CrOS') {
681 this.mousePasteButton = 2;
682 } else {
683 this.mousePasteButton = 3;
684 }
685};
686
687/**
688 * Enable or disable bold based on the enable-bold pref, autodetecting if
689 * necessary.
690 */
rginda9f5222b2012-03-05 11:53:28 -0800691hterm.Terminal.prototype.syncBoldSafeState = function() {
692 var enableBold = this.prefs_.get('enable-bold');
693 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700694 this.primaryScreen_.textAttributes.enableBold = enableBold;
695 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800696 return;
697 }
698
rgindaf7521392012-02-28 17:20:34 -0800699 var normalSize = this.scrollPort_.measureCharacterSize();
700 var boldSize = this.scrollPort_.measureCharacterSize('bold');
701
702 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800703 if (!isBoldSafe) {
704 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700705 'from normal. Font family is: ' +
706 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800707 }
rginda9f5222b2012-03-05 11:53:28 -0800708
Robert Gindaed016262012-10-26 16:27:09 -0700709 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
710 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800711};
712
713/**
rginda87b86462011-12-14 13:48:03 -0800714 * Return a copy of the current cursor position.
715 *
716 * @return {hterm.RowCol} The RowCol object representing the current position.
717 */
718hterm.Terminal.prototype.saveCursor = function() {
719 return this.screen_.cursorPosition.clone();
720};
721
rgindaa19afe22012-01-25 15:40:22 -0800722hterm.Terminal.prototype.getTextAttributes = function() {
723 return this.screen_.textAttributes;
724};
725
rginda1a09aa02012-06-18 21:11:25 -0700726hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
727 this.screen_.textAttributes = textAttributes;
728};
729
rginda87b86462011-12-14 13:48:03 -0800730/**
rgindaf522ce02012-04-17 17:49:17 -0700731 * Return the current browser zoom factor applied to the terminal.
732 *
733 * @return {number} The current browser zoom factor.
734 */
735hterm.Terminal.prototype.getZoomFactor = function() {
736 return this.scrollPort_.characterSize.zoomFactor;
737};
738
739/**
rginda9846e2f2012-01-27 13:53:33 -0800740 * Change the title of this terminal's window.
741 */
742hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800743 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800744};
745
746/**
rginda87b86462011-12-14 13:48:03 -0800747 * Restore a previously saved cursor position.
748 *
749 * @param {hterm.RowCol} cursor The position to restore.
750 */
751hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700752 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
753 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800754 this.screen_.setCursorPosition(row, column);
755 if (cursor.column > column ||
756 cursor.column == column && cursor.overflow) {
757 this.screen_.cursorPosition.overflow = true;
758 }
rginda87b86462011-12-14 13:48:03 -0800759};
760
761/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400762 * Clear the cursor's overflow flag.
763 */
764hterm.Terminal.prototype.clearCursorOverflow = function() {
765 this.screen_.cursorPosition.overflow = false;
766};
767
768/**
Robert Ginda830583c2013-08-07 13:20:46 -0700769 * Sets the cursor shape
770 */
771hterm.Terminal.prototype.setCursorShape = function(shape) {
772 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800773 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700774}
775
776/**
777 * Get the cursor shape
778 */
779hterm.Terminal.prototype.getCursorShape = function() {
780 return this.cursorShape_;
781}
782
783/**
rginda87b86462011-12-14 13:48:03 -0800784 * Set the width of the terminal, resizing the UI to match.
785 */
786hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800787 if (columnCount == null) {
788 this.div_.style.width = '100%';
789 return;
790 }
791
rginda35c456b2012-02-09 17:29:05 -0800792 this.div_.style.width = this.scrollPort_.characterSize.width *
Robert Ginda97769282013-02-01 15:30:30 -0800793 columnCount + this.scrollPort_.currentScrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400794 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800795 this.scheduleSyncCursorPosition_();
796};
rginda87b86462011-12-14 13:48:03 -0800797
rgindac9bc5502012-01-18 11:48:44 -0800798/**
rginda35c456b2012-02-09 17:29:05 -0800799 * Set the height of the terminal, resizing the UI to match.
800 */
801hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800802 if (rowCount == null) {
803 this.div_.style.height = '100%';
804 return;
805 }
806
rginda35c456b2012-02-09 17:29:05 -0800807 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700808 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800809 this.realizeSize_(this.screenSize.width, rowCount);
810 this.scheduleSyncCursorPosition_();
811};
812
813/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400814 * Deal with terminal size changes.
815 *
816 */
817hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
818 if (columnCount != this.screenSize.width)
819 this.realizeWidth_(columnCount);
820
821 if (rowCount != this.screenSize.height)
822 this.realizeHeight_(rowCount);
823
824 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700825 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400826};
827
828/**
rgindac9bc5502012-01-18 11:48:44 -0800829 * Deal with terminal width changes.
830 *
831 * This function does what needs to be done when the terminal width changes
832 * out from under us. It happens here rather than in onResize_() because this
833 * code may need to run synchronously to handle programmatic changes of
834 * terminal width.
835 *
836 * Relying on the browser to send us an async resize event means we may not be
837 * in the correct state yet when the next escape sequence hits.
838 */
839hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700840 if (columnCount <= 0)
841 throw new Error('Attempt to realize bad width: ' + columnCount);
842
rgindac9bc5502012-01-18 11:48:44 -0800843 var deltaColumns = columnCount - this.screen_.getWidth();
844
rginda87b86462011-12-14 13:48:03 -0800845 this.screenSize.width = columnCount;
846 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800847
848 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400849 if (this.defaultTabStops)
850 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800851 } else {
852 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400853 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800854 break;
855
856 this.tabStops_.pop();
857 }
858 }
859
860 this.screen_.setColumnCount(this.screenSize.width);
861};
862
863/**
864 * Deal with terminal height changes.
865 *
866 * This function does what needs to be done when the terminal height changes
867 * out from under us. It happens here rather than in onResize_() because this
868 * code may need to run synchronously to handle programmatic changes of
869 * terminal height.
870 *
871 * Relying on the browser to send us an async resize event means we may not be
872 * in the correct state yet when the next escape sequence hits.
873 */
874hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700875 if (rowCount <= 0)
876 throw new Error('Attempt to realize bad height: ' + rowCount);
877
rgindac9bc5502012-01-18 11:48:44 -0800878 var deltaRows = rowCount - this.screen_.getHeight();
879
880 this.screenSize.height = rowCount;
881
882 var cursor = this.saveCursor();
883
884 if (deltaRows < 0) {
885 // Screen got smaller.
886 deltaRows *= -1;
887 while (deltaRows) {
888 var lastRow = this.getRowCount() - 1;
889 if (lastRow - this.scrollbackRows_.length == cursor.row)
890 break;
891
892 if (this.getRowText(lastRow))
893 break;
894
895 this.screen_.popRow();
896 deltaRows--;
897 }
898
899 var ary = this.screen_.shiftRows(deltaRows);
900 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
901
902 // We just removed rows from the top of the screen, we need to update
903 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800904 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800905 } else if (deltaRows > 0) {
906 // Screen got larger.
907
908 if (deltaRows <= this.scrollbackRows_.length) {
909 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
910 var rows = this.scrollbackRows_.splice(
911 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
912 this.screen_.unshiftRows(rows);
913 deltaRows -= scrollbackCount;
914 cursor.row += scrollbackCount;
915 }
916
917 if (deltaRows)
918 this.appendRows_(deltaRows);
919 }
920
rginda35c456b2012-02-09 17:29:05 -0800921 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800922 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800923};
924
925/**
926 * Scroll the terminal to the top of the scrollback buffer.
927 */
928hterm.Terminal.prototype.scrollHome = function() {
929 this.scrollPort_.scrollRowToTop(0);
930};
931
932/**
933 * Scroll the terminal to the end.
934 */
935hterm.Terminal.prototype.scrollEnd = function() {
936 this.scrollPort_.scrollRowToBottom(this.getRowCount());
937};
938
939/**
940 * Scroll the terminal one page up (minus one line) relative to the current
941 * position.
942 */
943hterm.Terminal.prototype.scrollPageUp = function() {
944 var i = this.scrollPort_.getTopRowIndex();
945 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
946};
947
948/**
949 * Scroll the terminal one page down (minus one line) relative to the current
950 * position.
951 */
952hterm.Terminal.prototype.scrollPageDown = function() {
953 var i = this.scrollPort_.getTopRowIndex();
954 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800955};
956
rgindac9bc5502012-01-18 11:48:44 -0800957/**
Robert Ginda40932892012-12-10 17:26:40 -0800958 * Clear primary screen, secondary screen, and the scrollback buffer.
959 */
960hterm.Terminal.prototype.wipeContents = function() {
961 this.scrollbackRows_.length = 0;
962 this.scrollPort_.resetCache();
963
964 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
965 var bottom = screen.getHeight();
966 if (bottom > 0) {
967 this.renumberRows_(0, bottom);
968 this.clearHome(screen);
969 }
970 }.bind(this));
971
972 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -0700973 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -0800974};
975
976/**
rgindac9bc5502012-01-18 11:48:44 -0800977 * Full terminal reset.
978 */
rginda87b86462011-12-14 13:48:03 -0800979hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800980 this.clearAllTabStops();
981 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700982
983 this.clearHome(this.primaryScreen_);
984 this.primaryScreen_.textAttributes.reset();
985
986 this.clearHome(this.alternateScreen_);
987 this.alternateScreen_.textAttributes.reset();
988
rgindab8bc8932012-04-27 12:45:03 -0700989 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
990
Robert Ginda92e18102013-03-14 13:56:37 -0700991 this.vt.reset();
992
rgindac9bc5502012-01-18 11:48:44 -0800993 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800994};
995
rgindac9bc5502012-01-18 11:48:44 -0800996/**
997 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700998 *
999 * Perform a soft reset to the default values listed in
1000 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001001 */
rginda0f5c0292012-01-13 11:00:13 -08001002hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -07001003 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001004 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001005
rgindab8bc8932012-04-27 12:45:03 -07001006 // Xterm also resets the color palette on soft reset, even though it doesn't
1007 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -07001008 this.primaryScreen_.textAttributes.resetColorPalette();
1009 this.alternateScreen_.textAttributes.resetColorPalette();
1010
rgindab8bc8932012-04-27 12:45:03 -07001011 // The xterm man page explicitly says this will happen on soft reset.
1012 this.setVTScrollRegion(null, null);
1013
1014 // Xterm also shows the cursor on soft reset, but does not alter the blink
1015 // state.
rgindaa19afe22012-01-25 15:40:22 -08001016 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001017};
1018
rgindac9bc5502012-01-18 11:48:44 -08001019/**
1020 * Move the cursor forward to the next tab stop, or to the last column
1021 * if no more tab stops are set.
1022 */
1023hterm.Terminal.prototype.forwardTabStop = function() {
1024 var column = this.screen_.cursorPosition.column;
1025
1026 for (var i = 0; i < this.tabStops_.length; i++) {
1027 if (this.tabStops_[i] > column) {
1028 this.setCursorColumn(this.tabStops_[i]);
1029 return;
1030 }
1031 }
1032
David Benjamin66e954d2012-05-05 21:08:12 -04001033 // xterm does not clear the overflow flag on HT or CHT.
1034 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001035 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001036 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001037};
1038
rgindac9bc5502012-01-18 11:48:44 -08001039/**
1040 * Move the cursor backward to the previous tab stop, or to the first column
1041 * if no previous tab stops are set.
1042 */
1043hterm.Terminal.prototype.backwardTabStop = function() {
1044 var column = this.screen_.cursorPosition.column;
1045
1046 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1047 if (this.tabStops_[i] < column) {
1048 this.setCursorColumn(this.tabStops_[i]);
1049 return;
1050 }
1051 }
1052
1053 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001054};
1055
rgindac9bc5502012-01-18 11:48:44 -08001056/**
1057 * Set a tab stop at the given column.
1058 *
1059 * @param {int} column Zero based column.
1060 */
1061hterm.Terminal.prototype.setTabStop = function(column) {
1062 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1063 if (this.tabStops_[i] == column)
1064 return;
1065
1066 if (this.tabStops_[i] < column) {
1067 this.tabStops_.splice(i + 1, 0, column);
1068 return;
1069 }
1070 }
1071
1072 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001073};
1074
rgindac9bc5502012-01-18 11:48:44 -08001075/**
1076 * Clear the tab stop at the current cursor position.
1077 *
1078 * No effect if there is no tab stop at the current cursor position.
1079 */
1080hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1081 var column = this.screen_.cursorPosition.column;
1082
1083 var i = this.tabStops_.indexOf(column);
1084 if (i == -1)
1085 return;
1086
1087 this.tabStops_.splice(i, 1);
1088};
1089
1090/**
1091 * Clear all tab stops.
1092 */
1093hterm.Terminal.prototype.clearAllTabStops = function() {
1094 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001095 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001096};
1097
1098/**
1099 * Set up the default tab stops, starting from a given column.
1100 *
1101 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001102 * from the specified column, or 0 if no column is provided. It also flags
1103 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001104 *
1105 * This does not clear the existing tab stops first, use clearAllTabStops
1106 * for that.
1107 *
1108 * @param {int} opt_start Optional starting zero based starting column, useful
1109 * for filling out missing tab stops when the terminal is resized.
1110 */
1111hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1112 var start = opt_start || 0;
1113 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001114 // Round start up to a default tab stop.
1115 start = start - 1 - ((start - 1) % w) + w;
1116 for (var i = start; i < this.screenSize.width; i += w) {
1117 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001118 }
David Benjamin66e954d2012-05-05 21:08:12 -04001119
1120 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001121};
1122
rginda6d397402012-01-17 10:58:29 -08001123/**
rginda8ba33642011-12-14 12:31:31 -08001124 * Interpret a sequence of characters.
1125 *
1126 * Incomplete escape sequences are buffered until the next call.
1127 *
1128 * @param {string} str Sequence of characters to interpret or pass through.
1129 */
1130hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001131 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001132 this.scheduleSyncCursorPosition_();
1133};
1134
1135/**
1136 * Take over the given DIV for use as the terminal display.
1137 *
1138 * @param {HTMLDivElement} div The div to use as the terminal display.
1139 */
1140hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001141 this.div_ = div;
1142
rginda8ba33642011-12-14 12:31:31 -08001143 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001144 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001145 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1146 this.scrollPort_.setBackgroundPosition(
1147 this.prefs_.get('background-position'));
Robert Gindae76aa9f2014-03-14 12:29:12 -07001148 this.scrollPort_.setUserCss(this.prefs_.get('user-css'));
rginda30f20f62012-04-05 16:36:19 -07001149
rginda0918b652012-04-04 11:26:24 -07001150 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001151
rginda9f5222b2012-03-05 11:53:28 -08001152 this.setFontSize(this.prefs_.get('font-size'));
1153 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001154
David Reveman8f552492012-03-28 12:18:41 -04001155 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1156
rginda8ba33642011-12-14 12:31:31 -08001157 this.document_ = this.scrollPort_.getDocument();
1158
rginda4bba5e12012-06-20 16:15:30 -07001159 this.document_.body.oncontextmenu = function() { return false };
1160
1161 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001162 var screenNode = this.scrollPort_.getScreenNode();
1163 screenNode.addEventListener('mousedown', onMouse);
1164 screenNode.addEventListener('mouseup', onMouse);
1165 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001166 this.scrollPort_.onScrollWheel = onMouse;
1167
Toni Barzic0bfa8922013-11-22 11:18:35 -08001168 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001169 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001170 // Listen for mousedown events on the screenNode as in FF the focus
1171 // events don't bubble.
1172 screenNode.addEventListener('mousedown', function() {
1173 setTimeout(this.onFocusChange_.bind(this, true));
1174 }.bind(this));
1175
Toni Barzic0bfa8922013-11-22 11:18:35 -08001176 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001177 'blur', this.onFocusChange_.bind(this, false));
1178
1179 var style = this.document_.createElement('style');
1180 style.textContent =
1181 ('.cursor-node[focus="false"] {' +
1182 ' box-sizing: border-box;' +
1183 ' background-color: transparent !important;' +
1184 ' border-width: 2px;' +
1185 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001186 '}' +
1187 '.wc-node {' +
1188 ' display: inline-block;' +
1189 ' text-align: center;' +
1190 ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001191 '}');
1192 this.document_.head.appendChild(style);
1193
Ricky Liang48f05cb2013-12-31 23:35:29 +08001194 var styleSheets = this.document_.styleSheets;
1195 var cssRules = styleSheets[styleSheets.length - 1].cssRules;
1196 this.wcCssRule_ = cssRules[cssRules.length - 1];
1197
rginda8ba33642011-12-14 12:31:31 -08001198 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001199 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001200 this.cursorNode_.style.cssText =
1201 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001202 'top: -99px;' +
1203 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001204 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1205 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
Rob Spies06533ba2014-04-24 11:20:37 -07001206 '-webkit-transition: opacity, background-color 100ms linear;' +
1207 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001208
rginda8e92a692012-05-20 19:37:20 -07001209 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001210 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1211 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001212
rginda8ba33642011-12-14 12:31:31 -08001213 this.document_.body.appendChild(this.cursorNode_);
1214
rgindad5613292012-06-19 15:40:37 -07001215 // When 'enableMouseDragScroll' is off we reposition this element directly
1216 // under the mouse cursor after a click. This makes Chrome associate
1217 // subsequent mousemove events with the scroll-blocker. Since the
1218 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1219 // events do not cause the scrollport to scroll.
1220 //
1221 // It's a hack, but it's the cleanest way I could find.
1222 this.scrollBlockerNode_ = this.document_.createElement('div');
1223 this.scrollBlockerNode_.style.cssText =
1224 ('position: absolute;' +
1225 'top: -99px;' +
1226 'display: block;' +
1227 'width: 10px;' +
1228 'height: 10px;');
1229 this.document_.body.appendChild(this.scrollBlockerNode_);
1230
1231 var onMouse = this.onMouse_.bind(this);
1232 this.scrollPort_.onScrollWheel = onMouse;
1233 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1234 ].forEach(function(event) {
1235 this.scrollBlockerNode_.addEventListener(event, onMouse);
1236 this.cursorNode_.addEventListener(event, onMouse);
1237 this.document_.addEventListener(event, onMouse);
1238 }.bind(this));
1239
1240 this.cursorNode_.addEventListener('mousedown', function() {
1241 setTimeout(this.focus.bind(this));
1242 }.bind(this));
1243
rginda8ba33642011-12-14 12:31:31 -08001244 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001245
rginda87b86462011-12-14 13:48:03 -08001246 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001247 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001248};
1249
rginda0918b652012-04-04 11:26:24 -07001250/**
1251 * Return the HTML document that contains the terminal DOM nodes.
1252 */
rginda87b86462011-12-14 13:48:03 -08001253hterm.Terminal.prototype.getDocument = function() {
1254 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001255};
1256
1257/**
rginda0918b652012-04-04 11:26:24 -07001258 * Focus the terminal.
1259 */
1260hterm.Terminal.prototype.focus = function() {
1261 this.scrollPort_.focus();
1262};
1263
1264/**
rginda8ba33642011-12-14 12:31:31 -08001265 * Return the HTML Element for a given row index.
1266 *
1267 * This is a method from the RowProvider interface. The ScrollPort uses
1268 * it to fetch rows on demand as they are scrolled into view.
1269 *
1270 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1271 * pairs to conserve memory.
1272 *
1273 * @param {integer} index The zero-based row index, measured relative to the
1274 * start of the scrollback buffer. On-screen rows will always have the
1275 * largest indicies.
1276 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1277 */
1278hterm.Terminal.prototype.getRowNode = function(index) {
1279 if (index < this.scrollbackRows_.length)
1280 return this.scrollbackRows_[index];
1281
1282 var screenIndex = index - this.scrollbackRows_.length;
1283 return this.screen_.rowsArray[screenIndex];
1284};
1285
1286/**
1287 * Return the text content for a given range of rows.
1288 *
1289 * This is a method from the RowProvider interface. The ScrollPort uses
1290 * it to fetch text content on demand when the user attempts to copy their
1291 * selection to the clipboard.
1292 *
1293 * @param {integer} start The zero-based row index to start from, measured
1294 * relative to the start of the scrollback buffer. On-screen rows will
1295 * always have the largest indicies.
1296 * @param {integer} end The zero-based row index to end on, measured
1297 * relative to the start of the scrollback buffer.
1298 * @return {string} A single string containing the text value of the range of
1299 * rows. Lines will be newline delimited, with no trailing newline.
1300 */
1301hterm.Terminal.prototype.getRowsText = function(start, end) {
1302 var ary = [];
1303 for (var i = start; i < end; i++) {
1304 var node = this.getRowNode(i);
1305 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001306 if (i < end - 1 && !node.getAttribute('line-overflow'))
1307 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001308 }
1309
rgindaa09e7332012-08-17 12:49:51 -07001310 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001311};
1312
1313/**
1314 * Return the text content for a given row.
1315 *
1316 * This is a method from the RowProvider interface. The ScrollPort uses
1317 * it to fetch text content on demand when the user attempts to copy their
1318 * selection to the clipboard.
1319 *
1320 * @param {integer} index The zero-based row index to return, measured
1321 * relative to the start of the scrollback buffer. On-screen rows will
1322 * always have the largest indicies.
1323 * @return {string} A string containing the text value of the selected row.
1324 */
1325hterm.Terminal.prototype.getRowText = function(index) {
1326 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001327 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001328};
1329
1330/**
1331 * Return the total number of rows in the addressable screen and in the
1332 * scrollback buffer of this terminal.
1333 *
1334 * This is a method from the RowProvider interface. The ScrollPort uses
1335 * it to compute the size of the scrollbar.
1336 *
1337 * @return {integer} The number of rows in this terminal.
1338 */
1339hterm.Terminal.prototype.getRowCount = function() {
1340 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1341};
1342
1343/**
1344 * Create DOM nodes for new rows and append them to the end of the terminal.
1345 *
1346 * This is the only correct way to add a new DOM node for a row. Notice that
1347 * the new row is appended to the bottom of the list of rows, and does not
1348 * require renumbering (of the rowIndex property) of previous rows.
1349 *
1350 * If you think you want a new blank row somewhere in the middle of the
1351 * terminal, look into moveRows_().
1352 *
1353 * This method does not pay attention to vtScrollTop/Bottom, since you should
1354 * be using moveRows() in cases where they would matter.
1355 *
1356 * The cursor will be positioned at column 0 of the first inserted line.
1357 */
1358hterm.Terminal.prototype.appendRows_ = function(count) {
1359 var cursorRow = this.screen_.rowsArray.length;
1360 var offset = this.scrollbackRows_.length + cursorRow;
1361 for (var i = 0; i < count; i++) {
1362 var row = this.document_.createElement('x-row');
1363 row.appendChild(this.document_.createTextNode(''));
1364 row.rowIndex = offset + i;
1365 this.screen_.pushRow(row);
1366 }
1367
1368 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1369 if (extraRows > 0) {
1370 var ary = this.screen_.shiftRows(extraRows);
1371 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001372 if (this.scrollPort_.isScrolledEnd)
1373 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001374 }
1375
1376 if (cursorRow >= this.screen_.rowsArray.length)
1377 cursorRow = this.screen_.rowsArray.length - 1;
1378
rginda87b86462011-12-14 13:48:03 -08001379 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001380};
1381
1382/**
1383 * Relocate rows from one part of the addressable screen to another.
1384 *
1385 * This is used to recycle rows during VT scrolls (those which are driven
1386 * by VT commands, rather than by the user manipulating the scrollbar.)
1387 *
1388 * In this case, the blank lines scrolled into the scroll region are made of
1389 * the nodes we scrolled off. These have their rowIndex properties carefully
1390 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001391 */
1392hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1393 var ary = this.screen_.removeRows(fromIndex, count);
1394 this.screen_.insertRows(toIndex, ary);
1395
1396 var start, end;
1397 if (fromIndex < toIndex) {
1398 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001399 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001400 } else {
1401 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001402 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001403 }
1404
1405 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001406 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001407};
1408
1409/**
1410 * Renumber the rowIndex property of the given range of rows.
1411 *
1412 * The start and end indicies are relative to the screen, not the scrollback.
1413 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001414 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001415 * no need to renumber scrollback rows.
1416 */
Robert Ginda40932892012-12-10 17:26:40 -08001417hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1418 var screen = opt_screen || this.screen_;
1419
rginda8ba33642011-12-14 12:31:31 -08001420 var offset = this.scrollbackRows_.length;
1421 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001422 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001423 }
1424};
1425
1426/**
1427 * Print a string to the terminal.
1428 *
1429 * This respects the current insert and wraparound modes. It will add new lines
1430 * to the end of the terminal, scrolling off the top into the scrollback buffer
1431 * if necessary.
1432 *
1433 * The string is *not* parsed for escape codes. Use the interpret() method if
1434 * that's what you're after.
1435 *
1436 * @param{string} str The string to print.
1437 */
1438hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001439 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001440
Ricky Liang48f05cb2013-12-31 23:35:29 +08001441 var strWidth = lib.wc.strWidth(str);
1442
1443 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001444 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1445 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001446 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001447 }
rgindaa19afe22012-01-25 15:40:22 -08001448
Ricky Liang48f05cb2013-12-31 23:35:29 +08001449 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001450 var didOverflow = false;
1451 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001452
rgindaa9abdd82012-08-06 18:05:09 -07001453 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1454 didOverflow = true;
1455 count = this.screenSize.width - this.screen_.cursorPosition.column;
1456 }
rgindaa19afe22012-01-25 15:40:22 -08001457
rgindaa9abdd82012-08-06 18:05:09 -07001458 if (didOverflow && !this.options_.wraparound) {
1459 // If the string overflowed the line but wraparound is off, then the
1460 // last printed character should be the last of the string.
1461 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001462 substr = lib.wc.substr(str, startOffset, count - 1) +
1463 lib.wc.substr(str, strWidth - 1);
1464 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001465 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001466 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001467 }
rgindaa19afe22012-01-25 15:40:22 -08001468
Ricky Liang48f05cb2013-12-31 23:35:29 +08001469 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1470 for (var i = 0; i < tokens.length; i++) {
1471 if (tokens[i].wcNode)
1472 this.screen_.textAttributes.wcNode = true;
1473
1474 if (this.options_.insertMode) {
1475 this.screen_.insertString(tokens[i].str);
1476 } else {
1477 this.screen_.overwriteString(tokens[i].str);
1478 }
1479 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001480 }
1481
1482 this.screen_.maybeClipCurrentRow();
1483 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001484 }
rginda8ba33642011-12-14 12:31:31 -08001485
1486 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001487
rginda9f5222b2012-03-05 11:53:28 -08001488 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001489 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001490};
1491
1492/**
rginda87b86462011-12-14 13:48:03 -08001493 * Set the VT scroll region.
1494 *
rginda87b86462011-12-14 13:48:03 -08001495 * This also resets the cursor position to the absolute (0, 0) position, since
1496 * that's what xterm appears to do.
1497 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001498 * Setting the scroll region to the full height of the terminal will clear
1499 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1500 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1501 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1502 * continue to work as most users would expect.
1503 *
rginda87b86462011-12-14 13:48:03 -08001504 * @param {integer} scrollTop The zero-based top of the scroll region.
1505 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1506 * inclusive.
1507 */
1508hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001509 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001510 this.vtScrollTop_ = null;
1511 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001512 } else {
1513 this.vtScrollTop_ = scrollTop;
1514 this.vtScrollBottom_ = scrollBottom;
1515 }
rginda87b86462011-12-14 13:48:03 -08001516};
1517
1518/**
rginda8ba33642011-12-14 12:31:31 -08001519 * Return the top row index according to the VT.
1520 *
1521 * This will return 0 unless the terminal has been told to restrict scrolling
1522 * to some lower row. It is used for some VT cursor positioning and scrolling
1523 * commands.
1524 *
1525 * @return {integer} The topmost row in the terminal's scroll region.
1526 */
1527hterm.Terminal.prototype.getVTScrollTop = function() {
1528 if (this.vtScrollTop_ != null)
1529 return this.vtScrollTop_;
1530
1531 return 0;
rginda87b86462011-12-14 13:48:03 -08001532};
rginda8ba33642011-12-14 12:31:31 -08001533
1534/**
1535 * Return the bottom row index according to the VT.
1536 *
1537 * This will return the height of the terminal unless the it has been told to
1538 * restrict scrolling to some higher row. It is used for some VT cursor
1539 * positioning and scrolling commands.
1540 *
1541 * @return {integer} The bottommost row in the terminal's scroll region.
1542 */
1543hterm.Terminal.prototype.getVTScrollBottom = function() {
1544 if (this.vtScrollBottom_ != null)
1545 return this.vtScrollBottom_;
1546
rginda87b86462011-12-14 13:48:03 -08001547 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001548}
1549
1550/**
1551 * Process a '\n' character.
1552 *
1553 * If the cursor is on the final row of the terminal this will append a new
1554 * blank row to the screen and scroll the topmost row into the scrollback
1555 * buffer.
1556 *
1557 * Otherwise, this moves the cursor to column zero of the next row.
1558 */
1559hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001560 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1561 this.screen_.rowsArray.length - 1);
1562
1563 if (this.vtScrollBottom_ != null) {
1564 // A VT Scroll region is active, we never append new rows.
1565 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1566 // We're at the end of the VT Scroll Region, perform a VT scroll.
1567 this.vtScrollUp(1);
1568 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1569 } else if (cursorAtEndOfScreen) {
1570 // We're at the end of the screen, the only thing to do is put the
1571 // cursor to column 0.
1572 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1573 } else {
1574 // Anywhere else, advance the cursor row, and reset the column.
1575 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1576 }
1577 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001578 // We're at the end of the screen. Append a new row to the terminal,
1579 // shifting the top row into the scrollback.
1580 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001581 } else {
rginda87b86462011-12-14 13:48:03 -08001582 // Anywhere else in the screen just moves the cursor.
1583 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001584 }
1585};
1586
1587/**
1588 * Like newLine(), except maintain the cursor column.
1589 */
1590hterm.Terminal.prototype.lineFeed = function() {
1591 var column = this.screen_.cursorPosition.column;
1592 this.newLine();
1593 this.setCursorColumn(column);
1594};
1595
1596/**
rginda87b86462011-12-14 13:48:03 -08001597 * If autoCarriageReturn is set then newLine(), else lineFeed().
1598 */
1599hterm.Terminal.prototype.formFeed = function() {
1600 if (this.options_.autoCarriageReturn) {
1601 this.newLine();
1602 } else {
1603 this.lineFeed();
1604 }
1605};
1606
1607/**
1608 * Move the cursor up one row, possibly inserting a blank line.
1609 *
1610 * The cursor column is not changed.
1611 */
1612hterm.Terminal.prototype.reverseLineFeed = function() {
1613 var scrollTop = this.getVTScrollTop();
1614 var currentRow = this.screen_.cursorPosition.row;
1615
1616 if (currentRow == scrollTop) {
1617 this.insertLines(1);
1618 } else {
1619 this.setAbsoluteCursorRow(currentRow - 1);
1620 }
1621};
1622
1623/**
rginda8ba33642011-12-14 12:31:31 -08001624 * Replace all characters to the left of the current cursor with the space
1625 * character.
1626 *
1627 * TODO(rginda): This should probably *remove* the characters (not just replace
1628 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001629 * position.
rginda8ba33642011-12-14 12:31:31 -08001630 */
1631hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001632 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001633 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001634 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001635 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001636};
1637
1638/**
David Benjamin684a9b72012-05-01 17:19:58 -04001639 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001640 *
1641 * The cursor position is unchanged.
1642 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001643 * If the current background color is not the default background color this
1644 * will insert spaces rather than delete. This is unfortunate because the
1645 * trailing space will affect text selection, but it's difficult to come up
1646 * with a way to style empty space that wouldn't trip up the hterm.Screen
1647 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001648 *
1649 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1650 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1651 * crbug.com/232390 for details.
rginda8ba33642011-12-14 12:31:31 -08001652 */
1653hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001654 if (this.screen_.cursorPosition.overflow)
1655 return;
1656
Robert Ginda7fd57082012-09-25 14:41:47 -07001657 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1658 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001659
1660 if (this.screen_.textAttributes.background ===
1661 this.screen_.textAttributes.DEFAULT_COLOR) {
1662 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001663 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001664 this.screen_.cursorPosition.column + count) {
1665 this.screen_.deleteChars(count);
1666 this.clearCursorOverflow();
1667 return;
1668 }
1669 }
1670
rginda87b86462011-12-14 13:48:03 -08001671 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001672 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001673 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001674 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001675};
1676
1677/**
1678 * Erase the current line.
1679 *
1680 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001681 */
1682hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001683 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001684 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001685 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001686 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001687};
1688
1689/**
David Benjamina08d78f2012-05-05 00:28:49 -04001690 * Erase all characters from the start of the screen to the current cursor
1691 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001692 *
1693 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001694 */
1695hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001696 var cursor = this.saveCursor();
1697
1698 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001699
David Benjamina08d78f2012-05-05 00:28:49 -04001700 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001701 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001702 this.screen_.clearCursorRow();
1703 }
1704
rginda87b86462011-12-14 13:48:03 -08001705 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001706 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001707};
1708
1709/**
1710 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001711 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001712 *
1713 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001714 */
1715hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001716 var cursor = this.saveCursor();
1717
1718 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001719
David Benjamina08d78f2012-05-05 00:28:49 -04001720 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001721 for (var i = cursor.row + 1; i <= bottom; i++) {
1722 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001723 this.screen_.clearCursorRow();
1724 }
1725
rginda87b86462011-12-14 13:48:03 -08001726 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001727 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001728};
1729
1730/**
1731 * Fill the terminal with a given character.
1732 *
1733 * This methods does not respect the VT scroll region.
1734 *
1735 * @param {string} ch The character to use for the fill.
1736 */
1737hterm.Terminal.prototype.fill = function(ch) {
1738 var cursor = this.saveCursor();
1739
1740 this.setAbsoluteCursorPosition(0, 0);
1741 for (var row = 0; row < this.screenSize.height; row++) {
1742 for (var col = 0; col < this.screenSize.width; col++) {
1743 this.setAbsoluteCursorPosition(row, col);
1744 this.screen_.overwriteString(ch);
1745 }
1746 }
1747
1748 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001749};
1750
1751/**
rginda9ea433c2012-03-16 11:57:00 -07001752 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001753 *
rginda9ea433c2012-03-16 11:57:00 -07001754 * This does not respect the scroll region.
1755 *
1756 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1757 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001758 */
rginda9ea433c2012-03-16 11:57:00 -07001759hterm.Terminal.prototype.clearHome = function(opt_screen) {
1760 var screen = opt_screen || this.screen_;
1761 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001762
rginda11057d52012-04-25 12:29:56 -07001763 if (bottom == 0) {
1764 // Empty screen, nothing to do.
1765 return;
1766 }
1767
rgindae4d29232012-01-19 10:47:13 -08001768 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001769 screen.setCursorPosition(i, 0);
1770 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001771 }
1772
rginda9ea433c2012-03-16 11:57:00 -07001773 screen.setCursorPosition(0, 0);
1774};
1775
1776/**
1777 * Erase the entire display without changing the cursor position.
1778 *
1779 * The cursor position is unchanged. This does not respect the scroll
1780 * region.
1781 *
1782 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1783 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001784 */
1785hterm.Terminal.prototype.clear = function(opt_screen) {
1786 var screen = opt_screen || this.screen_;
1787 var cursor = screen.cursorPosition.clone();
1788 this.clearHome(screen);
1789 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001790};
1791
1792/**
1793 * VT command to insert lines at the current cursor row.
1794 *
1795 * This respects the current scroll region. Rows pushed off the bottom are
1796 * lost (they won't show up in the scrollback buffer).
1797 *
rginda8ba33642011-12-14 12:31:31 -08001798 * @param {integer} count The number of lines to insert.
1799 */
1800hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001801 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001802
1803 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001804 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001805
Robert Ginda579186b2012-09-26 11:40:04 -07001806 // The moveCount is the number of rows we need to relocate to make room for
1807 // the new row(s). The count is the distance to move them.
1808 var moveCount = bottom - cursorRow - count + 1;
1809 if (moveCount)
1810 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001811
Robert Ginda579186b2012-09-26 11:40:04 -07001812 for (var i = count - 1; i >= 0; i--) {
1813 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001814 this.screen_.clearCursorRow();
1815 }
rginda8ba33642011-12-14 12:31:31 -08001816};
1817
1818/**
1819 * VT command to delete lines at the current cursor row.
1820 *
1821 * New rows are added to the bottom of scroll region to take their place. New
1822 * rows are strictly there to take up space and have no content or style.
1823 */
1824hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001825 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001826
rginda87b86462011-12-14 13:48:03 -08001827 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001828 var bottom = this.getVTScrollBottom();
1829
rginda87b86462011-12-14 13:48:03 -08001830 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001831 count = Math.min(count, maxCount);
1832
rginda87b86462011-12-14 13:48:03 -08001833 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001834 if (count != maxCount)
1835 this.moveRows_(top, count, moveStart);
1836
1837 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001838 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001839 this.screen_.clearCursorRow();
1840 }
1841
rginda87b86462011-12-14 13:48:03 -08001842 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001843 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001844};
1845
1846/**
1847 * Inserts the given number of spaces at the current cursor position.
1848 *
rginda87b86462011-12-14 13:48:03 -08001849 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001850 */
1851hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001852 var cursor = this.saveCursor();
1853
rgindacbbd7482012-06-13 15:06:16 -07001854 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001855 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001856 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001857
1858 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001859 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001860};
1861
1862/**
1863 * Forward-delete the specified number of characters starting at the cursor
1864 * position.
1865 *
1866 * @param {integer} count The number of characters to delete.
1867 */
1868hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001869 var deleted = this.screen_.deleteChars(count);
1870 if (deleted && !this.screen_.textAttributes.isDefault()) {
1871 var cursor = this.saveCursor();
1872 this.setCursorColumn(this.screenSize.width - deleted);
1873 this.screen_.insertString(lib.f.getWhitespace(deleted));
1874 this.restoreCursor(cursor);
1875 }
1876
David Benjamin54e8bf62012-06-01 22:31:40 -04001877 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001878};
1879
1880/**
1881 * Shift rows in the scroll region upwards by a given number of lines.
1882 *
1883 * New rows are inserted at the bottom of the scroll region to fill the
1884 * vacated rows. The new rows not filled out with the current text attributes.
1885 *
1886 * This function does not affect the scrollback rows at all. Rows shifted
1887 * off the top are lost.
1888 *
rginda87b86462011-12-14 13:48:03 -08001889 * The cursor position is not altered.
1890 *
rginda8ba33642011-12-14 12:31:31 -08001891 * @param {integer} count The number of rows to scroll.
1892 */
1893hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001894 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001895
rginda87b86462011-12-14 13:48:03 -08001896 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001897 this.deleteLines(count);
1898
rginda87b86462011-12-14 13:48:03 -08001899 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001900};
1901
1902/**
1903 * Shift rows below the cursor down by a given number of lines.
1904 *
1905 * This function respects the current scroll region.
1906 *
1907 * New rows are inserted at the top of the scroll region to fill the
1908 * vacated rows. The new rows not filled out with the current text attributes.
1909 *
1910 * This function does not affect the scrollback rows at all. Rows shifted
1911 * off the bottom are lost.
1912 *
1913 * @param {integer} count The number of rows to scroll.
1914 */
1915hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001916 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001917
rginda87b86462011-12-14 13:48:03 -08001918 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001919 this.insertLines(opt_count);
1920
rginda87b86462011-12-14 13:48:03 -08001921 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001922};
1923
rginda87b86462011-12-14 13:48:03 -08001924
rginda8ba33642011-12-14 12:31:31 -08001925/**
1926 * Set the cursor position.
1927 *
1928 * The cursor row is relative to the scroll region if the terminal has
1929 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1930 *
1931 * @param {integer} row The new zero-based cursor row.
1932 * @param {integer} row The new zero-based cursor column.
1933 */
1934hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1935 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001936 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001937 } else {
rginda87b86462011-12-14 13:48:03 -08001938 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001939 }
rginda87b86462011-12-14 13:48:03 -08001940};
rginda8ba33642011-12-14 12:31:31 -08001941
rginda87b86462011-12-14 13:48:03 -08001942hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1943 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001944 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1945 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001946 this.screen_.setCursorPosition(row, column);
1947};
1948
1949hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001950 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1951 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001952 this.screen_.setCursorPosition(row, column);
1953};
1954
1955/**
1956 * Set the cursor column.
1957 *
1958 * @param {integer} column The new zero-based cursor column.
1959 */
1960hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001961 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001962};
1963
1964/**
1965 * Return the cursor column.
1966 *
1967 * @return {integer} The zero-based cursor column.
1968 */
1969hterm.Terminal.prototype.getCursorColumn = function() {
1970 return this.screen_.cursorPosition.column;
1971};
1972
1973/**
1974 * Set the cursor row.
1975 *
1976 * The cursor row is relative to the scroll region if the terminal has
1977 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1978 *
1979 * @param {integer} row The new cursor row.
1980 */
rginda87b86462011-12-14 13:48:03 -08001981hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1982 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001983};
1984
1985/**
1986 * Return the cursor row.
1987 *
1988 * @return {integer} The zero-based cursor row.
1989 */
1990hterm.Terminal.prototype.getCursorRow = function(row) {
1991 return this.screen_.cursorPosition.row;
1992};
1993
1994/**
1995 * Request that the ScrollPort redraw itself soon.
1996 *
1997 * The redraw will happen asynchronously, soon after the call stack winds down.
1998 * Multiple calls will be coalesced into a single redraw.
1999 */
2000hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002001 if (this.timeouts_.redraw)
2002 return;
rginda8ba33642011-12-14 12:31:31 -08002003
2004 var self = this;
rginda87b86462011-12-14 13:48:03 -08002005 this.timeouts_.redraw = setTimeout(function() {
2006 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002007 self.scrollPort_.redraw_();
2008 }, 0);
2009};
2010
2011/**
2012 * Request that the ScrollPort be scrolled to the bottom.
2013 *
2014 * The scroll will happen asynchronously, soon after the call stack winds down.
2015 * Multiple calls will be coalesced into a single scroll.
2016 *
2017 * This affects the scrollbar position of the ScrollPort, and has nothing to
2018 * do with the VT scroll commands.
2019 */
2020hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2021 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002022 return;
rginda8ba33642011-12-14 12:31:31 -08002023
2024 var self = this;
2025 this.timeouts_.scrollDown = setTimeout(function() {
2026 delete self.timeouts_.scrollDown;
2027 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2028 }, 10);
2029};
2030
2031/**
2032 * Move the cursor up a specified number of rows.
2033 *
2034 * @param {integer} count The number of rows to move the cursor.
2035 */
2036hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002037 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002038};
2039
2040/**
2041 * Move the cursor down a specified number of rows.
2042 *
2043 * @param {integer} count The number of rows to move the cursor.
2044 */
2045hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002046 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002047 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2048 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2049 this.screenSize.height - 1);
2050
rgindacbbd7482012-06-13 15:06:16 -07002051 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002052 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002053 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002054};
2055
2056/**
2057 * Move the cursor left a specified number of columns.
2058 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002059 * If reverse wraparound mode is enabled and the previous row wrapped into
2060 * the current row then we back up through the wraparound as well.
2061 *
rginda8ba33642011-12-14 12:31:31 -08002062 * @param {integer} count The number of columns to move the cursor.
2063 */
2064hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002065 count = count || 1;
2066
2067 if (count < 1)
2068 return;
2069
2070 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002071 if (this.options_.reverseWraparound) {
2072 if (this.screen_.cursorPosition.overflow) {
2073 // If this cursor is in the right margin, consume one count to get it
2074 // back to the last column. This only applies when we're in reverse
2075 // wraparound mode.
2076 count--;
2077 this.clearCursorOverflow();
2078
2079 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002080 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002081 }
2082
Robert Gindabfb32622014-07-17 13:20:27 -07002083 var newRow = this.screen_.cursorPosition.row;
2084 var newColumn = currentColumn - count;
2085 if (newColumn < 0) {
2086 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2087 if (newRow < 0) {
2088 // xterm also wraps from row 0 to the last row.
2089 newRow = this.screenSize.height + newRow % this.screenSize.height;
2090 }
2091 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2092 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002093
Robert Gindabfb32622014-07-17 13:20:27 -07002094 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2095
2096 } else {
2097 var newColumn = Math.max(currentColumn - count, 0);
2098 this.setCursorColumn(newColumn);
2099 }
rginda8ba33642011-12-14 12:31:31 -08002100};
2101
2102/**
2103 * Move the cursor right a specified number of columns.
2104 *
2105 * @param {integer} count The number of columns to move the cursor.
2106 */
2107hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002108 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002109
2110 if (count < 1)
2111 return;
2112
rgindacbbd7482012-06-13 15:06:16 -07002113 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002114 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002115 this.setCursorColumn(column);
2116};
2117
2118/**
2119 * Reverse the foreground and background colors of the terminal.
2120 *
2121 * This only affects text that was drawn with no attributes.
2122 *
2123 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2124 * been drawn with attributes that happen to coincide with the default
2125 * 'no-attribute' colors. My guess is probably not.
2126 */
2127hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002128 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002129 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002130 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2131 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002132 } else {
rginda9f5222b2012-03-05 11:53:28 -08002133 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2134 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002135 }
2136};
2137
2138/**
rginda87b86462011-12-14 13:48:03 -08002139 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002140 *
2141 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002142 */
2143hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002144 this.cursorNode_.style.backgroundColor =
2145 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002146
2147 var self = this;
2148 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002149 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002150 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002151
Michael Kelly485ecd12014-06-09 11:41:56 -04002152 // bellSquelchTimeout_ affects both audio and notification bells.
2153 if (this.bellSquelchTimeout_)
2154 return;
2155
Robert Ginda92e18102013-03-14 13:56:37 -07002156 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002157 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002158 this.bellSequelchTimeout_ = setTimeout(function() {
2159 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002160 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002161 } else {
2162 delete this.bellSquelchTimeout_;
2163 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002164
2165 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
2166 var n = new Notification(
2167 lib.f.replaceVars(hterm.desktopNotificationTitle,
Robert Ginda348dc2b2014-06-24 14:42:23 -07002168 {'title': window.document.title || 'hterm'}));
Michael Kelly485ecd12014-06-09 11:41:56 -04002169 this.bellNotificationList_.push(n);
2170 // TODO: Should we try to raise the window here?
2171 n.onclick = function() { self.closeBellNotifications_(); };
2172 }
rginda87b86462011-12-14 13:48:03 -08002173};
2174
2175/**
rginda8ba33642011-12-14 12:31:31 -08002176 * Set the origin mode bit.
2177 *
2178 * If origin mode is on, certain VT cursor and scrolling commands measure their
2179 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2180 * to the top of the addressable screen.
2181 *
2182 * Defaults to off.
2183 *
2184 * @param {boolean} state True to set origin mode, false to unset.
2185 */
2186hterm.Terminal.prototype.setOriginMode = function(state) {
2187 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002188 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002189};
2190
2191/**
2192 * Set the insert mode bit.
2193 *
2194 * If insert mode is on, existing text beyond the cursor position will be
2195 * shifted right to make room for new text. Otherwise, new text overwrites
2196 * any existing text.
2197 *
2198 * Defaults to off.
2199 *
2200 * @param {boolean} state True to set insert mode, false to unset.
2201 */
2202hterm.Terminal.prototype.setInsertMode = function(state) {
2203 this.options_.insertMode = state;
2204};
2205
2206/**
rginda87b86462011-12-14 13:48:03 -08002207 * Set the auto carriage return bit.
2208 *
2209 * If auto carriage return is on then a formfeed character is interpreted
2210 * as a newline, otherwise it's the same as a linefeed. The difference boils
2211 * down to whether or not the cursor column is reset.
2212 */
2213hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2214 this.options_.autoCarriageReturn = state;
2215};
2216
2217/**
rginda8ba33642011-12-14 12:31:31 -08002218 * Set the wraparound mode bit.
2219 *
2220 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2221 * to the start of the following row. Otherwise, the cursor is clamped to the
2222 * end of the screen and attempts to write past it are ignored.
2223 *
2224 * Defaults to on.
2225 *
2226 * @param {boolean} state True to set wraparound mode, false to unset.
2227 */
2228hterm.Terminal.prototype.setWraparound = function(state) {
2229 this.options_.wraparound = state;
2230};
2231
2232/**
2233 * Set the reverse-wraparound mode bit.
2234 *
2235 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2236 * to the end of the previous row. Otherwise, the cursor is clamped to column
2237 * 0.
2238 *
2239 * Defaults to off.
2240 *
2241 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2242 */
2243hterm.Terminal.prototype.setReverseWraparound = function(state) {
2244 this.options_.reverseWraparound = state;
2245};
2246
2247/**
2248 * Selects between the primary and alternate screens.
2249 *
2250 * If alternate mode is on, the alternate screen is active. Otherwise the
2251 * primary screen is active.
2252 *
2253 * Swapping screens has no effect on the scrollback buffer.
2254 *
2255 * Each screen maintains its own cursor position.
2256 *
2257 * Defaults to off.
2258 *
2259 * @param {boolean} state True to set alternate mode, false to unset.
2260 */
2261hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002262 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002263 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2264
rginda35c456b2012-02-09 17:29:05 -08002265 if (this.screen_.rowsArray.length &&
2266 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2267 // If the screen changed sizes while we were away, our rowIndexes may
2268 // be incorrect.
2269 var offset = this.scrollbackRows_.length;
2270 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002271 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002272 ary[i].rowIndex = offset + i;
2273 }
2274 }
rginda8ba33642011-12-14 12:31:31 -08002275
rginda35c456b2012-02-09 17:29:05 -08002276 this.realizeWidth_(this.screenSize.width);
2277 this.realizeHeight_(this.screenSize.height);
2278 this.scrollPort_.syncScrollHeight();
2279 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002280
rginda6d397402012-01-17 10:58:29 -08002281 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002282 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002283};
2284
2285/**
2286 * Set the cursor-blink mode bit.
2287 *
2288 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2289 * a visible cursor does not blink.
2290 *
2291 * You should make sure to turn blinking off if you're going to dispose of a
2292 * terminal, otherwise you'll leak a timeout.
2293 *
2294 * Defaults to on.
2295 *
2296 * @param {boolean} state True to set cursor-blink mode, false to unset.
2297 */
2298hterm.Terminal.prototype.setCursorBlink = function(state) {
2299 this.options_.cursorBlink = state;
2300
2301 if (!state && this.timeouts_.cursorBlink) {
2302 clearTimeout(this.timeouts_.cursorBlink);
2303 delete this.timeouts_.cursorBlink;
2304 }
2305
2306 if (this.options_.cursorVisible)
2307 this.setCursorVisible(true);
2308};
2309
2310/**
2311 * Set the cursor-visible mode bit.
2312 *
2313 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2314 *
2315 * Defaults to on.
2316 *
2317 * @param {boolean} state True to set cursor-visible mode, false to unset.
2318 */
2319hterm.Terminal.prototype.setCursorVisible = function(state) {
2320 this.options_.cursorVisible = state;
2321
2322 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002323 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002324 return;
2325 }
2326
rginda87b86462011-12-14 13:48:03 -08002327 this.syncCursorPosition_();
2328
2329 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002330
2331 if (this.options_.cursorBlink) {
2332 if (this.timeouts_.cursorBlink)
2333 return;
2334
Robert Gindaea2183e2014-07-17 09:51:51 -07002335 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002336 } else {
2337 if (this.timeouts_.cursorBlink) {
2338 clearTimeout(this.timeouts_.cursorBlink);
2339 delete this.timeouts_.cursorBlink;
2340 }
2341 }
2342};
2343
2344/**
rginda87b86462011-12-14 13:48:03 -08002345 * Synchronizes the visible cursor and document selection with the current
2346 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002347 */
2348hterm.Terminal.prototype.syncCursorPosition_ = function() {
2349 var topRowIndex = this.scrollPort_.getTopRowIndex();
2350 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2351 var cursorRowIndex = this.scrollbackRows_.length +
2352 this.screen_.cursorPosition.row;
2353
2354 if (cursorRowIndex > bottomRowIndex) {
2355 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002356 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002357 return;
2358 }
2359
2360 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002361 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2362 'px';
2363 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2364 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002365
2366 this.cursorNode_.setAttribute('title',
2367 '(' + this.screen_.cursorPosition.row +
2368 ', ' + this.screen_.cursorPosition.column +
2369 ')');
2370
2371 // Update the caret for a11y purposes.
2372 var selection = this.document_.getSelection();
2373 if (selection && selection.isCollapsed)
2374 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002375};
2376
Robert Gindafb1be6a2013-12-11 11:56:22 -08002377/**
2378 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2379 * and character cell dimensions.
2380 */
Robert Ginda830583c2013-08-07 13:20:46 -07002381hterm.Terminal.prototype.restyleCursor_ = function() {
2382 var shape = this.cursorShape_;
2383
2384 if (this.cursorNode_.getAttribute('focus') == 'false') {
2385 // Always show a block cursor when unfocused.
2386 shape = hterm.Terminal.cursorShape.BLOCK;
2387 }
2388
2389 var style = this.cursorNode_.style;
2390
Robert Gindafb1be6a2013-12-11 11:56:22 -08002391 style.width = this.scrollPort_.characterSize.width + 'px';
2392
Robert Ginda830583c2013-08-07 13:20:46 -07002393 switch (shape) {
2394 case hterm.Terminal.cursorShape.BEAM:
2395 style.height = this.scrollPort_.characterSize.height + 'px';
2396 style.backgroundColor = 'transparent';
2397 style.borderBottomStyle = null;
2398 style.borderLeftStyle = 'solid';
2399 break;
2400
2401 case hterm.Terminal.cursorShape.UNDERLINE:
2402 style.height = this.scrollPort_.characterSize.baseline + 'px';
2403 style.backgroundColor = 'transparent';
2404 style.borderBottomStyle = 'solid';
2405 // correct the size to put it exactly at the baseline
2406 style.borderLeftStyle = null;
2407 break;
2408
2409 default:
2410 style.height = this.scrollPort_.characterSize.height + 'px';
2411 style.backgroundColor = this.cursorColor_;
2412 style.borderBottomStyle = null;
2413 style.borderLeftStyle = null;
2414 break;
2415 }
2416};
2417
rginda8ba33642011-12-14 12:31:31 -08002418/**
2419 * Synchronizes the visible cursor with the current cursor coordinates.
2420 *
2421 * The sync will happen asynchronously, soon after the call stack winds down.
2422 * Multiple calls will be coalesced into a single sync.
2423 */
2424hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2425 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002426 return;
rginda8ba33642011-12-14 12:31:31 -08002427
2428 var self = this;
2429 this.timeouts_.syncCursor = setTimeout(function() {
2430 self.syncCursorPosition_();
2431 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002432 }, 0);
2433};
2434
rgindacc2996c2012-02-24 14:59:31 -08002435/**
rgindaf522ce02012-04-17 17:49:17 -07002436 * Show or hide the zoom warning.
2437 *
2438 * The zoom warning is a message warning the user that their browser zoom must
2439 * be set to 100% in order for hterm to function properly.
2440 *
2441 * @param {boolean} state True to show the message, false to hide it.
2442 */
2443hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2444 if (!this.zoomWarningNode_) {
2445 if (!state)
2446 return;
2447
2448 this.zoomWarningNode_ = this.document_.createElement('div');
2449 this.zoomWarningNode_.style.cssText = (
2450 'color: black;' +
2451 'background-color: #ff2222;' +
2452 'font-size: large;' +
2453 'border-radius: 8px;' +
2454 'opacity: 0.75;' +
2455 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2456 'top: 0.5em;' +
2457 'right: 1.2em;' +
2458 'position: absolute;' +
2459 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002460 '-webkit-user-select: none;' +
2461 '-moz-text-size-adjust: none;' +
2462 '-moz-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002463 }
2464
Robert Gindab4839c22013-02-28 16:52:10 -08002465 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2466 hterm.zoomWarningMessage,
2467 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2468
rgindaf522ce02012-04-17 17:49:17 -07002469 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2470
2471 if (state) {
2472 if (!this.zoomWarningNode_.parentNode)
2473 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2474 } else if (this.zoomWarningNode_.parentNode) {
2475 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2476 }
2477};
2478
2479/**
rgindacc2996c2012-02-24 14:59:31 -08002480 * Show the terminal overlay for a given amount of time.
2481 *
2482 * The terminal overlay appears in inverse video in a large font, centered
2483 * over the terminal. You should probably keep the overlay message brief,
2484 * since it's in a large font and you probably aren't going to check the size
2485 * of the terminal first.
2486 *
2487 * @param {string} msg The text (not HTML) message to display in the overlay.
2488 * @param {number} opt_timeout The amount of time to wait before fading out
2489 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2490 * stay up forever (or until the next overlay).
2491 */
2492hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002493 if (!this.overlayNode_) {
2494 if (!this.div_)
2495 return;
2496
2497 this.overlayNode_ = this.document_.createElement('div');
2498 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002499 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002500 'font-size: xx-large;' +
2501 'opacity: 0.75;' +
2502 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2503 'position: absolute;' +
2504 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002505 '-webkit-transition: opacity 180ms ease-in;' +
2506 '-moz-user-select: none;' +
2507 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002508
2509 this.overlayNode_.addEventListener('mousedown', function(e) {
2510 e.preventDefault();
2511 e.stopPropagation();
2512 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002513 }
2514
rginda9f5222b2012-03-05 11:53:28 -08002515 this.overlayNode_.style.color = this.prefs_.get('background-color');
2516 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2517 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2518
rgindaf0090c92012-02-10 14:58:52 -08002519 this.overlayNode_.textContent = msg;
2520 this.overlayNode_.style.opacity = '0.75';
2521
2522 if (!this.overlayNode_.parentNode)
2523 this.div_.appendChild(this.overlayNode_);
2524
Robert Ginda97769282013-02-01 15:30:30 -08002525 var divSize = hterm.getClientSize(this.div_);
2526 var overlaySize = hterm.getClientSize(this.overlayNode_);
2527
2528 this.overlayNode_.style.top = (divSize.height - overlaySize.height) / 2;
2529 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
2530 this.scrollPort_.currentScrollbarWidthPx) / 2;
rgindaf0090c92012-02-10 14:58:52 -08002531
2532 var self = this;
2533
2534 if (this.overlayTimeout_)
2535 clearTimeout(this.overlayTimeout_);
2536
rgindacc2996c2012-02-24 14:59:31 -08002537 if (opt_timeout === null)
2538 return;
2539
rgindaf0090c92012-02-10 14:58:52 -08002540 this.overlayTimeout_ = setTimeout(function() {
2541 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002542 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002543 if (self.overlayNode_.parentNode)
2544 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002545 self.overlayTimeout_ = null;
2546 self.overlayNode_.style.opacity = '0.75';
2547 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002548 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002549};
2550
rginda4bba5e12012-06-20 16:15:30 -07002551/**
2552 * Paste from the system clipboard to the terminal.
2553 */
2554hterm.Terminal.prototype.paste = function() {
2555 hterm.pasteFromClipboard(this.document_);
2556};
2557
2558/**
2559 * Copy a string to the system clipboard.
2560 *
2561 * Note: If there is a selected range in the terminal, it'll be cleared.
2562 */
2563hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002564 if (this.prefs_.get('enable-clipboard-notice'))
2565 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2566
rgindaa09e7332012-08-17 12:49:51 -07002567 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002568 copySource.textContent = str;
2569 copySource.style.cssText = (
2570 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002571 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002572 'position: absolute;' +
2573 'top: -99px');
2574
2575 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002576
rginda4bba5e12012-06-20 16:15:30 -07002577 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002578 var anchorNode = selection.anchorNode;
2579 var anchorOffset = selection.anchorOffset;
2580 var focusNode = selection.focusNode;
2581 var focusOffset = selection.focusOffset;
2582
rginda4bba5e12012-06-20 16:15:30 -07002583 selection.selectAllChildren(copySource);
2584
rgindaa09e7332012-08-17 12:49:51 -07002585 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002586
Rob Spies56953412014-04-28 14:09:47 -07002587 // IE doesn't support selection.extend. This means that the selection
2588 // won't return on IE.
Rob Spies0bec09b2014-06-06 15:58:09 -07002589 if (this.clearSelectionAfterCopy && selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002590 selection.collapse(anchorNode, anchorOffset);
2591 selection.extend(focusNode, focusOffset);
2592 }
rgindafaa74742012-08-21 13:34:03 -07002593
rginda4bba5e12012-06-20 16:15:30 -07002594 copySource.parentNode.removeChild(copySource);
2595};
2596
rgindaa09e7332012-08-17 12:49:51 -07002597hterm.Terminal.prototype.getSelectionText = function() {
2598 var selection = this.scrollPort_.selection;
2599 selection.sync();
2600
2601 if (selection.isCollapsed)
2602 return null;
2603
2604
2605 // Start offset measures from the beginning of the line.
2606 var startOffset = selection.startOffset;
2607 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002608
Robert Gindafdbb3f22012-09-06 20:23:06 -07002609 if (node.nodeName != 'X-ROW') {
2610 // If the selection doesn't start on an x-row node, then it must be
2611 // somewhere inside the x-row. Add any characters from previous siblings
2612 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002613
2614 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2615 // If node is the text node in a styled span, move up to the span node.
2616 node = node.parentNode;
2617 }
2618
Robert Gindafdbb3f22012-09-06 20:23:06 -07002619 while (node.previousSibling) {
2620 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002621 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002622 }
rgindaa09e7332012-08-17 12:49:51 -07002623 }
2624
2625 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002626 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2627 selection.endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002628 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002629
Robert Gindafdbb3f22012-09-06 20:23:06 -07002630 if (node.nodeName != 'X-ROW') {
2631 // If the selection doesn't end on an x-row node, then it must be
2632 // somewhere inside the x-row. Add any characters from following siblings
2633 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002634
2635 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2636 // If node is the text node in a styled span, move up to the span node.
2637 node = node.parentNode;
2638 }
2639
Robert Gindafdbb3f22012-09-06 20:23:06 -07002640 while (node.nextSibling) {
2641 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002642 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002643 }
rgindaa09e7332012-08-17 12:49:51 -07002644 }
2645
2646 var rv = this.getRowsText(selection.startRow.rowIndex,
2647 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002648 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002649};
2650
rginda4bba5e12012-06-20 16:15:30 -07002651/**
2652 * Copy the current selection to the system clipboard, then clear it after a
2653 * short delay.
2654 */
2655hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002656 var text = this.getSelectionText();
2657 if (text != null)
2658 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002659};
2660
rgindaf0090c92012-02-10 14:58:52 -08002661hterm.Terminal.prototype.overlaySize = function() {
2662 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2663};
2664
rginda87b86462011-12-14 13:48:03 -08002665/**
2666 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2667 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002668 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002669 */
2670hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002671 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002672 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2673
Robert Ginda8cb7d902013-06-20 14:37:18 -07002674 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002675};
2676
2677/**
rgindad5613292012-06-19 15:40:37 -07002678 * Add the terminalRow and terminalColumn properties to mouse events and
2679 * then forward on to onMouse().
2680 *
2681 * The terminalRow and terminalColumn properties contain the (row, column)
2682 * coordinates for the mouse event.
2683 */
2684hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002685 if (e.processedByTerminalHandler_) {
2686 // We register our event handlers on the document, as well as the cursor
2687 // and the scroll blocker. Mouse events that occur on the cursor or
2688 // scroll blocker will also appear on the document, but we don't want to
2689 // process them twice.
2690 //
2691 // We can't just prevent bubbling because that has other side effects, so
2692 // we decorate the event object with this property instead.
2693 return;
2694 }
2695
2696 e.processedByTerminalHandler_ = true;
2697
Robert Gindaeda48db2014-07-17 09:25:30 -07002698 // One based row/column stored on the mouse event.
2699 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2700 this.scrollPort_.characterSize.height) + 1;
2701 e.terminalColumn = parseInt(e.clientX /
2702 this.scrollPort_.characterSize.width) + 1;
2703
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002704 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2705 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002706 return;
2707 }
2708
Robert Gindaeda48db2014-07-17 09:25:30 -07002709 if (this.options_.cursorVisible) {
2710 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
2711 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
2712 this.cursorNode_.style.display = 'none';
2713 } else if (this.cursorNode_.style.display == 'none') {
2714 this.cursorNode_.style.display = '';
2715 }
2716 }
rgindad5613292012-06-19 15:40:37 -07002717
Robert Ginda928cf632014-03-05 15:07:41 -08002718 if (e.type == 'mousedown') {
2719 if (e.altKey || this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED) {
2720 // If VT mouse reporting is disabled, or has been defeated with
2721 // alt-mousedown, then the mouse will act on the local selection.
2722 this.reportMouseEvents_ = false;
2723 this.setSelectionEnabled(true);
2724 } else {
2725 // Otherwise we defer ownership of the mouse to the VT.
2726 this.reportMouseEvents_ = true;
Robert Ginda3ae37822014-05-15 13:05:35 -07002727 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08002728 this.setSelectionEnabled(false);
2729 e.preventDefault();
2730 }
2731 }
2732
2733 if (!this.reportMouseEvents_) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002734 if (e.type == 'dblclick') {
2735 this.screen_.expandSelection(this.document_.getSelection());
2736 hterm.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07002737 }
2738
Robert Ginda928cf632014-03-05 15:07:41 -08002739 if (e.type == 'mousedown' && e.which == this.mousePasteButton)
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002740 this.paste();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002741
2742 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2743 !this.document_.getSelection().isCollapsed) {
2744 hterm.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002745 }
2746
2747 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
2748 this.scrollBlockerNode_.engaged) {
2749 // Disengage the scroll-blocker after one of these events.
2750 this.scrollBlockerNode_.engaged = false;
2751 this.scrollBlockerNode_.style.top = '-99px';
2752 }
2753
Robert Ginda928cf632014-03-05 15:07:41 -08002754 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002755 if (!this.scrollBlockerNode_.engaged) {
2756 if (e.type == 'mousedown') {
2757 // Move the scroll-blocker into place if we want to keep the scrollport
2758 // from scrolling.
2759 this.scrollBlockerNode_.engaged = true;
2760 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2761 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2762 } else if (e.type == 'mousemove') {
2763 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
2764 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07002765 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002766 e.preventDefault();
2767 }
2768 }
Robert Ginda928cf632014-03-05 15:07:41 -08002769
2770 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002771 }
2772
Robert Ginda928cf632014-03-05 15:07:41 -08002773 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
2774 // Restore this on mouseup in case it was temporarily defeated with a
2775 // alt-mousedown. Only do this when the selection is empty so that
2776 // we don't immediately kill the users selection.
2777 this.reportMouseEvents_ = (this.vt.mouseReport !=
2778 this.vt.MOUSE_REPORT_DISABLED);
2779 }
rgindad5613292012-06-19 15:40:37 -07002780};
2781
2782/**
2783 * Clients should override this if they care to know about mouse events.
2784 *
2785 * The event parameter will be a normal DOM mouse click event with additional
2786 * 'terminalRow' and 'terminalColumn' properties.
2787 */
2788hterm.Terminal.prototype.onMouse = function(e) { };
2789
2790/**
rginda8e92a692012-05-20 19:37:20 -07002791 * React when focus changes.
2792 */
Rob Spies06533ba2014-04-24 11:20:37 -07002793hterm.Terminal.prototype.onFocusChange_ = function(focused) {
2794 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07002795 this.restyleCursor_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002796 if (focused === true)
2797 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07002798};
2799
2800/**
rginda8ba33642011-12-14 12:31:31 -08002801 * React when the ScrollPort is scrolled.
2802 */
2803hterm.Terminal.prototype.onScroll_ = function() {
2804 this.scheduleSyncCursorPosition_();
2805};
2806
2807/**
rginda9846e2f2012-01-27 13:53:33 -08002808 * React when text is pasted into the scrollPort.
2809 */
2810hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Ginda8cb7d902013-06-20 14:37:18 -07002811 this.onVTKeystroke(e.text.replace(/\n/mg, '\r'));
rginda9846e2f2012-01-27 13:53:33 -08002812};
2813
2814/**
rgindaa09e7332012-08-17 12:49:51 -07002815 * React when the user tries to copy from the scrollPort.
2816 */
2817hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07002818 if (!this.useDefaultWindowCopy) {
2819 e.preventDefault();
2820 setTimeout(this.copySelectionToClipboard.bind(this), 0);
2821 }
rgindaa09e7332012-08-17 12:49:51 -07002822};
2823
2824/**
rginda8ba33642011-12-14 12:31:31 -08002825 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002826 *
2827 * Note: This function should not directly contain code that alters the internal
2828 * state of the terminal. That kind of code belongs in realizeWidth or
2829 * realizeHeight, so that it can be executed synchronously in the case of a
2830 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002831 */
2832hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08002833 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002834 this.scrollPort_.characterSize.width);
Robert Ginda19f61292014-03-04 14:07:57 -08002835 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
rginda35c456b2012-02-09 17:29:05 -08002836 this.scrollPort_.characterSize.height);
2837
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002838 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002839 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002840 // gets removed from the document or during the initial load, and we can't
2841 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002842 return;
2843 }
2844
rgindaa8ba17d2012-08-15 14:41:10 -07002845 var isNewSize = (columnCount != this.screenSize.width ||
2846 rowCount != this.screenSize.height);
2847
2848 // We do this even if the size didn't change, just to be sure everything is
2849 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002850 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002851 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002852
2853 if (isNewSize)
2854 this.overlaySize();
2855
Robert Gindafb1be6a2013-12-11 11:56:22 -08002856 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07002857 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002858};
2859
2860/**
2861 * Service the cursor blink timeout.
2862 */
2863hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07002864 if (!this.options_.cursorBlink) {
2865 delete this.timeouts_.cursorBlink;
2866 return;
2867 }
2868
Robert Ginda830583c2013-08-07 13:20:46 -07002869 if (this.cursorNode_.getAttribute('focus') == 'false' ||
2870 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08002871 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07002872 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
2873 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08002874 } else {
rginda87b86462011-12-14 13:48:03 -08002875 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07002876 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
2877 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08002878 }
2879};
David Reveman8f552492012-03-28 12:18:41 -04002880
2881/**
2882 * Set the scrollbar-visible mode bit.
2883 *
2884 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2885 * Otherwise it will not.
2886 *
2887 * Defaults to on.
2888 *
2889 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2890 */
2891hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2892 this.scrollPort_.setScrollbarVisible(state);
2893};
Michael Kelly485ecd12014-06-09 11:41:56 -04002894
2895/**
2896 * Close all web notifications created by terminal bells.
2897 */
2898hterm.Terminal.prototype.closeBellNotifications_ = function() {
2899 this.bellNotificationList_.forEach(function(n) {
2900 n.close();
2901 });
2902 this.bellNotificationList_.length = 0;
2903};