blob: baef0821dee5b54a5f2daa5d9f1ef4cfb4fa939a [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;
Michael Kelly485ecd12014-06-09 11:41:56 -0400108
rginda6d397402012-01-17 10:58:29 -0800109 // Cursor position and attributes saved with DECSC.
110 this.savedOptions_ = {};
111
rginda8ba33642011-12-14 12:31:31 -0800112 // The current mode bits for the terminal.
113 this.options_ = new hterm.Options();
114
115 // Timeouts we might need to clear.
116 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800117
118 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800119 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800120
rgindafeaf3142012-01-31 15:14:20 -0800121 // The keyboard hander.
122 this.keyboard = new hterm.Keyboard(this);
123
rginda87b86462011-12-14 13:48:03 -0800124 // General IO interface that can be given to third parties without exposing
125 // the entire terminal object.
126 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800127
rgindad5613292012-06-19 15:40:37 -0700128 // True if mouse-click-drag should scroll the terminal.
129 this.enableMouseDragScroll = true;
130
Robert Ginda57f03b42012-09-13 11:02:48 -0700131 this.copyOnSelect = null;
rginda4bba5e12012-06-20 16:15:30 -0700132 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700133
Rob Spies0bec09b2014-06-06 15:58:09 -0700134 // Whether to use the default window copy behaviour.
135 this.useDefaultWindowCopy = false;
136
137 this.clearSelectionAfterCopy = true;
138
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400139 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800140 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700141
142 this.setProfile(opt_profileId || 'default',
143 function() { this.onTerminalReady() }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800144};
145
146/**
Robert Ginda830583c2013-08-07 13:20:46 -0700147 * Possible cursor shapes.
148 */
149hterm.Terminal.cursorShape = {
150 BLOCK: 'BLOCK',
151 BEAM: 'BEAM',
152 UNDERLINE: 'UNDERLINE'
153};
154
155/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700156 * Clients should override this to be notified when the terminal is ready
157 * for use.
158 *
159 * The terminal initialization is asynchronous, and shouldn't be used before
160 * this method is called.
161 */
162hterm.Terminal.prototype.onTerminalReady = function() { };
163
164/**
rginda35c456b2012-02-09 17:29:05 -0800165 * Default tab with of 8 to match xterm.
166 */
167hterm.Terminal.prototype.tabWidth = 8;
168
169/**
rginda9f5222b2012-03-05 11:53:28 -0800170 * Select a preference profile.
171 *
172 * This will load the terminal preferences for the given profile name and
173 * associate subsequent preference changes with the new preference profile.
174 *
175 * @param {string} newName The name of the preference profile. Forward slash
176 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700177 * @param {function} opt_callback Optional callback to invoke when the profile
178 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800179 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700180hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
181 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800182
Robert Ginda57f03b42012-09-13 11:02:48 -0700183 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800184
Robert Ginda57f03b42012-09-13 11:02:48 -0700185 if (this.prefs_)
186 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800187
Robert Ginda57f03b42012-09-13 11:02:48 -0700188 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
189 this.prefs_.addObservers(null, {
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700190 'alt-backspace-is-meta-backspace': function(v) {
191 terminal.keyboard.altBackspaceIsMetaBackspace = v;
192 },
193
Robert Ginda57f03b42012-09-13 11:02:48 -0700194 'alt-is-meta': function(v) {
195 terminal.keyboard.altIsMeta = v;
196 },
197
198 'alt-sends-what': function(v) {
199 if (!/^(escape|8-bit|browser-key)$/.test(v))
200 v = 'escape';
201
202 terminal.keyboard.altSendsWhat = v;
203 },
204
205 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800206 var ary = v.match(/^lib-resource:(\S+)/);
207 if (ary) {
208 terminal.bellAudio_.setAttribute('src',
209 lib.resource.getDataUrl(ary[1]));
210 } else {
211 terminal.bellAudio_.setAttribute('src', v);
212 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700213 },
214
Michael Kelly485ecd12014-06-09 11:41:56 -0400215 'desktop-notification-bell': function(v) {
216 if (v && Notification) {
217 // We cannot rely on having notification permission by default.
218 if (Notification.permission !== 'granted') {
219 Notification.requestPermission(function(permission) {
220 terminal.desktopNotificationBell_ = (permission === 'granted');
221 });
222 } else {
223 terminal.desktopNotificationBell_ = true;
224 }
225 } else {
226 terminal.desktopNotificationBell_ = false;
227 }
228 },
229
Robert Ginda57f03b42012-09-13 11:02:48 -0700230 'background-color': function(v) {
231 terminal.setBackgroundColor(v);
232 },
233
234 'background-image': function(v) {
235 terminal.scrollPort_.setBackgroundImage(v);
236 },
237
238 'background-size': function(v) {
239 terminal.scrollPort_.setBackgroundSize(v);
240 },
241
242 'background-position': function(v) {
243 terminal.scrollPort_.setBackgroundPosition(v);
244 },
245
246 'backspace-sends-backspace': function(v) {
247 terminal.keyboard.backspaceSendsBackspace = v;
248 },
249
250 'cursor-blink': function(v) {
251 terminal.setCursorBlink(!!v);
252 },
253
254 'cursor-color': function(v) {
255 terminal.setCursorColor(v);
256 },
257
258 'color-palette-overrides': function(v) {
259 if (!(v == null || v instanceof Object || v instanceof Array)) {
260 console.warn('Preference color-palette-overrides is not an array or ' +
261 'object: ' + v);
262 return;
rginda9f5222b2012-03-05 11:53:28 -0800263 }
rginda9f5222b2012-03-05 11:53:28 -0800264
Robert Ginda57f03b42012-09-13 11:02:48 -0700265 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700266
Robert Ginda57f03b42012-09-13 11:02:48 -0700267 if (v) {
268 for (var key in v) {
269 var i = parseInt(key);
270 if (isNaN(i) || i < 0 || i > 255) {
271 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
272 continue;
273 }
274
275 if (v[i]) {
276 var rgb = lib.colors.normalizeCSS(v[i]);
277 if (rgb)
278 lib.colors.colorPalette[i] = rgb;
279 }
280 }
rginda30f20f62012-04-05 16:36:19 -0700281 }
rginda30f20f62012-04-05 16:36:19 -0700282
Robert Ginda57f03b42012-09-13 11:02:48 -0700283 terminal.primaryScreen_.textAttributes.resetColorPalette()
284 terminal.alternateScreen_.textAttributes.resetColorPalette();
285 },
rginda30f20f62012-04-05 16:36:19 -0700286
Robert Ginda57f03b42012-09-13 11:02:48 -0700287 'copy-on-select': function(v) {
288 terminal.copyOnSelect = !!v;
289 },
rginda9f5222b2012-03-05 11:53:28 -0800290
Rob Spies0bec09b2014-06-06 15:58:09 -0700291 'use-default-window-copy': function(v) {
292 terminal.useDefaultWindowCopy = !!v;
293 },
294
295 'clear-selection-after-copy': function(v) {
296 terminal.clearSelectionAfterCopy = !!v;
297 },
298
Robert Ginda7e5e9522014-03-14 12:23:58 -0700299 'ctrl-plus-minus-zero-zoom': function(v) {
300 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
301 },
302
Robert Gindafb5a3f92014-05-13 14:12:00 -0700303 'ctrl-c-copy': function(v) {
304 terminal.keyboard.ctrlCCopy = v;
305 },
306
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100307 'ctrl-v-paste': function(v) {
308 terminal.keyboard.ctrlVPaste = v;
309 },
310
Masaya Suzuki273aa982014-05-31 07:25:55 +0900311 'east-asian-ambiguous-as-two-column': function(v) {
312 lib.wc.regardCjkAmbiguous = v;
313 },
314
Robert Ginda57f03b42012-09-13 11:02:48 -0700315 'enable-8-bit-control': function(v) {
316 terminal.vt.enable8BitControl = !!v;
317 },
rginda30f20f62012-04-05 16:36:19 -0700318
Robert Ginda57f03b42012-09-13 11:02:48 -0700319 'enable-bold': function(v) {
320 terminal.syncBoldSafeState();
321 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400322
Robert Ginda3e278d72014-03-25 13:18:51 -0700323 'enable-bold-as-bright': function(v) {
324 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
325 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
326 },
327
Robert Ginda57f03b42012-09-13 11:02:48 -0700328 'enable-clipboard-write': function(v) {
329 terminal.vt.enableClipboardWrite = !!v;
330 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400331
Robert Ginda3755e752013-05-31 13:34:09 -0700332 'enable-dec12': function(v) {
333 terminal.vt.enableDec12 = !!v;
334 },
335
Robert Ginda57f03b42012-09-13 11:02:48 -0700336 'font-family': function(v) {
337 terminal.syncFontFamily();
338 },
rginda30f20f62012-04-05 16:36:19 -0700339
Robert Ginda57f03b42012-09-13 11:02:48 -0700340 'font-size': function(v) {
341 terminal.setFontSize(v);
342 },
rginda9875d902012-08-20 16:21:57 -0700343
Robert Ginda57f03b42012-09-13 11:02:48 -0700344 'font-smoothing': function(v) {
345 terminal.syncFontFamily();
346 },
rgindade84e382012-04-20 15:39:31 -0700347
Robert Ginda57f03b42012-09-13 11:02:48 -0700348 'foreground-color': function(v) {
349 terminal.setForegroundColor(v);
350 },
rginda30f20f62012-04-05 16:36:19 -0700351
Robert Ginda57f03b42012-09-13 11:02:48 -0700352 'home-keys-scroll': function(v) {
353 terminal.keyboard.homeKeysScroll = v;
354 },
rginda4bba5e12012-06-20 16:15:30 -0700355
Robert Ginda57f03b42012-09-13 11:02:48 -0700356 'max-string-sequence': function(v) {
357 terminal.vt.maxStringSequence = v;
358 },
rginda11057d52012-04-25 12:29:56 -0700359
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700360 'media-keys-are-fkeys': function(v) {
361 terminal.keyboard.mediaKeysAreFKeys = v;
362 },
363
Robert Ginda57f03b42012-09-13 11:02:48 -0700364 'meta-sends-escape': function(v) {
365 terminal.keyboard.metaSendsEscape = v;
366 },
rginda30f20f62012-04-05 16:36:19 -0700367
Robert Ginda57f03b42012-09-13 11:02:48 -0700368 'mouse-paste-button': function(v) {
369 terminal.syncMousePasteButton();
370 },
rgindaa8ba17d2012-08-15 14:41:10 -0700371
Robert Gindae76aa9f2014-03-14 12:29:12 -0700372 'page-keys-scroll': function(v) {
373 terminal.keyboard.pageKeysScroll = v;
374 },
375
Robert Ginda40932892012-12-10 17:26:40 -0800376 'pass-alt-number': function(v) {
377 if (v == null) {
378 var osx = window.navigator.userAgent.match(/Mac OS X/);
379
380 // Let Alt-1..9 pass to the browser (to control tab switching) on
381 // non-OS X systems, or if hterm is not opened in an app window.
382 v = (!osx && hterm.windowType != 'popup');
383 }
384
385 terminal.passAltNumber = v;
386 },
387
388 'pass-ctrl-number': function(v) {
389 if (v == null) {
390 var osx = window.navigator.userAgent.match(/Mac OS X/);
391
392 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
393 // non-OS X systems, or if hterm is not opened in an app window.
394 v = (!osx && hterm.windowType != 'popup');
395 }
396
397 terminal.passCtrlNumber = v;
398 },
399
400 'pass-meta-number': function(v) {
401 if (v == null) {
402 var osx = window.navigator.userAgent.match(/Mac OS X/);
403
404 // Let Meta-1..9 pass to the browser (to control tab switching) on
405 // OS X systems, or if hterm is not opened in an app window.
406 v = (osx && hterm.windowType != 'popup');
407 }
408
409 terminal.passMetaNumber = v;
410 },
411
Marius Schilder77857b32014-05-14 16:21:26 -0700412 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700413 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700414 },
415
Robert Ginda8cb7d902013-06-20 14:37:18 -0700416 'receive-encoding': function(v) {
417 if (!(/^(utf-8|raw)$/).test(v)) {
418 console.warn('Invalid value for "receive-encoding": ' + v);
419 v = 'utf-8';
420 }
421
422 terminal.vt.characterEncoding = v;
423 },
424
Robert Ginda57f03b42012-09-13 11:02:48 -0700425 'scroll-on-keystroke': function(v) {
426 terminal.scrollOnKeystroke_ = v;
427 },
rginda9f5222b2012-03-05 11:53:28 -0800428
Robert Ginda57f03b42012-09-13 11:02:48 -0700429 'scroll-on-output': function(v) {
430 terminal.scrollOnOutput_ = v;
431 },
rginda30f20f62012-04-05 16:36:19 -0700432
Robert Ginda57f03b42012-09-13 11:02:48 -0700433 'scrollbar-visible': function(v) {
434 terminal.setScrollbarVisible(v);
435 },
rginda9f5222b2012-03-05 11:53:28 -0800436
Robert Ginda8cb7d902013-06-20 14:37:18 -0700437 'send-encoding': function(v) {
438 if (!(/^(utf-8|raw)$/).test(v)) {
439 console.warn('Invalid value for "send-encoding": ' + v);
440 v = 'utf-8';
441 }
442
443 terminal.keyboard.characterEncoding = v;
444 },
445
Robert Ginda57f03b42012-09-13 11:02:48 -0700446 'shift-insert-paste': function(v) {
447 terminal.keyboard.shiftInsertPaste = v;
448 },
rginda9f5222b2012-03-05 11:53:28 -0800449
Robert Gindae76aa9f2014-03-14 12:29:12 -0700450 'user-css': function(v) {
451 terminal.scrollPort_.setUserCss(v);
Robert Ginda57f03b42012-09-13 11:02:48 -0700452 }
453 });
rginda30f20f62012-04-05 16:36:19 -0700454
Robert Ginda57f03b42012-09-13 11:02:48 -0700455 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800456 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700457
458 if (opt_callback)
459 opt_callback();
460 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800461};
462
Rob Spies56953412014-04-28 14:09:47 -0700463
464/**
465 * Returns the preferences manager used for configuring this terminal.
466 */
467hterm.Terminal.prototype.getPrefs = function() {
468 return this.prefs_;
469};
470
471
rginda8e92a692012-05-20 19:37:20 -0700472/**
473 * Set the color for the cursor.
474 *
475 * If you want this setting to persist, set it through prefs_, rather than
476 * with this method.
477 */
478hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700479 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700480 this.cursorNode_.style.backgroundColor = color;
481 this.cursorNode_.style.borderColor = color;
482};
483
484/**
485 * Return the current cursor color as a string.
486 */
487hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700488 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700489};
490
491/**
rgindad5613292012-06-19 15:40:37 -0700492 * Enable or disable mouse based text selection in the terminal.
493 */
494hterm.Terminal.prototype.setSelectionEnabled = function(state) {
495 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700496};
497
498/**
rginda8e92a692012-05-20 19:37:20 -0700499 * Set the background color.
500 *
501 * If you want this setting to persist, set it through prefs_, rather than
502 * with this method.
503 */
504hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700505 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700506 this.primaryScreen_.textAttributes.setDefaults(
507 this.foregroundColor_, this.backgroundColor_);
508 this.alternateScreen_.textAttributes.setDefaults(
509 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700510 this.scrollPort_.setBackgroundColor(color);
511};
512
rginda9f5222b2012-03-05 11:53:28 -0800513/**
514 * Return the current terminal background color.
515 *
516 * Intended for use by other classes, so we don't have to expose the entire
517 * prefs_ object.
518 */
519hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700520 return this.backgroundColor_;
521};
522
523/**
524 * Set the foreground color.
525 *
526 * If you want this setting to persist, set it through prefs_, rather than
527 * with this method.
528 */
529hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700530 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700531 this.primaryScreen_.textAttributes.setDefaults(
532 this.foregroundColor_, this.backgroundColor_);
533 this.alternateScreen_.textAttributes.setDefaults(
534 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700535 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800536};
537
538/**
539 * Return the current terminal foreground color.
540 *
541 * Intended for use by other classes, so we don't have to expose the entire
542 * prefs_ object.
543 */
544hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700545 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800546};
547
548/**
rginda87b86462011-12-14 13:48:03 -0800549 * Create a new instance of a terminal command and run it with a given
550 * argument string.
551 *
552 * @param {function} commandClass The constructor for a terminal command.
553 * @param {string} argString The argument string to pass to the command.
554 */
555hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700556 var environment = this.prefs_.get('environment');
557 if (typeof environment != 'object' || environment == null)
558 environment = {};
559
rginda87b86462011-12-14 13:48:03 -0800560 var self = this;
561 this.command = new commandClass(
562 { argString: argString || '',
563 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700564 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800565 onExit: function(code) {
566 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800567 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700568 if (self.prefs_.get('close-on-exit'))
569 window.close();
rginda87b86462011-12-14 13:48:03 -0800570 }
571 });
572
rgindafeaf3142012-01-31 15:14:20 -0800573 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800574 this.command.run();
575};
576
577/**
rgindafeaf3142012-01-31 15:14:20 -0800578 * Returns true if the current screen is the primary screen, false otherwise.
579 */
580hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700581 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800582};
583
584/**
585 * Install the keyboard handler for this terminal.
586 *
587 * This will prevent the browser from seeing any keystrokes sent to the
588 * terminal.
589 */
590hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700591 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800592}
593
594/**
595 * Uninstall the keyboard handler for this terminal.
596 */
597hterm.Terminal.prototype.uninstallKeyboard = function() {
598 this.keyboard.installKeyboard(null);
599}
600
601/**
rginda35c456b2012-02-09 17:29:05 -0800602 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800603 *
604 * Call setFontSize(0) to reset to the default font size.
605 *
606 * This function does not modify the font-size preference.
607 *
608 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800609 */
610hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800611 if (px === 0)
612 px = this.prefs_.get('font-size');
613
rginda35c456b2012-02-09 17:29:05 -0800614 this.scrollPort_.setFontSize(px);
Ricky Liang48f05cb2013-12-31 23:35:29 +0800615 if (this.wcCssRule_) {
616 this.wcCssRule_.style.width = this.scrollPort_.characterSize.width * 2 +
617 'px';
618 }
rginda35c456b2012-02-09 17:29:05 -0800619};
620
621/**
622 * Get the current font size.
623 */
624hterm.Terminal.prototype.getFontSize = function() {
625 return this.scrollPort_.getFontSize();
626};
627
628/**
rginda8e92a692012-05-20 19:37:20 -0700629 * Get the current font family.
630 */
631hterm.Terminal.prototype.getFontFamily = function() {
632 return this.scrollPort_.getFontFamily();
633};
634
635/**
rginda35c456b2012-02-09 17:29:05 -0800636 * Set the CSS "font-family" for this terminal.
637 */
rginda9f5222b2012-03-05 11:53:28 -0800638hterm.Terminal.prototype.syncFontFamily = function() {
639 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
640 this.prefs_.get('font-smoothing'));
641 this.syncBoldSafeState();
642};
643
rginda4bba5e12012-06-20 16:15:30 -0700644/**
645 * Set this.mousePasteButton based on the mouse-paste-button pref,
646 * autodetecting if necessary.
647 */
648hterm.Terminal.prototype.syncMousePasteButton = function() {
649 var button = this.prefs_.get('mouse-paste-button');
650 if (typeof button == 'number') {
651 this.mousePasteButton = button;
652 return;
653 }
654
655 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
656 if (!ary || ary[2] == 'CrOS') {
657 this.mousePasteButton = 2;
658 } else {
659 this.mousePasteButton = 3;
660 }
661};
662
663/**
664 * Enable or disable bold based on the enable-bold pref, autodetecting if
665 * necessary.
666 */
rginda9f5222b2012-03-05 11:53:28 -0800667hterm.Terminal.prototype.syncBoldSafeState = function() {
668 var enableBold = this.prefs_.get('enable-bold');
669 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700670 this.primaryScreen_.textAttributes.enableBold = enableBold;
671 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800672 return;
673 }
674
rgindaf7521392012-02-28 17:20:34 -0800675 var normalSize = this.scrollPort_.measureCharacterSize();
676 var boldSize = this.scrollPort_.measureCharacterSize('bold');
677
678 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800679 if (!isBoldSafe) {
680 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700681 'from normal. Font family is: ' +
682 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800683 }
rginda9f5222b2012-03-05 11:53:28 -0800684
Robert Gindaed016262012-10-26 16:27:09 -0700685 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
686 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800687};
688
689/**
rginda87b86462011-12-14 13:48:03 -0800690 * Return a copy of the current cursor position.
691 *
692 * @return {hterm.RowCol} The RowCol object representing the current position.
693 */
694hterm.Terminal.prototype.saveCursor = function() {
695 return this.screen_.cursorPosition.clone();
696};
697
rgindaa19afe22012-01-25 15:40:22 -0800698hterm.Terminal.prototype.getTextAttributes = function() {
699 return this.screen_.textAttributes;
700};
701
rginda1a09aa02012-06-18 21:11:25 -0700702hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
703 this.screen_.textAttributes = textAttributes;
704};
705
rginda87b86462011-12-14 13:48:03 -0800706/**
rgindaf522ce02012-04-17 17:49:17 -0700707 * Return the current browser zoom factor applied to the terminal.
708 *
709 * @return {number} The current browser zoom factor.
710 */
711hterm.Terminal.prototype.getZoomFactor = function() {
712 return this.scrollPort_.characterSize.zoomFactor;
713};
714
715/**
rginda9846e2f2012-01-27 13:53:33 -0800716 * Change the title of this terminal's window.
717 */
718hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800719 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800720};
721
722/**
rginda87b86462011-12-14 13:48:03 -0800723 * Restore a previously saved cursor position.
724 *
725 * @param {hterm.RowCol} cursor The position to restore.
726 */
727hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700728 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
729 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800730 this.screen_.setCursorPosition(row, column);
731 if (cursor.column > column ||
732 cursor.column == column && cursor.overflow) {
733 this.screen_.cursorPosition.overflow = true;
734 }
rginda87b86462011-12-14 13:48:03 -0800735};
736
737/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400738 * Clear the cursor's overflow flag.
739 */
740hterm.Terminal.prototype.clearCursorOverflow = function() {
741 this.screen_.cursorPosition.overflow = false;
742};
743
744/**
Robert Ginda830583c2013-08-07 13:20:46 -0700745 * Sets the cursor shape
746 */
747hterm.Terminal.prototype.setCursorShape = function(shape) {
748 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800749 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700750}
751
752/**
753 * Get the cursor shape
754 */
755hterm.Terminal.prototype.getCursorShape = function() {
756 return this.cursorShape_;
757}
758
759/**
rginda87b86462011-12-14 13:48:03 -0800760 * Set the width of the terminal, resizing the UI to match.
761 */
762hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800763 if (columnCount == null) {
764 this.div_.style.width = '100%';
765 return;
766 }
767
rginda35c456b2012-02-09 17:29:05 -0800768 this.div_.style.width = this.scrollPort_.characterSize.width *
Robert Ginda97769282013-02-01 15:30:30 -0800769 columnCount + this.scrollPort_.currentScrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400770 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800771 this.scheduleSyncCursorPosition_();
772};
rginda87b86462011-12-14 13:48:03 -0800773
rgindac9bc5502012-01-18 11:48:44 -0800774/**
rginda35c456b2012-02-09 17:29:05 -0800775 * Set the height of the terminal, resizing the UI to match.
776 */
777hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800778 if (rowCount == null) {
779 this.div_.style.height = '100%';
780 return;
781 }
782
rginda35c456b2012-02-09 17:29:05 -0800783 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700784 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800785 this.realizeSize_(this.screenSize.width, rowCount);
786 this.scheduleSyncCursorPosition_();
787};
788
789/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400790 * Deal with terminal size changes.
791 *
792 */
793hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
794 if (columnCount != this.screenSize.width)
795 this.realizeWidth_(columnCount);
796
797 if (rowCount != this.screenSize.height)
798 this.realizeHeight_(rowCount);
799
800 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700801 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400802};
803
804/**
rgindac9bc5502012-01-18 11:48:44 -0800805 * Deal with terminal width changes.
806 *
807 * This function does what needs to be done when the terminal width changes
808 * out from under us. It happens here rather than in onResize_() because this
809 * code may need to run synchronously to handle programmatic changes of
810 * terminal width.
811 *
812 * Relying on the browser to send us an async resize event means we may not be
813 * in the correct state yet when the next escape sequence hits.
814 */
815hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700816 if (columnCount <= 0)
817 throw new Error('Attempt to realize bad width: ' + columnCount);
818
rgindac9bc5502012-01-18 11:48:44 -0800819 var deltaColumns = columnCount - this.screen_.getWidth();
820
rginda87b86462011-12-14 13:48:03 -0800821 this.screenSize.width = columnCount;
822 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800823
824 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400825 if (this.defaultTabStops)
826 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800827 } else {
828 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400829 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800830 break;
831
832 this.tabStops_.pop();
833 }
834 }
835
836 this.screen_.setColumnCount(this.screenSize.width);
837};
838
839/**
840 * Deal with terminal height changes.
841 *
842 * This function does what needs to be done when the terminal height changes
843 * out from under us. It happens here rather than in onResize_() because this
844 * code may need to run synchronously to handle programmatic changes of
845 * terminal height.
846 *
847 * Relying on the browser to send us an async resize event means we may not be
848 * in the correct state yet when the next escape sequence hits.
849 */
850hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700851 if (rowCount <= 0)
852 throw new Error('Attempt to realize bad height: ' + rowCount);
853
rgindac9bc5502012-01-18 11:48:44 -0800854 var deltaRows = rowCount - this.screen_.getHeight();
855
856 this.screenSize.height = rowCount;
857
858 var cursor = this.saveCursor();
859
860 if (deltaRows < 0) {
861 // Screen got smaller.
862 deltaRows *= -1;
863 while (deltaRows) {
864 var lastRow = this.getRowCount() - 1;
865 if (lastRow - this.scrollbackRows_.length == cursor.row)
866 break;
867
868 if (this.getRowText(lastRow))
869 break;
870
871 this.screen_.popRow();
872 deltaRows--;
873 }
874
875 var ary = this.screen_.shiftRows(deltaRows);
876 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
877
878 // We just removed rows from the top of the screen, we need to update
879 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800880 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800881 } else if (deltaRows > 0) {
882 // Screen got larger.
883
884 if (deltaRows <= this.scrollbackRows_.length) {
885 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
886 var rows = this.scrollbackRows_.splice(
887 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
888 this.screen_.unshiftRows(rows);
889 deltaRows -= scrollbackCount;
890 cursor.row += scrollbackCount;
891 }
892
893 if (deltaRows)
894 this.appendRows_(deltaRows);
895 }
896
rginda35c456b2012-02-09 17:29:05 -0800897 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800898 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800899};
900
901/**
902 * Scroll the terminal to the top of the scrollback buffer.
903 */
904hterm.Terminal.prototype.scrollHome = function() {
905 this.scrollPort_.scrollRowToTop(0);
906};
907
908/**
909 * Scroll the terminal to the end.
910 */
911hterm.Terminal.prototype.scrollEnd = function() {
912 this.scrollPort_.scrollRowToBottom(this.getRowCount());
913};
914
915/**
916 * Scroll the terminal one page up (minus one line) relative to the current
917 * position.
918 */
919hterm.Terminal.prototype.scrollPageUp = function() {
920 var i = this.scrollPort_.getTopRowIndex();
921 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
922};
923
924/**
925 * Scroll the terminal one page down (minus one line) relative to the current
926 * position.
927 */
928hterm.Terminal.prototype.scrollPageDown = function() {
929 var i = this.scrollPort_.getTopRowIndex();
930 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800931};
932
rgindac9bc5502012-01-18 11:48:44 -0800933/**
Robert Ginda40932892012-12-10 17:26:40 -0800934 * Clear primary screen, secondary screen, and the scrollback buffer.
935 */
936hterm.Terminal.prototype.wipeContents = function() {
937 this.scrollbackRows_.length = 0;
938 this.scrollPort_.resetCache();
939
940 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
941 var bottom = screen.getHeight();
942 if (bottom > 0) {
943 this.renumberRows_(0, bottom);
944 this.clearHome(screen);
945 }
946 }.bind(this));
947
948 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -0700949 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -0800950};
951
952/**
rgindac9bc5502012-01-18 11:48:44 -0800953 * Full terminal reset.
954 */
rginda87b86462011-12-14 13:48:03 -0800955hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800956 this.clearAllTabStops();
957 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700958
959 this.clearHome(this.primaryScreen_);
960 this.primaryScreen_.textAttributes.reset();
961
962 this.clearHome(this.alternateScreen_);
963 this.alternateScreen_.textAttributes.reset();
964
rgindab8bc8932012-04-27 12:45:03 -0700965 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
966
Robert Ginda92e18102013-03-14 13:56:37 -0700967 this.vt.reset();
968
rgindac9bc5502012-01-18 11:48:44 -0800969 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800970};
971
rgindac9bc5502012-01-18 11:48:44 -0800972/**
973 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700974 *
975 * Perform a soft reset to the default values listed in
976 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800977 */
rginda0f5c0292012-01-13 11:00:13 -0800978hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700979 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800980 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700981
rgindab8bc8932012-04-27 12:45:03 -0700982 // Xterm also resets the color palette on soft reset, even though it doesn't
983 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700984 this.primaryScreen_.textAttributes.resetColorPalette();
985 this.alternateScreen_.textAttributes.resetColorPalette();
986
rgindab8bc8932012-04-27 12:45:03 -0700987 // The xterm man page explicitly says this will happen on soft reset.
988 this.setVTScrollRegion(null, null);
989
990 // Xterm also shows the cursor on soft reset, but does not alter the blink
991 // state.
rgindaa19afe22012-01-25 15:40:22 -0800992 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800993};
994
rgindac9bc5502012-01-18 11:48:44 -0800995/**
996 * Move the cursor forward to the next tab stop, or to the last column
997 * if no more tab stops are set.
998 */
999hterm.Terminal.prototype.forwardTabStop = function() {
1000 var column = this.screen_.cursorPosition.column;
1001
1002 for (var i = 0; i < this.tabStops_.length; i++) {
1003 if (this.tabStops_[i] > column) {
1004 this.setCursorColumn(this.tabStops_[i]);
1005 return;
1006 }
1007 }
1008
David Benjamin66e954d2012-05-05 21:08:12 -04001009 // xterm does not clear the overflow flag on HT or CHT.
1010 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001011 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001012 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001013};
1014
rgindac9bc5502012-01-18 11:48:44 -08001015/**
1016 * Move the cursor backward to the previous tab stop, or to the first column
1017 * if no previous tab stops are set.
1018 */
1019hterm.Terminal.prototype.backwardTabStop = function() {
1020 var column = this.screen_.cursorPosition.column;
1021
1022 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1023 if (this.tabStops_[i] < column) {
1024 this.setCursorColumn(this.tabStops_[i]);
1025 return;
1026 }
1027 }
1028
1029 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001030};
1031
rgindac9bc5502012-01-18 11:48:44 -08001032/**
1033 * Set a tab stop at the given column.
1034 *
1035 * @param {int} column Zero based column.
1036 */
1037hterm.Terminal.prototype.setTabStop = function(column) {
1038 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1039 if (this.tabStops_[i] == column)
1040 return;
1041
1042 if (this.tabStops_[i] < column) {
1043 this.tabStops_.splice(i + 1, 0, column);
1044 return;
1045 }
1046 }
1047
1048 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001049};
1050
rgindac9bc5502012-01-18 11:48:44 -08001051/**
1052 * Clear the tab stop at the current cursor position.
1053 *
1054 * No effect if there is no tab stop at the current cursor position.
1055 */
1056hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1057 var column = this.screen_.cursorPosition.column;
1058
1059 var i = this.tabStops_.indexOf(column);
1060 if (i == -1)
1061 return;
1062
1063 this.tabStops_.splice(i, 1);
1064};
1065
1066/**
1067 * Clear all tab stops.
1068 */
1069hterm.Terminal.prototype.clearAllTabStops = function() {
1070 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001071 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001072};
1073
1074/**
1075 * Set up the default tab stops, starting from a given column.
1076 *
1077 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001078 * from the specified column, or 0 if no column is provided. It also flags
1079 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001080 *
1081 * This does not clear the existing tab stops first, use clearAllTabStops
1082 * for that.
1083 *
1084 * @param {int} opt_start Optional starting zero based starting column, useful
1085 * for filling out missing tab stops when the terminal is resized.
1086 */
1087hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1088 var start = opt_start || 0;
1089 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001090 // Round start up to a default tab stop.
1091 start = start - 1 - ((start - 1) % w) + w;
1092 for (var i = start; i < this.screenSize.width; i += w) {
1093 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001094 }
David Benjamin66e954d2012-05-05 21:08:12 -04001095
1096 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001097};
1098
rginda6d397402012-01-17 10:58:29 -08001099/**
rginda8ba33642011-12-14 12:31:31 -08001100 * Interpret a sequence of characters.
1101 *
1102 * Incomplete escape sequences are buffered until the next call.
1103 *
1104 * @param {string} str Sequence of characters to interpret or pass through.
1105 */
1106hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001107 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001108 this.scheduleSyncCursorPosition_();
1109};
1110
1111/**
1112 * Take over the given DIV for use as the terminal display.
1113 *
1114 * @param {HTMLDivElement} div The div to use as the terminal display.
1115 */
1116hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001117 this.div_ = div;
1118
rginda8ba33642011-12-14 12:31:31 -08001119 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001120 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001121 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1122 this.scrollPort_.setBackgroundPosition(
1123 this.prefs_.get('background-position'));
Robert Gindae76aa9f2014-03-14 12:29:12 -07001124 this.scrollPort_.setUserCss(this.prefs_.get('user-css'));
rginda30f20f62012-04-05 16:36:19 -07001125
rginda0918b652012-04-04 11:26:24 -07001126 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001127
rginda9f5222b2012-03-05 11:53:28 -08001128 this.setFontSize(this.prefs_.get('font-size'));
1129 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001130
David Reveman8f552492012-03-28 12:18:41 -04001131 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1132
rginda8ba33642011-12-14 12:31:31 -08001133 this.document_ = this.scrollPort_.getDocument();
1134
rginda4bba5e12012-06-20 16:15:30 -07001135 this.document_.body.oncontextmenu = function() { return false };
1136
1137 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001138 var screenNode = this.scrollPort_.getScreenNode();
1139 screenNode.addEventListener('mousedown', onMouse);
1140 screenNode.addEventListener('mouseup', onMouse);
1141 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001142 this.scrollPort_.onScrollWheel = onMouse;
1143
Toni Barzic0bfa8922013-11-22 11:18:35 -08001144 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001145 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001146 // Listen for mousedown events on the screenNode as in FF the focus
1147 // events don't bubble.
1148 screenNode.addEventListener('mousedown', function() {
1149 setTimeout(this.onFocusChange_.bind(this, true));
1150 }.bind(this));
1151
Toni Barzic0bfa8922013-11-22 11:18:35 -08001152 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001153 'blur', this.onFocusChange_.bind(this, false));
1154
1155 var style = this.document_.createElement('style');
1156 style.textContent =
1157 ('.cursor-node[focus="false"] {' +
1158 ' box-sizing: border-box;' +
1159 ' background-color: transparent !important;' +
1160 ' border-width: 2px;' +
1161 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001162 '}' +
1163 '.wc-node {' +
1164 ' display: inline-block;' +
1165 ' text-align: center;' +
1166 ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001167 '}');
1168 this.document_.head.appendChild(style);
1169
Ricky Liang48f05cb2013-12-31 23:35:29 +08001170 var styleSheets = this.document_.styleSheets;
1171 var cssRules = styleSheets[styleSheets.length - 1].cssRules;
1172 this.wcCssRule_ = cssRules[cssRules.length - 1];
1173
rginda8ba33642011-12-14 12:31:31 -08001174 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001175 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001176 this.cursorNode_.style.cssText =
1177 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001178 'top: -99px;' +
1179 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001180 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1181 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
Rob Spies06533ba2014-04-24 11:20:37 -07001182 '-webkit-transition: opacity, background-color 100ms linear;' +
1183 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001184
rginda8e92a692012-05-20 19:37:20 -07001185 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001186 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1187 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001188
rginda8ba33642011-12-14 12:31:31 -08001189 this.document_.body.appendChild(this.cursorNode_);
1190
rgindad5613292012-06-19 15:40:37 -07001191 // When 'enableMouseDragScroll' is off we reposition this element directly
1192 // under the mouse cursor after a click. This makes Chrome associate
1193 // subsequent mousemove events with the scroll-blocker. Since the
1194 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1195 // events do not cause the scrollport to scroll.
1196 //
1197 // It's a hack, but it's the cleanest way I could find.
1198 this.scrollBlockerNode_ = this.document_.createElement('div');
1199 this.scrollBlockerNode_.style.cssText =
1200 ('position: absolute;' +
1201 'top: -99px;' +
1202 'display: block;' +
1203 'width: 10px;' +
1204 'height: 10px;');
1205 this.document_.body.appendChild(this.scrollBlockerNode_);
1206
1207 var onMouse = this.onMouse_.bind(this);
1208 this.scrollPort_.onScrollWheel = onMouse;
1209 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1210 ].forEach(function(event) {
1211 this.scrollBlockerNode_.addEventListener(event, onMouse);
1212 this.cursorNode_.addEventListener(event, onMouse);
1213 this.document_.addEventListener(event, onMouse);
1214 }.bind(this));
1215
1216 this.cursorNode_.addEventListener('mousedown', function() {
1217 setTimeout(this.focus.bind(this));
1218 }.bind(this));
1219
rginda8ba33642011-12-14 12:31:31 -08001220 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001221
rginda87b86462011-12-14 13:48:03 -08001222 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001223 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001224};
1225
rginda0918b652012-04-04 11:26:24 -07001226/**
1227 * Return the HTML document that contains the terminal DOM nodes.
1228 */
rginda87b86462011-12-14 13:48:03 -08001229hterm.Terminal.prototype.getDocument = function() {
1230 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001231};
1232
1233/**
rginda0918b652012-04-04 11:26:24 -07001234 * Focus the terminal.
1235 */
1236hterm.Terminal.prototype.focus = function() {
1237 this.scrollPort_.focus();
1238};
1239
1240/**
rginda8ba33642011-12-14 12:31:31 -08001241 * Return the HTML Element for a given row index.
1242 *
1243 * This is a method from the RowProvider interface. The ScrollPort uses
1244 * it to fetch rows on demand as they are scrolled into view.
1245 *
1246 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1247 * pairs to conserve memory.
1248 *
1249 * @param {integer} index The zero-based row index, measured relative to the
1250 * start of the scrollback buffer. On-screen rows will always have the
1251 * largest indicies.
1252 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1253 */
1254hterm.Terminal.prototype.getRowNode = function(index) {
1255 if (index < this.scrollbackRows_.length)
1256 return this.scrollbackRows_[index];
1257
1258 var screenIndex = index - this.scrollbackRows_.length;
1259 return this.screen_.rowsArray[screenIndex];
1260};
1261
1262/**
1263 * Return the text content for a given range of rows.
1264 *
1265 * This is a method from the RowProvider interface. The ScrollPort uses
1266 * it to fetch text content on demand when the user attempts to copy their
1267 * selection to the clipboard.
1268 *
1269 * @param {integer} start The zero-based row index to start from, measured
1270 * relative to the start of the scrollback buffer. On-screen rows will
1271 * always have the largest indicies.
1272 * @param {integer} end The zero-based row index to end on, measured
1273 * relative to the start of the scrollback buffer.
1274 * @return {string} A single string containing the text value of the range of
1275 * rows. Lines will be newline delimited, with no trailing newline.
1276 */
1277hterm.Terminal.prototype.getRowsText = function(start, end) {
1278 var ary = [];
1279 for (var i = start; i < end; i++) {
1280 var node = this.getRowNode(i);
1281 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001282 if (i < end - 1 && !node.getAttribute('line-overflow'))
1283 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001284 }
1285
rgindaa09e7332012-08-17 12:49:51 -07001286 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001287};
1288
1289/**
1290 * Return the text content for a given row.
1291 *
1292 * This is a method from the RowProvider interface. The ScrollPort uses
1293 * it to fetch text content on demand when the user attempts to copy their
1294 * selection to the clipboard.
1295 *
1296 * @param {integer} index The zero-based row index to return, measured
1297 * relative to the start of the scrollback buffer. On-screen rows will
1298 * always have the largest indicies.
1299 * @return {string} A string containing the text value of the selected row.
1300 */
1301hterm.Terminal.prototype.getRowText = function(index) {
1302 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001303 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001304};
1305
1306/**
1307 * Return the total number of rows in the addressable screen and in the
1308 * scrollback buffer of this terminal.
1309 *
1310 * This is a method from the RowProvider interface. The ScrollPort uses
1311 * it to compute the size of the scrollbar.
1312 *
1313 * @return {integer} The number of rows in this terminal.
1314 */
1315hterm.Terminal.prototype.getRowCount = function() {
1316 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1317};
1318
1319/**
1320 * Create DOM nodes for new rows and append them to the end of the terminal.
1321 *
1322 * This is the only correct way to add a new DOM node for a row. Notice that
1323 * the new row is appended to the bottom of the list of rows, and does not
1324 * require renumbering (of the rowIndex property) of previous rows.
1325 *
1326 * If you think you want a new blank row somewhere in the middle of the
1327 * terminal, look into moveRows_().
1328 *
1329 * This method does not pay attention to vtScrollTop/Bottom, since you should
1330 * be using moveRows() in cases where they would matter.
1331 *
1332 * The cursor will be positioned at column 0 of the first inserted line.
1333 */
1334hterm.Terminal.prototype.appendRows_ = function(count) {
1335 var cursorRow = this.screen_.rowsArray.length;
1336 var offset = this.scrollbackRows_.length + cursorRow;
1337 for (var i = 0; i < count; i++) {
1338 var row = this.document_.createElement('x-row');
1339 row.appendChild(this.document_.createTextNode(''));
1340 row.rowIndex = offset + i;
1341 this.screen_.pushRow(row);
1342 }
1343
1344 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1345 if (extraRows > 0) {
1346 var ary = this.screen_.shiftRows(extraRows);
1347 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001348 if (this.scrollPort_.isScrolledEnd)
1349 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001350 }
1351
1352 if (cursorRow >= this.screen_.rowsArray.length)
1353 cursorRow = this.screen_.rowsArray.length - 1;
1354
rginda87b86462011-12-14 13:48:03 -08001355 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001356};
1357
1358/**
1359 * Relocate rows from one part of the addressable screen to another.
1360 *
1361 * This is used to recycle rows during VT scrolls (those which are driven
1362 * by VT commands, rather than by the user manipulating the scrollbar.)
1363 *
1364 * In this case, the blank lines scrolled into the scroll region are made of
1365 * the nodes we scrolled off. These have their rowIndex properties carefully
1366 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001367 */
1368hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1369 var ary = this.screen_.removeRows(fromIndex, count);
1370 this.screen_.insertRows(toIndex, ary);
1371
1372 var start, end;
1373 if (fromIndex < toIndex) {
1374 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001375 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001376 } else {
1377 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001378 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001379 }
1380
1381 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001382 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001383};
1384
1385/**
1386 * Renumber the rowIndex property of the given range of rows.
1387 *
1388 * The start and end indicies are relative to the screen, not the scrollback.
1389 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001390 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001391 * no need to renumber scrollback rows.
1392 */
Robert Ginda40932892012-12-10 17:26:40 -08001393hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1394 var screen = opt_screen || this.screen_;
1395
rginda8ba33642011-12-14 12:31:31 -08001396 var offset = this.scrollbackRows_.length;
1397 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001398 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001399 }
1400};
1401
1402/**
1403 * Print a string to the terminal.
1404 *
1405 * This respects the current insert and wraparound modes. It will add new lines
1406 * to the end of the terminal, scrolling off the top into the scrollback buffer
1407 * if necessary.
1408 *
1409 * The string is *not* parsed for escape codes. Use the interpret() method if
1410 * that's what you're after.
1411 *
1412 * @param{string} str The string to print.
1413 */
1414hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001415 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001416
Ricky Liang48f05cb2013-12-31 23:35:29 +08001417 var strWidth = lib.wc.strWidth(str);
1418
1419 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001420 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1421 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001422 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001423 }
rgindaa19afe22012-01-25 15:40:22 -08001424
Ricky Liang48f05cb2013-12-31 23:35:29 +08001425 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001426 var didOverflow = false;
1427 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001428
rgindaa9abdd82012-08-06 18:05:09 -07001429 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1430 didOverflow = true;
1431 count = this.screenSize.width - this.screen_.cursorPosition.column;
1432 }
rgindaa19afe22012-01-25 15:40:22 -08001433
rgindaa9abdd82012-08-06 18:05:09 -07001434 if (didOverflow && !this.options_.wraparound) {
1435 // If the string overflowed the line but wraparound is off, then the
1436 // last printed character should be the last of the string.
1437 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001438 substr = lib.wc.substr(str, startOffset, count - 1) +
1439 lib.wc.substr(str, strWidth - 1);
1440 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001441 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001442 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001443 }
rgindaa19afe22012-01-25 15:40:22 -08001444
Ricky Liang48f05cb2013-12-31 23:35:29 +08001445 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1446 for (var i = 0; i < tokens.length; i++) {
1447 if (tokens[i].wcNode)
1448 this.screen_.textAttributes.wcNode = true;
1449
1450 if (this.options_.insertMode) {
1451 this.screen_.insertString(tokens[i].str);
1452 } else {
1453 this.screen_.overwriteString(tokens[i].str);
1454 }
1455 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001456 }
1457
1458 this.screen_.maybeClipCurrentRow();
1459 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001460 }
rginda8ba33642011-12-14 12:31:31 -08001461
1462 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001463
rginda9f5222b2012-03-05 11:53:28 -08001464 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001465 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001466};
1467
1468/**
rginda87b86462011-12-14 13:48:03 -08001469 * Set the VT scroll region.
1470 *
rginda87b86462011-12-14 13:48:03 -08001471 * This also resets the cursor position to the absolute (0, 0) position, since
1472 * that's what xterm appears to do.
1473 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001474 * Setting the scroll region to the full height of the terminal will clear
1475 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1476 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1477 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1478 * continue to work as most users would expect.
1479 *
rginda87b86462011-12-14 13:48:03 -08001480 * @param {integer} scrollTop The zero-based top of the scroll region.
1481 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1482 * inclusive.
1483 */
1484hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001485 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001486 this.vtScrollTop_ = null;
1487 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001488 } else {
1489 this.vtScrollTop_ = scrollTop;
1490 this.vtScrollBottom_ = scrollBottom;
1491 }
rginda87b86462011-12-14 13:48:03 -08001492};
1493
1494/**
rginda8ba33642011-12-14 12:31:31 -08001495 * Return the top row index according to the VT.
1496 *
1497 * This will return 0 unless the terminal has been told to restrict scrolling
1498 * to some lower row. It is used for some VT cursor positioning and scrolling
1499 * commands.
1500 *
1501 * @return {integer} The topmost row in the terminal's scroll region.
1502 */
1503hterm.Terminal.prototype.getVTScrollTop = function() {
1504 if (this.vtScrollTop_ != null)
1505 return this.vtScrollTop_;
1506
1507 return 0;
rginda87b86462011-12-14 13:48:03 -08001508};
rginda8ba33642011-12-14 12:31:31 -08001509
1510/**
1511 * Return the bottom row index according to the VT.
1512 *
1513 * This will return the height of the terminal unless the it has been told to
1514 * restrict scrolling to some higher row. It is used for some VT cursor
1515 * positioning and scrolling commands.
1516 *
1517 * @return {integer} The bottommost row in the terminal's scroll region.
1518 */
1519hterm.Terminal.prototype.getVTScrollBottom = function() {
1520 if (this.vtScrollBottom_ != null)
1521 return this.vtScrollBottom_;
1522
rginda87b86462011-12-14 13:48:03 -08001523 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001524}
1525
1526/**
1527 * Process a '\n' character.
1528 *
1529 * If the cursor is on the final row of the terminal this will append a new
1530 * blank row to the screen and scroll the topmost row into the scrollback
1531 * buffer.
1532 *
1533 * Otherwise, this moves the cursor to column zero of the next row.
1534 */
1535hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001536 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1537 this.screen_.rowsArray.length - 1);
1538
1539 if (this.vtScrollBottom_ != null) {
1540 // A VT Scroll region is active, we never append new rows.
1541 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1542 // We're at the end of the VT Scroll Region, perform a VT scroll.
1543 this.vtScrollUp(1);
1544 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1545 } else if (cursorAtEndOfScreen) {
1546 // We're at the end of the screen, the only thing to do is put the
1547 // cursor to column 0.
1548 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1549 } else {
1550 // Anywhere else, advance the cursor row, and reset the column.
1551 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1552 }
1553 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001554 // We're at the end of the screen. Append a new row to the terminal,
1555 // shifting the top row into the scrollback.
1556 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001557 } else {
rginda87b86462011-12-14 13:48:03 -08001558 // Anywhere else in the screen just moves the cursor.
1559 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001560 }
1561};
1562
1563/**
1564 * Like newLine(), except maintain the cursor column.
1565 */
1566hterm.Terminal.prototype.lineFeed = function() {
1567 var column = this.screen_.cursorPosition.column;
1568 this.newLine();
1569 this.setCursorColumn(column);
1570};
1571
1572/**
rginda87b86462011-12-14 13:48:03 -08001573 * If autoCarriageReturn is set then newLine(), else lineFeed().
1574 */
1575hterm.Terminal.prototype.formFeed = function() {
1576 if (this.options_.autoCarriageReturn) {
1577 this.newLine();
1578 } else {
1579 this.lineFeed();
1580 }
1581};
1582
1583/**
1584 * Move the cursor up one row, possibly inserting a blank line.
1585 *
1586 * The cursor column is not changed.
1587 */
1588hterm.Terminal.prototype.reverseLineFeed = function() {
1589 var scrollTop = this.getVTScrollTop();
1590 var currentRow = this.screen_.cursorPosition.row;
1591
1592 if (currentRow == scrollTop) {
1593 this.insertLines(1);
1594 } else {
1595 this.setAbsoluteCursorRow(currentRow - 1);
1596 }
1597};
1598
1599/**
rginda8ba33642011-12-14 12:31:31 -08001600 * Replace all characters to the left of the current cursor with the space
1601 * character.
1602 *
1603 * TODO(rginda): This should probably *remove* the characters (not just replace
1604 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001605 * position.
rginda8ba33642011-12-14 12:31:31 -08001606 */
1607hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001608 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001609 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001610 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001611 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001612};
1613
1614/**
David Benjamin684a9b72012-05-01 17:19:58 -04001615 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001616 *
1617 * The cursor position is unchanged.
1618 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001619 * If the current background color is not the default background color this
1620 * will insert spaces rather than delete. This is unfortunate because the
1621 * trailing space will affect text selection, but it's difficult to come up
1622 * with a way to style empty space that wouldn't trip up the hterm.Screen
1623 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001624 *
1625 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1626 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1627 * crbug.com/232390 for details.
rginda8ba33642011-12-14 12:31:31 -08001628 */
1629hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001630 if (this.screen_.cursorPosition.overflow)
1631 return;
1632
Robert Ginda7fd57082012-09-25 14:41:47 -07001633 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1634 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001635
1636 if (this.screen_.textAttributes.background ===
1637 this.screen_.textAttributes.DEFAULT_COLOR) {
1638 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001639 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001640 this.screen_.cursorPosition.column + count) {
1641 this.screen_.deleteChars(count);
1642 this.clearCursorOverflow();
1643 return;
1644 }
1645 }
1646
rginda87b86462011-12-14 13:48:03 -08001647 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001648 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001649 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001650 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001651};
1652
1653/**
1654 * Erase the current line.
1655 *
1656 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001657 */
1658hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001659 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001660 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001661 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001662 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001663};
1664
1665/**
David Benjamina08d78f2012-05-05 00:28:49 -04001666 * Erase all characters from the start of the screen to the current cursor
1667 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001668 *
1669 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001670 */
1671hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001672 var cursor = this.saveCursor();
1673
1674 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001675
David Benjamina08d78f2012-05-05 00:28:49 -04001676 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001677 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001678 this.screen_.clearCursorRow();
1679 }
1680
rginda87b86462011-12-14 13:48:03 -08001681 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001682 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001683};
1684
1685/**
1686 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001687 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001688 *
1689 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001690 */
1691hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001692 var cursor = this.saveCursor();
1693
1694 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001695
David Benjamina08d78f2012-05-05 00:28:49 -04001696 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001697 for (var i = cursor.row + 1; i <= bottom; i++) {
1698 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001699 this.screen_.clearCursorRow();
1700 }
1701
rginda87b86462011-12-14 13:48:03 -08001702 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001703 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001704};
1705
1706/**
1707 * Fill the terminal with a given character.
1708 *
1709 * This methods does not respect the VT scroll region.
1710 *
1711 * @param {string} ch The character to use for the fill.
1712 */
1713hterm.Terminal.prototype.fill = function(ch) {
1714 var cursor = this.saveCursor();
1715
1716 this.setAbsoluteCursorPosition(0, 0);
1717 for (var row = 0; row < this.screenSize.height; row++) {
1718 for (var col = 0; col < this.screenSize.width; col++) {
1719 this.setAbsoluteCursorPosition(row, col);
1720 this.screen_.overwriteString(ch);
1721 }
1722 }
1723
1724 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001725};
1726
1727/**
rginda9ea433c2012-03-16 11:57:00 -07001728 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001729 *
rginda9ea433c2012-03-16 11:57:00 -07001730 * This does not respect the scroll region.
1731 *
1732 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1733 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001734 */
rginda9ea433c2012-03-16 11:57:00 -07001735hterm.Terminal.prototype.clearHome = function(opt_screen) {
1736 var screen = opt_screen || this.screen_;
1737 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001738
rginda11057d52012-04-25 12:29:56 -07001739 if (bottom == 0) {
1740 // Empty screen, nothing to do.
1741 return;
1742 }
1743
rgindae4d29232012-01-19 10:47:13 -08001744 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001745 screen.setCursorPosition(i, 0);
1746 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001747 }
1748
rginda9ea433c2012-03-16 11:57:00 -07001749 screen.setCursorPosition(0, 0);
1750};
1751
1752/**
1753 * Erase the entire display without changing the cursor position.
1754 *
1755 * The cursor position is unchanged. This does not respect the scroll
1756 * region.
1757 *
1758 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1759 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001760 */
1761hterm.Terminal.prototype.clear = function(opt_screen) {
1762 var screen = opt_screen || this.screen_;
1763 var cursor = screen.cursorPosition.clone();
1764 this.clearHome(screen);
1765 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001766};
1767
1768/**
1769 * VT command to insert lines at the current cursor row.
1770 *
1771 * This respects the current scroll region. Rows pushed off the bottom are
1772 * lost (they won't show up in the scrollback buffer).
1773 *
rginda8ba33642011-12-14 12:31:31 -08001774 * @param {integer} count The number of lines to insert.
1775 */
1776hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001777 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001778
1779 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001780 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001781
Robert Ginda579186b2012-09-26 11:40:04 -07001782 // The moveCount is the number of rows we need to relocate to make room for
1783 // the new row(s). The count is the distance to move them.
1784 var moveCount = bottom - cursorRow - count + 1;
1785 if (moveCount)
1786 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001787
Robert Ginda579186b2012-09-26 11:40:04 -07001788 for (var i = count - 1; i >= 0; i--) {
1789 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001790 this.screen_.clearCursorRow();
1791 }
rginda8ba33642011-12-14 12:31:31 -08001792};
1793
1794/**
1795 * VT command to delete lines at the current cursor row.
1796 *
1797 * New rows are added to the bottom of scroll region to take their place. New
1798 * rows are strictly there to take up space and have no content or style.
1799 */
1800hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001801 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001802
rginda87b86462011-12-14 13:48:03 -08001803 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001804 var bottom = this.getVTScrollBottom();
1805
rginda87b86462011-12-14 13:48:03 -08001806 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001807 count = Math.min(count, maxCount);
1808
rginda87b86462011-12-14 13:48:03 -08001809 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001810 if (count != maxCount)
1811 this.moveRows_(top, count, moveStart);
1812
1813 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001814 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001815 this.screen_.clearCursorRow();
1816 }
1817
rginda87b86462011-12-14 13:48:03 -08001818 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001819 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001820};
1821
1822/**
1823 * Inserts the given number of spaces at the current cursor position.
1824 *
rginda87b86462011-12-14 13:48:03 -08001825 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001826 */
1827hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001828 var cursor = this.saveCursor();
1829
rgindacbbd7482012-06-13 15:06:16 -07001830 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001831 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001832 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001833
1834 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001835 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001836};
1837
1838/**
1839 * Forward-delete the specified number of characters starting at the cursor
1840 * position.
1841 *
1842 * @param {integer} count The number of characters to delete.
1843 */
1844hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001845 var deleted = this.screen_.deleteChars(count);
1846 if (deleted && !this.screen_.textAttributes.isDefault()) {
1847 var cursor = this.saveCursor();
1848 this.setCursorColumn(this.screenSize.width - deleted);
1849 this.screen_.insertString(lib.f.getWhitespace(deleted));
1850 this.restoreCursor(cursor);
1851 }
1852
David Benjamin54e8bf62012-06-01 22:31:40 -04001853 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001854};
1855
1856/**
1857 * Shift rows in the scroll region upwards by a given number of lines.
1858 *
1859 * New rows are inserted at the bottom of the scroll region to fill the
1860 * vacated rows. The new rows not filled out with the current text attributes.
1861 *
1862 * This function does not affect the scrollback rows at all. Rows shifted
1863 * off the top are lost.
1864 *
rginda87b86462011-12-14 13:48:03 -08001865 * The cursor position is not altered.
1866 *
rginda8ba33642011-12-14 12:31:31 -08001867 * @param {integer} count The number of rows to scroll.
1868 */
1869hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001870 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001871
rginda87b86462011-12-14 13:48:03 -08001872 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001873 this.deleteLines(count);
1874
rginda87b86462011-12-14 13:48:03 -08001875 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001876};
1877
1878/**
1879 * Shift rows below the cursor down by a given number of lines.
1880 *
1881 * This function respects the current scroll region.
1882 *
1883 * New rows are inserted at the top of the scroll region to fill the
1884 * vacated rows. The new rows not filled out with the current text attributes.
1885 *
1886 * This function does not affect the scrollback rows at all. Rows shifted
1887 * off the bottom are lost.
1888 *
1889 * @param {integer} count The number of rows to scroll.
1890 */
1891hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001892 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001893
rginda87b86462011-12-14 13:48:03 -08001894 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001895 this.insertLines(opt_count);
1896
rginda87b86462011-12-14 13:48:03 -08001897 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001898};
1899
rginda87b86462011-12-14 13:48:03 -08001900
rginda8ba33642011-12-14 12:31:31 -08001901/**
1902 * Set the cursor position.
1903 *
1904 * The cursor row is relative to the scroll region if the terminal has
1905 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1906 *
1907 * @param {integer} row The new zero-based cursor row.
1908 * @param {integer} row The new zero-based cursor column.
1909 */
1910hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1911 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001912 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001913 } else {
rginda87b86462011-12-14 13:48:03 -08001914 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001915 }
rginda87b86462011-12-14 13:48:03 -08001916};
rginda8ba33642011-12-14 12:31:31 -08001917
rginda87b86462011-12-14 13:48:03 -08001918hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1919 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001920 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1921 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001922 this.screen_.setCursorPosition(row, column);
1923};
1924
1925hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001926 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1927 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001928 this.screen_.setCursorPosition(row, column);
1929};
1930
1931/**
1932 * Set the cursor column.
1933 *
1934 * @param {integer} column The new zero-based cursor column.
1935 */
1936hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001937 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001938};
1939
1940/**
1941 * Return the cursor column.
1942 *
1943 * @return {integer} The zero-based cursor column.
1944 */
1945hterm.Terminal.prototype.getCursorColumn = function() {
1946 return this.screen_.cursorPosition.column;
1947};
1948
1949/**
1950 * Set the cursor row.
1951 *
1952 * The cursor row is relative to the scroll region if the terminal has
1953 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1954 *
1955 * @param {integer} row The new cursor row.
1956 */
rginda87b86462011-12-14 13:48:03 -08001957hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1958 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001959};
1960
1961/**
1962 * Return the cursor row.
1963 *
1964 * @return {integer} The zero-based cursor row.
1965 */
1966hterm.Terminal.prototype.getCursorRow = function(row) {
1967 return this.screen_.cursorPosition.row;
1968};
1969
1970/**
1971 * Request that the ScrollPort redraw itself soon.
1972 *
1973 * The redraw will happen asynchronously, soon after the call stack winds down.
1974 * Multiple calls will be coalesced into a single redraw.
1975 */
1976hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001977 if (this.timeouts_.redraw)
1978 return;
rginda8ba33642011-12-14 12:31:31 -08001979
1980 var self = this;
rginda87b86462011-12-14 13:48:03 -08001981 this.timeouts_.redraw = setTimeout(function() {
1982 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001983 self.scrollPort_.redraw_();
1984 }, 0);
1985};
1986
1987/**
1988 * Request that the ScrollPort be scrolled to the bottom.
1989 *
1990 * The scroll will happen asynchronously, soon after the call stack winds down.
1991 * Multiple calls will be coalesced into a single scroll.
1992 *
1993 * This affects the scrollbar position of the ScrollPort, and has nothing to
1994 * do with the VT scroll commands.
1995 */
1996hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1997 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001998 return;
rginda8ba33642011-12-14 12:31:31 -08001999
2000 var self = this;
2001 this.timeouts_.scrollDown = setTimeout(function() {
2002 delete self.timeouts_.scrollDown;
2003 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2004 }, 10);
2005};
2006
2007/**
2008 * Move the cursor up a specified number of rows.
2009 *
2010 * @param {integer} count The number of rows to move the cursor.
2011 */
2012hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002013 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002014};
2015
2016/**
2017 * Move the cursor down a specified number of rows.
2018 *
2019 * @param {integer} count The number of rows to move the cursor.
2020 */
2021hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002022 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002023 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2024 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2025 this.screenSize.height - 1);
2026
rgindacbbd7482012-06-13 15:06:16 -07002027 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002028 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002029 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002030};
2031
2032/**
2033 * Move the cursor left a specified number of columns.
2034 *
2035 * @param {integer} count The number of columns to move the cursor.
2036 */
2037hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002038 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002039};
2040
2041/**
2042 * Move the cursor right a specified number of columns.
2043 *
2044 * @param {integer} count The number of columns to move the cursor.
2045 */
2046hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002047 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07002048 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002049 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002050 this.setCursorColumn(column);
2051};
2052
2053/**
2054 * Reverse the foreground and background colors of the terminal.
2055 *
2056 * This only affects text that was drawn with no attributes.
2057 *
2058 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2059 * been drawn with attributes that happen to coincide with the default
2060 * 'no-attribute' colors. My guess is probably not.
2061 */
2062hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002063 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002064 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002065 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2066 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002067 } else {
rginda9f5222b2012-03-05 11:53:28 -08002068 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2069 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002070 }
2071};
2072
2073/**
rginda87b86462011-12-14 13:48:03 -08002074 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002075 *
2076 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002077 */
2078hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002079 this.cursorNode_.style.backgroundColor =
2080 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002081
2082 var self = this;
2083 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002084 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002085 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002086
Michael Kelly485ecd12014-06-09 11:41:56 -04002087 // bellSquelchTimeout_ affects both audio and notification bells.
2088 if (this.bellSquelchTimeout_)
2089 return;
2090
Robert Ginda92e18102013-03-14 13:56:37 -07002091 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002092 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002093 this.bellSequelchTimeout_ = setTimeout(function() {
2094 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002095 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002096 } else {
2097 delete this.bellSquelchTimeout_;
2098 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002099
2100 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
2101 var n = new Notification(
2102 lib.f.replaceVars(hterm.desktopNotificationTitle,
2103 {'title': this.document_.title || 'hterm'}));
2104 this.bellNotificationList_.push(n);
2105 // TODO: Should we try to raise the window here?
2106 n.onclick = function() { self.closeBellNotifications_(); };
2107 }
rginda87b86462011-12-14 13:48:03 -08002108};
2109
2110/**
rginda8ba33642011-12-14 12:31:31 -08002111 * Set the origin mode bit.
2112 *
2113 * If origin mode is on, certain VT cursor and scrolling commands measure their
2114 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2115 * to the top of the addressable screen.
2116 *
2117 * Defaults to off.
2118 *
2119 * @param {boolean} state True to set origin mode, false to unset.
2120 */
2121hterm.Terminal.prototype.setOriginMode = function(state) {
2122 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002123 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002124};
2125
2126/**
2127 * Set the insert mode bit.
2128 *
2129 * If insert mode is on, existing text beyond the cursor position will be
2130 * shifted right to make room for new text. Otherwise, new text overwrites
2131 * any existing text.
2132 *
2133 * Defaults to off.
2134 *
2135 * @param {boolean} state True to set insert mode, false to unset.
2136 */
2137hterm.Terminal.prototype.setInsertMode = function(state) {
2138 this.options_.insertMode = state;
2139};
2140
2141/**
rginda87b86462011-12-14 13:48:03 -08002142 * Set the auto carriage return bit.
2143 *
2144 * If auto carriage return is on then a formfeed character is interpreted
2145 * as a newline, otherwise it's the same as a linefeed. The difference boils
2146 * down to whether or not the cursor column is reset.
2147 */
2148hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2149 this.options_.autoCarriageReturn = state;
2150};
2151
2152/**
rginda8ba33642011-12-14 12:31:31 -08002153 * Set the wraparound mode bit.
2154 *
2155 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2156 * to the start of the following row. Otherwise, the cursor is clamped to the
2157 * end of the screen and attempts to write past it are ignored.
2158 *
2159 * Defaults to on.
2160 *
2161 * @param {boolean} state True to set wraparound mode, false to unset.
2162 */
2163hterm.Terminal.prototype.setWraparound = function(state) {
2164 this.options_.wraparound = state;
2165};
2166
2167/**
2168 * Set the reverse-wraparound mode bit.
2169 *
2170 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2171 * to the end of the previous row. Otherwise, the cursor is clamped to column
2172 * 0.
2173 *
2174 * Defaults to off.
2175 *
2176 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2177 */
2178hterm.Terminal.prototype.setReverseWraparound = function(state) {
2179 this.options_.reverseWraparound = state;
2180};
2181
2182/**
2183 * Selects between the primary and alternate screens.
2184 *
2185 * If alternate mode is on, the alternate screen is active. Otherwise the
2186 * primary screen is active.
2187 *
2188 * Swapping screens has no effect on the scrollback buffer.
2189 *
2190 * Each screen maintains its own cursor position.
2191 *
2192 * Defaults to off.
2193 *
2194 * @param {boolean} state True to set alternate mode, false to unset.
2195 */
2196hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002197 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002198 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2199
rginda35c456b2012-02-09 17:29:05 -08002200 if (this.screen_.rowsArray.length &&
2201 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2202 // If the screen changed sizes while we were away, our rowIndexes may
2203 // be incorrect.
2204 var offset = this.scrollbackRows_.length;
2205 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002206 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002207 ary[i].rowIndex = offset + i;
2208 }
2209 }
rginda8ba33642011-12-14 12:31:31 -08002210
rginda35c456b2012-02-09 17:29:05 -08002211 this.realizeWidth_(this.screenSize.width);
2212 this.realizeHeight_(this.screenSize.height);
2213 this.scrollPort_.syncScrollHeight();
2214 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002215
rginda6d397402012-01-17 10:58:29 -08002216 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002217 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002218};
2219
2220/**
2221 * Set the cursor-blink mode bit.
2222 *
2223 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2224 * a visible cursor does not blink.
2225 *
2226 * You should make sure to turn blinking off if you're going to dispose of a
2227 * terminal, otherwise you'll leak a timeout.
2228 *
2229 * Defaults to on.
2230 *
2231 * @param {boolean} state True to set cursor-blink mode, false to unset.
2232 */
2233hterm.Terminal.prototype.setCursorBlink = function(state) {
2234 this.options_.cursorBlink = state;
2235
2236 if (!state && this.timeouts_.cursorBlink) {
2237 clearTimeout(this.timeouts_.cursorBlink);
2238 delete this.timeouts_.cursorBlink;
2239 }
2240
2241 if (this.options_.cursorVisible)
2242 this.setCursorVisible(true);
2243};
2244
2245/**
2246 * Set the cursor-visible mode bit.
2247 *
2248 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2249 *
2250 * Defaults to on.
2251 *
2252 * @param {boolean} state True to set cursor-visible mode, false to unset.
2253 */
2254hterm.Terminal.prototype.setCursorVisible = function(state) {
2255 this.options_.cursorVisible = state;
2256
2257 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002258 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002259 return;
2260 }
2261
rginda87b86462011-12-14 13:48:03 -08002262 this.syncCursorPosition_();
2263
2264 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002265
2266 if (this.options_.cursorBlink) {
2267 if (this.timeouts_.cursorBlink)
2268 return;
2269
2270 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2271 500);
2272 } else {
2273 if (this.timeouts_.cursorBlink) {
2274 clearTimeout(this.timeouts_.cursorBlink);
2275 delete this.timeouts_.cursorBlink;
2276 }
2277 }
2278};
2279
2280/**
rginda87b86462011-12-14 13:48:03 -08002281 * Synchronizes the visible cursor and document selection with the current
2282 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002283 */
2284hterm.Terminal.prototype.syncCursorPosition_ = function() {
2285 var topRowIndex = this.scrollPort_.getTopRowIndex();
2286 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2287 var cursorRowIndex = this.scrollbackRows_.length +
2288 this.screen_.cursorPosition.row;
2289
2290 if (cursorRowIndex > bottomRowIndex) {
2291 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002292 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002293 return;
2294 }
2295
2296 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002297 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2298 'px';
2299 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2300 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002301
2302 this.cursorNode_.setAttribute('title',
2303 '(' + this.screen_.cursorPosition.row +
2304 ', ' + this.screen_.cursorPosition.column +
2305 ')');
2306
2307 // Update the caret for a11y purposes.
2308 var selection = this.document_.getSelection();
2309 if (selection && selection.isCollapsed)
2310 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002311};
2312
Robert Gindafb1be6a2013-12-11 11:56:22 -08002313/**
2314 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2315 * and character cell dimensions.
2316 */
Robert Ginda830583c2013-08-07 13:20:46 -07002317hterm.Terminal.prototype.restyleCursor_ = function() {
2318 var shape = this.cursorShape_;
2319
2320 if (this.cursorNode_.getAttribute('focus') == 'false') {
2321 // Always show a block cursor when unfocused.
2322 shape = hterm.Terminal.cursorShape.BLOCK;
2323 }
2324
2325 var style = this.cursorNode_.style;
2326
Robert Gindafb1be6a2013-12-11 11:56:22 -08002327 style.width = this.scrollPort_.characterSize.width + 'px';
2328
Robert Ginda830583c2013-08-07 13:20:46 -07002329 switch (shape) {
2330 case hterm.Terminal.cursorShape.BEAM:
2331 style.height = this.scrollPort_.characterSize.height + 'px';
2332 style.backgroundColor = 'transparent';
2333 style.borderBottomStyle = null;
2334 style.borderLeftStyle = 'solid';
2335 break;
2336
2337 case hterm.Terminal.cursorShape.UNDERLINE:
2338 style.height = this.scrollPort_.characterSize.baseline + 'px';
2339 style.backgroundColor = 'transparent';
2340 style.borderBottomStyle = 'solid';
2341 // correct the size to put it exactly at the baseline
2342 style.borderLeftStyle = null;
2343 break;
2344
2345 default:
2346 style.height = this.scrollPort_.characterSize.height + 'px';
2347 style.backgroundColor = this.cursorColor_;
2348 style.borderBottomStyle = null;
2349 style.borderLeftStyle = null;
2350 break;
2351 }
2352};
2353
rginda8ba33642011-12-14 12:31:31 -08002354/**
2355 * Synchronizes the visible cursor with the current cursor coordinates.
2356 *
2357 * The sync will happen asynchronously, soon after the call stack winds down.
2358 * Multiple calls will be coalesced into a single sync.
2359 */
2360hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2361 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002362 return;
rginda8ba33642011-12-14 12:31:31 -08002363
2364 var self = this;
2365 this.timeouts_.syncCursor = setTimeout(function() {
2366 self.syncCursorPosition_();
2367 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002368 }, 0);
2369};
2370
rgindacc2996c2012-02-24 14:59:31 -08002371/**
rgindaf522ce02012-04-17 17:49:17 -07002372 * Show or hide the zoom warning.
2373 *
2374 * The zoom warning is a message warning the user that their browser zoom must
2375 * be set to 100% in order for hterm to function properly.
2376 *
2377 * @param {boolean} state True to show the message, false to hide it.
2378 */
2379hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2380 if (!this.zoomWarningNode_) {
2381 if (!state)
2382 return;
2383
2384 this.zoomWarningNode_ = this.document_.createElement('div');
2385 this.zoomWarningNode_.style.cssText = (
2386 'color: black;' +
2387 'background-color: #ff2222;' +
2388 'font-size: large;' +
2389 'border-radius: 8px;' +
2390 'opacity: 0.75;' +
2391 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2392 'top: 0.5em;' +
2393 'right: 1.2em;' +
2394 'position: absolute;' +
2395 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002396 '-webkit-user-select: none;' +
2397 '-moz-text-size-adjust: none;' +
2398 '-moz-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002399 }
2400
Robert Gindab4839c22013-02-28 16:52:10 -08002401 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2402 hterm.zoomWarningMessage,
2403 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2404
rgindaf522ce02012-04-17 17:49:17 -07002405 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2406
2407 if (state) {
2408 if (!this.zoomWarningNode_.parentNode)
2409 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2410 } else if (this.zoomWarningNode_.parentNode) {
2411 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2412 }
2413};
2414
2415/**
rgindacc2996c2012-02-24 14:59:31 -08002416 * Show the terminal overlay for a given amount of time.
2417 *
2418 * The terminal overlay appears in inverse video in a large font, centered
2419 * over the terminal. You should probably keep the overlay message brief,
2420 * since it's in a large font and you probably aren't going to check the size
2421 * of the terminal first.
2422 *
2423 * @param {string} msg The text (not HTML) message to display in the overlay.
2424 * @param {number} opt_timeout The amount of time to wait before fading out
2425 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2426 * stay up forever (or until the next overlay).
2427 */
2428hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002429 if (!this.overlayNode_) {
2430 if (!this.div_)
2431 return;
2432
2433 this.overlayNode_ = this.document_.createElement('div');
2434 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002435 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002436 'font-size: xx-large;' +
2437 'opacity: 0.75;' +
2438 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2439 'position: absolute;' +
2440 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002441 '-webkit-transition: opacity 180ms ease-in;' +
2442 '-moz-user-select: none;' +
2443 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002444
2445 this.overlayNode_.addEventListener('mousedown', function(e) {
2446 e.preventDefault();
2447 e.stopPropagation();
2448 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002449 }
2450
rginda9f5222b2012-03-05 11:53:28 -08002451 this.overlayNode_.style.color = this.prefs_.get('background-color');
2452 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2453 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2454
rgindaf0090c92012-02-10 14:58:52 -08002455 this.overlayNode_.textContent = msg;
2456 this.overlayNode_.style.opacity = '0.75';
2457
2458 if (!this.overlayNode_.parentNode)
2459 this.div_.appendChild(this.overlayNode_);
2460
Robert Ginda97769282013-02-01 15:30:30 -08002461 var divSize = hterm.getClientSize(this.div_);
2462 var overlaySize = hterm.getClientSize(this.overlayNode_);
2463
2464 this.overlayNode_.style.top = (divSize.height - overlaySize.height) / 2;
2465 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
2466 this.scrollPort_.currentScrollbarWidthPx) / 2;
rgindaf0090c92012-02-10 14:58:52 -08002467
2468 var self = this;
2469
2470 if (this.overlayTimeout_)
2471 clearTimeout(this.overlayTimeout_);
2472
rgindacc2996c2012-02-24 14:59:31 -08002473 if (opt_timeout === null)
2474 return;
2475
rgindaf0090c92012-02-10 14:58:52 -08002476 this.overlayTimeout_ = setTimeout(function() {
2477 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002478 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002479 if (self.overlayNode_.parentNode)
2480 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002481 self.overlayTimeout_ = null;
2482 self.overlayNode_.style.opacity = '0.75';
2483 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002484 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002485};
2486
rginda4bba5e12012-06-20 16:15:30 -07002487/**
2488 * Paste from the system clipboard to the terminal.
2489 */
2490hterm.Terminal.prototype.paste = function() {
2491 hterm.pasteFromClipboard(this.document_);
2492};
2493
2494/**
2495 * Copy a string to the system clipboard.
2496 *
2497 * Note: If there is a selected range in the terminal, it'll be cleared.
2498 */
2499hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002500 if (this.prefs_.get('enable-clipboard-notice'))
2501 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2502
rgindaa09e7332012-08-17 12:49:51 -07002503 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002504 copySource.textContent = str;
2505 copySource.style.cssText = (
2506 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002507 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002508 'position: absolute;' +
2509 'top: -99px');
2510
2511 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002512
rginda4bba5e12012-06-20 16:15:30 -07002513 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002514 var anchorNode = selection.anchorNode;
2515 var anchorOffset = selection.anchorOffset;
2516 var focusNode = selection.focusNode;
2517 var focusOffset = selection.focusOffset;
2518
rginda4bba5e12012-06-20 16:15:30 -07002519 selection.selectAllChildren(copySource);
2520
rgindaa09e7332012-08-17 12:49:51 -07002521 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002522
Rob Spies56953412014-04-28 14:09:47 -07002523 // IE doesn't support selection.extend. This means that the selection
2524 // won't return on IE.
Rob Spies0bec09b2014-06-06 15:58:09 -07002525 if (this.clearSelectionAfterCopy && selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002526 selection.collapse(anchorNode, anchorOffset);
2527 selection.extend(focusNode, focusOffset);
2528 }
rgindafaa74742012-08-21 13:34:03 -07002529
rginda4bba5e12012-06-20 16:15:30 -07002530 copySource.parentNode.removeChild(copySource);
2531};
2532
rgindaa09e7332012-08-17 12:49:51 -07002533hterm.Terminal.prototype.getSelectionText = function() {
2534 var selection = this.scrollPort_.selection;
2535 selection.sync();
2536
2537 if (selection.isCollapsed)
2538 return null;
2539
2540
2541 // Start offset measures from the beginning of the line.
2542 var startOffset = selection.startOffset;
2543 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002544
Robert Gindafdbb3f22012-09-06 20:23:06 -07002545 if (node.nodeName != 'X-ROW') {
2546 // If the selection doesn't start on an x-row node, then it must be
2547 // somewhere inside the x-row. Add any characters from previous siblings
2548 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002549
2550 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2551 // If node is the text node in a styled span, move up to the span node.
2552 node = node.parentNode;
2553 }
2554
Robert Gindafdbb3f22012-09-06 20:23:06 -07002555 while (node.previousSibling) {
2556 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002557 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002558 }
rgindaa09e7332012-08-17 12:49:51 -07002559 }
2560
2561 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002562 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2563 selection.endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002564 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002565
Robert Gindafdbb3f22012-09-06 20:23:06 -07002566 if (node.nodeName != 'X-ROW') {
2567 // If the selection doesn't end on an x-row node, then it must be
2568 // somewhere inside the x-row. Add any characters from following siblings
2569 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002570
2571 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2572 // If node is the text node in a styled span, move up to the span node.
2573 node = node.parentNode;
2574 }
2575
Robert Gindafdbb3f22012-09-06 20:23:06 -07002576 while (node.nextSibling) {
2577 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002578 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002579 }
rgindaa09e7332012-08-17 12:49:51 -07002580 }
2581
2582 var rv = this.getRowsText(selection.startRow.rowIndex,
2583 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002584 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002585};
2586
rginda4bba5e12012-06-20 16:15:30 -07002587/**
2588 * Copy the current selection to the system clipboard, then clear it after a
2589 * short delay.
2590 */
2591hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002592 var text = this.getSelectionText();
2593 if (text != null)
2594 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002595};
2596
rgindaf0090c92012-02-10 14:58:52 -08002597hterm.Terminal.prototype.overlaySize = function() {
2598 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2599};
2600
rginda87b86462011-12-14 13:48:03 -08002601/**
2602 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2603 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002604 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002605 */
2606hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002607 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002608 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2609
Robert Ginda8cb7d902013-06-20 14:37:18 -07002610 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002611};
2612
2613/**
rgindad5613292012-06-19 15:40:37 -07002614 * Add the terminalRow and terminalColumn properties to mouse events and
2615 * then forward on to onMouse().
2616 *
2617 * The terminalRow and terminalColumn properties contain the (row, column)
2618 * coordinates for the mouse event.
2619 */
2620hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002621 if (e.processedByTerminalHandler_) {
2622 // We register our event handlers on the document, as well as the cursor
2623 // and the scroll blocker. Mouse events that occur on the cursor or
2624 // scroll blocker will also appear on the document, but we don't want to
2625 // process them twice.
2626 //
2627 // We can't just prevent bubbling because that has other side effects, so
2628 // we decorate the event object with this property instead.
2629 return;
2630 }
2631
2632 e.processedByTerminalHandler_ = true;
2633
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002634 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2635 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002636 return;
2637 }
2638
rgindad5613292012-06-19 15:40:37 -07002639 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2640 this.scrollPort_.characterSize.height) + 1;
2641 e.terminalColumn = parseInt(e.clientX /
2642 this.scrollPort_.characterSize.width) + 1;
2643
Robert Ginda928cf632014-03-05 15:07:41 -08002644 if (e.type == 'mousedown') {
2645 if (e.altKey || this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED) {
2646 // If VT mouse reporting is disabled, or has been defeated with
2647 // alt-mousedown, then the mouse will act on the local selection.
2648 this.reportMouseEvents_ = false;
2649 this.setSelectionEnabled(true);
2650 } else {
2651 // Otherwise we defer ownership of the mouse to the VT.
2652 this.reportMouseEvents_ = true;
Robert Ginda3ae37822014-05-15 13:05:35 -07002653 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08002654 this.setSelectionEnabled(false);
2655 e.preventDefault();
2656 }
2657 }
2658
2659 if (!this.reportMouseEvents_) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002660 if (e.type == 'dblclick') {
2661 this.screen_.expandSelection(this.document_.getSelection());
2662 hterm.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07002663 }
2664
Robert Ginda928cf632014-03-05 15:07:41 -08002665 if (e.type == 'mousedown' && e.which == this.mousePasteButton)
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002666 this.paste();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002667
2668 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2669 !this.document_.getSelection().isCollapsed) {
2670 hterm.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002671 }
2672
2673 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
2674 this.scrollBlockerNode_.engaged) {
2675 // Disengage the scroll-blocker after one of these events.
2676 this.scrollBlockerNode_.engaged = false;
2677 this.scrollBlockerNode_.style.top = '-99px';
2678 }
2679
Robert Ginda928cf632014-03-05 15:07:41 -08002680 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002681 if (!this.scrollBlockerNode_.engaged) {
2682 if (e.type == 'mousedown') {
2683 // Move the scroll-blocker into place if we want to keep the scrollport
2684 // from scrolling.
2685 this.scrollBlockerNode_.engaged = true;
2686 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2687 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2688 } else if (e.type == 'mousemove') {
2689 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
2690 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07002691 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002692 e.preventDefault();
2693 }
2694 }
Robert Ginda928cf632014-03-05 15:07:41 -08002695
2696 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002697 }
2698
Robert Ginda928cf632014-03-05 15:07:41 -08002699 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
2700 // Restore this on mouseup in case it was temporarily defeated with a
2701 // alt-mousedown. Only do this when the selection is empty so that
2702 // we don't immediately kill the users selection.
2703 this.reportMouseEvents_ = (this.vt.mouseReport !=
2704 this.vt.MOUSE_REPORT_DISABLED);
2705 }
rgindad5613292012-06-19 15:40:37 -07002706};
2707
2708/**
2709 * Clients should override this if they care to know about mouse events.
2710 *
2711 * The event parameter will be a normal DOM mouse click event with additional
2712 * 'terminalRow' and 'terminalColumn' properties.
2713 */
2714hterm.Terminal.prototype.onMouse = function(e) { };
2715
2716/**
rginda8e92a692012-05-20 19:37:20 -07002717 * React when focus changes.
2718 */
Rob Spies06533ba2014-04-24 11:20:37 -07002719hterm.Terminal.prototype.onFocusChange_ = function(focused) {
2720 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07002721 this.restyleCursor_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002722 if (focused === true)
2723 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07002724};
2725
2726/**
rginda8ba33642011-12-14 12:31:31 -08002727 * React when the ScrollPort is scrolled.
2728 */
2729hterm.Terminal.prototype.onScroll_ = function() {
2730 this.scheduleSyncCursorPosition_();
2731};
2732
2733/**
rginda9846e2f2012-01-27 13:53:33 -08002734 * React when text is pasted into the scrollPort.
2735 */
2736hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Ginda8cb7d902013-06-20 14:37:18 -07002737 this.onVTKeystroke(e.text.replace(/\n/mg, '\r'));
rginda9846e2f2012-01-27 13:53:33 -08002738};
2739
2740/**
rgindaa09e7332012-08-17 12:49:51 -07002741 * React when the user tries to copy from the scrollPort.
2742 */
2743hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07002744 if (!this.useDefaultWindowCopy) {
2745 e.preventDefault();
2746 setTimeout(this.copySelectionToClipboard.bind(this), 0);
2747 }
rgindaa09e7332012-08-17 12:49:51 -07002748};
2749
2750/**
rginda8ba33642011-12-14 12:31:31 -08002751 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002752 *
2753 * Note: This function should not directly contain code that alters the internal
2754 * state of the terminal. That kind of code belongs in realizeWidth or
2755 * realizeHeight, so that it can be executed synchronously in the case of a
2756 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002757 */
2758hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08002759 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002760 this.scrollPort_.characterSize.width);
Robert Ginda19f61292014-03-04 14:07:57 -08002761 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
rginda35c456b2012-02-09 17:29:05 -08002762 this.scrollPort_.characterSize.height);
2763
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002764 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002765 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002766 // gets removed from the document or during the initial load, and we can't
2767 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002768 return;
2769 }
2770
rgindaa8ba17d2012-08-15 14:41:10 -07002771 var isNewSize = (columnCount != this.screenSize.width ||
2772 rowCount != this.screenSize.height);
2773
2774 // We do this even if the size didn't change, just to be sure everything is
2775 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002776 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002777 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002778
2779 if (isNewSize)
2780 this.overlaySize();
2781
Robert Gindafb1be6a2013-12-11 11:56:22 -08002782 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07002783 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002784};
2785
2786/**
2787 * Service the cursor blink timeout.
2788 */
2789hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Ginda830583c2013-08-07 13:20:46 -07002790 if (this.cursorNode_.getAttribute('focus') == 'false' ||
2791 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08002792 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002793 } else {
rginda87b86462011-12-14 13:48:03 -08002794 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002795 }
2796};
David Reveman8f552492012-03-28 12:18:41 -04002797
2798/**
2799 * Set the scrollbar-visible mode bit.
2800 *
2801 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2802 * Otherwise it will not.
2803 *
2804 * Defaults to on.
2805 *
2806 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2807 */
2808hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2809 this.scrollPort_.setScrollbarVisible(state);
2810};
Michael Kelly485ecd12014-06-09 11:41:56 -04002811
2812/**
2813 * Close all web notifications created by terminal bells.
2814 */
2815hterm.Terminal.prototype.closeBellNotifications_ = function() {
2816 this.bellNotificationList_.forEach(function(n) {
2817 n.close();
2818 });
2819 this.bellNotificationList_.length = 0;
2820};