blob: 1d8a01c3e12c60c57a48c26798d67780e22f3b78 [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
Masaya Suzuki273aa982014-05-31 07:25:55 +09007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource', 'lib.wc',
Robert Ginda57f03b42012-09-13 11:02:48 -07008 'hterm.Keyboard', 'hterm.Options', 'hterm.PreferenceManager',
Ricky Liang48f05cb2013-12-31 23:35:29 +08009 'hterm.Screen', 'hterm.ScrollPort', 'hterm.Size',
10 'hterm.TextAttributes', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070053 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080054
rginda87b86462011-12-14 13:48:03 -080055 // The div that contains this terminal.
56 this.div_ = null;
57
rgindac9bc5502012-01-18 11:48:44 -080058 // The document that contains the scrollPort. Defaulted to the global
59 // document here so that the terminal is functional even if it hasn't been
60 // inserted into a document yet, but re-set in decorate().
61 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080062
rginda8ba33642011-12-14 12:31:31 -080063 // The rows that have scrolled off screen and are no longer addressable.
64 this.scrollbackRows_ = [];
65
rgindac9bc5502012-01-18 11:48:44 -080066 // Saved tab stops.
67 this.tabStops_ = [];
68
David Benjamin66e954d2012-05-05 21:08:12 -040069 // Keep track of whether default tab stops have been erased; after a TBC
70 // clears all tab stops, defaults aren't restored on resize until a reset.
71 this.defaultTabStops = true;
72
rginda8ba33642011-12-14 12:31:31 -080073 // The VT's notion of the top and bottom rows. Used during some VT
74 // cursor positioning and scrolling commands.
75 this.vtScrollTop_ = null;
76 this.vtScrollBottom_ = null;
77
78 // The DIV element for the visible cursor.
79 this.cursorNode_ = null;
80
Robert Ginda830583c2013-08-07 13:20:46 -070081 // The current cursor shape of the terminal.
82 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
83
84 // The current color of the cursor.
85 this.cursorColor_ = null;
86
rginda9f5222b2012-03-05 11:53:28 -080087 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070088 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070089 this.backgroundColor_ = null;
90 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070091 this.scrollOnOutput_ = null;
92 this.scrollOnKeystroke_ = null;
rginda9f5222b2012-03-05 11:53:28 -080093
Robert Ginda928cf632014-03-05 15:07:41 -080094 // True if we should send mouse events to the vt, false if we want them
95 // to manage the local text selection.
96 this.reportMouseEvents_ = false;
97
rgindaf0090c92012-02-10 14:58:52 -080098 // Terminal bell sound.
99 this.bellAudio_ = this.document_.createElement('audio');
rgindaf0090c92012-02-10 14:58:52 -0800100 this.bellAudio_.setAttribute('preload', 'auto');
101
Michael Kelly485ecd12014-06-09 11:41:56 -0400102 // All terminal bell notifications that have been generated (not necessarily
103 // shown).
104 this.bellNotificationList_ = [];
105
106 // Whether we have permission to display notifications.
107 this.desktopNotificationBell_ = false;
108 //this.notificationPermission_ = (Notification &&
109 //Notification.permission === 'granted');
110
rginda6d397402012-01-17 10:58:29 -0800111 // Cursor position and attributes saved with DECSC.
112 this.savedOptions_ = {};
113
rginda8ba33642011-12-14 12:31:31 -0800114 // The current mode bits for the terminal.
115 this.options_ = new hterm.Options();
116
117 // Timeouts we might need to clear.
118 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800119
120 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800121 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800122
rgindafeaf3142012-01-31 15:14:20 -0800123 // The keyboard hander.
124 this.keyboard = new hterm.Keyboard(this);
125
rginda87b86462011-12-14 13:48:03 -0800126 // General IO interface that can be given to third parties without exposing
127 // the entire terminal object.
128 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800129
rgindad5613292012-06-19 15:40:37 -0700130 // True if mouse-click-drag should scroll the terminal.
131 this.enableMouseDragScroll = true;
132
Robert Ginda57f03b42012-09-13 11:02:48 -0700133 this.copyOnSelect = null;
rginda4bba5e12012-06-20 16:15:30 -0700134 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700135
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400136 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800137 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700138
139 this.setProfile(opt_profileId || 'default',
140 function() { this.onTerminalReady() }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800141};
142
143/**
Robert Ginda830583c2013-08-07 13:20:46 -0700144 * Possible cursor shapes.
145 */
146hterm.Terminal.cursorShape = {
147 BLOCK: 'BLOCK',
148 BEAM: 'BEAM',
149 UNDERLINE: 'UNDERLINE'
150};
151
152/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700153 * Clients should override this to be notified when the terminal is ready
154 * for use.
155 *
156 * The terminal initialization is asynchronous, and shouldn't be used before
157 * this method is called.
158 */
159hterm.Terminal.prototype.onTerminalReady = function() { };
160
161/**
rginda35c456b2012-02-09 17:29:05 -0800162 * Default tab with of 8 to match xterm.
163 */
164hterm.Terminal.prototype.tabWidth = 8;
165
166/**
rginda9f5222b2012-03-05 11:53:28 -0800167 * Select a preference profile.
168 *
169 * This will load the terminal preferences for the given profile name and
170 * associate subsequent preference changes with the new preference profile.
171 *
172 * @param {string} newName The name of the preference profile. Forward slash
173 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700174 * @param {function} opt_callback Optional callback to invoke when the profile
175 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800176 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700177hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
178 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800179
Robert Ginda57f03b42012-09-13 11:02:48 -0700180 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800181
Robert Ginda57f03b42012-09-13 11:02:48 -0700182 if (this.prefs_)
183 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800184
Robert Ginda57f03b42012-09-13 11:02:48 -0700185 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
186 this.prefs_.addObservers(null, {
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700187 'alt-backspace-is-meta-backspace': function(v) {
188 terminal.keyboard.altBackspaceIsMetaBackspace = v;
189 },
190
Robert Ginda57f03b42012-09-13 11:02:48 -0700191 'alt-is-meta': function(v) {
192 terminal.keyboard.altIsMeta = v;
193 },
194
195 'alt-sends-what': function(v) {
196 if (!/^(escape|8-bit|browser-key)$/.test(v))
197 v = 'escape';
198
199 terminal.keyboard.altSendsWhat = v;
200 },
201
202 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800203 var ary = v.match(/^lib-resource:(\S+)/);
204 if (ary) {
205 terminal.bellAudio_.setAttribute('src',
206 lib.resource.getDataUrl(ary[1]));
207 } else {
208 terminal.bellAudio_.setAttribute('src', v);
209 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700210 },
211
Michael Kelly485ecd12014-06-09 11:41:56 -0400212 'desktop-notification-bell': function(v) {
213 if (v && Notification) {
214 // We cannot rely on having notification permission by default.
215 if (Notification.permission !== 'granted') {
216 Notification.requestPermission(function(permission) {
217 terminal.desktopNotificationBell_ = (permission === 'granted');
218 });
219 } else {
220 terminal.desktopNotificationBell_ = true;
221 }
222 } else {
223 terminal.desktopNotificationBell_ = false;
224 }
225 },
226
Robert Ginda57f03b42012-09-13 11:02:48 -0700227 'background-color': function(v) {
228 terminal.setBackgroundColor(v);
229 },
230
231 'background-image': function(v) {
232 terminal.scrollPort_.setBackgroundImage(v);
233 },
234
235 'background-size': function(v) {
236 terminal.scrollPort_.setBackgroundSize(v);
237 },
238
239 'background-position': function(v) {
240 terminal.scrollPort_.setBackgroundPosition(v);
241 },
242
243 'backspace-sends-backspace': function(v) {
244 terminal.keyboard.backspaceSendsBackspace = v;
245 },
246
247 'cursor-blink': function(v) {
248 terminal.setCursorBlink(!!v);
249 },
250
251 'cursor-color': function(v) {
252 terminal.setCursorColor(v);
253 },
254
255 'color-palette-overrides': function(v) {
256 if (!(v == null || v instanceof Object || v instanceof Array)) {
257 console.warn('Preference color-palette-overrides is not an array or ' +
258 'object: ' + v);
259 return;
rginda9f5222b2012-03-05 11:53:28 -0800260 }
rginda9f5222b2012-03-05 11:53:28 -0800261
Robert Ginda57f03b42012-09-13 11:02:48 -0700262 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700263
Robert Ginda57f03b42012-09-13 11:02:48 -0700264 if (v) {
265 for (var key in v) {
266 var i = parseInt(key);
267 if (isNaN(i) || i < 0 || i > 255) {
268 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
269 continue;
270 }
271
272 if (v[i]) {
273 var rgb = lib.colors.normalizeCSS(v[i]);
274 if (rgb)
275 lib.colors.colorPalette[i] = rgb;
276 }
277 }
rginda30f20f62012-04-05 16:36:19 -0700278 }
rginda30f20f62012-04-05 16:36:19 -0700279
Robert Ginda57f03b42012-09-13 11:02:48 -0700280 terminal.primaryScreen_.textAttributes.resetColorPalette()
281 terminal.alternateScreen_.textAttributes.resetColorPalette();
282 },
rginda30f20f62012-04-05 16:36:19 -0700283
Robert Ginda57f03b42012-09-13 11:02:48 -0700284 'copy-on-select': function(v) {
285 terminal.copyOnSelect = !!v;
286 },
rginda9f5222b2012-03-05 11:53:28 -0800287
Robert Ginda7e5e9522014-03-14 12:23:58 -0700288 'ctrl-plus-minus-zero-zoom': function(v) {
289 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
290 },
291
Robert Gindafb5a3f92014-05-13 14:12:00 -0700292 'ctrl-c-copy': function(v) {
293 terminal.keyboard.ctrlCCopy = v;
294 },
295
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100296 'ctrl-v-paste': function(v) {
297 terminal.keyboard.ctrlVPaste = v;
298 },
299
Masaya Suzuki273aa982014-05-31 07:25:55 +0900300 'east-asian-ambiguous-as-two-column': function(v) {
301 lib.wc.regardCjkAmbiguous = v;
302 },
303
Robert Ginda57f03b42012-09-13 11:02:48 -0700304 'enable-8-bit-control': function(v) {
305 terminal.vt.enable8BitControl = !!v;
306 },
rginda30f20f62012-04-05 16:36:19 -0700307
Robert Ginda57f03b42012-09-13 11:02:48 -0700308 'enable-bold': function(v) {
309 terminal.syncBoldSafeState();
310 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400311
Robert Ginda3e278d72014-03-25 13:18:51 -0700312 'enable-bold-as-bright': function(v) {
313 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
314 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
315 },
316
Robert Ginda57f03b42012-09-13 11:02:48 -0700317 'enable-clipboard-write': function(v) {
318 terminal.vt.enableClipboardWrite = !!v;
319 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400320
Robert Ginda3755e752013-05-31 13:34:09 -0700321 'enable-dec12': function(v) {
322 terminal.vt.enableDec12 = !!v;
323 },
324
Robert Ginda57f03b42012-09-13 11:02:48 -0700325 'font-family': function(v) {
326 terminal.syncFontFamily();
327 },
rginda30f20f62012-04-05 16:36:19 -0700328
Robert Ginda57f03b42012-09-13 11:02:48 -0700329 'font-size': function(v) {
330 terminal.setFontSize(v);
331 },
rginda9875d902012-08-20 16:21:57 -0700332
Robert Ginda57f03b42012-09-13 11:02:48 -0700333 'font-smoothing': function(v) {
334 terminal.syncFontFamily();
335 },
rgindade84e382012-04-20 15:39:31 -0700336
Robert Ginda57f03b42012-09-13 11:02:48 -0700337 'foreground-color': function(v) {
338 terminal.setForegroundColor(v);
339 },
rginda30f20f62012-04-05 16:36:19 -0700340
Robert Ginda57f03b42012-09-13 11:02:48 -0700341 'home-keys-scroll': function(v) {
342 terminal.keyboard.homeKeysScroll = v;
343 },
rginda4bba5e12012-06-20 16:15:30 -0700344
Robert Ginda57f03b42012-09-13 11:02:48 -0700345 'max-string-sequence': function(v) {
346 terminal.vt.maxStringSequence = v;
347 },
rginda11057d52012-04-25 12:29:56 -0700348
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700349 'media-keys-are-fkeys': function(v) {
350 terminal.keyboard.mediaKeysAreFKeys = v;
351 },
352
Robert Ginda57f03b42012-09-13 11:02:48 -0700353 'meta-sends-escape': function(v) {
354 terminal.keyboard.metaSendsEscape = v;
355 },
rginda30f20f62012-04-05 16:36:19 -0700356
Robert Ginda57f03b42012-09-13 11:02:48 -0700357 'mouse-paste-button': function(v) {
358 terminal.syncMousePasteButton();
359 },
rgindaa8ba17d2012-08-15 14:41:10 -0700360
Robert Gindae76aa9f2014-03-14 12:29:12 -0700361 'page-keys-scroll': function(v) {
362 terminal.keyboard.pageKeysScroll = v;
363 },
364
Robert Ginda40932892012-12-10 17:26:40 -0800365 'pass-alt-number': function(v) {
366 if (v == null) {
367 var osx = window.navigator.userAgent.match(/Mac OS X/);
368
369 // Let Alt-1..9 pass to the browser (to control tab switching) on
370 // non-OS X systems, or if hterm is not opened in an app window.
371 v = (!osx && hterm.windowType != 'popup');
372 }
373
374 terminal.passAltNumber = v;
375 },
376
377 'pass-ctrl-number': function(v) {
378 if (v == null) {
379 var osx = window.navigator.userAgent.match(/Mac OS X/);
380
381 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
382 // non-OS X systems, or if hterm is not opened in an app window.
383 v = (!osx && hterm.windowType != 'popup');
384 }
385
386 terminal.passCtrlNumber = v;
387 },
388
389 'pass-meta-number': function(v) {
390 if (v == null) {
391 var osx = window.navigator.userAgent.match(/Mac OS X/);
392
393 // Let Meta-1..9 pass to the browser (to control tab switching) on
394 // OS X systems, or if hterm is not opened in an app window.
395 v = (osx && hterm.windowType != 'popup');
396 }
397
398 terminal.passMetaNumber = v;
399 },
400
Marius Schilder77857b32014-05-14 16:21:26 -0700401 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700402 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700403 },
404
Robert Ginda8cb7d902013-06-20 14:37:18 -0700405 'receive-encoding': function(v) {
406 if (!(/^(utf-8|raw)$/).test(v)) {
407 console.warn('Invalid value for "receive-encoding": ' + v);
408 v = 'utf-8';
409 }
410
411 terminal.vt.characterEncoding = v;
412 },
413
Robert Ginda57f03b42012-09-13 11:02:48 -0700414 'scroll-on-keystroke': function(v) {
415 terminal.scrollOnKeystroke_ = v;
416 },
rginda9f5222b2012-03-05 11:53:28 -0800417
Robert Ginda57f03b42012-09-13 11:02:48 -0700418 'scroll-on-output': function(v) {
419 terminal.scrollOnOutput_ = v;
420 },
rginda30f20f62012-04-05 16:36:19 -0700421
Robert Ginda57f03b42012-09-13 11:02:48 -0700422 'scrollbar-visible': function(v) {
423 terminal.setScrollbarVisible(v);
424 },
rginda9f5222b2012-03-05 11:53:28 -0800425
Robert Ginda8cb7d902013-06-20 14:37:18 -0700426 'send-encoding': function(v) {
427 if (!(/^(utf-8|raw)$/).test(v)) {
428 console.warn('Invalid value for "send-encoding": ' + v);
429 v = 'utf-8';
430 }
431
432 terminal.keyboard.characterEncoding = v;
433 },
434
Robert Ginda57f03b42012-09-13 11:02:48 -0700435 'shift-insert-paste': function(v) {
436 terminal.keyboard.shiftInsertPaste = v;
437 },
rginda9f5222b2012-03-05 11:53:28 -0800438
Robert Gindae76aa9f2014-03-14 12:29:12 -0700439 'user-css': function(v) {
440 terminal.scrollPort_.setUserCss(v);
Robert Ginda57f03b42012-09-13 11:02:48 -0700441 }
442 });
rginda30f20f62012-04-05 16:36:19 -0700443
Robert Ginda57f03b42012-09-13 11:02:48 -0700444 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800445 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700446
447 if (opt_callback)
448 opt_callback();
449 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800450};
451
Rob Spies56953412014-04-28 14:09:47 -0700452
453/**
454 * Returns the preferences manager used for configuring this terminal.
455 */
456hterm.Terminal.prototype.getPrefs = function() {
457 return this.prefs_;
458};
459
460
rginda8e92a692012-05-20 19:37:20 -0700461/**
462 * Set the color for the cursor.
463 *
464 * If you want this setting to persist, set it through prefs_, rather than
465 * with this method.
466 */
467hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700468 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700469 this.cursorNode_.style.backgroundColor = color;
470 this.cursorNode_.style.borderColor = color;
471};
472
473/**
474 * Return the current cursor color as a string.
475 */
476hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700477 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700478};
479
480/**
rgindad5613292012-06-19 15:40:37 -0700481 * Enable or disable mouse based text selection in the terminal.
482 */
483hterm.Terminal.prototype.setSelectionEnabled = function(state) {
484 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700485};
486
487/**
rginda8e92a692012-05-20 19:37:20 -0700488 * Set the background color.
489 *
490 * If you want this setting to persist, set it through prefs_, rather than
491 * with this method.
492 */
493hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700494 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700495 this.primaryScreen_.textAttributes.setDefaults(
496 this.foregroundColor_, this.backgroundColor_);
497 this.alternateScreen_.textAttributes.setDefaults(
498 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700499 this.scrollPort_.setBackgroundColor(color);
500};
501
rginda9f5222b2012-03-05 11:53:28 -0800502/**
503 * Return the current terminal background color.
504 *
505 * Intended for use by other classes, so we don't have to expose the entire
506 * prefs_ object.
507 */
508hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700509 return this.backgroundColor_;
510};
511
512/**
513 * Set the foreground color.
514 *
515 * If you want this setting to persist, set it through prefs_, rather than
516 * with this method.
517 */
518hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700519 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700520 this.primaryScreen_.textAttributes.setDefaults(
521 this.foregroundColor_, this.backgroundColor_);
522 this.alternateScreen_.textAttributes.setDefaults(
523 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700524 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800525};
526
527/**
528 * Return the current terminal foreground color.
529 *
530 * Intended for use by other classes, so we don't have to expose the entire
531 * prefs_ object.
532 */
533hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700534 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800535};
536
537/**
rginda87b86462011-12-14 13:48:03 -0800538 * Create a new instance of a terminal command and run it with a given
539 * argument string.
540 *
541 * @param {function} commandClass The constructor for a terminal command.
542 * @param {string} argString The argument string to pass to the command.
543 */
544hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700545 var environment = this.prefs_.get('environment');
546 if (typeof environment != 'object' || environment == null)
547 environment = {};
548
rginda87b86462011-12-14 13:48:03 -0800549 var self = this;
550 this.command = new commandClass(
551 { argString: argString || '',
552 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700553 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800554 onExit: function(code) {
555 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800556 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700557 if (self.prefs_.get('close-on-exit'))
558 window.close();
rginda87b86462011-12-14 13:48:03 -0800559 }
560 });
561
rgindafeaf3142012-01-31 15:14:20 -0800562 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800563 this.command.run();
564};
565
566/**
rgindafeaf3142012-01-31 15:14:20 -0800567 * Returns true if the current screen is the primary screen, false otherwise.
568 */
569hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700570 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800571};
572
573/**
574 * Install the keyboard handler for this terminal.
575 *
576 * This will prevent the browser from seeing any keystrokes sent to the
577 * terminal.
578 */
579hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700580 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800581}
582
583/**
584 * Uninstall the keyboard handler for this terminal.
585 */
586hterm.Terminal.prototype.uninstallKeyboard = function() {
587 this.keyboard.installKeyboard(null);
588}
589
590/**
rginda35c456b2012-02-09 17:29:05 -0800591 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800592 *
593 * Call setFontSize(0) to reset to the default font size.
594 *
595 * This function does not modify the font-size preference.
596 *
597 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800598 */
599hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800600 if (px === 0)
601 px = this.prefs_.get('font-size');
602
rginda35c456b2012-02-09 17:29:05 -0800603 this.scrollPort_.setFontSize(px);
Ricky Liang48f05cb2013-12-31 23:35:29 +0800604 if (this.wcCssRule_) {
605 this.wcCssRule_.style.width = this.scrollPort_.characterSize.width * 2 +
606 'px';
607 }
rginda35c456b2012-02-09 17:29:05 -0800608};
609
610/**
611 * Get the current font size.
612 */
613hterm.Terminal.prototype.getFontSize = function() {
614 return this.scrollPort_.getFontSize();
615};
616
617/**
rginda8e92a692012-05-20 19:37:20 -0700618 * Get the current font family.
619 */
620hterm.Terminal.prototype.getFontFamily = function() {
621 return this.scrollPort_.getFontFamily();
622};
623
624/**
rginda35c456b2012-02-09 17:29:05 -0800625 * Set the CSS "font-family" for this terminal.
626 */
rginda9f5222b2012-03-05 11:53:28 -0800627hterm.Terminal.prototype.syncFontFamily = function() {
628 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
629 this.prefs_.get('font-smoothing'));
630 this.syncBoldSafeState();
631};
632
rginda4bba5e12012-06-20 16:15:30 -0700633/**
634 * Set this.mousePasteButton based on the mouse-paste-button pref,
635 * autodetecting if necessary.
636 */
637hterm.Terminal.prototype.syncMousePasteButton = function() {
638 var button = this.prefs_.get('mouse-paste-button');
639 if (typeof button == 'number') {
640 this.mousePasteButton = button;
641 return;
642 }
643
644 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
645 if (!ary || ary[2] == 'CrOS') {
646 this.mousePasteButton = 2;
647 } else {
648 this.mousePasteButton = 3;
649 }
650};
651
652/**
653 * Enable or disable bold based on the enable-bold pref, autodetecting if
654 * necessary.
655 */
rginda9f5222b2012-03-05 11:53:28 -0800656hterm.Terminal.prototype.syncBoldSafeState = function() {
657 var enableBold = this.prefs_.get('enable-bold');
658 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700659 this.primaryScreen_.textAttributes.enableBold = enableBold;
660 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800661 return;
662 }
663
rgindaf7521392012-02-28 17:20:34 -0800664 var normalSize = this.scrollPort_.measureCharacterSize();
665 var boldSize = this.scrollPort_.measureCharacterSize('bold');
666
667 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800668 if (!isBoldSafe) {
669 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700670 'from normal. Font family is: ' +
671 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800672 }
rginda9f5222b2012-03-05 11:53:28 -0800673
Robert Gindaed016262012-10-26 16:27:09 -0700674 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
675 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800676};
677
678/**
rginda87b86462011-12-14 13:48:03 -0800679 * Return a copy of the current cursor position.
680 *
681 * @return {hterm.RowCol} The RowCol object representing the current position.
682 */
683hterm.Terminal.prototype.saveCursor = function() {
684 return this.screen_.cursorPosition.clone();
685};
686
rgindaa19afe22012-01-25 15:40:22 -0800687hterm.Terminal.prototype.getTextAttributes = function() {
688 return this.screen_.textAttributes;
689};
690
rginda1a09aa02012-06-18 21:11:25 -0700691hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
692 this.screen_.textAttributes = textAttributes;
693};
694
rginda87b86462011-12-14 13:48:03 -0800695/**
rgindaf522ce02012-04-17 17:49:17 -0700696 * Return the current browser zoom factor applied to the terminal.
697 *
698 * @return {number} The current browser zoom factor.
699 */
700hterm.Terminal.prototype.getZoomFactor = function() {
701 return this.scrollPort_.characterSize.zoomFactor;
702};
703
704/**
rginda9846e2f2012-01-27 13:53:33 -0800705 * Change the title of this terminal's window.
706 */
707hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800708 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800709};
710
711/**
rginda87b86462011-12-14 13:48:03 -0800712 * Restore a previously saved cursor position.
713 *
714 * @param {hterm.RowCol} cursor The position to restore.
715 */
716hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700717 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
718 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800719 this.screen_.setCursorPosition(row, column);
720 if (cursor.column > column ||
721 cursor.column == column && cursor.overflow) {
722 this.screen_.cursorPosition.overflow = true;
723 }
rginda87b86462011-12-14 13:48:03 -0800724};
725
726/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400727 * Clear the cursor's overflow flag.
728 */
729hterm.Terminal.prototype.clearCursorOverflow = function() {
730 this.screen_.cursorPosition.overflow = false;
731};
732
733/**
Robert Ginda830583c2013-08-07 13:20:46 -0700734 * Sets the cursor shape
735 */
736hterm.Terminal.prototype.setCursorShape = function(shape) {
737 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800738 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700739}
740
741/**
742 * Get the cursor shape
743 */
744hterm.Terminal.prototype.getCursorShape = function() {
745 return this.cursorShape_;
746}
747
748/**
rginda87b86462011-12-14 13:48:03 -0800749 * Set the width of the terminal, resizing the UI to match.
750 */
751hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800752 if (columnCount == null) {
753 this.div_.style.width = '100%';
754 return;
755 }
756
rginda35c456b2012-02-09 17:29:05 -0800757 this.div_.style.width = this.scrollPort_.characterSize.width *
Robert Ginda97769282013-02-01 15:30:30 -0800758 columnCount + this.scrollPort_.currentScrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400759 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800760 this.scheduleSyncCursorPosition_();
761};
rginda87b86462011-12-14 13:48:03 -0800762
rgindac9bc5502012-01-18 11:48:44 -0800763/**
rginda35c456b2012-02-09 17:29:05 -0800764 * Set the height of the terminal, resizing the UI to match.
765 */
766hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800767 if (rowCount == null) {
768 this.div_.style.height = '100%';
769 return;
770 }
771
rginda35c456b2012-02-09 17:29:05 -0800772 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700773 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800774 this.realizeSize_(this.screenSize.width, rowCount);
775 this.scheduleSyncCursorPosition_();
776};
777
778/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400779 * Deal with terminal size changes.
780 *
781 */
782hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
783 if (columnCount != this.screenSize.width)
784 this.realizeWidth_(columnCount);
785
786 if (rowCount != this.screenSize.height)
787 this.realizeHeight_(rowCount);
788
789 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700790 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400791};
792
793/**
rgindac9bc5502012-01-18 11:48:44 -0800794 * Deal with terminal width changes.
795 *
796 * This function does what needs to be done when the terminal width changes
797 * out from under us. It happens here rather than in onResize_() because this
798 * code may need to run synchronously to handle programmatic changes of
799 * terminal width.
800 *
801 * Relying on the browser to send us an async resize event means we may not be
802 * in the correct state yet when the next escape sequence hits.
803 */
804hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700805 if (columnCount <= 0)
806 throw new Error('Attempt to realize bad width: ' + columnCount);
807
rgindac9bc5502012-01-18 11:48:44 -0800808 var deltaColumns = columnCount - this.screen_.getWidth();
809
rginda87b86462011-12-14 13:48:03 -0800810 this.screenSize.width = columnCount;
811 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800812
813 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400814 if (this.defaultTabStops)
815 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800816 } else {
817 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400818 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800819 break;
820
821 this.tabStops_.pop();
822 }
823 }
824
825 this.screen_.setColumnCount(this.screenSize.width);
826};
827
828/**
829 * Deal with terminal height changes.
830 *
831 * This function does what needs to be done when the terminal height changes
832 * out from under us. It happens here rather than in onResize_() because this
833 * code may need to run synchronously to handle programmatic changes of
834 * terminal height.
835 *
836 * Relying on the browser to send us an async resize event means we may not be
837 * in the correct state yet when the next escape sequence hits.
838 */
839hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700840 if (rowCount <= 0)
841 throw new Error('Attempt to realize bad height: ' + rowCount);
842
rgindac9bc5502012-01-18 11:48:44 -0800843 var deltaRows = rowCount - this.screen_.getHeight();
844
845 this.screenSize.height = rowCount;
846
847 var cursor = this.saveCursor();
848
849 if (deltaRows < 0) {
850 // Screen got smaller.
851 deltaRows *= -1;
852 while (deltaRows) {
853 var lastRow = this.getRowCount() - 1;
854 if (lastRow - this.scrollbackRows_.length == cursor.row)
855 break;
856
857 if (this.getRowText(lastRow))
858 break;
859
860 this.screen_.popRow();
861 deltaRows--;
862 }
863
864 var ary = this.screen_.shiftRows(deltaRows);
865 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
866
867 // We just removed rows from the top of the screen, we need to update
868 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800869 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800870 } else if (deltaRows > 0) {
871 // Screen got larger.
872
873 if (deltaRows <= this.scrollbackRows_.length) {
874 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
875 var rows = this.scrollbackRows_.splice(
876 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
877 this.screen_.unshiftRows(rows);
878 deltaRows -= scrollbackCount;
879 cursor.row += scrollbackCount;
880 }
881
882 if (deltaRows)
883 this.appendRows_(deltaRows);
884 }
885
rginda35c456b2012-02-09 17:29:05 -0800886 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800887 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800888};
889
890/**
891 * Scroll the terminal to the top of the scrollback buffer.
892 */
893hterm.Terminal.prototype.scrollHome = function() {
894 this.scrollPort_.scrollRowToTop(0);
895};
896
897/**
898 * Scroll the terminal to the end.
899 */
900hterm.Terminal.prototype.scrollEnd = function() {
901 this.scrollPort_.scrollRowToBottom(this.getRowCount());
902};
903
904/**
905 * Scroll the terminal one page up (minus one line) relative to the current
906 * position.
907 */
908hterm.Terminal.prototype.scrollPageUp = function() {
909 var i = this.scrollPort_.getTopRowIndex();
910 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
911};
912
913/**
914 * Scroll the terminal one page down (minus one line) relative to the current
915 * position.
916 */
917hterm.Terminal.prototype.scrollPageDown = function() {
918 var i = this.scrollPort_.getTopRowIndex();
919 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800920};
921
rgindac9bc5502012-01-18 11:48:44 -0800922/**
Robert Ginda40932892012-12-10 17:26:40 -0800923 * Clear primary screen, secondary screen, and the scrollback buffer.
924 */
925hterm.Terminal.prototype.wipeContents = function() {
926 this.scrollbackRows_.length = 0;
927 this.scrollPort_.resetCache();
928
929 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
930 var bottom = screen.getHeight();
931 if (bottom > 0) {
932 this.renumberRows_(0, bottom);
933 this.clearHome(screen);
934 }
935 }.bind(this));
936
937 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -0700938 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -0800939};
940
941/**
rgindac9bc5502012-01-18 11:48:44 -0800942 * Full terminal reset.
943 */
rginda87b86462011-12-14 13:48:03 -0800944hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800945 this.clearAllTabStops();
946 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700947
948 this.clearHome(this.primaryScreen_);
949 this.primaryScreen_.textAttributes.reset();
950
951 this.clearHome(this.alternateScreen_);
952 this.alternateScreen_.textAttributes.reset();
953
rgindab8bc8932012-04-27 12:45:03 -0700954 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
955
Robert Ginda92e18102013-03-14 13:56:37 -0700956 this.vt.reset();
957
rgindac9bc5502012-01-18 11:48:44 -0800958 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800959};
960
rgindac9bc5502012-01-18 11:48:44 -0800961/**
962 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700963 *
964 * Perform a soft reset to the default values listed in
965 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800966 */
rginda0f5c0292012-01-13 11:00:13 -0800967hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700968 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800969 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700970
rgindab8bc8932012-04-27 12:45:03 -0700971 // Xterm also resets the color palette on soft reset, even though it doesn't
972 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700973 this.primaryScreen_.textAttributes.resetColorPalette();
974 this.alternateScreen_.textAttributes.resetColorPalette();
975
rgindab8bc8932012-04-27 12:45:03 -0700976 // The xterm man page explicitly says this will happen on soft reset.
977 this.setVTScrollRegion(null, null);
978
979 // Xterm also shows the cursor on soft reset, but does not alter the blink
980 // state.
rgindaa19afe22012-01-25 15:40:22 -0800981 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800982};
983
rgindac9bc5502012-01-18 11:48:44 -0800984/**
985 * Move the cursor forward to the next tab stop, or to the last column
986 * if no more tab stops are set.
987 */
988hterm.Terminal.prototype.forwardTabStop = function() {
989 var column = this.screen_.cursorPosition.column;
990
991 for (var i = 0; i < this.tabStops_.length; i++) {
992 if (this.tabStops_[i] > column) {
993 this.setCursorColumn(this.tabStops_[i]);
994 return;
995 }
996 }
997
David Benjamin66e954d2012-05-05 21:08:12 -0400998 // xterm does not clear the overflow flag on HT or CHT.
999 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001000 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001001 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001002};
1003
rgindac9bc5502012-01-18 11:48:44 -08001004/**
1005 * Move the cursor backward to the previous tab stop, or to the first column
1006 * if no previous tab stops are set.
1007 */
1008hterm.Terminal.prototype.backwardTabStop = function() {
1009 var column = this.screen_.cursorPosition.column;
1010
1011 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1012 if (this.tabStops_[i] < column) {
1013 this.setCursorColumn(this.tabStops_[i]);
1014 return;
1015 }
1016 }
1017
1018 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001019};
1020
rgindac9bc5502012-01-18 11:48:44 -08001021/**
1022 * Set a tab stop at the given column.
1023 *
1024 * @param {int} column Zero based column.
1025 */
1026hterm.Terminal.prototype.setTabStop = function(column) {
1027 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1028 if (this.tabStops_[i] == column)
1029 return;
1030
1031 if (this.tabStops_[i] < column) {
1032 this.tabStops_.splice(i + 1, 0, column);
1033 return;
1034 }
1035 }
1036
1037 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001038};
1039
rgindac9bc5502012-01-18 11:48:44 -08001040/**
1041 * Clear the tab stop at the current cursor position.
1042 *
1043 * No effect if there is no tab stop at the current cursor position.
1044 */
1045hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1046 var column = this.screen_.cursorPosition.column;
1047
1048 var i = this.tabStops_.indexOf(column);
1049 if (i == -1)
1050 return;
1051
1052 this.tabStops_.splice(i, 1);
1053};
1054
1055/**
1056 * Clear all tab stops.
1057 */
1058hterm.Terminal.prototype.clearAllTabStops = function() {
1059 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001060 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001061};
1062
1063/**
1064 * Set up the default tab stops, starting from a given column.
1065 *
1066 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001067 * from the specified column, or 0 if no column is provided. It also flags
1068 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001069 *
1070 * This does not clear the existing tab stops first, use clearAllTabStops
1071 * for that.
1072 *
1073 * @param {int} opt_start Optional starting zero based starting column, useful
1074 * for filling out missing tab stops when the terminal is resized.
1075 */
1076hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1077 var start = opt_start || 0;
1078 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001079 // Round start up to a default tab stop.
1080 start = start - 1 - ((start - 1) % w) + w;
1081 for (var i = start; i < this.screenSize.width; i += w) {
1082 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001083 }
David Benjamin66e954d2012-05-05 21:08:12 -04001084
1085 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001086};
1087
rginda6d397402012-01-17 10:58:29 -08001088/**
rginda8ba33642011-12-14 12:31:31 -08001089 * Interpret a sequence of characters.
1090 *
1091 * Incomplete escape sequences are buffered until the next call.
1092 *
1093 * @param {string} str Sequence of characters to interpret or pass through.
1094 */
1095hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001096 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001097 this.scheduleSyncCursorPosition_();
1098};
1099
1100/**
1101 * Take over the given DIV for use as the terminal display.
1102 *
1103 * @param {HTMLDivElement} div The div to use as the terminal display.
1104 */
1105hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001106 this.div_ = div;
1107
rginda8ba33642011-12-14 12:31:31 -08001108 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001109 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001110 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1111 this.scrollPort_.setBackgroundPosition(
1112 this.prefs_.get('background-position'));
Robert Gindae76aa9f2014-03-14 12:29:12 -07001113 this.scrollPort_.setUserCss(this.prefs_.get('user-css'));
rginda30f20f62012-04-05 16:36:19 -07001114
rginda0918b652012-04-04 11:26:24 -07001115 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001116
rginda9f5222b2012-03-05 11:53:28 -08001117 this.setFontSize(this.prefs_.get('font-size'));
1118 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001119
David Reveman8f552492012-03-28 12:18:41 -04001120 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1121
rginda8ba33642011-12-14 12:31:31 -08001122 this.document_ = this.scrollPort_.getDocument();
1123
rginda4bba5e12012-06-20 16:15:30 -07001124 this.document_.body.oncontextmenu = function() { return false };
1125
1126 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001127 var screenNode = this.scrollPort_.getScreenNode();
1128 screenNode.addEventListener('mousedown', onMouse);
1129 screenNode.addEventListener('mouseup', onMouse);
1130 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001131 this.scrollPort_.onScrollWheel = onMouse;
1132
Toni Barzic0bfa8922013-11-22 11:18:35 -08001133 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001134 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001135 // Listen for mousedown events on the screenNode as in FF the focus
1136 // events don't bubble.
1137 screenNode.addEventListener('mousedown', function() {
1138 setTimeout(this.onFocusChange_.bind(this, true));
1139 }.bind(this));
1140
Toni Barzic0bfa8922013-11-22 11:18:35 -08001141 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001142 'blur', this.onFocusChange_.bind(this, false));
1143
1144 var style = this.document_.createElement('style');
1145 style.textContent =
1146 ('.cursor-node[focus="false"] {' +
1147 ' box-sizing: border-box;' +
1148 ' background-color: transparent !important;' +
1149 ' border-width: 2px;' +
1150 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001151 '}' +
1152 '.wc-node {' +
1153 ' display: inline-block;' +
1154 ' text-align: center;' +
1155 ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001156 '}');
1157 this.document_.head.appendChild(style);
1158
Ricky Liang48f05cb2013-12-31 23:35:29 +08001159 var styleSheets = this.document_.styleSheets;
1160 var cssRules = styleSheets[styleSheets.length - 1].cssRules;
1161 this.wcCssRule_ = cssRules[cssRules.length - 1];
1162
rginda8ba33642011-12-14 12:31:31 -08001163 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001164 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001165 this.cursorNode_.style.cssText =
1166 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001167 'top: -99px;' +
1168 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001169 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1170 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
Rob Spies06533ba2014-04-24 11:20:37 -07001171 '-webkit-transition: opacity, background-color 100ms linear;' +
1172 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001173
rginda8e92a692012-05-20 19:37:20 -07001174 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001175 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1176 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001177
rginda8ba33642011-12-14 12:31:31 -08001178 this.document_.body.appendChild(this.cursorNode_);
1179
rgindad5613292012-06-19 15:40:37 -07001180 // When 'enableMouseDragScroll' is off we reposition this element directly
1181 // under the mouse cursor after a click. This makes Chrome associate
1182 // subsequent mousemove events with the scroll-blocker. Since the
1183 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1184 // events do not cause the scrollport to scroll.
1185 //
1186 // It's a hack, but it's the cleanest way I could find.
1187 this.scrollBlockerNode_ = this.document_.createElement('div');
1188 this.scrollBlockerNode_.style.cssText =
1189 ('position: absolute;' +
1190 'top: -99px;' +
1191 'display: block;' +
1192 'width: 10px;' +
1193 'height: 10px;');
1194 this.document_.body.appendChild(this.scrollBlockerNode_);
1195
1196 var onMouse = this.onMouse_.bind(this);
1197 this.scrollPort_.onScrollWheel = onMouse;
1198 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1199 ].forEach(function(event) {
1200 this.scrollBlockerNode_.addEventListener(event, onMouse);
1201 this.cursorNode_.addEventListener(event, onMouse);
1202 this.document_.addEventListener(event, onMouse);
1203 }.bind(this));
1204
1205 this.cursorNode_.addEventListener('mousedown', function() {
1206 setTimeout(this.focus.bind(this));
1207 }.bind(this));
1208
rginda8ba33642011-12-14 12:31:31 -08001209 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001210
rginda87b86462011-12-14 13:48:03 -08001211 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001212 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001213};
1214
rginda0918b652012-04-04 11:26:24 -07001215/**
1216 * Return the HTML document that contains the terminal DOM nodes.
1217 */
rginda87b86462011-12-14 13:48:03 -08001218hterm.Terminal.prototype.getDocument = function() {
1219 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001220};
1221
1222/**
rginda0918b652012-04-04 11:26:24 -07001223 * Focus the terminal.
1224 */
1225hterm.Terminal.prototype.focus = function() {
1226 this.scrollPort_.focus();
1227};
1228
1229/**
rginda8ba33642011-12-14 12:31:31 -08001230 * Return the HTML Element for a given row index.
1231 *
1232 * This is a method from the RowProvider interface. The ScrollPort uses
1233 * it to fetch rows on demand as they are scrolled into view.
1234 *
1235 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1236 * pairs to conserve memory.
1237 *
1238 * @param {integer} index The zero-based row index, measured relative to the
1239 * start of the scrollback buffer. On-screen rows will always have the
1240 * largest indicies.
1241 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1242 */
1243hterm.Terminal.prototype.getRowNode = function(index) {
1244 if (index < this.scrollbackRows_.length)
1245 return this.scrollbackRows_[index];
1246
1247 var screenIndex = index - this.scrollbackRows_.length;
1248 return this.screen_.rowsArray[screenIndex];
1249};
1250
1251/**
1252 * Return the text content for a given range of rows.
1253 *
1254 * This is a method from the RowProvider interface. The ScrollPort uses
1255 * it to fetch text content on demand when the user attempts to copy their
1256 * selection to the clipboard.
1257 *
1258 * @param {integer} start The zero-based row index to start from, measured
1259 * relative to the start of the scrollback buffer. On-screen rows will
1260 * always have the largest indicies.
1261 * @param {integer} end The zero-based row index to end on, measured
1262 * relative to the start of the scrollback buffer.
1263 * @return {string} A single string containing the text value of the range of
1264 * rows. Lines will be newline delimited, with no trailing newline.
1265 */
1266hterm.Terminal.prototype.getRowsText = function(start, end) {
1267 var ary = [];
1268 for (var i = start; i < end; i++) {
1269 var node = this.getRowNode(i);
1270 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001271 if (i < end - 1 && !node.getAttribute('line-overflow'))
1272 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001273 }
1274
rgindaa09e7332012-08-17 12:49:51 -07001275 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001276};
1277
1278/**
1279 * Return the text content for a given row.
1280 *
1281 * This is a method from the RowProvider interface. The ScrollPort uses
1282 * it to fetch text content on demand when the user attempts to copy their
1283 * selection to the clipboard.
1284 *
1285 * @param {integer} index The zero-based row index to return, measured
1286 * relative to the start of the scrollback buffer. On-screen rows will
1287 * always have the largest indicies.
1288 * @return {string} A string containing the text value of the selected row.
1289 */
1290hterm.Terminal.prototype.getRowText = function(index) {
1291 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001292 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001293};
1294
1295/**
1296 * Return the total number of rows in the addressable screen and in the
1297 * scrollback buffer of this terminal.
1298 *
1299 * This is a method from the RowProvider interface. The ScrollPort uses
1300 * it to compute the size of the scrollbar.
1301 *
1302 * @return {integer} The number of rows in this terminal.
1303 */
1304hterm.Terminal.prototype.getRowCount = function() {
1305 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1306};
1307
1308/**
1309 * Create DOM nodes for new rows and append them to the end of the terminal.
1310 *
1311 * This is the only correct way to add a new DOM node for a row. Notice that
1312 * the new row is appended to the bottom of the list of rows, and does not
1313 * require renumbering (of the rowIndex property) of previous rows.
1314 *
1315 * If you think you want a new blank row somewhere in the middle of the
1316 * terminal, look into moveRows_().
1317 *
1318 * This method does not pay attention to vtScrollTop/Bottom, since you should
1319 * be using moveRows() in cases where they would matter.
1320 *
1321 * The cursor will be positioned at column 0 of the first inserted line.
1322 */
1323hterm.Terminal.prototype.appendRows_ = function(count) {
1324 var cursorRow = this.screen_.rowsArray.length;
1325 var offset = this.scrollbackRows_.length + cursorRow;
1326 for (var i = 0; i < count; i++) {
1327 var row = this.document_.createElement('x-row');
1328 row.appendChild(this.document_.createTextNode(''));
1329 row.rowIndex = offset + i;
1330 this.screen_.pushRow(row);
1331 }
1332
1333 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1334 if (extraRows > 0) {
1335 var ary = this.screen_.shiftRows(extraRows);
1336 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001337 if (this.scrollPort_.isScrolledEnd)
1338 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001339 }
1340
1341 if (cursorRow >= this.screen_.rowsArray.length)
1342 cursorRow = this.screen_.rowsArray.length - 1;
1343
rginda87b86462011-12-14 13:48:03 -08001344 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001345};
1346
1347/**
1348 * Relocate rows from one part of the addressable screen to another.
1349 *
1350 * This is used to recycle rows during VT scrolls (those which are driven
1351 * by VT commands, rather than by the user manipulating the scrollbar.)
1352 *
1353 * In this case, the blank lines scrolled into the scroll region are made of
1354 * the nodes we scrolled off. These have their rowIndex properties carefully
1355 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001356 */
1357hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1358 var ary = this.screen_.removeRows(fromIndex, count);
1359 this.screen_.insertRows(toIndex, ary);
1360
1361 var start, end;
1362 if (fromIndex < toIndex) {
1363 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001364 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001365 } else {
1366 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001367 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001368 }
1369
1370 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001371 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001372};
1373
1374/**
1375 * Renumber the rowIndex property of the given range of rows.
1376 *
1377 * The start and end indicies are relative to the screen, not the scrollback.
1378 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001379 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001380 * no need to renumber scrollback rows.
1381 */
Robert Ginda40932892012-12-10 17:26:40 -08001382hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1383 var screen = opt_screen || this.screen_;
1384
rginda8ba33642011-12-14 12:31:31 -08001385 var offset = this.scrollbackRows_.length;
1386 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001387 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001388 }
1389};
1390
1391/**
1392 * Print a string to the terminal.
1393 *
1394 * This respects the current insert and wraparound modes. It will add new lines
1395 * to the end of the terminal, scrolling off the top into the scrollback buffer
1396 * if necessary.
1397 *
1398 * The string is *not* parsed for escape codes. Use the interpret() method if
1399 * that's what you're after.
1400 *
1401 * @param{string} str The string to print.
1402 */
1403hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001404 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001405
Ricky Liang48f05cb2013-12-31 23:35:29 +08001406 var strWidth = lib.wc.strWidth(str);
1407
1408 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001409 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1410 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001411 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001412 }
rgindaa19afe22012-01-25 15:40:22 -08001413
Ricky Liang48f05cb2013-12-31 23:35:29 +08001414 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001415 var didOverflow = false;
1416 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001417
rgindaa9abdd82012-08-06 18:05:09 -07001418 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1419 didOverflow = true;
1420 count = this.screenSize.width - this.screen_.cursorPosition.column;
1421 }
rgindaa19afe22012-01-25 15:40:22 -08001422
rgindaa9abdd82012-08-06 18:05:09 -07001423 if (didOverflow && !this.options_.wraparound) {
1424 // If the string overflowed the line but wraparound is off, then the
1425 // last printed character should be the last of the string.
1426 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001427 substr = lib.wc.substr(str, startOffset, count - 1) +
1428 lib.wc.substr(str, strWidth - 1);
1429 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001430 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001431 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001432 }
rgindaa19afe22012-01-25 15:40:22 -08001433
Ricky Liang48f05cb2013-12-31 23:35:29 +08001434 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1435 for (var i = 0; i < tokens.length; i++) {
1436 if (tokens[i].wcNode)
1437 this.screen_.textAttributes.wcNode = true;
1438
1439 if (this.options_.insertMode) {
1440 this.screen_.insertString(tokens[i].str);
1441 } else {
1442 this.screen_.overwriteString(tokens[i].str);
1443 }
1444 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001445 }
1446
1447 this.screen_.maybeClipCurrentRow();
1448 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001449 }
rginda8ba33642011-12-14 12:31:31 -08001450
1451 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001452
rginda9f5222b2012-03-05 11:53:28 -08001453 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001454 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001455};
1456
1457/**
rginda87b86462011-12-14 13:48:03 -08001458 * Set the VT scroll region.
1459 *
rginda87b86462011-12-14 13:48:03 -08001460 * This also resets the cursor position to the absolute (0, 0) position, since
1461 * that's what xterm appears to do.
1462 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001463 * Setting the scroll region to the full height of the terminal will clear
1464 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1465 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1466 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1467 * continue to work as most users would expect.
1468 *
rginda87b86462011-12-14 13:48:03 -08001469 * @param {integer} scrollTop The zero-based top of the scroll region.
1470 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1471 * inclusive.
1472 */
1473hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001474 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001475 this.vtScrollTop_ = null;
1476 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001477 } else {
1478 this.vtScrollTop_ = scrollTop;
1479 this.vtScrollBottom_ = scrollBottom;
1480 }
rginda87b86462011-12-14 13:48:03 -08001481};
1482
1483/**
rginda8ba33642011-12-14 12:31:31 -08001484 * Return the top row index according to the VT.
1485 *
1486 * This will return 0 unless the terminal has been told to restrict scrolling
1487 * to some lower row. It is used for some VT cursor positioning and scrolling
1488 * commands.
1489 *
1490 * @return {integer} The topmost row in the terminal's scroll region.
1491 */
1492hterm.Terminal.prototype.getVTScrollTop = function() {
1493 if (this.vtScrollTop_ != null)
1494 return this.vtScrollTop_;
1495
1496 return 0;
rginda87b86462011-12-14 13:48:03 -08001497};
rginda8ba33642011-12-14 12:31:31 -08001498
1499/**
1500 * Return the bottom row index according to the VT.
1501 *
1502 * This will return the height of the terminal unless the it has been told to
1503 * restrict scrolling to some higher row. It is used for some VT cursor
1504 * positioning and scrolling commands.
1505 *
1506 * @return {integer} The bottommost row in the terminal's scroll region.
1507 */
1508hterm.Terminal.prototype.getVTScrollBottom = function() {
1509 if (this.vtScrollBottom_ != null)
1510 return this.vtScrollBottom_;
1511
rginda87b86462011-12-14 13:48:03 -08001512 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001513}
1514
1515/**
1516 * Process a '\n' character.
1517 *
1518 * If the cursor is on the final row of the terminal this will append a new
1519 * blank row to the screen and scroll the topmost row into the scrollback
1520 * buffer.
1521 *
1522 * Otherwise, this moves the cursor to column zero of the next row.
1523 */
1524hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001525 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1526 this.screen_.rowsArray.length - 1);
1527
1528 if (this.vtScrollBottom_ != null) {
1529 // A VT Scroll region is active, we never append new rows.
1530 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1531 // We're at the end of the VT Scroll Region, perform a VT scroll.
1532 this.vtScrollUp(1);
1533 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1534 } else if (cursorAtEndOfScreen) {
1535 // We're at the end of the screen, the only thing to do is put the
1536 // cursor to column 0.
1537 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1538 } else {
1539 // Anywhere else, advance the cursor row, and reset the column.
1540 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1541 }
1542 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001543 // We're at the end of the screen. Append a new row to the terminal,
1544 // shifting the top row into the scrollback.
1545 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001546 } else {
rginda87b86462011-12-14 13:48:03 -08001547 // Anywhere else in the screen just moves the cursor.
1548 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001549 }
1550};
1551
1552/**
1553 * Like newLine(), except maintain the cursor column.
1554 */
1555hterm.Terminal.prototype.lineFeed = function() {
1556 var column = this.screen_.cursorPosition.column;
1557 this.newLine();
1558 this.setCursorColumn(column);
1559};
1560
1561/**
rginda87b86462011-12-14 13:48:03 -08001562 * If autoCarriageReturn is set then newLine(), else lineFeed().
1563 */
1564hterm.Terminal.prototype.formFeed = function() {
1565 if (this.options_.autoCarriageReturn) {
1566 this.newLine();
1567 } else {
1568 this.lineFeed();
1569 }
1570};
1571
1572/**
1573 * Move the cursor up one row, possibly inserting a blank line.
1574 *
1575 * The cursor column is not changed.
1576 */
1577hterm.Terminal.prototype.reverseLineFeed = function() {
1578 var scrollTop = this.getVTScrollTop();
1579 var currentRow = this.screen_.cursorPosition.row;
1580
1581 if (currentRow == scrollTop) {
1582 this.insertLines(1);
1583 } else {
1584 this.setAbsoluteCursorRow(currentRow - 1);
1585 }
1586};
1587
1588/**
rginda8ba33642011-12-14 12:31:31 -08001589 * Replace all characters to the left of the current cursor with the space
1590 * character.
1591 *
1592 * TODO(rginda): This should probably *remove* the characters (not just replace
1593 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001594 * position.
rginda8ba33642011-12-14 12:31:31 -08001595 */
1596hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001597 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001598 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001599 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001600 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001601};
1602
1603/**
David Benjamin684a9b72012-05-01 17:19:58 -04001604 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001605 *
1606 * The cursor position is unchanged.
1607 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001608 * If the current background color is not the default background color this
1609 * will insert spaces rather than delete. This is unfortunate because the
1610 * trailing space will affect text selection, but it's difficult to come up
1611 * with a way to style empty space that wouldn't trip up the hterm.Screen
1612 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001613 *
1614 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1615 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1616 * crbug.com/232390 for details.
rginda8ba33642011-12-14 12:31:31 -08001617 */
1618hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001619 if (this.screen_.cursorPosition.overflow)
1620 return;
1621
Robert Ginda7fd57082012-09-25 14:41:47 -07001622 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1623 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001624
1625 if (this.screen_.textAttributes.background ===
1626 this.screen_.textAttributes.DEFAULT_COLOR) {
1627 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001628 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001629 this.screen_.cursorPosition.column + count) {
1630 this.screen_.deleteChars(count);
1631 this.clearCursorOverflow();
1632 return;
1633 }
1634 }
1635
rginda87b86462011-12-14 13:48:03 -08001636 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001637 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001638 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001639 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001640};
1641
1642/**
1643 * Erase the current line.
1644 *
1645 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001646 */
1647hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001648 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001649 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001650 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001651 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001652};
1653
1654/**
David Benjamina08d78f2012-05-05 00:28:49 -04001655 * Erase all characters from the start of the screen to the current cursor
1656 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001657 *
1658 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001659 */
1660hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001661 var cursor = this.saveCursor();
1662
1663 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001664
David Benjamina08d78f2012-05-05 00:28:49 -04001665 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001666 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001667 this.screen_.clearCursorRow();
1668 }
1669
rginda87b86462011-12-14 13:48:03 -08001670 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001671 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001672};
1673
1674/**
1675 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001676 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001677 *
1678 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001679 */
1680hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001681 var cursor = this.saveCursor();
1682
1683 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001684
David Benjamina08d78f2012-05-05 00:28:49 -04001685 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001686 for (var i = cursor.row + 1; i <= bottom; i++) {
1687 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001688 this.screen_.clearCursorRow();
1689 }
1690
rginda87b86462011-12-14 13:48:03 -08001691 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001692 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001693};
1694
1695/**
1696 * Fill the terminal with a given character.
1697 *
1698 * This methods does not respect the VT scroll region.
1699 *
1700 * @param {string} ch The character to use for the fill.
1701 */
1702hterm.Terminal.prototype.fill = function(ch) {
1703 var cursor = this.saveCursor();
1704
1705 this.setAbsoluteCursorPosition(0, 0);
1706 for (var row = 0; row < this.screenSize.height; row++) {
1707 for (var col = 0; col < this.screenSize.width; col++) {
1708 this.setAbsoluteCursorPosition(row, col);
1709 this.screen_.overwriteString(ch);
1710 }
1711 }
1712
1713 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001714};
1715
1716/**
rginda9ea433c2012-03-16 11:57:00 -07001717 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001718 *
rginda9ea433c2012-03-16 11:57:00 -07001719 * This does not respect the scroll region.
1720 *
1721 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1722 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001723 */
rginda9ea433c2012-03-16 11:57:00 -07001724hterm.Terminal.prototype.clearHome = function(opt_screen) {
1725 var screen = opt_screen || this.screen_;
1726 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001727
rginda11057d52012-04-25 12:29:56 -07001728 if (bottom == 0) {
1729 // Empty screen, nothing to do.
1730 return;
1731 }
1732
rgindae4d29232012-01-19 10:47:13 -08001733 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001734 screen.setCursorPosition(i, 0);
1735 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001736 }
1737
rginda9ea433c2012-03-16 11:57:00 -07001738 screen.setCursorPosition(0, 0);
1739};
1740
1741/**
1742 * Erase the entire display without changing the cursor position.
1743 *
1744 * The cursor position is unchanged. This does not respect the scroll
1745 * region.
1746 *
1747 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1748 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001749 */
1750hterm.Terminal.prototype.clear = function(opt_screen) {
1751 var screen = opt_screen || this.screen_;
1752 var cursor = screen.cursorPosition.clone();
1753 this.clearHome(screen);
1754 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001755};
1756
1757/**
1758 * VT command to insert lines at the current cursor row.
1759 *
1760 * This respects the current scroll region. Rows pushed off the bottom are
1761 * lost (they won't show up in the scrollback buffer).
1762 *
rginda8ba33642011-12-14 12:31:31 -08001763 * @param {integer} count The number of lines to insert.
1764 */
1765hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001766 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001767
1768 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001769 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001770
Robert Ginda579186b2012-09-26 11:40:04 -07001771 // The moveCount is the number of rows we need to relocate to make room for
1772 // the new row(s). The count is the distance to move them.
1773 var moveCount = bottom - cursorRow - count + 1;
1774 if (moveCount)
1775 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001776
Robert Ginda579186b2012-09-26 11:40:04 -07001777 for (var i = count - 1; i >= 0; i--) {
1778 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001779 this.screen_.clearCursorRow();
1780 }
rginda8ba33642011-12-14 12:31:31 -08001781};
1782
1783/**
1784 * VT command to delete lines at the current cursor row.
1785 *
1786 * New rows are added to the bottom of scroll region to take their place. New
1787 * rows are strictly there to take up space and have no content or style.
1788 */
1789hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001790 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001791
rginda87b86462011-12-14 13:48:03 -08001792 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001793 var bottom = this.getVTScrollBottom();
1794
rginda87b86462011-12-14 13:48:03 -08001795 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001796 count = Math.min(count, maxCount);
1797
rginda87b86462011-12-14 13:48:03 -08001798 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001799 if (count != maxCount)
1800 this.moveRows_(top, count, moveStart);
1801
1802 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001803 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001804 this.screen_.clearCursorRow();
1805 }
1806
rginda87b86462011-12-14 13:48:03 -08001807 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001808 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001809};
1810
1811/**
1812 * Inserts the given number of spaces at the current cursor position.
1813 *
rginda87b86462011-12-14 13:48:03 -08001814 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001815 */
1816hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001817 var cursor = this.saveCursor();
1818
rgindacbbd7482012-06-13 15:06:16 -07001819 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001820 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001821 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001822
1823 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001824 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001825};
1826
1827/**
1828 * Forward-delete the specified number of characters starting at the cursor
1829 * position.
1830 *
1831 * @param {integer} count The number of characters to delete.
1832 */
1833hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001834 var deleted = this.screen_.deleteChars(count);
1835 if (deleted && !this.screen_.textAttributes.isDefault()) {
1836 var cursor = this.saveCursor();
1837 this.setCursorColumn(this.screenSize.width - deleted);
1838 this.screen_.insertString(lib.f.getWhitespace(deleted));
1839 this.restoreCursor(cursor);
1840 }
1841
David Benjamin54e8bf62012-06-01 22:31:40 -04001842 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001843};
1844
1845/**
1846 * Shift rows in the scroll region upwards by a given number of lines.
1847 *
1848 * New rows are inserted at the bottom of the scroll region to fill the
1849 * vacated rows. The new rows not filled out with the current text attributes.
1850 *
1851 * This function does not affect the scrollback rows at all. Rows shifted
1852 * off the top are lost.
1853 *
rginda87b86462011-12-14 13:48:03 -08001854 * The cursor position is not altered.
1855 *
rginda8ba33642011-12-14 12:31:31 -08001856 * @param {integer} count The number of rows to scroll.
1857 */
1858hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001859 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001860
rginda87b86462011-12-14 13:48:03 -08001861 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001862 this.deleteLines(count);
1863
rginda87b86462011-12-14 13:48:03 -08001864 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001865};
1866
1867/**
1868 * Shift rows below the cursor down by a given number of lines.
1869 *
1870 * This function respects the current scroll region.
1871 *
1872 * New rows are inserted at the top of the scroll region to fill the
1873 * vacated rows. The new rows not filled out with the current text attributes.
1874 *
1875 * This function does not affect the scrollback rows at all. Rows shifted
1876 * off the bottom are lost.
1877 *
1878 * @param {integer} count The number of rows to scroll.
1879 */
1880hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001881 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001882
rginda87b86462011-12-14 13:48:03 -08001883 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001884 this.insertLines(opt_count);
1885
rginda87b86462011-12-14 13:48:03 -08001886 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001887};
1888
rginda87b86462011-12-14 13:48:03 -08001889
rginda8ba33642011-12-14 12:31:31 -08001890/**
1891 * Set the cursor position.
1892 *
1893 * The cursor row is relative to the scroll region if the terminal has
1894 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1895 *
1896 * @param {integer} row The new zero-based cursor row.
1897 * @param {integer} row The new zero-based cursor column.
1898 */
1899hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1900 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001901 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001902 } else {
rginda87b86462011-12-14 13:48:03 -08001903 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001904 }
rginda87b86462011-12-14 13:48:03 -08001905};
rginda8ba33642011-12-14 12:31:31 -08001906
rginda87b86462011-12-14 13:48:03 -08001907hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1908 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001909 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1910 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001911 this.screen_.setCursorPosition(row, column);
1912};
1913
1914hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001915 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1916 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001917 this.screen_.setCursorPosition(row, column);
1918};
1919
1920/**
1921 * Set the cursor column.
1922 *
1923 * @param {integer} column The new zero-based cursor column.
1924 */
1925hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001926 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001927};
1928
1929/**
1930 * Return the cursor column.
1931 *
1932 * @return {integer} The zero-based cursor column.
1933 */
1934hterm.Terminal.prototype.getCursorColumn = function() {
1935 return this.screen_.cursorPosition.column;
1936};
1937
1938/**
1939 * Set the cursor row.
1940 *
1941 * The cursor row is relative to the scroll region if the terminal has
1942 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1943 *
1944 * @param {integer} row The new cursor row.
1945 */
rginda87b86462011-12-14 13:48:03 -08001946hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1947 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001948};
1949
1950/**
1951 * Return the cursor row.
1952 *
1953 * @return {integer} The zero-based cursor row.
1954 */
1955hterm.Terminal.prototype.getCursorRow = function(row) {
1956 return this.screen_.cursorPosition.row;
1957};
1958
1959/**
1960 * Request that the ScrollPort redraw itself soon.
1961 *
1962 * The redraw will happen asynchronously, soon after the call stack winds down.
1963 * Multiple calls will be coalesced into a single redraw.
1964 */
1965hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001966 if (this.timeouts_.redraw)
1967 return;
rginda8ba33642011-12-14 12:31:31 -08001968
1969 var self = this;
rginda87b86462011-12-14 13:48:03 -08001970 this.timeouts_.redraw = setTimeout(function() {
1971 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001972 self.scrollPort_.redraw_();
1973 }, 0);
1974};
1975
1976/**
1977 * Request that the ScrollPort be scrolled to the bottom.
1978 *
1979 * The scroll will happen asynchronously, soon after the call stack winds down.
1980 * Multiple calls will be coalesced into a single scroll.
1981 *
1982 * This affects the scrollbar position of the ScrollPort, and has nothing to
1983 * do with the VT scroll commands.
1984 */
1985hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1986 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001987 return;
rginda8ba33642011-12-14 12:31:31 -08001988
1989 var self = this;
1990 this.timeouts_.scrollDown = setTimeout(function() {
1991 delete self.timeouts_.scrollDown;
1992 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1993 }, 10);
1994};
1995
1996/**
1997 * Move the cursor up a specified number of rows.
1998 *
1999 * @param {integer} count The number of rows to move the cursor.
2000 */
2001hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002002 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002003};
2004
2005/**
2006 * Move the cursor down a specified number of rows.
2007 *
2008 * @param {integer} count The number of rows to move the cursor.
2009 */
2010hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002011 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002012 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2013 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2014 this.screenSize.height - 1);
2015
rgindacbbd7482012-06-13 15:06:16 -07002016 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002017 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002018 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002019};
2020
2021/**
2022 * Move the cursor left a specified number of columns.
2023 *
2024 * @param {integer} count The number of columns to move the cursor.
2025 */
2026hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002027 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002028};
2029
2030/**
2031 * Move the cursor right a specified number of columns.
2032 *
2033 * @param {integer} count The number of columns to move the cursor.
2034 */
2035hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002036 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07002037 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002038 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002039 this.setCursorColumn(column);
2040};
2041
2042/**
2043 * Reverse the foreground and background colors of the terminal.
2044 *
2045 * This only affects text that was drawn with no attributes.
2046 *
2047 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2048 * been drawn with attributes that happen to coincide with the default
2049 * 'no-attribute' colors. My guess is probably not.
2050 */
2051hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002052 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002053 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002054 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2055 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002056 } else {
rginda9f5222b2012-03-05 11:53:28 -08002057 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2058 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002059 }
2060};
2061
2062/**
rginda87b86462011-12-14 13:48:03 -08002063 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002064 *
2065 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002066 */
2067hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002068 this.cursorNode_.style.backgroundColor =
2069 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002070
2071 var self = this;
2072 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002073 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002074 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002075
Michael Kelly485ecd12014-06-09 11:41:56 -04002076 // bellSquelchTimeout_ affects both audio and notification bells.
2077 if (this.bellSquelchTimeout_)
2078 return;
2079
Robert Ginda92e18102013-03-14 13:56:37 -07002080 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002081 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002082 this.bellSequelchTimeout_ = setTimeout(function() {
2083 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002084 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002085 } else {
2086 delete this.bellSquelchTimeout_;
2087 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002088
2089 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
2090 var n = new Notification(
2091 lib.f.replaceVars(hterm.desktopNotificationTitle,
2092 {'title': this.document_.title || 'hterm'}));
2093 this.bellNotificationList_.push(n);
2094 // TODO: Should we try to raise the window here?
2095 n.onclick = function() { self.closeBellNotifications_(); };
2096 }
rginda87b86462011-12-14 13:48:03 -08002097};
2098
2099/**
rginda8ba33642011-12-14 12:31:31 -08002100 * Set the origin mode bit.
2101 *
2102 * If origin mode is on, certain VT cursor and scrolling commands measure their
2103 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2104 * to the top of the addressable screen.
2105 *
2106 * Defaults to off.
2107 *
2108 * @param {boolean} state True to set origin mode, false to unset.
2109 */
2110hterm.Terminal.prototype.setOriginMode = function(state) {
2111 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002112 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002113};
2114
2115/**
2116 * Set the insert mode bit.
2117 *
2118 * If insert mode is on, existing text beyond the cursor position will be
2119 * shifted right to make room for new text. Otherwise, new text overwrites
2120 * any existing text.
2121 *
2122 * Defaults to off.
2123 *
2124 * @param {boolean} state True to set insert mode, false to unset.
2125 */
2126hterm.Terminal.prototype.setInsertMode = function(state) {
2127 this.options_.insertMode = state;
2128};
2129
2130/**
rginda87b86462011-12-14 13:48:03 -08002131 * Set the auto carriage return bit.
2132 *
2133 * If auto carriage return is on then a formfeed character is interpreted
2134 * as a newline, otherwise it's the same as a linefeed. The difference boils
2135 * down to whether or not the cursor column is reset.
2136 */
2137hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2138 this.options_.autoCarriageReturn = state;
2139};
2140
2141/**
rginda8ba33642011-12-14 12:31:31 -08002142 * Set the wraparound mode bit.
2143 *
2144 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2145 * to the start of the following row. Otherwise, the cursor is clamped to the
2146 * end of the screen and attempts to write past it are ignored.
2147 *
2148 * Defaults to on.
2149 *
2150 * @param {boolean} state True to set wraparound mode, false to unset.
2151 */
2152hterm.Terminal.prototype.setWraparound = function(state) {
2153 this.options_.wraparound = state;
2154};
2155
2156/**
2157 * Set the reverse-wraparound mode bit.
2158 *
2159 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2160 * to the end of the previous row. Otherwise, the cursor is clamped to column
2161 * 0.
2162 *
2163 * Defaults to off.
2164 *
2165 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2166 */
2167hterm.Terminal.prototype.setReverseWraparound = function(state) {
2168 this.options_.reverseWraparound = state;
2169};
2170
2171/**
2172 * Selects between the primary and alternate screens.
2173 *
2174 * If alternate mode is on, the alternate screen is active. Otherwise the
2175 * primary screen is active.
2176 *
2177 * Swapping screens has no effect on the scrollback buffer.
2178 *
2179 * Each screen maintains its own cursor position.
2180 *
2181 * Defaults to off.
2182 *
2183 * @param {boolean} state True to set alternate mode, false to unset.
2184 */
2185hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002186 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002187 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2188
rginda35c456b2012-02-09 17:29:05 -08002189 if (this.screen_.rowsArray.length &&
2190 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2191 // If the screen changed sizes while we were away, our rowIndexes may
2192 // be incorrect.
2193 var offset = this.scrollbackRows_.length;
2194 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002195 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002196 ary[i].rowIndex = offset + i;
2197 }
2198 }
rginda8ba33642011-12-14 12:31:31 -08002199
rginda35c456b2012-02-09 17:29:05 -08002200 this.realizeWidth_(this.screenSize.width);
2201 this.realizeHeight_(this.screenSize.height);
2202 this.scrollPort_.syncScrollHeight();
2203 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002204
rginda6d397402012-01-17 10:58:29 -08002205 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002206 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002207};
2208
2209/**
2210 * Set the cursor-blink mode bit.
2211 *
2212 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2213 * a visible cursor does not blink.
2214 *
2215 * You should make sure to turn blinking off if you're going to dispose of a
2216 * terminal, otherwise you'll leak a timeout.
2217 *
2218 * Defaults to on.
2219 *
2220 * @param {boolean} state True to set cursor-blink mode, false to unset.
2221 */
2222hterm.Terminal.prototype.setCursorBlink = function(state) {
2223 this.options_.cursorBlink = state;
2224
2225 if (!state && this.timeouts_.cursorBlink) {
2226 clearTimeout(this.timeouts_.cursorBlink);
2227 delete this.timeouts_.cursorBlink;
2228 }
2229
2230 if (this.options_.cursorVisible)
2231 this.setCursorVisible(true);
2232};
2233
2234/**
2235 * Set the cursor-visible mode bit.
2236 *
2237 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2238 *
2239 * Defaults to on.
2240 *
2241 * @param {boolean} state True to set cursor-visible mode, false to unset.
2242 */
2243hterm.Terminal.prototype.setCursorVisible = function(state) {
2244 this.options_.cursorVisible = state;
2245
2246 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002247 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002248 return;
2249 }
2250
rginda87b86462011-12-14 13:48:03 -08002251 this.syncCursorPosition_();
2252
2253 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002254
2255 if (this.options_.cursorBlink) {
2256 if (this.timeouts_.cursorBlink)
2257 return;
2258
2259 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2260 500);
2261 } else {
2262 if (this.timeouts_.cursorBlink) {
2263 clearTimeout(this.timeouts_.cursorBlink);
2264 delete this.timeouts_.cursorBlink;
2265 }
2266 }
2267};
2268
2269/**
rginda87b86462011-12-14 13:48:03 -08002270 * Synchronizes the visible cursor and document selection with the current
2271 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002272 */
2273hterm.Terminal.prototype.syncCursorPosition_ = function() {
2274 var topRowIndex = this.scrollPort_.getTopRowIndex();
2275 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2276 var cursorRowIndex = this.scrollbackRows_.length +
2277 this.screen_.cursorPosition.row;
2278
2279 if (cursorRowIndex > bottomRowIndex) {
2280 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002281 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002282 return;
2283 }
2284
2285 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002286 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2287 'px';
2288 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2289 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002290
2291 this.cursorNode_.setAttribute('title',
2292 '(' + this.screen_.cursorPosition.row +
2293 ', ' + this.screen_.cursorPosition.column +
2294 ')');
2295
2296 // Update the caret for a11y purposes.
2297 var selection = this.document_.getSelection();
2298 if (selection && selection.isCollapsed)
2299 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002300};
2301
Robert Gindafb1be6a2013-12-11 11:56:22 -08002302/**
2303 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2304 * and character cell dimensions.
2305 */
Robert Ginda830583c2013-08-07 13:20:46 -07002306hterm.Terminal.prototype.restyleCursor_ = function() {
2307 var shape = this.cursorShape_;
2308
2309 if (this.cursorNode_.getAttribute('focus') == 'false') {
2310 // Always show a block cursor when unfocused.
2311 shape = hterm.Terminal.cursorShape.BLOCK;
2312 }
2313
2314 var style = this.cursorNode_.style;
2315
Robert Gindafb1be6a2013-12-11 11:56:22 -08002316 style.width = this.scrollPort_.characterSize.width + 'px';
2317
Robert Ginda830583c2013-08-07 13:20:46 -07002318 switch (shape) {
2319 case hterm.Terminal.cursorShape.BEAM:
2320 style.height = this.scrollPort_.characterSize.height + 'px';
2321 style.backgroundColor = 'transparent';
2322 style.borderBottomStyle = null;
2323 style.borderLeftStyle = 'solid';
2324 break;
2325
2326 case hterm.Terminal.cursorShape.UNDERLINE:
2327 style.height = this.scrollPort_.characterSize.baseline + 'px';
2328 style.backgroundColor = 'transparent';
2329 style.borderBottomStyle = 'solid';
2330 // correct the size to put it exactly at the baseline
2331 style.borderLeftStyle = null;
2332 break;
2333
2334 default:
2335 style.height = this.scrollPort_.characterSize.height + 'px';
2336 style.backgroundColor = this.cursorColor_;
2337 style.borderBottomStyle = null;
2338 style.borderLeftStyle = null;
2339 break;
2340 }
2341};
2342
rginda8ba33642011-12-14 12:31:31 -08002343/**
2344 * Synchronizes the visible cursor with the current cursor coordinates.
2345 *
2346 * The sync will happen asynchronously, soon after the call stack winds down.
2347 * Multiple calls will be coalesced into a single sync.
2348 */
2349hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2350 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002351 return;
rginda8ba33642011-12-14 12:31:31 -08002352
2353 var self = this;
2354 this.timeouts_.syncCursor = setTimeout(function() {
2355 self.syncCursorPosition_();
2356 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002357 }, 0);
2358};
2359
rgindacc2996c2012-02-24 14:59:31 -08002360/**
rgindaf522ce02012-04-17 17:49:17 -07002361 * Show or hide the zoom warning.
2362 *
2363 * The zoom warning is a message warning the user that their browser zoom must
2364 * be set to 100% in order for hterm to function properly.
2365 *
2366 * @param {boolean} state True to show the message, false to hide it.
2367 */
2368hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2369 if (!this.zoomWarningNode_) {
2370 if (!state)
2371 return;
2372
2373 this.zoomWarningNode_ = this.document_.createElement('div');
2374 this.zoomWarningNode_.style.cssText = (
2375 'color: black;' +
2376 'background-color: #ff2222;' +
2377 'font-size: large;' +
2378 'border-radius: 8px;' +
2379 'opacity: 0.75;' +
2380 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2381 'top: 0.5em;' +
2382 'right: 1.2em;' +
2383 'position: absolute;' +
2384 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002385 '-webkit-user-select: none;' +
2386 '-moz-text-size-adjust: none;' +
2387 '-moz-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002388 }
2389
Robert Gindab4839c22013-02-28 16:52:10 -08002390 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2391 hterm.zoomWarningMessage,
2392 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2393
rgindaf522ce02012-04-17 17:49:17 -07002394 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2395
2396 if (state) {
2397 if (!this.zoomWarningNode_.parentNode)
2398 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2399 } else if (this.zoomWarningNode_.parentNode) {
2400 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2401 }
2402};
2403
2404/**
rgindacc2996c2012-02-24 14:59:31 -08002405 * Show the terminal overlay for a given amount of time.
2406 *
2407 * The terminal overlay appears in inverse video in a large font, centered
2408 * over the terminal. You should probably keep the overlay message brief,
2409 * since it's in a large font and you probably aren't going to check the size
2410 * of the terminal first.
2411 *
2412 * @param {string} msg The text (not HTML) message to display in the overlay.
2413 * @param {number} opt_timeout The amount of time to wait before fading out
2414 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2415 * stay up forever (or until the next overlay).
2416 */
2417hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002418 if (!this.overlayNode_) {
2419 if (!this.div_)
2420 return;
2421
2422 this.overlayNode_ = this.document_.createElement('div');
2423 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002424 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002425 'font-size: xx-large;' +
2426 'opacity: 0.75;' +
2427 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2428 'position: absolute;' +
2429 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002430 '-webkit-transition: opacity 180ms ease-in;' +
2431 '-moz-user-select: none;' +
2432 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002433
2434 this.overlayNode_.addEventListener('mousedown', function(e) {
2435 e.preventDefault();
2436 e.stopPropagation();
2437 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002438 }
2439
rginda9f5222b2012-03-05 11:53:28 -08002440 this.overlayNode_.style.color = this.prefs_.get('background-color');
2441 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2442 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2443
rgindaf0090c92012-02-10 14:58:52 -08002444 this.overlayNode_.textContent = msg;
2445 this.overlayNode_.style.opacity = '0.75';
2446
2447 if (!this.overlayNode_.parentNode)
2448 this.div_.appendChild(this.overlayNode_);
2449
Robert Ginda97769282013-02-01 15:30:30 -08002450 var divSize = hterm.getClientSize(this.div_);
2451 var overlaySize = hterm.getClientSize(this.overlayNode_);
2452
2453 this.overlayNode_.style.top = (divSize.height - overlaySize.height) / 2;
2454 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
2455 this.scrollPort_.currentScrollbarWidthPx) / 2;
rgindaf0090c92012-02-10 14:58:52 -08002456
2457 var self = this;
2458
2459 if (this.overlayTimeout_)
2460 clearTimeout(this.overlayTimeout_);
2461
rgindacc2996c2012-02-24 14:59:31 -08002462 if (opt_timeout === null)
2463 return;
2464
rgindaf0090c92012-02-10 14:58:52 -08002465 this.overlayTimeout_ = setTimeout(function() {
2466 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002467 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002468 if (self.overlayNode_.parentNode)
2469 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002470 self.overlayTimeout_ = null;
2471 self.overlayNode_.style.opacity = '0.75';
2472 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002473 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002474};
2475
rginda4bba5e12012-06-20 16:15:30 -07002476/**
2477 * Paste from the system clipboard to the terminal.
2478 */
2479hterm.Terminal.prototype.paste = function() {
2480 hterm.pasteFromClipboard(this.document_);
2481};
2482
2483/**
2484 * Copy a string to the system clipboard.
2485 *
2486 * Note: If there is a selected range in the terminal, it'll be cleared.
2487 */
2488hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002489 if (this.prefs_.get('enable-clipboard-notice'))
2490 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2491
rgindaa09e7332012-08-17 12:49:51 -07002492 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002493 copySource.textContent = str;
2494 copySource.style.cssText = (
2495 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002496 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002497 'position: absolute;' +
2498 'top: -99px');
2499
2500 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002501
rginda4bba5e12012-06-20 16:15:30 -07002502 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002503 var anchorNode = selection.anchorNode;
2504 var anchorOffset = selection.anchorOffset;
2505 var focusNode = selection.focusNode;
2506 var focusOffset = selection.focusOffset;
2507
rginda4bba5e12012-06-20 16:15:30 -07002508 selection.selectAllChildren(copySource);
2509
rgindaa09e7332012-08-17 12:49:51 -07002510 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002511
Rob Spies56953412014-04-28 14:09:47 -07002512 // IE doesn't support selection.extend. This means that the selection
2513 // won't return on IE.
2514 if (selection.extend) {
2515 selection.collapse(anchorNode, anchorOffset);
2516 selection.extend(focusNode, focusOffset);
2517 }
rgindafaa74742012-08-21 13:34:03 -07002518
rginda4bba5e12012-06-20 16:15:30 -07002519 copySource.parentNode.removeChild(copySource);
2520};
2521
rgindaa09e7332012-08-17 12:49:51 -07002522hterm.Terminal.prototype.getSelectionText = function() {
2523 var selection = this.scrollPort_.selection;
2524 selection.sync();
2525
2526 if (selection.isCollapsed)
2527 return null;
2528
2529
2530 // Start offset measures from the beginning of the line.
2531 var startOffset = selection.startOffset;
2532 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002533
Robert Gindafdbb3f22012-09-06 20:23:06 -07002534 if (node.nodeName != 'X-ROW') {
2535 // If the selection doesn't start on an x-row node, then it must be
2536 // somewhere inside the x-row. Add any characters from previous siblings
2537 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002538
2539 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2540 // If node is the text node in a styled span, move up to the span node.
2541 node = node.parentNode;
2542 }
2543
Robert Gindafdbb3f22012-09-06 20:23:06 -07002544 while (node.previousSibling) {
2545 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002546 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002547 }
rgindaa09e7332012-08-17 12:49:51 -07002548 }
2549
2550 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002551 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2552 selection.endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002553 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002554
Robert Gindafdbb3f22012-09-06 20:23:06 -07002555 if (node.nodeName != 'X-ROW') {
2556 // If the selection doesn't end on an x-row node, then it must be
2557 // somewhere inside the x-row. Add any characters from following siblings
2558 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002559
2560 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2561 // If node is the text node in a styled span, move up to the span node.
2562 node = node.parentNode;
2563 }
2564
Robert Gindafdbb3f22012-09-06 20:23:06 -07002565 while (node.nextSibling) {
2566 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002567 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002568 }
rgindaa09e7332012-08-17 12:49:51 -07002569 }
2570
2571 var rv = this.getRowsText(selection.startRow.rowIndex,
2572 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002573 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002574};
2575
rginda4bba5e12012-06-20 16:15:30 -07002576/**
2577 * Copy the current selection to the system clipboard, then clear it after a
2578 * short delay.
2579 */
2580hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002581 var text = this.getSelectionText();
2582 if (text != null)
2583 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002584};
2585
rgindaf0090c92012-02-10 14:58:52 -08002586hterm.Terminal.prototype.overlaySize = function() {
2587 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2588};
2589
rginda87b86462011-12-14 13:48:03 -08002590/**
2591 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2592 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002593 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002594 */
2595hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002596 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002597 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2598
Robert Ginda8cb7d902013-06-20 14:37:18 -07002599 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002600};
2601
2602/**
rgindad5613292012-06-19 15:40:37 -07002603 * Add the terminalRow and terminalColumn properties to mouse events and
2604 * then forward on to onMouse().
2605 *
2606 * The terminalRow and terminalColumn properties contain the (row, column)
2607 * coordinates for the mouse event.
2608 */
2609hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002610 if (e.processedByTerminalHandler_) {
2611 // We register our event handlers on the document, as well as the cursor
2612 // and the scroll blocker. Mouse events that occur on the cursor or
2613 // scroll blocker will also appear on the document, but we don't want to
2614 // process them twice.
2615 //
2616 // We can't just prevent bubbling because that has other side effects, so
2617 // we decorate the event object with this property instead.
2618 return;
2619 }
2620
2621 e.processedByTerminalHandler_ = true;
2622
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002623 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2624 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002625 return;
2626 }
2627
rgindad5613292012-06-19 15:40:37 -07002628 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2629 this.scrollPort_.characterSize.height) + 1;
2630 e.terminalColumn = parseInt(e.clientX /
2631 this.scrollPort_.characterSize.width) + 1;
2632
Robert Ginda928cf632014-03-05 15:07:41 -08002633 if (e.type == 'mousedown') {
2634 if (e.altKey || this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED) {
2635 // If VT mouse reporting is disabled, or has been defeated with
2636 // alt-mousedown, then the mouse will act on the local selection.
2637 this.reportMouseEvents_ = false;
2638 this.setSelectionEnabled(true);
2639 } else {
2640 // Otherwise we defer ownership of the mouse to the VT.
2641 this.reportMouseEvents_ = true;
Robert Ginda3ae37822014-05-15 13:05:35 -07002642 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08002643 this.setSelectionEnabled(false);
2644 e.preventDefault();
2645 }
2646 }
2647
2648 if (!this.reportMouseEvents_) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002649 if (e.type == 'dblclick') {
2650 this.screen_.expandSelection(this.document_.getSelection());
2651 hterm.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07002652 }
2653
Robert Ginda928cf632014-03-05 15:07:41 -08002654 if (e.type == 'mousedown' && e.which == this.mousePasteButton)
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002655 this.paste();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002656
2657 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2658 !this.document_.getSelection().isCollapsed) {
2659 hterm.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002660 }
2661
2662 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
2663 this.scrollBlockerNode_.engaged) {
2664 // Disengage the scroll-blocker after one of these events.
2665 this.scrollBlockerNode_.engaged = false;
2666 this.scrollBlockerNode_.style.top = '-99px';
2667 }
2668
Robert Ginda928cf632014-03-05 15:07:41 -08002669 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002670 if (!this.scrollBlockerNode_.engaged) {
2671 if (e.type == 'mousedown') {
2672 // Move the scroll-blocker into place if we want to keep the scrollport
2673 // from scrolling.
2674 this.scrollBlockerNode_.engaged = true;
2675 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2676 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2677 } else if (e.type == 'mousemove') {
2678 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
2679 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07002680 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002681 e.preventDefault();
2682 }
2683 }
Robert Ginda928cf632014-03-05 15:07:41 -08002684
2685 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002686 }
2687
Robert Ginda928cf632014-03-05 15:07:41 -08002688 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
2689 // Restore this on mouseup in case it was temporarily defeated with a
2690 // alt-mousedown. Only do this when the selection is empty so that
2691 // we don't immediately kill the users selection.
2692 this.reportMouseEvents_ = (this.vt.mouseReport !=
2693 this.vt.MOUSE_REPORT_DISABLED);
2694 }
rgindad5613292012-06-19 15:40:37 -07002695};
2696
2697/**
2698 * Clients should override this if they care to know about mouse events.
2699 *
2700 * The event parameter will be a normal DOM mouse click event with additional
2701 * 'terminalRow' and 'terminalColumn' properties.
2702 */
2703hterm.Terminal.prototype.onMouse = function(e) { };
2704
2705/**
rginda8e92a692012-05-20 19:37:20 -07002706 * React when focus changes.
2707 */
Rob Spies06533ba2014-04-24 11:20:37 -07002708hterm.Terminal.prototype.onFocusChange_ = function(focused) {
2709 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07002710 this.restyleCursor_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002711 if (focused === true)
2712 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07002713};
2714
2715/**
rginda8ba33642011-12-14 12:31:31 -08002716 * React when the ScrollPort is scrolled.
2717 */
2718hterm.Terminal.prototype.onScroll_ = function() {
2719 this.scheduleSyncCursorPosition_();
2720};
2721
2722/**
rginda9846e2f2012-01-27 13:53:33 -08002723 * React when text is pasted into the scrollPort.
2724 */
2725hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Ginda8cb7d902013-06-20 14:37:18 -07002726 this.onVTKeystroke(e.text.replace(/\n/mg, '\r'));
rginda9846e2f2012-01-27 13:53:33 -08002727};
2728
2729/**
rgindaa09e7332012-08-17 12:49:51 -07002730 * React when the user tries to copy from the scrollPort.
2731 */
2732hterm.Terminal.prototype.onCopy_ = function(e) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002733 e.preventDefault();
2734 setTimeout(this.copySelectionToClipboard.bind(this), 0);
rgindaa09e7332012-08-17 12:49:51 -07002735};
2736
2737/**
rginda8ba33642011-12-14 12:31:31 -08002738 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002739 *
2740 * Note: This function should not directly contain code that alters the internal
2741 * state of the terminal. That kind of code belongs in realizeWidth or
2742 * realizeHeight, so that it can be executed synchronously in the case of a
2743 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002744 */
2745hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08002746 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002747 this.scrollPort_.characterSize.width);
Robert Ginda19f61292014-03-04 14:07:57 -08002748 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
rginda35c456b2012-02-09 17:29:05 -08002749 this.scrollPort_.characterSize.height);
2750
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002751 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002752 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002753 // gets removed from the document or during the initial load, and we can't
2754 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002755 return;
2756 }
2757
rgindaa8ba17d2012-08-15 14:41:10 -07002758 var isNewSize = (columnCount != this.screenSize.width ||
2759 rowCount != this.screenSize.height);
2760
2761 // We do this even if the size didn't change, just to be sure everything is
2762 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002763 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002764 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002765
2766 if (isNewSize)
2767 this.overlaySize();
2768
Robert Gindafb1be6a2013-12-11 11:56:22 -08002769 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07002770 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002771};
2772
2773/**
2774 * Service the cursor blink timeout.
2775 */
2776hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Ginda830583c2013-08-07 13:20:46 -07002777 if (this.cursorNode_.getAttribute('focus') == 'false' ||
2778 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08002779 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002780 } else {
rginda87b86462011-12-14 13:48:03 -08002781 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002782 }
2783};
David Reveman8f552492012-03-28 12:18:41 -04002784
2785/**
2786 * Set the scrollbar-visible mode bit.
2787 *
2788 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2789 * Otherwise it will not.
2790 *
2791 * Defaults to on.
2792 *
2793 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2794 */
2795hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2796 this.scrollPort_.setScrollbarVisible(state);
2797};
Michael Kelly485ecd12014-06-09 11:41:56 -04002798
2799/**
2800 * Close all web notifications created by terminal bells.
2801 */
2802hterm.Terminal.prototype.closeBellNotifications_ = function() {
2803 this.bellNotificationList_.forEach(function(n) {
2804 n.close();
2805 });
2806 this.bellNotificationList_.length = 0;
2807};