blob: a4e19610e2a7efc1bfc2cff39ee4daee036ac700 [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
Masaya Suzuki273aa982014-05-31 07:25:55 +09007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource', 'lib.wc',
Robert Ginda57f03b42012-09-13 11:02:48 -07008 'hterm.Keyboard', 'hterm.Options', 'hterm.PreferenceManager',
Ricky Liang48f05cb2013-12-31 23:35:29 +08009 'hterm.Screen', 'hterm.ScrollPort', 'hterm.Size',
10 'hterm.TextAttributes', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070053 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080054
rginda87b86462011-12-14 13:48:03 -080055 // The div that contains this terminal.
56 this.div_ = null;
57
rgindac9bc5502012-01-18 11:48:44 -080058 // The document that contains the scrollPort. Defaulted to the global
59 // document here so that the terminal is functional even if it hasn't been
60 // inserted into a document yet, but re-set in decorate().
61 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080062
rginda8ba33642011-12-14 12:31:31 -080063 // The rows that have scrolled off screen and are no longer addressable.
64 this.scrollbackRows_ = [];
65
rgindac9bc5502012-01-18 11:48:44 -080066 // Saved tab stops.
67 this.tabStops_ = [];
68
David Benjamin66e954d2012-05-05 21:08:12 -040069 // Keep track of whether default tab stops have been erased; after a TBC
70 // clears all tab stops, defaults aren't restored on resize until a reset.
71 this.defaultTabStops = true;
72
rginda8ba33642011-12-14 12:31:31 -080073 // The VT's notion of the top and bottom rows. Used during some VT
74 // cursor positioning and scrolling commands.
75 this.vtScrollTop_ = null;
76 this.vtScrollBottom_ = null;
77
78 // The DIV element for the visible cursor.
79 this.cursorNode_ = null;
80
Robert Ginda830583c2013-08-07 13:20:46 -070081 // The current cursor shape of the terminal.
82 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
83
84 // The current color of the cursor.
85 this.cursorColor_ = null;
86
rginda9f5222b2012-03-05 11:53:28 -080087 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070088 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070089 this.backgroundColor_ = null;
90 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070091 this.scrollOnOutput_ = null;
92 this.scrollOnKeystroke_ = null;
rginda9f5222b2012-03-05 11:53:28 -080093
Robert Ginda928cf632014-03-05 15:07:41 -080094 // True if we should send mouse events to the vt, false if we want them
95 // to manage the local text selection.
96 this.reportMouseEvents_ = false;
97
rgindaf0090c92012-02-10 14:58:52 -080098 // Terminal bell sound.
99 this.bellAudio_ = this.document_.createElement('audio');
rgindaf0090c92012-02-10 14:58:52 -0800100 this.bellAudio_.setAttribute('preload', 'auto');
101
Michael Kelly485ecd12014-06-09 11:41:56 -0400102 // All terminal bell notifications that have been generated (not necessarily
103 // shown).
104 this.bellNotificationList_ = [];
105
106 // Whether we have permission to display notifications.
107 this.desktopNotificationBell_ = false;
108 //this.notificationPermission_ = (Notification &&
109 //Notification.permission === 'granted');
110
rginda6d397402012-01-17 10:58:29 -0800111 // Cursor position and attributes saved with DECSC.
112 this.savedOptions_ = {};
113
rginda8ba33642011-12-14 12:31:31 -0800114 // The current mode bits for the terminal.
115 this.options_ = new hterm.Options();
116
117 // Timeouts we might need to clear.
118 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800119
120 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800121 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800122
rgindafeaf3142012-01-31 15:14:20 -0800123 // The keyboard hander.
124 this.keyboard = new hterm.Keyboard(this);
125
rginda87b86462011-12-14 13:48:03 -0800126 // General IO interface that can be given to third parties without exposing
127 // the entire terminal object.
128 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800129
rgindad5613292012-06-19 15:40:37 -0700130 // True if mouse-click-drag should scroll the terminal.
131 this.enableMouseDragScroll = true;
132
Robert Ginda57f03b42012-09-13 11:02:48 -0700133 this.copyOnSelect = null;
rginda4bba5e12012-06-20 16:15:30 -0700134 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700135
Rob Spies0bec09b2014-06-06 15:58:09 -0700136 // Whether to use the default window copy behaviour.
137 this.useDefaultWindowCopy = false;
138
139 this.clearSelectionAfterCopy = true;
140
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400141 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800142 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700143
144 this.setProfile(opt_profileId || 'default',
145 function() { this.onTerminalReady() }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800146};
147
148/**
Robert Ginda830583c2013-08-07 13:20:46 -0700149 * Possible cursor shapes.
150 */
151hterm.Terminal.cursorShape = {
152 BLOCK: 'BLOCK',
153 BEAM: 'BEAM',
154 UNDERLINE: 'UNDERLINE'
155};
156
157/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700158 * Clients should override this to be notified when the terminal is ready
159 * for use.
160 *
161 * The terminal initialization is asynchronous, and shouldn't be used before
162 * this method is called.
163 */
164hterm.Terminal.prototype.onTerminalReady = function() { };
165
166/**
rginda35c456b2012-02-09 17:29:05 -0800167 * Default tab with of 8 to match xterm.
168 */
169hterm.Terminal.prototype.tabWidth = 8;
170
171/**
rginda9f5222b2012-03-05 11:53:28 -0800172 * Select a preference profile.
173 *
174 * This will load the terminal preferences for the given profile name and
175 * associate subsequent preference changes with the new preference profile.
176 *
177 * @param {string} newName The name of the preference profile. Forward slash
178 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700179 * @param {function} opt_callback Optional callback to invoke when the profile
180 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800181 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700182hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
183 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800184
Robert Ginda57f03b42012-09-13 11:02:48 -0700185 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800186
Robert Ginda57f03b42012-09-13 11:02:48 -0700187 if (this.prefs_)
188 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800189
Robert Ginda57f03b42012-09-13 11:02:48 -0700190 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
191 this.prefs_.addObservers(null, {
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700192 'alt-backspace-is-meta-backspace': function(v) {
193 terminal.keyboard.altBackspaceIsMetaBackspace = v;
194 },
195
Robert Ginda57f03b42012-09-13 11:02:48 -0700196 'alt-is-meta': function(v) {
197 terminal.keyboard.altIsMeta = v;
198 },
199
200 'alt-sends-what': function(v) {
201 if (!/^(escape|8-bit|browser-key)$/.test(v))
202 v = 'escape';
203
204 terminal.keyboard.altSendsWhat = v;
205 },
206
207 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800208 var ary = v.match(/^lib-resource:(\S+)/);
209 if (ary) {
210 terminal.bellAudio_.setAttribute('src',
211 lib.resource.getDataUrl(ary[1]));
212 } else {
213 terminal.bellAudio_.setAttribute('src', v);
214 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700215 },
216
Michael Kelly485ecd12014-06-09 11:41:56 -0400217 'desktop-notification-bell': function(v) {
218 if (v && Notification) {
219 // We cannot rely on having notification permission by default.
220 if (Notification.permission !== 'granted') {
221 Notification.requestPermission(function(permission) {
222 terminal.desktopNotificationBell_ = (permission === 'granted');
223 });
224 } else {
225 terminal.desktopNotificationBell_ = true;
226 }
227 } else {
228 terminal.desktopNotificationBell_ = false;
229 }
230 },
231
Robert Ginda57f03b42012-09-13 11:02:48 -0700232 'background-color': function(v) {
233 terminal.setBackgroundColor(v);
234 },
235
236 'background-image': function(v) {
237 terminal.scrollPort_.setBackgroundImage(v);
238 },
239
240 'background-size': function(v) {
241 terminal.scrollPort_.setBackgroundSize(v);
242 },
243
244 'background-position': function(v) {
245 terminal.scrollPort_.setBackgroundPosition(v);
246 },
247
248 'backspace-sends-backspace': function(v) {
249 terminal.keyboard.backspaceSendsBackspace = v;
250 },
251
252 'cursor-blink': function(v) {
253 terminal.setCursorBlink(!!v);
254 },
255
256 'cursor-color': function(v) {
257 terminal.setCursorColor(v);
258 },
259
260 'color-palette-overrides': function(v) {
261 if (!(v == null || v instanceof Object || v instanceof Array)) {
262 console.warn('Preference color-palette-overrides is not an array or ' +
263 'object: ' + v);
264 return;
rginda9f5222b2012-03-05 11:53:28 -0800265 }
rginda9f5222b2012-03-05 11:53:28 -0800266
Robert Ginda57f03b42012-09-13 11:02:48 -0700267 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700268
Robert Ginda57f03b42012-09-13 11:02:48 -0700269 if (v) {
270 for (var key in v) {
271 var i = parseInt(key);
272 if (isNaN(i) || i < 0 || i > 255) {
273 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
274 continue;
275 }
276
277 if (v[i]) {
278 var rgb = lib.colors.normalizeCSS(v[i]);
279 if (rgb)
280 lib.colors.colorPalette[i] = rgb;
281 }
282 }
rginda30f20f62012-04-05 16:36:19 -0700283 }
rginda30f20f62012-04-05 16:36:19 -0700284
Robert Ginda57f03b42012-09-13 11:02:48 -0700285 terminal.primaryScreen_.textAttributes.resetColorPalette()
286 terminal.alternateScreen_.textAttributes.resetColorPalette();
287 },
rginda30f20f62012-04-05 16:36:19 -0700288
Robert Ginda57f03b42012-09-13 11:02:48 -0700289 'copy-on-select': function(v) {
290 terminal.copyOnSelect = !!v;
291 },
rginda9f5222b2012-03-05 11:53:28 -0800292
Rob Spies0bec09b2014-06-06 15:58:09 -0700293 'use-default-window-copy': function(v) {
294 terminal.useDefaultWindowCopy = !!v;
295 },
296
297 'clear-selection-after-copy': function(v) {
298 terminal.clearSelectionAfterCopy = !!v;
299 },
300
Robert Ginda7e5e9522014-03-14 12:23:58 -0700301 'ctrl-plus-minus-zero-zoom': function(v) {
302 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
303 },
304
Robert Gindafb5a3f92014-05-13 14:12:00 -0700305 'ctrl-c-copy': function(v) {
306 terminal.keyboard.ctrlCCopy = v;
307 },
308
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100309 'ctrl-v-paste': function(v) {
310 terminal.keyboard.ctrlVPaste = v;
311 },
312
Masaya Suzuki273aa982014-05-31 07:25:55 +0900313 'east-asian-ambiguous-as-two-column': function(v) {
314 lib.wc.regardCjkAmbiguous = v;
315 },
316
Robert Ginda57f03b42012-09-13 11:02:48 -0700317 'enable-8-bit-control': function(v) {
318 terminal.vt.enable8BitControl = !!v;
319 },
rginda30f20f62012-04-05 16:36:19 -0700320
Robert Ginda57f03b42012-09-13 11:02:48 -0700321 'enable-bold': function(v) {
322 terminal.syncBoldSafeState();
323 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400324
Robert Ginda3e278d72014-03-25 13:18:51 -0700325 'enable-bold-as-bright': function(v) {
326 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
327 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
328 },
329
Robert Ginda57f03b42012-09-13 11:02:48 -0700330 'enable-clipboard-write': function(v) {
331 terminal.vt.enableClipboardWrite = !!v;
332 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400333
Robert Ginda3755e752013-05-31 13:34:09 -0700334 'enable-dec12': function(v) {
335 terminal.vt.enableDec12 = !!v;
336 },
337
Robert Ginda57f03b42012-09-13 11:02:48 -0700338 'font-family': function(v) {
339 terminal.syncFontFamily();
340 },
rginda30f20f62012-04-05 16:36:19 -0700341
Robert Ginda57f03b42012-09-13 11:02:48 -0700342 'font-size': function(v) {
343 terminal.setFontSize(v);
344 },
rginda9875d902012-08-20 16:21:57 -0700345
Robert Ginda57f03b42012-09-13 11:02:48 -0700346 'font-smoothing': function(v) {
347 terminal.syncFontFamily();
348 },
rgindade84e382012-04-20 15:39:31 -0700349
Robert Ginda57f03b42012-09-13 11:02:48 -0700350 'foreground-color': function(v) {
351 terminal.setForegroundColor(v);
352 },
rginda30f20f62012-04-05 16:36:19 -0700353
Robert Ginda57f03b42012-09-13 11:02:48 -0700354 'home-keys-scroll': function(v) {
355 terminal.keyboard.homeKeysScroll = v;
356 },
rginda4bba5e12012-06-20 16:15:30 -0700357
Robert Ginda57f03b42012-09-13 11:02:48 -0700358 'max-string-sequence': function(v) {
359 terminal.vt.maxStringSequence = v;
360 },
rginda11057d52012-04-25 12:29:56 -0700361
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700362 'media-keys-are-fkeys': function(v) {
363 terminal.keyboard.mediaKeysAreFKeys = v;
364 },
365
Robert Ginda57f03b42012-09-13 11:02:48 -0700366 'meta-sends-escape': function(v) {
367 terminal.keyboard.metaSendsEscape = v;
368 },
rginda30f20f62012-04-05 16:36:19 -0700369
Robert Ginda57f03b42012-09-13 11:02:48 -0700370 'mouse-paste-button': function(v) {
371 terminal.syncMousePasteButton();
372 },
rgindaa8ba17d2012-08-15 14:41:10 -0700373
Robert Gindae76aa9f2014-03-14 12:29:12 -0700374 'page-keys-scroll': function(v) {
375 terminal.keyboard.pageKeysScroll = v;
376 },
377
Robert Ginda40932892012-12-10 17:26:40 -0800378 'pass-alt-number': function(v) {
379 if (v == null) {
380 var osx = window.navigator.userAgent.match(/Mac OS X/);
381
382 // Let Alt-1..9 pass to the browser (to control tab switching) on
383 // non-OS X systems, or if hterm is not opened in an app window.
384 v = (!osx && hterm.windowType != 'popup');
385 }
386
387 terminal.passAltNumber = v;
388 },
389
390 'pass-ctrl-number': function(v) {
391 if (v == null) {
392 var osx = window.navigator.userAgent.match(/Mac OS X/);
393
394 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
395 // non-OS X systems, or if hterm is not opened in an app window.
396 v = (!osx && hterm.windowType != 'popup');
397 }
398
399 terminal.passCtrlNumber = v;
400 },
401
402 'pass-meta-number': function(v) {
403 if (v == null) {
404 var osx = window.navigator.userAgent.match(/Mac OS X/);
405
406 // Let Meta-1..9 pass to the browser (to control tab switching) on
407 // OS X systems, or if hterm is not opened in an app window.
408 v = (osx && hterm.windowType != 'popup');
409 }
410
411 terminal.passMetaNumber = v;
412 },
413
Marius Schilder77857b32014-05-14 16:21:26 -0700414 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700415 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700416 },
417
Robert Ginda8cb7d902013-06-20 14:37:18 -0700418 'receive-encoding': function(v) {
419 if (!(/^(utf-8|raw)$/).test(v)) {
420 console.warn('Invalid value for "receive-encoding": ' + v);
421 v = 'utf-8';
422 }
423
424 terminal.vt.characterEncoding = v;
425 },
426
Robert Ginda57f03b42012-09-13 11:02:48 -0700427 'scroll-on-keystroke': function(v) {
428 terminal.scrollOnKeystroke_ = v;
429 },
rginda9f5222b2012-03-05 11:53:28 -0800430
Robert Ginda57f03b42012-09-13 11:02:48 -0700431 'scroll-on-output': function(v) {
432 terminal.scrollOnOutput_ = v;
433 },
rginda30f20f62012-04-05 16:36:19 -0700434
Robert Ginda57f03b42012-09-13 11:02:48 -0700435 'scrollbar-visible': function(v) {
436 terminal.setScrollbarVisible(v);
437 },
rginda9f5222b2012-03-05 11:53:28 -0800438
Robert Ginda8cb7d902013-06-20 14:37:18 -0700439 'send-encoding': function(v) {
440 if (!(/^(utf-8|raw)$/).test(v)) {
441 console.warn('Invalid value for "send-encoding": ' + v);
442 v = 'utf-8';
443 }
444
445 terminal.keyboard.characterEncoding = v;
446 },
447
Robert Ginda57f03b42012-09-13 11:02:48 -0700448 'shift-insert-paste': function(v) {
449 terminal.keyboard.shiftInsertPaste = v;
450 },
rginda9f5222b2012-03-05 11:53:28 -0800451
Robert Gindae76aa9f2014-03-14 12:29:12 -0700452 'user-css': function(v) {
453 terminal.scrollPort_.setUserCss(v);
Robert Ginda57f03b42012-09-13 11:02:48 -0700454 }
455 });
rginda30f20f62012-04-05 16:36:19 -0700456
Robert Ginda57f03b42012-09-13 11:02:48 -0700457 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800458 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700459
460 if (opt_callback)
461 opt_callback();
462 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800463};
464
Rob Spies56953412014-04-28 14:09:47 -0700465
466/**
467 * Returns the preferences manager used for configuring this terminal.
468 */
469hterm.Terminal.prototype.getPrefs = function() {
470 return this.prefs_;
471};
472
473
rginda8e92a692012-05-20 19:37:20 -0700474/**
475 * Set the color for the cursor.
476 *
477 * If you want this setting to persist, set it through prefs_, rather than
478 * with this method.
479 */
480hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700481 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700482 this.cursorNode_.style.backgroundColor = color;
483 this.cursorNode_.style.borderColor = color;
484};
485
486/**
487 * Return the current cursor color as a string.
488 */
489hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700490 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700491};
492
493/**
rgindad5613292012-06-19 15:40:37 -0700494 * Enable or disable mouse based text selection in the terminal.
495 */
496hterm.Terminal.prototype.setSelectionEnabled = function(state) {
497 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700498};
499
500/**
rginda8e92a692012-05-20 19:37:20 -0700501 * Set the background color.
502 *
503 * If you want this setting to persist, set it through prefs_, rather than
504 * with this method.
505 */
506hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700507 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700508 this.primaryScreen_.textAttributes.setDefaults(
509 this.foregroundColor_, this.backgroundColor_);
510 this.alternateScreen_.textAttributes.setDefaults(
511 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700512 this.scrollPort_.setBackgroundColor(color);
513};
514
rginda9f5222b2012-03-05 11:53:28 -0800515/**
516 * Return the current terminal background color.
517 *
518 * Intended for use by other classes, so we don't have to expose the entire
519 * prefs_ object.
520 */
521hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700522 return this.backgroundColor_;
523};
524
525/**
526 * Set the foreground color.
527 *
528 * If you want this setting to persist, set it through prefs_, rather than
529 * with this method.
530 */
531hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700532 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700533 this.primaryScreen_.textAttributes.setDefaults(
534 this.foregroundColor_, this.backgroundColor_);
535 this.alternateScreen_.textAttributes.setDefaults(
536 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700537 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800538};
539
540/**
541 * Return the current terminal foreground color.
542 *
543 * Intended for use by other classes, so we don't have to expose the entire
544 * prefs_ object.
545 */
546hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700547 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800548};
549
550/**
rginda87b86462011-12-14 13:48:03 -0800551 * Create a new instance of a terminal command and run it with a given
552 * argument string.
553 *
554 * @param {function} commandClass The constructor for a terminal command.
555 * @param {string} argString The argument string to pass to the command.
556 */
557hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700558 var environment = this.prefs_.get('environment');
559 if (typeof environment != 'object' || environment == null)
560 environment = {};
561
rginda87b86462011-12-14 13:48:03 -0800562 var self = this;
563 this.command = new commandClass(
564 { argString: argString || '',
565 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700566 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800567 onExit: function(code) {
568 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800569 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700570 if (self.prefs_.get('close-on-exit'))
571 window.close();
rginda87b86462011-12-14 13:48:03 -0800572 }
573 });
574
rgindafeaf3142012-01-31 15:14:20 -0800575 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800576 this.command.run();
577};
578
579/**
rgindafeaf3142012-01-31 15:14:20 -0800580 * Returns true if the current screen is the primary screen, false otherwise.
581 */
582hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700583 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800584};
585
586/**
587 * Install the keyboard handler for this terminal.
588 *
589 * This will prevent the browser from seeing any keystrokes sent to the
590 * terminal.
591 */
592hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700593 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800594}
595
596/**
597 * Uninstall the keyboard handler for this terminal.
598 */
599hterm.Terminal.prototype.uninstallKeyboard = function() {
600 this.keyboard.installKeyboard(null);
601}
602
603/**
rginda35c456b2012-02-09 17:29:05 -0800604 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800605 *
606 * Call setFontSize(0) to reset to the default font size.
607 *
608 * This function does not modify the font-size preference.
609 *
610 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800611 */
612hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800613 if (px === 0)
614 px = this.prefs_.get('font-size');
615
rginda35c456b2012-02-09 17:29:05 -0800616 this.scrollPort_.setFontSize(px);
Ricky Liang48f05cb2013-12-31 23:35:29 +0800617 if (this.wcCssRule_) {
618 this.wcCssRule_.style.width = this.scrollPort_.characterSize.width * 2 +
619 'px';
620 }
rginda35c456b2012-02-09 17:29:05 -0800621};
622
623/**
624 * Get the current font size.
625 */
626hterm.Terminal.prototype.getFontSize = function() {
627 return this.scrollPort_.getFontSize();
628};
629
630/**
rginda8e92a692012-05-20 19:37:20 -0700631 * Get the current font family.
632 */
633hterm.Terminal.prototype.getFontFamily = function() {
634 return this.scrollPort_.getFontFamily();
635};
636
637/**
rginda35c456b2012-02-09 17:29:05 -0800638 * Set the CSS "font-family" for this terminal.
639 */
rginda9f5222b2012-03-05 11:53:28 -0800640hterm.Terminal.prototype.syncFontFamily = function() {
641 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
642 this.prefs_.get('font-smoothing'));
643 this.syncBoldSafeState();
644};
645
rginda4bba5e12012-06-20 16:15:30 -0700646/**
647 * Set this.mousePasteButton based on the mouse-paste-button pref,
648 * autodetecting if necessary.
649 */
650hterm.Terminal.prototype.syncMousePasteButton = function() {
651 var button = this.prefs_.get('mouse-paste-button');
652 if (typeof button == 'number') {
653 this.mousePasteButton = button;
654 return;
655 }
656
657 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
658 if (!ary || ary[2] == 'CrOS') {
659 this.mousePasteButton = 2;
660 } else {
661 this.mousePasteButton = 3;
662 }
663};
664
665/**
666 * Enable or disable bold based on the enable-bold pref, autodetecting if
667 * necessary.
668 */
rginda9f5222b2012-03-05 11:53:28 -0800669hterm.Terminal.prototype.syncBoldSafeState = function() {
670 var enableBold = this.prefs_.get('enable-bold');
671 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700672 this.primaryScreen_.textAttributes.enableBold = enableBold;
673 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800674 return;
675 }
676
rgindaf7521392012-02-28 17:20:34 -0800677 var normalSize = this.scrollPort_.measureCharacterSize();
678 var boldSize = this.scrollPort_.measureCharacterSize('bold');
679
680 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800681 if (!isBoldSafe) {
682 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700683 'from normal. Font family is: ' +
684 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800685 }
rginda9f5222b2012-03-05 11:53:28 -0800686
Robert Gindaed016262012-10-26 16:27:09 -0700687 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
688 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800689};
690
691/**
rginda87b86462011-12-14 13:48:03 -0800692 * Return a copy of the current cursor position.
693 *
694 * @return {hterm.RowCol} The RowCol object representing the current position.
695 */
696hterm.Terminal.prototype.saveCursor = function() {
697 return this.screen_.cursorPosition.clone();
698};
699
rgindaa19afe22012-01-25 15:40:22 -0800700hterm.Terminal.prototype.getTextAttributes = function() {
701 return this.screen_.textAttributes;
702};
703
rginda1a09aa02012-06-18 21:11:25 -0700704hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
705 this.screen_.textAttributes = textAttributes;
706};
707
rginda87b86462011-12-14 13:48:03 -0800708/**
rgindaf522ce02012-04-17 17:49:17 -0700709 * Return the current browser zoom factor applied to the terminal.
710 *
711 * @return {number} The current browser zoom factor.
712 */
713hterm.Terminal.prototype.getZoomFactor = function() {
714 return this.scrollPort_.characterSize.zoomFactor;
715};
716
717/**
rginda9846e2f2012-01-27 13:53:33 -0800718 * Change the title of this terminal's window.
719 */
720hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800721 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800722};
723
724/**
rginda87b86462011-12-14 13:48:03 -0800725 * Restore a previously saved cursor position.
726 *
727 * @param {hterm.RowCol} cursor The position to restore.
728 */
729hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700730 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
731 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800732 this.screen_.setCursorPosition(row, column);
733 if (cursor.column > column ||
734 cursor.column == column && cursor.overflow) {
735 this.screen_.cursorPosition.overflow = true;
736 }
rginda87b86462011-12-14 13:48:03 -0800737};
738
739/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400740 * Clear the cursor's overflow flag.
741 */
742hterm.Terminal.prototype.clearCursorOverflow = function() {
743 this.screen_.cursorPosition.overflow = false;
744};
745
746/**
Robert Ginda830583c2013-08-07 13:20:46 -0700747 * Sets the cursor shape
748 */
749hterm.Terminal.prototype.setCursorShape = function(shape) {
750 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800751 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700752}
753
754/**
755 * Get the cursor shape
756 */
757hterm.Terminal.prototype.getCursorShape = function() {
758 return this.cursorShape_;
759}
760
761/**
rginda87b86462011-12-14 13:48:03 -0800762 * Set the width of the terminal, resizing the UI to match.
763 */
764hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800765 if (columnCount == null) {
766 this.div_.style.width = '100%';
767 return;
768 }
769
rginda35c456b2012-02-09 17:29:05 -0800770 this.div_.style.width = this.scrollPort_.characterSize.width *
Robert Ginda97769282013-02-01 15:30:30 -0800771 columnCount + this.scrollPort_.currentScrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400772 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800773 this.scheduleSyncCursorPosition_();
774};
rginda87b86462011-12-14 13:48:03 -0800775
rgindac9bc5502012-01-18 11:48:44 -0800776/**
rginda35c456b2012-02-09 17:29:05 -0800777 * Set the height of the terminal, resizing the UI to match.
778 */
779hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800780 if (rowCount == null) {
781 this.div_.style.height = '100%';
782 return;
783 }
784
rginda35c456b2012-02-09 17:29:05 -0800785 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700786 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800787 this.realizeSize_(this.screenSize.width, rowCount);
788 this.scheduleSyncCursorPosition_();
789};
790
791/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400792 * Deal with terminal size changes.
793 *
794 */
795hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
796 if (columnCount != this.screenSize.width)
797 this.realizeWidth_(columnCount);
798
799 if (rowCount != this.screenSize.height)
800 this.realizeHeight_(rowCount);
801
802 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700803 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400804};
805
806/**
rgindac9bc5502012-01-18 11:48:44 -0800807 * Deal with terminal width changes.
808 *
809 * This function does what needs to be done when the terminal width changes
810 * out from under us. It happens here rather than in onResize_() because this
811 * code may need to run synchronously to handle programmatic changes of
812 * terminal width.
813 *
814 * Relying on the browser to send us an async resize event means we may not be
815 * in the correct state yet when the next escape sequence hits.
816 */
817hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700818 if (columnCount <= 0)
819 throw new Error('Attempt to realize bad width: ' + columnCount);
820
rgindac9bc5502012-01-18 11:48:44 -0800821 var deltaColumns = columnCount - this.screen_.getWidth();
822
rginda87b86462011-12-14 13:48:03 -0800823 this.screenSize.width = columnCount;
824 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800825
826 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400827 if (this.defaultTabStops)
828 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800829 } else {
830 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400831 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800832 break;
833
834 this.tabStops_.pop();
835 }
836 }
837
838 this.screen_.setColumnCount(this.screenSize.width);
839};
840
841/**
842 * Deal with terminal height changes.
843 *
844 * This function does what needs to be done when the terminal height changes
845 * out from under us. It happens here rather than in onResize_() because this
846 * code may need to run synchronously to handle programmatic changes of
847 * terminal height.
848 *
849 * Relying on the browser to send us an async resize event means we may not be
850 * in the correct state yet when the next escape sequence hits.
851 */
852hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700853 if (rowCount <= 0)
854 throw new Error('Attempt to realize bad height: ' + rowCount);
855
rgindac9bc5502012-01-18 11:48:44 -0800856 var deltaRows = rowCount - this.screen_.getHeight();
857
858 this.screenSize.height = rowCount;
859
860 var cursor = this.saveCursor();
861
862 if (deltaRows < 0) {
863 // Screen got smaller.
864 deltaRows *= -1;
865 while (deltaRows) {
866 var lastRow = this.getRowCount() - 1;
867 if (lastRow - this.scrollbackRows_.length == cursor.row)
868 break;
869
870 if (this.getRowText(lastRow))
871 break;
872
873 this.screen_.popRow();
874 deltaRows--;
875 }
876
877 var ary = this.screen_.shiftRows(deltaRows);
878 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
879
880 // We just removed rows from the top of the screen, we need to update
881 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800882 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800883 } else if (deltaRows > 0) {
884 // Screen got larger.
885
886 if (deltaRows <= this.scrollbackRows_.length) {
887 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
888 var rows = this.scrollbackRows_.splice(
889 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
890 this.screen_.unshiftRows(rows);
891 deltaRows -= scrollbackCount;
892 cursor.row += scrollbackCount;
893 }
894
895 if (deltaRows)
896 this.appendRows_(deltaRows);
897 }
898
rginda35c456b2012-02-09 17:29:05 -0800899 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800900 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800901};
902
903/**
904 * Scroll the terminal to the top of the scrollback buffer.
905 */
906hterm.Terminal.prototype.scrollHome = function() {
907 this.scrollPort_.scrollRowToTop(0);
908};
909
910/**
911 * Scroll the terminal to the end.
912 */
913hterm.Terminal.prototype.scrollEnd = function() {
914 this.scrollPort_.scrollRowToBottom(this.getRowCount());
915};
916
917/**
918 * Scroll the terminal one page up (minus one line) relative to the current
919 * position.
920 */
921hterm.Terminal.prototype.scrollPageUp = function() {
922 var i = this.scrollPort_.getTopRowIndex();
923 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
924};
925
926/**
927 * Scroll the terminal one page down (minus one line) relative to the current
928 * position.
929 */
930hterm.Terminal.prototype.scrollPageDown = function() {
931 var i = this.scrollPort_.getTopRowIndex();
932 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800933};
934
rgindac9bc5502012-01-18 11:48:44 -0800935/**
Robert Ginda40932892012-12-10 17:26:40 -0800936 * Clear primary screen, secondary screen, and the scrollback buffer.
937 */
938hterm.Terminal.prototype.wipeContents = function() {
939 this.scrollbackRows_.length = 0;
940 this.scrollPort_.resetCache();
941
942 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
943 var bottom = screen.getHeight();
944 if (bottom > 0) {
945 this.renumberRows_(0, bottom);
946 this.clearHome(screen);
947 }
948 }.bind(this));
949
950 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -0700951 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -0800952};
953
954/**
rgindac9bc5502012-01-18 11:48:44 -0800955 * Full terminal reset.
956 */
rginda87b86462011-12-14 13:48:03 -0800957hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800958 this.clearAllTabStops();
959 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700960
961 this.clearHome(this.primaryScreen_);
962 this.primaryScreen_.textAttributes.reset();
963
964 this.clearHome(this.alternateScreen_);
965 this.alternateScreen_.textAttributes.reset();
966
rgindab8bc8932012-04-27 12:45:03 -0700967 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
968
Robert Ginda92e18102013-03-14 13:56:37 -0700969 this.vt.reset();
970
rgindac9bc5502012-01-18 11:48:44 -0800971 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800972};
973
rgindac9bc5502012-01-18 11:48:44 -0800974/**
975 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700976 *
977 * Perform a soft reset to the default values listed in
978 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800979 */
rginda0f5c0292012-01-13 11:00:13 -0800980hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700981 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800982 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700983
rgindab8bc8932012-04-27 12:45:03 -0700984 // Xterm also resets the color palette on soft reset, even though it doesn't
985 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700986 this.primaryScreen_.textAttributes.resetColorPalette();
987 this.alternateScreen_.textAttributes.resetColorPalette();
988
rgindab8bc8932012-04-27 12:45:03 -0700989 // The xterm man page explicitly says this will happen on soft reset.
990 this.setVTScrollRegion(null, null);
991
992 // Xterm also shows the cursor on soft reset, but does not alter the blink
993 // state.
rgindaa19afe22012-01-25 15:40:22 -0800994 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800995};
996
rgindac9bc5502012-01-18 11:48:44 -0800997/**
998 * Move the cursor forward to the next tab stop, or to the last column
999 * if no more tab stops are set.
1000 */
1001hterm.Terminal.prototype.forwardTabStop = function() {
1002 var column = this.screen_.cursorPosition.column;
1003
1004 for (var i = 0; i < this.tabStops_.length; i++) {
1005 if (this.tabStops_[i] > column) {
1006 this.setCursorColumn(this.tabStops_[i]);
1007 return;
1008 }
1009 }
1010
David Benjamin66e954d2012-05-05 21:08:12 -04001011 // xterm does not clear the overflow flag on HT or CHT.
1012 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001013 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001014 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001015};
1016
rgindac9bc5502012-01-18 11:48:44 -08001017/**
1018 * Move the cursor backward to the previous tab stop, or to the first column
1019 * if no previous tab stops are set.
1020 */
1021hterm.Terminal.prototype.backwardTabStop = function() {
1022 var column = this.screen_.cursorPosition.column;
1023
1024 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1025 if (this.tabStops_[i] < column) {
1026 this.setCursorColumn(this.tabStops_[i]);
1027 return;
1028 }
1029 }
1030
1031 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001032};
1033
rgindac9bc5502012-01-18 11:48:44 -08001034/**
1035 * Set a tab stop at the given column.
1036 *
1037 * @param {int} column Zero based column.
1038 */
1039hterm.Terminal.prototype.setTabStop = function(column) {
1040 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1041 if (this.tabStops_[i] == column)
1042 return;
1043
1044 if (this.tabStops_[i] < column) {
1045 this.tabStops_.splice(i + 1, 0, column);
1046 return;
1047 }
1048 }
1049
1050 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001051};
1052
rgindac9bc5502012-01-18 11:48:44 -08001053/**
1054 * Clear the tab stop at the current cursor position.
1055 *
1056 * No effect if there is no tab stop at the current cursor position.
1057 */
1058hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1059 var column = this.screen_.cursorPosition.column;
1060
1061 var i = this.tabStops_.indexOf(column);
1062 if (i == -1)
1063 return;
1064
1065 this.tabStops_.splice(i, 1);
1066};
1067
1068/**
1069 * Clear all tab stops.
1070 */
1071hterm.Terminal.prototype.clearAllTabStops = function() {
1072 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001073 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001074};
1075
1076/**
1077 * Set up the default tab stops, starting from a given column.
1078 *
1079 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001080 * from the specified column, or 0 if no column is provided. It also flags
1081 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001082 *
1083 * This does not clear the existing tab stops first, use clearAllTabStops
1084 * for that.
1085 *
1086 * @param {int} opt_start Optional starting zero based starting column, useful
1087 * for filling out missing tab stops when the terminal is resized.
1088 */
1089hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1090 var start = opt_start || 0;
1091 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001092 // Round start up to a default tab stop.
1093 start = start - 1 - ((start - 1) % w) + w;
1094 for (var i = start; i < this.screenSize.width; i += w) {
1095 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001096 }
David Benjamin66e954d2012-05-05 21:08:12 -04001097
1098 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001099};
1100
rginda6d397402012-01-17 10:58:29 -08001101/**
rginda8ba33642011-12-14 12:31:31 -08001102 * Interpret a sequence of characters.
1103 *
1104 * Incomplete escape sequences are buffered until the next call.
1105 *
1106 * @param {string} str Sequence of characters to interpret or pass through.
1107 */
1108hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001109 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001110 this.scheduleSyncCursorPosition_();
1111};
1112
1113/**
1114 * Take over the given DIV for use as the terminal display.
1115 *
1116 * @param {HTMLDivElement} div The div to use as the terminal display.
1117 */
1118hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001119 this.div_ = div;
1120
rginda8ba33642011-12-14 12:31:31 -08001121 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001122 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001123 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1124 this.scrollPort_.setBackgroundPosition(
1125 this.prefs_.get('background-position'));
Robert Gindae76aa9f2014-03-14 12:29:12 -07001126 this.scrollPort_.setUserCss(this.prefs_.get('user-css'));
rginda30f20f62012-04-05 16:36:19 -07001127
rginda0918b652012-04-04 11:26:24 -07001128 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001129
rginda9f5222b2012-03-05 11:53:28 -08001130 this.setFontSize(this.prefs_.get('font-size'));
1131 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001132
David Reveman8f552492012-03-28 12:18:41 -04001133 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1134
rginda8ba33642011-12-14 12:31:31 -08001135 this.document_ = this.scrollPort_.getDocument();
1136
rginda4bba5e12012-06-20 16:15:30 -07001137 this.document_.body.oncontextmenu = function() { return false };
1138
1139 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001140 var screenNode = this.scrollPort_.getScreenNode();
1141 screenNode.addEventListener('mousedown', onMouse);
1142 screenNode.addEventListener('mouseup', onMouse);
1143 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001144 this.scrollPort_.onScrollWheel = onMouse;
1145
Toni Barzic0bfa8922013-11-22 11:18:35 -08001146 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001147 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001148 // Listen for mousedown events on the screenNode as in FF the focus
1149 // events don't bubble.
1150 screenNode.addEventListener('mousedown', function() {
1151 setTimeout(this.onFocusChange_.bind(this, true));
1152 }.bind(this));
1153
Toni Barzic0bfa8922013-11-22 11:18:35 -08001154 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001155 'blur', this.onFocusChange_.bind(this, false));
1156
1157 var style = this.document_.createElement('style');
1158 style.textContent =
1159 ('.cursor-node[focus="false"] {' +
1160 ' box-sizing: border-box;' +
1161 ' background-color: transparent !important;' +
1162 ' border-width: 2px;' +
1163 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001164 '}' +
1165 '.wc-node {' +
1166 ' display: inline-block;' +
1167 ' text-align: center;' +
1168 ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001169 '}');
1170 this.document_.head.appendChild(style);
1171
Ricky Liang48f05cb2013-12-31 23:35:29 +08001172 var styleSheets = this.document_.styleSheets;
1173 var cssRules = styleSheets[styleSheets.length - 1].cssRules;
1174 this.wcCssRule_ = cssRules[cssRules.length - 1];
1175
rginda8ba33642011-12-14 12:31:31 -08001176 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001177 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001178 this.cursorNode_.style.cssText =
1179 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001180 'top: -99px;' +
1181 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001182 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1183 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
Rob Spies06533ba2014-04-24 11:20:37 -07001184 '-webkit-transition: opacity, background-color 100ms linear;' +
1185 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001186
rginda8e92a692012-05-20 19:37:20 -07001187 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001188 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1189 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001190
rginda8ba33642011-12-14 12:31:31 -08001191 this.document_.body.appendChild(this.cursorNode_);
1192
rgindad5613292012-06-19 15:40:37 -07001193 // When 'enableMouseDragScroll' is off we reposition this element directly
1194 // under the mouse cursor after a click. This makes Chrome associate
1195 // subsequent mousemove events with the scroll-blocker. Since the
1196 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1197 // events do not cause the scrollport to scroll.
1198 //
1199 // It's a hack, but it's the cleanest way I could find.
1200 this.scrollBlockerNode_ = this.document_.createElement('div');
1201 this.scrollBlockerNode_.style.cssText =
1202 ('position: absolute;' +
1203 'top: -99px;' +
1204 'display: block;' +
1205 'width: 10px;' +
1206 'height: 10px;');
1207 this.document_.body.appendChild(this.scrollBlockerNode_);
1208
1209 var onMouse = this.onMouse_.bind(this);
1210 this.scrollPort_.onScrollWheel = onMouse;
1211 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1212 ].forEach(function(event) {
1213 this.scrollBlockerNode_.addEventListener(event, onMouse);
1214 this.cursorNode_.addEventListener(event, onMouse);
1215 this.document_.addEventListener(event, onMouse);
1216 }.bind(this));
1217
1218 this.cursorNode_.addEventListener('mousedown', function() {
1219 setTimeout(this.focus.bind(this));
1220 }.bind(this));
1221
rginda8ba33642011-12-14 12:31:31 -08001222 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001223
rginda87b86462011-12-14 13:48:03 -08001224 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001225 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001226};
1227
rginda0918b652012-04-04 11:26:24 -07001228/**
1229 * Return the HTML document that contains the terminal DOM nodes.
1230 */
rginda87b86462011-12-14 13:48:03 -08001231hterm.Terminal.prototype.getDocument = function() {
1232 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001233};
1234
1235/**
rginda0918b652012-04-04 11:26:24 -07001236 * Focus the terminal.
1237 */
1238hterm.Terminal.prototype.focus = function() {
1239 this.scrollPort_.focus();
1240};
1241
1242/**
rginda8ba33642011-12-14 12:31:31 -08001243 * Return the HTML Element for a given row index.
1244 *
1245 * This is a method from the RowProvider interface. The ScrollPort uses
1246 * it to fetch rows on demand as they are scrolled into view.
1247 *
1248 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1249 * pairs to conserve memory.
1250 *
1251 * @param {integer} index The zero-based row index, measured relative to the
1252 * start of the scrollback buffer. On-screen rows will always have the
1253 * largest indicies.
1254 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1255 */
1256hterm.Terminal.prototype.getRowNode = function(index) {
1257 if (index < this.scrollbackRows_.length)
1258 return this.scrollbackRows_[index];
1259
1260 var screenIndex = index - this.scrollbackRows_.length;
1261 return this.screen_.rowsArray[screenIndex];
1262};
1263
1264/**
1265 * Return the text content for a given range of rows.
1266 *
1267 * This is a method from the RowProvider interface. The ScrollPort uses
1268 * it to fetch text content on demand when the user attempts to copy their
1269 * selection to the clipboard.
1270 *
1271 * @param {integer} start The zero-based row index to start from, measured
1272 * relative to the start of the scrollback buffer. On-screen rows will
1273 * always have the largest indicies.
1274 * @param {integer} end The zero-based row index to end on, measured
1275 * relative to the start of the scrollback buffer.
1276 * @return {string} A single string containing the text value of the range of
1277 * rows. Lines will be newline delimited, with no trailing newline.
1278 */
1279hterm.Terminal.prototype.getRowsText = function(start, end) {
1280 var ary = [];
1281 for (var i = start; i < end; i++) {
1282 var node = this.getRowNode(i);
1283 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001284 if (i < end - 1 && !node.getAttribute('line-overflow'))
1285 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001286 }
1287
rgindaa09e7332012-08-17 12:49:51 -07001288 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001289};
1290
1291/**
1292 * Return the text content for a given row.
1293 *
1294 * This is a method from the RowProvider interface. The ScrollPort uses
1295 * it to fetch text content on demand when the user attempts to copy their
1296 * selection to the clipboard.
1297 *
1298 * @param {integer} index The zero-based row index to return, measured
1299 * relative to the start of the scrollback buffer. On-screen rows will
1300 * always have the largest indicies.
1301 * @return {string} A string containing the text value of the selected row.
1302 */
1303hterm.Terminal.prototype.getRowText = function(index) {
1304 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001305 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001306};
1307
1308/**
1309 * Return the total number of rows in the addressable screen and in the
1310 * scrollback buffer of this terminal.
1311 *
1312 * This is a method from the RowProvider interface. The ScrollPort uses
1313 * it to compute the size of the scrollbar.
1314 *
1315 * @return {integer} The number of rows in this terminal.
1316 */
1317hterm.Terminal.prototype.getRowCount = function() {
1318 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1319};
1320
1321/**
1322 * Create DOM nodes for new rows and append them to the end of the terminal.
1323 *
1324 * This is the only correct way to add a new DOM node for a row. Notice that
1325 * the new row is appended to the bottom of the list of rows, and does not
1326 * require renumbering (of the rowIndex property) of previous rows.
1327 *
1328 * If you think you want a new blank row somewhere in the middle of the
1329 * terminal, look into moveRows_().
1330 *
1331 * This method does not pay attention to vtScrollTop/Bottom, since you should
1332 * be using moveRows() in cases where they would matter.
1333 *
1334 * The cursor will be positioned at column 0 of the first inserted line.
1335 */
1336hterm.Terminal.prototype.appendRows_ = function(count) {
1337 var cursorRow = this.screen_.rowsArray.length;
1338 var offset = this.scrollbackRows_.length + cursorRow;
1339 for (var i = 0; i < count; i++) {
1340 var row = this.document_.createElement('x-row');
1341 row.appendChild(this.document_.createTextNode(''));
1342 row.rowIndex = offset + i;
1343 this.screen_.pushRow(row);
1344 }
1345
1346 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1347 if (extraRows > 0) {
1348 var ary = this.screen_.shiftRows(extraRows);
1349 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001350 if (this.scrollPort_.isScrolledEnd)
1351 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001352 }
1353
1354 if (cursorRow >= this.screen_.rowsArray.length)
1355 cursorRow = this.screen_.rowsArray.length - 1;
1356
rginda87b86462011-12-14 13:48:03 -08001357 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001358};
1359
1360/**
1361 * Relocate rows from one part of the addressable screen to another.
1362 *
1363 * This is used to recycle rows during VT scrolls (those which are driven
1364 * by VT commands, rather than by the user manipulating the scrollbar.)
1365 *
1366 * In this case, the blank lines scrolled into the scroll region are made of
1367 * the nodes we scrolled off. These have their rowIndex properties carefully
1368 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001369 */
1370hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1371 var ary = this.screen_.removeRows(fromIndex, count);
1372 this.screen_.insertRows(toIndex, ary);
1373
1374 var start, end;
1375 if (fromIndex < toIndex) {
1376 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001377 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001378 } else {
1379 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001380 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001381 }
1382
1383 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001384 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001385};
1386
1387/**
1388 * Renumber the rowIndex property of the given range of rows.
1389 *
1390 * The start and end indicies are relative to the screen, not the scrollback.
1391 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001392 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001393 * no need to renumber scrollback rows.
1394 */
Robert Ginda40932892012-12-10 17:26:40 -08001395hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1396 var screen = opt_screen || this.screen_;
1397
rginda8ba33642011-12-14 12:31:31 -08001398 var offset = this.scrollbackRows_.length;
1399 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001400 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001401 }
1402};
1403
1404/**
1405 * Print a string to the terminal.
1406 *
1407 * This respects the current insert and wraparound modes. It will add new lines
1408 * to the end of the terminal, scrolling off the top into the scrollback buffer
1409 * if necessary.
1410 *
1411 * The string is *not* parsed for escape codes. Use the interpret() method if
1412 * that's what you're after.
1413 *
1414 * @param{string} str The string to print.
1415 */
1416hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001417 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001418
Ricky Liang48f05cb2013-12-31 23:35:29 +08001419 var strWidth = lib.wc.strWidth(str);
1420
1421 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001422 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1423 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001424 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001425 }
rgindaa19afe22012-01-25 15:40:22 -08001426
Ricky Liang48f05cb2013-12-31 23:35:29 +08001427 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001428 var didOverflow = false;
1429 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001430
rgindaa9abdd82012-08-06 18:05:09 -07001431 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1432 didOverflow = true;
1433 count = this.screenSize.width - this.screen_.cursorPosition.column;
1434 }
rgindaa19afe22012-01-25 15:40:22 -08001435
rgindaa9abdd82012-08-06 18:05:09 -07001436 if (didOverflow && !this.options_.wraparound) {
1437 // If the string overflowed the line but wraparound is off, then the
1438 // last printed character should be the last of the string.
1439 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001440 substr = lib.wc.substr(str, startOffset, count - 1) +
1441 lib.wc.substr(str, strWidth - 1);
1442 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001443 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001444 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001445 }
rgindaa19afe22012-01-25 15:40:22 -08001446
Ricky Liang48f05cb2013-12-31 23:35:29 +08001447 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1448 for (var i = 0; i < tokens.length; i++) {
1449 if (tokens[i].wcNode)
1450 this.screen_.textAttributes.wcNode = true;
1451
1452 if (this.options_.insertMode) {
1453 this.screen_.insertString(tokens[i].str);
1454 } else {
1455 this.screen_.overwriteString(tokens[i].str);
1456 }
1457 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001458 }
1459
1460 this.screen_.maybeClipCurrentRow();
1461 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001462 }
rginda8ba33642011-12-14 12:31:31 -08001463
1464 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001465
rginda9f5222b2012-03-05 11:53:28 -08001466 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001467 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001468};
1469
1470/**
rginda87b86462011-12-14 13:48:03 -08001471 * Set the VT scroll region.
1472 *
rginda87b86462011-12-14 13:48:03 -08001473 * This also resets the cursor position to the absolute (0, 0) position, since
1474 * that's what xterm appears to do.
1475 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001476 * Setting the scroll region to the full height of the terminal will clear
1477 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1478 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1479 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1480 * continue to work as most users would expect.
1481 *
rginda87b86462011-12-14 13:48:03 -08001482 * @param {integer} scrollTop The zero-based top of the scroll region.
1483 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1484 * inclusive.
1485 */
1486hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001487 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001488 this.vtScrollTop_ = null;
1489 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001490 } else {
1491 this.vtScrollTop_ = scrollTop;
1492 this.vtScrollBottom_ = scrollBottom;
1493 }
rginda87b86462011-12-14 13:48:03 -08001494};
1495
1496/**
rginda8ba33642011-12-14 12:31:31 -08001497 * Return the top row index according to the VT.
1498 *
1499 * This will return 0 unless the terminal has been told to restrict scrolling
1500 * to some lower row. It is used for some VT cursor positioning and scrolling
1501 * commands.
1502 *
1503 * @return {integer} The topmost row in the terminal's scroll region.
1504 */
1505hterm.Terminal.prototype.getVTScrollTop = function() {
1506 if (this.vtScrollTop_ != null)
1507 return this.vtScrollTop_;
1508
1509 return 0;
rginda87b86462011-12-14 13:48:03 -08001510};
rginda8ba33642011-12-14 12:31:31 -08001511
1512/**
1513 * Return the bottom row index according to the VT.
1514 *
1515 * This will return the height of the terminal unless the it has been told to
1516 * restrict scrolling to some higher row. It is used for some VT cursor
1517 * positioning and scrolling commands.
1518 *
1519 * @return {integer} The bottommost row in the terminal's scroll region.
1520 */
1521hterm.Terminal.prototype.getVTScrollBottom = function() {
1522 if (this.vtScrollBottom_ != null)
1523 return this.vtScrollBottom_;
1524
rginda87b86462011-12-14 13:48:03 -08001525 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001526}
1527
1528/**
1529 * Process a '\n' character.
1530 *
1531 * If the cursor is on the final row of the terminal this will append a new
1532 * blank row to the screen and scroll the topmost row into the scrollback
1533 * buffer.
1534 *
1535 * Otherwise, this moves the cursor to column zero of the next row.
1536 */
1537hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001538 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1539 this.screen_.rowsArray.length - 1);
1540
1541 if (this.vtScrollBottom_ != null) {
1542 // A VT Scroll region is active, we never append new rows.
1543 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1544 // We're at the end of the VT Scroll Region, perform a VT scroll.
1545 this.vtScrollUp(1);
1546 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1547 } else if (cursorAtEndOfScreen) {
1548 // We're at the end of the screen, the only thing to do is put the
1549 // cursor to column 0.
1550 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1551 } else {
1552 // Anywhere else, advance the cursor row, and reset the column.
1553 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1554 }
1555 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001556 // We're at the end of the screen. Append a new row to the terminal,
1557 // shifting the top row into the scrollback.
1558 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001559 } else {
rginda87b86462011-12-14 13:48:03 -08001560 // Anywhere else in the screen just moves the cursor.
1561 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001562 }
1563};
1564
1565/**
1566 * Like newLine(), except maintain the cursor column.
1567 */
1568hterm.Terminal.prototype.lineFeed = function() {
1569 var column = this.screen_.cursorPosition.column;
1570 this.newLine();
1571 this.setCursorColumn(column);
1572};
1573
1574/**
rginda87b86462011-12-14 13:48:03 -08001575 * If autoCarriageReturn is set then newLine(), else lineFeed().
1576 */
1577hterm.Terminal.prototype.formFeed = function() {
1578 if (this.options_.autoCarriageReturn) {
1579 this.newLine();
1580 } else {
1581 this.lineFeed();
1582 }
1583};
1584
1585/**
1586 * Move the cursor up one row, possibly inserting a blank line.
1587 *
1588 * The cursor column is not changed.
1589 */
1590hterm.Terminal.prototype.reverseLineFeed = function() {
1591 var scrollTop = this.getVTScrollTop();
1592 var currentRow = this.screen_.cursorPosition.row;
1593
1594 if (currentRow == scrollTop) {
1595 this.insertLines(1);
1596 } else {
1597 this.setAbsoluteCursorRow(currentRow - 1);
1598 }
1599};
1600
1601/**
rginda8ba33642011-12-14 12:31:31 -08001602 * Replace all characters to the left of the current cursor with the space
1603 * character.
1604 *
1605 * TODO(rginda): This should probably *remove* the characters (not just replace
1606 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001607 * position.
rginda8ba33642011-12-14 12:31:31 -08001608 */
1609hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001610 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001611 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001612 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001613 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001614};
1615
1616/**
David Benjamin684a9b72012-05-01 17:19:58 -04001617 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001618 *
1619 * The cursor position is unchanged.
1620 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001621 * If the current background color is not the default background color this
1622 * will insert spaces rather than delete. This is unfortunate because the
1623 * trailing space will affect text selection, but it's difficult to come up
1624 * with a way to style empty space that wouldn't trip up the hterm.Screen
1625 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001626 *
1627 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1628 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1629 * crbug.com/232390 for details.
rginda8ba33642011-12-14 12:31:31 -08001630 */
1631hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001632 if (this.screen_.cursorPosition.overflow)
1633 return;
1634
Robert Ginda7fd57082012-09-25 14:41:47 -07001635 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1636 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001637
1638 if (this.screen_.textAttributes.background ===
1639 this.screen_.textAttributes.DEFAULT_COLOR) {
1640 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001641 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001642 this.screen_.cursorPosition.column + count) {
1643 this.screen_.deleteChars(count);
1644 this.clearCursorOverflow();
1645 return;
1646 }
1647 }
1648
rginda87b86462011-12-14 13:48:03 -08001649 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001650 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001651 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001652 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001653};
1654
1655/**
1656 * Erase the current line.
1657 *
1658 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001659 */
1660hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001661 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001662 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001663 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001664 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001665};
1666
1667/**
David Benjamina08d78f2012-05-05 00:28:49 -04001668 * Erase all characters from the start of the screen to the current cursor
1669 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001670 *
1671 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001672 */
1673hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001674 var cursor = this.saveCursor();
1675
1676 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001677
David Benjamina08d78f2012-05-05 00:28:49 -04001678 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001679 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001680 this.screen_.clearCursorRow();
1681 }
1682
rginda87b86462011-12-14 13:48:03 -08001683 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001684 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001685};
1686
1687/**
1688 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001689 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001690 *
1691 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001692 */
1693hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001694 var cursor = this.saveCursor();
1695
1696 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001697
David Benjamina08d78f2012-05-05 00:28:49 -04001698 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001699 for (var i = cursor.row + 1; i <= bottom; i++) {
1700 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001701 this.screen_.clearCursorRow();
1702 }
1703
rginda87b86462011-12-14 13:48:03 -08001704 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001705 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001706};
1707
1708/**
1709 * Fill the terminal with a given character.
1710 *
1711 * This methods does not respect the VT scroll region.
1712 *
1713 * @param {string} ch The character to use for the fill.
1714 */
1715hterm.Terminal.prototype.fill = function(ch) {
1716 var cursor = this.saveCursor();
1717
1718 this.setAbsoluteCursorPosition(0, 0);
1719 for (var row = 0; row < this.screenSize.height; row++) {
1720 for (var col = 0; col < this.screenSize.width; col++) {
1721 this.setAbsoluteCursorPosition(row, col);
1722 this.screen_.overwriteString(ch);
1723 }
1724 }
1725
1726 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001727};
1728
1729/**
rginda9ea433c2012-03-16 11:57:00 -07001730 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001731 *
rginda9ea433c2012-03-16 11:57:00 -07001732 * This does not respect the scroll region.
1733 *
1734 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1735 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001736 */
rginda9ea433c2012-03-16 11:57:00 -07001737hterm.Terminal.prototype.clearHome = function(opt_screen) {
1738 var screen = opt_screen || this.screen_;
1739 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001740
rginda11057d52012-04-25 12:29:56 -07001741 if (bottom == 0) {
1742 // Empty screen, nothing to do.
1743 return;
1744 }
1745
rgindae4d29232012-01-19 10:47:13 -08001746 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001747 screen.setCursorPosition(i, 0);
1748 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001749 }
1750
rginda9ea433c2012-03-16 11:57:00 -07001751 screen.setCursorPosition(0, 0);
1752};
1753
1754/**
1755 * Erase the entire display without changing the cursor position.
1756 *
1757 * The cursor position is unchanged. This does not respect the scroll
1758 * region.
1759 *
1760 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1761 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001762 */
1763hterm.Terminal.prototype.clear = function(opt_screen) {
1764 var screen = opt_screen || this.screen_;
1765 var cursor = screen.cursorPosition.clone();
1766 this.clearHome(screen);
1767 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001768};
1769
1770/**
1771 * VT command to insert lines at the current cursor row.
1772 *
1773 * This respects the current scroll region. Rows pushed off the bottom are
1774 * lost (they won't show up in the scrollback buffer).
1775 *
rginda8ba33642011-12-14 12:31:31 -08001776 * @param {integer} count The number of lines to insert.
1777 */
1778hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001779 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001780
1781 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001782 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001783
Robert Ginda579186b2012-09-26 11:40:04 -07001784 // The moveCount is the number of rows we need to relocate to make room for
1785 // the new row(s). The count is the distance to move them.
1786 var moveCount = bottom - cursorRow - count + 1;
1787 if (moveCount)
1788 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001789
Robert Ginda579186b2012-09-26 11:40:04 -07001790 for (var i = count - 1; i >= 0; i--) {
1791 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001792 this.screen_.clearCursorRow();
1793 }
rginda8ba33642011-12-14 12:31:31 -08001794};
1795
1796/**
1797 * VT command to delete lines at the current cursor row.
1798 *
1799 * New rows are added to the bottom of scroll region to take their place. New
1800 * rows are strictly there to take up space and have no content or style.
1801 */
1802hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001803 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001804
rginda87b86462011-12-14 13:48:03 -08001805 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001806 var bottom = this.getVTScrollBottom();
1807
rginda87b86462011-12-14 13:48:03 -08001808 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001809 count = Math.min(count, maxCount);
1810
rginda87b86462011-12-14 13:48:03 -08001811 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001812 if (count != maxCount)
1813 this.moveRows_(top, count, moveStart);
1814
1815 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001816 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001817 this.screen_.clearCursorRow();
1818 }
1819
rginda87b86462011-12-14 13:48:03 -08001820 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001821 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001822};
1823
1824/**
1825 * Inserts the given number of spaces at the current cursor position.
1826 *
rginda87b86462011-12-14 13:48:03 -08001827 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001828 */
1829hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001830 var cursor = this.saveCursor();
1831
rgindacbbd7482012-06-13 15:06:16 -07001832 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001833 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001834 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001835
1836 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001837 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001838};
1839
1840/**
1841 * Forward-delete the specified number of characters starting at the cursor
1842 * position.
1843 *
1844 * @param {integer} count The number of characters to delete.
1845 */
1846hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001847 var deleted = this.screen_.deleteChars(count);
1848 if (deleted && !this.screen_.textAttributes.isDefault()) {
1849 var cursor = this.saveCursor();
1850 this.setCursorColumn(this.screenSize.width - deleted);
1851 this.screen_.insertString(lib.f.getWhitespace(deleted));
1852 this.restoreCursor(cursor);
1853 }
1854
David Benjamin54e8bf62012-06-01 22:31:40 -04001855 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001856};
1857
1858/**
1859 * Shift rows in the scroll region upwards by a given number of lines.
1860 *
1861 * New rows are inserted at the bottom of the scroll region to fill the
1862 * vacated rows. The new rows not filled out with the current text attributes.
1863 *
1864 * This function does not affect the scrollback rows at all. Rows shifted
1865 * off the top are lost.
1866 *
rginda87b86462011-12-14 13:48:03 -08001867 * The cursor position is not altered.
1868 *
rginda8ba33642011-12-14 12:31:31 -08001869 * @param {integer} count The number of rows to scroll.
1870 */
1871hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001872 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001873
rginda87b86462011-12-14 13:48:03 -08001874 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001875 this.deleteLines(count);
1876
rginda87b86462011-12-14 13:48:03 -08001877 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001878};
1879
1880/**
1881 * Shift rows below the cursor down by a given number of lines.
1882 *
1883 * This function respects the current scroll region.
1884 *
1885 * New rows are inserted at the top of the scroll region to fill the
1886 * vacated rows. The new rows not filled out with the current text attributes.
1887 *
1888 * This function does not affect the scrollback rows at all. Rows shifted
1889 * off the bottom are lost.
1890 *
1891 * @param {integer} count The number of rows to scroll.
1892 */
1893hterm.Terminal.prototype.vtScrollDown = function(opt_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.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001897 this.insertLines(opt_count);
1898
rginda87b86462011-12-14 13:48:03 -08001899 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001900};
1901
rginda87b86462011-12-14 13:48:03 -08001902
rginda8ba33642011-12-14 12:31:31 -08001903/**
1904 * Set the cursor position.
1905 *
1906 * The cursor row is relative to the scroll region if the terminal has
1907 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1908 *
1909 * @param {integer} row The new zero-based cursor row.
1910 * @param {integer} row The new zero-based cursor column.
1911 */
1912hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1913 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001914 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001915 } else {
rginda87b86462011-12-14 13:48:03 -08001916 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001917 }
rginda87b86462011-12-14 13:48:03 -08001918};
rginda8ba33642011-12-14 12:31:31 -08001919
rginda87b86462011-12-14 13:48:03 -08001920hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1921 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001922 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1923 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001924 this.screen_.setCursorPosition(row, column);
1925};
1926
1927hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001928 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1929 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001930 this.screen_.setCursorPosition(row, column);
1931};
1932
1933/**
1934 * Set the cursor column.
1935 *
1936 * @param {integer} column The new zero-based cursor column.
1937 */
1938hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001939 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001940};
1941
1942/**
1943 * Return the cursor column.
1944 *
1945 * @return {integer} The zero-based cursor column.
1946 */
1947hterm.Terminal.prototype.getCursorColumn = function() {
1948 return this.screen_.cursorPosition.column;
1949};
1950
1951/**
1952 * Set the cursor row.
1953 *
1954 * The cursor row is relative to the scroll region if the terminal has
1955 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1956 *
1957 * @param {integer} row The new cursor row.
1958 */
rginda87b86462011-12-14 13:48:03 -08001959hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1960 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001961};
1962
1963/**
1964 * Return the cursor row.
1965 *
1966 * @return {integer} The zero-based cursor row.
1967 */
1968hterm.Terminal.prototype.getCursorRow = function(row) {
1969 return this.screen_.cursorPosition.row;
1970};
1971
1972/**
1973 * Request that the ScrollPort redraw itself soon.
1974 *
1975 * The redraw will happen asynchronously, soon after the call stack winds down.
1976 * Multiple calls will be coalesced into a single redraw.
1977 */
1978hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001979 if (this.timeouts_.redraw)
1980 return;
rginda8ba33642011-12-14 12:31:31 -08001981
1982 var self = this;
rginda87b86462011-12-14 13:48:03 -08001983 this.timeouts_.redraw = setTimeout(function() {
1984 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001985 self.scrollPort_.redraw_();
1986 }, 0);
1987};
1988
1989/**
1990 * Request that the ScrollPort be scrolled to the bottom.
1991 *
1992 * The scroll will happen asynchronously, soon after the call stack winds down.
1993 * Multiple calls will be coalesced into a single scroll.
1994 *
1995 * This affects the scrollbar position of the ScrollPort, and has nothing to
1996 * do with the VT scroll commands.
1997 */
1998hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1999 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002000 return;
rginda8ba33642011-12-14 12:31:31 -08002001
2002 var self = this;
2003 this.timeouts_.scrollDown = setTimeout(function() {
2004 delete self.timeouts_.scrollDown;
2005 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2006 }, 10);
2007};
2008
2009/**
2010 * Move the cursor up a specified number of rows.
2011 *
2012 * @param {integer} count The number of rows to move the cursor.
2013 */
2014hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002015 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002016};
2017
2018/**
2019 * Move the cursor down a specified number of rows.
2020 *
2021 * @param {integer} count The number of rows to move the cursor.
2022 */
2023hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002024 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002025 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2026 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2027 this.screenSize.height - 1);
2028
rgindacbbd7482012-06-13 15:06:16 -07002029 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002030 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002031 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002032};
2033
2034/**
2035 * Move the cursor left a specified number of columns.
2036 *
2037 * @param {integer} count The number of columns to move the cursor.
2038 */
2039hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002040 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002041};
2042
2043/**
2044 * Move the cursor right a specified number of columns.
2045 *
2046 * @param {integer} count The number of columns to move the cursor.
2047 */
2048hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002049 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07002050 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002051 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002052 this.setCursorColumn(column);
2053};
2054
2055/**
2056 * Reverse the foreground and background colors of the terminal.
2057 *
2058 * This only affects text that was drawn with no attributes.
2059 *
2060 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2061 * been drawn with attributes that happen to coincide with the default
2062 * 'no-attribute' colors. My guess is probably not.
2063 */
2064hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002065 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002066 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002067 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2068 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002069 } else {
rginda9f5222b2012-03-05 11:53:28 -08002070 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2071 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002072 }
2073};
2074
2075/**
rginda87b86462011-12-14 13:48:03 -08002076 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002077 *
2078 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002079 */
2080hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002081 this.cursorNode_.style.backgroundColor =
2082 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002083
2084 var self = this;
2085 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002086 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002087 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002088
Michael Kelly485ecd12014-06-09 11:41:56 -04002089 // bellSquelchTimeout_ affects both audio and notification bells.
2090 if (this.bellSquelchTimeout_)
2091 return;
2092
Robert Ginda92e18102013-03-14 13:56:37 -07002093 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002094 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002095 this.bellSequelchTimeout_ = setTimeout(function() {
2096 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002097 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002098 } else {
2099 delete this.bellSquelchTimeout_;
2100 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002101
2102 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
2103 var n = new Notification(
2104 lib.f.replaceVars(hterm.desktopNotificationTitle,
2105 {'title': this.document_.title || 'hterm'}));
2106 this.bellNotificationList_.push(n);
2107 // TODO: Should we try to raise the window here?
2108 n.onclick = function() { self.closeBellNotifications_(); };
2109 }
rginda87b86462011-12-14 13:48:03 -08002110};
2111
2112/**
rginda8ba33642011-12-14 12:31:31 -08002113 * Set the origin mode bit.
2114 *
2115 * If origin mode is on, certain VT cursor and scrolling commands measure their
2116 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2117 * to the top of the addressable screen.
2118 *
2119 * Defaults to off.
2120 *
2121 * @param {boolean} state True to set origin mode, false to unset.
2122 */
2123hterm.Terminal.prototype.setOriginMode = function(state) {
2124 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002125 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002126};
2127
2128/**
2129 * Set the insert mode bit.
2130 *
2131 * If insert mode is on, existing text beyond the cursor position will be
2132 * shifted right to make room for new text. Otherwise, new text overwrites
2133 * any existing text.
2134 *
2135 * Defaults to off.
2136 *
2137 * @param {boolean} state True to set insert mode, false to unset.
2138 */
2139hterm.Terminal.prototype.setInsertMode = function(state) {
2140 this.options_.insertMode = state;
2141};
2142
2143/**
rginda87b86462011-12-14 13:48:03 -08002144 * Set the auto carriage return bit.
2145 *
2146 * If auto carriage return is on then a formfeed character is interpreted
2147 * as a newline, otherwise it's the same as a linefeed. The difference boils
2148 * down to whether or not the cursor column is reset.
2149 */
2150hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2151 this.options_.autoCarriageReturn = state;
2152};
2153
2154/**
rginda8ba33642011-12-14 12:31:31 -08002155 * Set the wraparound mode bit.
2156 *
2157 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2158 * to the start of the following row. Otherwise, the cursor is clamped to the
2159 * end of the screen and attempts to write past it are ignored.
2160 *
2161 * Defaults to on.
2162 *
2163 * @param {boolean} state True to set wraparound mode, false to unset.
2164 */
2165hterm.Terminal.prototype.setWraparound = function(state) {
2166 this.options_.wraparound = state;
2167};
2168
2169/**
2170 * Set the reverse-wraparound mode bit.
2171 *
2172 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2173 * to the end of the previous row. Otherwise, the cursor is clamped to column
2174 * 0.
2175 *
2176 * Defaults to off.
2177 *
2178 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2179 */
2180hterm.Terminal.prototype.setReverseWraparound = function(state) {
2181 this.options_.reverseWraparound = state;
2182};
2183
2184/**
2185 * Selects between the primary and alternate screens.
2186 *
2187 * If alternate mode is on, the alternate screen is active. Otherwise the
2188 * primary screen is active.
2189 *
2190 * Swapping screens has no effect on the scrollback buffer.
2191 *
2192 * Each screen maintains its own cursor position.
2193 *
2194 * Defaults to off.
2195 *
2196 * @param {boolean} state True to set alternate mode, false to unset.
2197 */
2198hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002199 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002200 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2201
rginda35c456b2012-02-09 17:29:05 -08002202 if (this.screen_.rowsArray.length &&
2203 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2204 // If the screen changed sizes while we were away, our rowIndexes may
2205 // be incorrect.
2206 var offset = this.scrollbackRows_.length;
2207 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002208 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002209 ary[i].rowIndex = offset + i;
2210 }
2211 }
rginda8ba33642011-12-14 12:31:31 -08002212
rginda35c456b2012-02-09 17:29:05 -08002213 this.realizeWidth_(this.screenSize.width);
2214 this.realizeHeight_(this.screenSize.height);
2215 this.scrollPort_.syncScrollHeight();
2216 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002217
rginda6d397402012-01-17 10:58:29 -08002218 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002219 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002220};
2221
2222/**
2223 * Set the cursor-blink mode bit.
2224 *
2225 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2226 * a visible cursor does not blink.
2227 *
2228 * You should make sure to turn blinking off if you're going to dispose of a
2229 * terminal, otherwise you'll leak a timeout.
2230 *
2231 * Defaults to on.
2232 *
2233 * @param {boolean} state True to set cursor-blink mode, false to unset.
2234 */
2235hterm.Terminal.prototype.setCursorBlink = function(state) {
2236 this.options_.cursorBlink = state;
2237
2238 if (!state && this.timeouts_.cursorBlink) {
2239 clearTimeout(this.timeouts_.cursorBlink);
2240 delete this.timeouts_.cursorBlink;
2241 }
2242
2243 if (this.options_.cursorVisible)
2244 this.setCursorVisible(true);
2245};
2246
2247/**
2248 * Set the cursor-visible mode bit.
2249 *
2250 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2251 *
2252 * Defaults to on.
2253 *
2254 * @param {boolean} state True to set cursor-visible mode, false to unset.
2255 */
2256hterm.Terminal.prototype.setCursorVisible = function(state) {
2257 this.options_.cursorVisible = state;
2258
2259 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002260 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002261 return;
2262 }
2263
rginda87b86462011-12-14 13:48:03 -08002264 this.syncCursorPosition_();
2265
2266 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002267
2268 if (this.options_.cursorBlink) {
2269 if (this.timeouts_.cursorBlink)
2270 return;
2271
2272 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2273 500);
2274 } else {
2275 if (this.timeouts_.cursorBlink) {
2276 clearTimeout(this.timeouts_.cursorBlink);
2277 delete this.timeouts_.cursorBlink;
2278 }
2279 }
2280};
2281
2282/**
rginda87b86462011-12-14 13:48:03 -08002283 * Synchronizes the visible cursor and document selection with the current
2284 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002285 */
2286hterm.Terminal.prototype.syncCursorPosition_ = function() {
2287 var topRowIndex = this.scrollPort_.getTopRowIndex();
2288 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2289 var cursorRowIndex = this.scrollbackRows_.length +
2290 this.screen_.cursorPosition.row;
2291
2292 if (cursorRowIndex > bottomRowIndex) {
2293 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002294 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002295 return;
2296 }
2297
2298 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002299 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2300 'px';
2301 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2302 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002303
2304 this.cursorNode_.setAttribute('title',
2305 '(' + this.screen_.cursorPosition.row +
2306 ', ' + this.screen_.cursorPosition.column +
2307 ')');
2308
2309 // Update the caret for a11y purposes.
2310 var selection = this.document_.getSelection();
2311 if (selection && selection.isCollapsed)
2312 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002313};
2314
Robert Gindafb1be6a2013-12-11 11:56:22 -08002315/**
2316 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2317 * and character cell dimensions.
2318 */
Robert Ginda830583c2013-08-07 13:20:46 -07002319hterm.Terminal.prototype.restyleCursor_ = function() {
2320 var shape = this.cursorShape_;
2321
2322 if (this.cursorNode_.getAttribute('focus') == 'false') {
2323 // Always show a block cursor when unfocused.
2324 shape = hterm.Terminal.cursorShape.BLOCK;
2325 }
2326
2327 var style = this.cursorNode_.style;
2328
Robert Gindafb1be6a2013-12-11 11:56:22 -08002329 style.width = this.scrollPort_.characterSize.width + 'px';
2330
Robert Ginda830583c2013-08-07 13:20:46 -07002331 switch (shape) {
2332 case hterm.Terminal.cursorShape.BEAM:
2333 style.height = this.scrollPort_.characterSize.height + 'px';
2334 style.backgroundColor = 'transparent';
2335 style.borderBottomStyle = null;
2336 style.borderLeftStyle = 'solid';
2337 break;
2338
2339 case hterm.Terminal.cursorShape.UNDERLINE:
2340 style.height = this.scrollPort_.characterSize.baseline + 'px';
2341 style.backgroundColor = 'transparent';
2342 style.borderBottomStyle = 'solid';
2343 // correct the size to put it exactly at the baseline
2344 style.borderLeftStyle = null;
2345 break;
2346
2347 default:
2348 style.height = this.scrollPort_.characterSize.height + 'px';
2349 style.backgroundColor = this.cursorColor_;
2350 style.borderBottomStyle = null;
2351 style.borderLeftStyle = null;
2352 break;
2353 }
2354};
2355
rginda8ba33642011-12-14 12:31:31 -08002356/**
2357 * Synchronizes the visible cursor with the current cursor coordinates.
2358 *
2359 * The sync will happen asynchronously, soon after the call stack winds down.
2360 * Multiple calls will be coalesced into a single sync.
2361 */
2362hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2363 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002364 return;
rginda8ba33642011-12-14 12:31:31 -08002365
2366 var self = this;
2367 this.timeouts_.syncCursor = setTimeout(function() {
2368 self.syncCursorPosition_();
2369 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002370 }, 0);
2371};
2372
rgindacc2996c2012-02-24 14:59:31 -08002373/**
rgindaf522ce02012-04-17 17:49:17 -07002374 * Show or hide the zoom warning.
2375 *
2376 * The zoom warning is a message warning the user that their browser zoom must
2377 * be set to 100% in order for hterm to function properly.
2378 *
2379 * @param {boolean} state True to show the message, false to hide it.
2380 */
2381hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2382 if (!this.zoomWarningNode_) {
2383 if (!state)
2384 return;
2385
2386 this.zoomWarningNode_ = this.document_.createElement('div');
2387 this.zoomWarningNode_.style.cssText = (
2388 'color: black;' +
2389 'background-color: #ff2222;' +
2390 'font-size: large;' +
2391 'border-radius: 8px;' +
2392 'opacity: 0.75;' +
2393 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2394 'top: 0.5em;' +
2395 'right: 1.2em;' +
2396 'position: absolute;' +
2397 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002398 '-webkit-user-select: none;' +
2399 '-moz-text-size-adjust: none;' +
2400 '-moz-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002401 }
2402
Robert Gindab4839c22013-02-28 16:52:10 -08002403 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2404 hterm.zoomWarningMessage,
2405 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2406
rgindaf522ce02012-04-17 17:49:17 -07002407 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2408
2409 if (state) {
2410 if (!this.zoomWarningNode_.parentNode)
2411 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2412 } else if (this.zoomWarningNode_.parentNode) {
2413 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2414 }
2415};
2416
2417/**
rgindacc2996c2012-02-24 14:59:31 -08002418 * Show the terminal overlay for a given amount of time.
2419 *
2420 * The terminal overlay appears in inverse video in a large font, centered
2421 * over the terminal. You should probably keep the overlay message brief,
2422 * since it's in a large font and you probably aren't going to check the size
2423 * of the terminal first.
2424 *
2425 * @param {string} msg The text (not HTML) message to display in the overlay.
2426 * @param {number} opt_timeout The amount of time to wait before fading out
2427 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2428 * stay up forever (or until the next overlay).
2429 */
2430hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002431 if (!this.overlayNode_) {
2432 if (!this.div_)
2433 return;
2434
2435 this.overlayNode_ = this.document_.createElement('div');
2436 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002437 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002438 'font-size: xx-large;' +
2439 'opacity: 0.75;' +
2440 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2441 'position: absolute;' +
2442 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002443 '-webkit-transition: opacity 180ms ease-in;' +
2444 '-moz-user-select: none;' +
2445 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002446
2447 this.overlayNode_.addEventListener('mousedown', function(e) {
2448 e.preventDefault();
2449 e.stopPropagation();
2450 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002451 }
2452
rginda9f5222b2012-03-05 11:53:28 -08002453 this.overlayNode_.style.color = this.prefs_.get('background-color');
2454 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2455 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2456
rgindaf0090c92012-02-10 14:58:52 -08002457 this.overlayNode_.textContent = msg;
2458 this.overlayNode_.style.opacity = '0.75';
2459
2460 if (!this.overlayNode_.parentNode)
2461 this.div_.appendChild(this.overlayNode_);
2462
Robert Ginda97769282013-02-01 15:30:30 -08002463 var divSize = hterm.getClientSize(this.div_);
2464 var overlaySize = hterm.getClientSize(this.overlayNode_);
2465
2466 this.overlayNode_.style.top = (divSize.height - overlaySize.height) / 2;
2467 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
2468 this.scrollPort_.currentScrollbarWidthPx) / 2;
rgindaf0090c92012-02-10 14:58:52 -08002469
2470 var self = this;
2471
2472 if (this.overlayTimeout_)
2473 clearTimeout(this.overlayTimeout_);
2474
rgindacc2996c2012-02-24 14:59:31 -08002475 if (opt_timeout === null)
2476 return;
2477
rgindaf0090c92012-02-10 14:58:52 -08002478 this.overlayTimeout_ = setTimeout(function() {
2479 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002480 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002481 if (self.overlayNode_.parentNode)
2482 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002483 self.overlayTimeout_ = null;
2484 self.overlayNode_.style.opacity = '0.75';
2485 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002486 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002487};
2488
rginda4bba5e12012-06-20 16:15:30 -07002489/**
2490 * Paste from the system clipboard to the terminal.
2491 */
2492hterm.Terminal.prototype.paste = function() {
2493 hterm.pasteFromClipboard(this.document_);
2494};
2495
2496/**
2497 * Copy a string to the system clipboard.
2498 *
2499 * Note: If there is a selected range in the terminal, it'll be cleared.
2500 */
2501hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002502 if (this.prefs_.get('enable-clipboard-notice'))
2503 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2504
rgindaa09e7332012-08-17 12:49:51 -07002505 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002506 copySource.textContent = str;
2507 copySource.style.cssText = (
2508 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002509 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002510 'position: absolute;' +
2511 'top: -99px');
2512
2513 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002514
rginda4bba5e12012-06-20 16:15:30 -07002515 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002516 var anchorNode = selection.anchorNode;
2517 var anchorOffset = selection.anchorOffset;
2518 var focusNode = selection.focusNode;
2519 var focusOffset = selection.focusOffset;
2520
rginda4bba5e12012-06-20 16:15:30 -07002521 selection.selectAllChildren(copySource);
2522
rgindaa09e7332012-08-17 12:49:51 -07002523 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002524
Rob Spies56953412014-04-28 14:09:47 -07002525 // IE doesn't support selection.extend. This means that the selection
2526 // won't return on IE.
Rob Spies0bec09b2014-06-06 15:58:09 -07002527 if (this.clearSelectionAfterCopy && selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002528 selection.collapse(anchorNode, anchorOffset);
2529 selection.extend(focusNode, focusOffset);
2530 }
rgindafaa74742012-08-21 13:34:03 -07002531
rginda4bba5e12012-06-20 16:15:30 -07002532 copySource.parentNode.removeChild(copySource);
2533};
2534
rgindaa09e7332012-08-17 12:49:51 -07002535hterm.Terminal.prototype.getSelectionText = function() {
2536 var selection = this.scrollPort_.selection;
2537 selection.sync();
2538
2539 if (selection.isCollapsed)
2540 return null;
2541
2542
2543 // Start offset measures from the beginning of the line.
2544 var startOffset = selection.startOffset;
2545 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002546
Robert Gindafdbb3f22012-09-06 20:23:06 -07002547 if (node.nodeName != 'X-ROW') {
2548 // If the selection doesn't start on an x-row node, then it must be
2549 // somewhere inside the x-row. Add any characters from previous siblings
2550 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002551
2552 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2553 // If node is the text node in a styled span, move up to the span node.
2554 node = node.parentNode;
2555 }
2556
Robert Gindafdbb3f22012-09-06 20:23:06 -07002557 while (node.previousSibling) {
2558 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002559 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002560 }
rgindaa09e7332012-08-17 12:49:51 -07002561 }
2562
2563 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002564 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2565 selection.endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002566 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002567
Robert Gindafdbb3f22012-09-06 20:23:06 -07002568 if (node.nodeName != 'X-ROW') {
2569 // If the selection doesn't end on an x-row node, then it must be
2570 // somewhere inside the x-row. Add any characters from following siblings
2571 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002572
2573 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2574 // If node is the text node in a styled span, move up to the span node.
2575 node = node.parentNode;
2576 }
2577
Robert Gindafdbb3f22012-09-06 20:23:06 -07002578 while (node.nextSibling) {
2579 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002580 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002581 }
rgindaa09e7332012-08-17 12:49:51 -07002582 }
2583
2584 var rv = this.getRowsText(selection.startRow.rowIndex,
2585 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002586 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002587};
2588
rginda4bba5e12012-06-20 16:15:30 -07002589/**
2590 * Copy the current selection to the system clipboard, then clear it after a
2591 * short delay.
2592 */
2593hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002594 var text = this.getSelectionText();
2595 if (text != null)
2596 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002597};
2598
rgindaf0090c92012-02-10 14:58:52 -08002599hterm.Terminal.prototype.overlaySize = function() {
2600 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2601};
2602
rginda87b86462011-12-14 13:48:03 -08002603/**
2604 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2605 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002606 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002607 */
2608hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002609 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002610 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2611
Robert Ginda8cb7d902013-06-20 14:37:18 -07002612 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002613};
2614
2615/**
rgindad5613292012-06-19 15:40:37 -07002616 * Add the terminalRow and terminalColumn properties to mouse events and
2617 * then forward on to onMouse().
2618 *
2619 * The terminalRow and terminalColumn properties contain the (row, column)
2620 * coordinates for the mouse event.
2621 */
2622hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002623 if (e.processedByTerminalHandler_) {
2624 // We register our event handlers on the document, as well as the cursor
2625 // and the scroll blocker. Mouse events that occur on the cursor or
2626 // scroll blocker will also appear on the document, but we don't want to
2627 // process them twice.
2628 //
2629 // We can't just prevent bubbling because that has other side effects, so
2630 // we decorate the event object with this property instead.
2631 return;
2632 }
2633
2634 e.processedByTerminalHandler_ = true;
2635
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002636 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2637 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002638 return;
2639 }
2640
rgindad5613292012-06-19 15:40:37 -07002641 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2642 this.scrollPort_.characterSize.height) + 1;
2643 e.terminalColumn = parseInt(e.clientX /
2644 this.scrollPort_.characterSize.width) + 1;
2645
Robert Ginda928cf632014-03-05 15:07:41 -08002646 if (e.type == 'mousedown') {
2647 if (e.altKey || this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED) {
2648 // If VT mouse reporting is disabled, or has been defeated with
2649 // alt-mousedown, then the mouse will act on the local selection.
2650 this.reportMouseEvents_ = false;
2651 this.setSelectionEnabled(true);
2652 } else {
2653 // Otherwise we defer ownership of the mouse to the VT.
2654 this.reportMouseEvents_ = true;
Robert Ginda3ae37822014-05-15 13:05:35 -07002655 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08002656 this.setSelectionEnabled(false);
2657 e.preventDefault();
2658 }
2659 }
2660
2661 if (!this.reportMouseEvents_) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002662 if (e.type == 'dblclick') {
2663 this.screen_.expandSelection(this.document_.getSelection());
2664 hterm.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07002665 }
2666
Robert Ginda928cf632014-03-05 15:07:41 -08002667 if (e.type == 'mousedown' && e.which == this.mousePasteButton)
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002668 this.paste();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002669
2670 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2671 !this.document_.getSelection().isCollapsed) {
2672 hterm.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002673 }
2674
2675 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
2676 this.scrollBlockerNode_.engaged) {
2677 // Disengage the scroll-blocker after one of these events.
2678 this.scrollBlockerNode_.engaged = false;
2679 this.scrollBlockerNode_.style.top = '-99px';
2680 }
2681
Robert Ginda928cf632014-03-05 15:07:41 -08002682 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002683 if (!this.scrollBlockerNode_.engaged) {
2684 if (e.type == 'mousedown') {
2685 // Move the scroll-blocker into place if we want to keep the scrollport
2686 // from scrolling.
2687 this.scrollBlockerNode_.engaged = true;
2688 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2689 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2690 } else if (e.type == 'mousemove') {
2691 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
2692 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07002693 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002694 e.preventDefault();
2695 }
2696 }
Robert Ginda928cf632014-03-05 15:07:41 -08002697
2698 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002699 }
2700
Robert Ginda928cf632014-03-05 15:07:41 -08002701 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
2702 // Restore this on mouseup in case it was temporarily defeated with a
2703 // alt-mousedown. Only do this when the selection is empty so that
2704 // we don't immediately kill the users selection.
2705 this.reportMouseEvents_ = (this.vt.mouseReport !=
2706 this.vt.MOUSE_REPORT_DISABLED);
2707 }
rgindad5613292012-06-19 15:40:37 -07002708};
2709
2710/**
2711 * Clients should override this if they care to know about mouse events.
2712 *
2713 * The event parameter will be a normal DOM mouse click event with additional
2714 * 'terminalRow' and 'terminalColumn' properties.
2715 */
2716hterm.Terminal.prototype.onMouse = function(e) { };
2717
2718/**
rginda8e92a692012-05-20 19:37:20 -07002719 * React when focus changes.
2720 */
Rob Spies06533ba2014-04-24 11:20:37 -07002721hterm.Terminal.prototype.onFocusChange_ = function(focused) {
2722 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07002723 this.restyleCursor_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002724 if (focused === true)
2725 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07002726};
2727
2728/**
rginda8ba33642011-12-14 12:31:31 -08002729 * React when the ScrollPort is scrolled.
2730 */
2731hterm.Terminal.prototype.onScroll_ = function() {
2732 this.scheduleSyncCursorPosition_();
2733};
2734
2735/**
rginda9846e2f2012-01-27 13:53:33 -08002736 * React when text is pasted into the scrollPort.
2737 */
2738hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Ginda8cb7d902013-06-20 14:37:18 -07002739 this.onVTKeystroke(e.text.replace(/\n/mg, '\r'));
rginda9846e2f2012-01-27 13:53:33 -08002740};
2741
2742/**
rgindaa09e7332012-08-17 12:49:51 -07002743 * React when the user tries to copy from the scrollPort.
2744 */
2745hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07002746 if (!this.useDefaultWindowCopy) {
2747 e.preventDefault();
2748 setTimeout(this.copySelectionToClipboard.bind(this), 0);
2749 }
rgindaa09e7332012-08-17 12:49:51 -07002750};
2751
2752/**
rginda8ba33642011-12-14 12:31:31 -08002753 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002754 *
2755 * Note: This function should not directly contain code that alters the internal
2756 * state of the terminal. That kind of code belongs in realizeWidth or
2757 * realizeHeight, so that it can be executed synchronously in the case of a
2758 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002759 */
2760hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08002761 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002762 this.scrollPort_.characterSize.width);
Robert Ginda19f61292014-03-04 14:07:57 -08002763 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
rginda35c456b2012-02-09 17:29:05 -08002764 this.scrollPort_.characterSize.height);
2765
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002766 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002767 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002768 // gets removed from the document or during the initial load, and we can't
2769 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002770 return;
2771 }
2772
rgindaa8ba17d2012-08-15 14:41:10 -07002773 var isNewSize = (columnCount != this.screenSize.width ||
2774 rowCount != this.screenSize.height);
2775
2776 // We do this even if the size didn't change, just to be sure everything is
2777 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002778 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002779 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002780
2781 if (isNewSize)
2782 this.overlaySize();
2783
Robert Gindafb1be6a2013-12-11 11:56:22 -08002784 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07002785 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002786};
2787
2788/**
2789 * Service the cursor blink timeout.
2790 */
2791hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Ginda830583c2013-08-07 13:20:46 -07002792 if (this.cursorNode_.getAttribute('focus') == 'false' ||
2793 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08002794 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002795 } else {
rginda87b86462011-12-14 13:48:03 -08002796 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002797 }
2798};
David Reveman8f552492012-03-28 12:18:41 -04002799
2800/**
2801 * Set the scrollbar-visible mode bit.
2802 *
2803 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2804 * Otherwise it will not.
2805 *
2806 * Defaults to on.
2807 *
2808 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2809 */
2810hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2811 this.scrollPort_.setScrollbarVisible(state);
2812};
Michael Kelly485ecd12014-06-09 11:41:56 -04002813
2814/**
2815 * Close all web notifications created by terminal bells.
2816 */
2817hterm.Terminal.prototype.closeBellNotifications_ = function() {
2818 this.bellNotificationList_.forEach(function(n) {
2819 n.close();
2820 });
2821 this.bellNotificationList_.length = 0;
2822};