blob: fbfae79997cf1d8dcdd667ebec57b98bba7c8680 [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
rginda6d397402012-01-17 10:58:29 -0800102 // Cursor position and attributes saved with DECSC.
103 this.savedOptions_ = {};
104
rginda8ba33642011-12-14 12:31:31 -0800105 // The current mode bits for the terminal.
106 this.options_ = new hterm.Options();
107
108 // Timeouts we might need to clear.
109 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800110
111 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800112 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800113
rgindafeaf3142012-01-31 15:14:20 -0800114 // The keyboard hander.
115 this.keyboard = new hterm.Keyboard(this);
116
rginda87b86462011-12-14 13:48:03 -0800117 // General IO interface that can be given to third parties without exposing
118 // the entire terminal object.
119 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800120
rgindad5613292012-06-19 15:40:37 -0700121 // True if mouse-click-drag should scroll the terminal.
122 this.enableMouseDragScroll = true;
123
Robert Ginda57f03b42012-09-13 11:02:48 -0700124 this.copyOnSelect = null;
rginda4bba5e12012-06-20 16:15:30 -0700125 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700126
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400127 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800128 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700129
130 this.setProfile(opt_profileId || 'default',
131 function() { this.onTerminalReady() }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800132};
133
134/**
Robert Ginda830583c2013-08-07 13:20:46 -0700135 * Possible cursor shapes.
136 */
137hterm.Terminal.cursorShape = {
138 BLOCK: 'BLOCK',
139 BEAM: 'BEAM',
140 UNDERLINE: 'UNDERLINE'
141};
142
143/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700144 * Clients should override this to be notified when the terminal is ready
145 * for use.
146 *
147 * The terminal initialization is asynchronous, and shouldn't be used before
148 * this method is called.
149 */
150hterm.Terminal.prototype.onTerminalReady = function() { };
151
152/**
rginda35c456b2012-02-09 17:29:05 -0800153 * Default tab with of 8 to match xterm.
154 */
155hterm.Terminal.prototype.tabWidth = 8;
156
157/**
rginda9f5222b2012-03-05 11:53:28 -0800158 * Select a preference profile.
159 *
160 * This will load the terminal preferences for the given profile name and
161 * associate subsequent preference changes with the new preference profile.
162 *
163 * @param {string} newName The name of the preference profile. Forward slash
164 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700165 * @param {function} opt_callback Optional callback to invoke when the profile
166 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800167 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700168hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
169 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800170
Robert Ginda57f03b42012-09-13 11:02:48 -0700171 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800172
Robert Ginda57f03b42012-09-13 11:02:48 -0700173 if (this.prefs_)
174 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800175
Robert Ginda57f03b42012-09-13 11:02:48 -0700176 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
177 this.prefs_.addObservers(null, {
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700178 'alt-backspace-is-meta-backspace': function(v) {
179 terminal.keyboard.altBackspaceIsMetaBackspace = v;
180 },
181
Robert Ginda57f03b42012-09-13 11:02:48 -0700182 'alt-is-meta': function(v) {
183 terminal.keyboard.altIsMeta = v;
184 },
185
186 'alt-sends-what': function(v) {
187 if (!/^(escape|8-bit|browser-key)$/.test(v))
188 v = 'escape';
189
190 terminal.keyboard.altSendsWhat = v;
191 },
192
193 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800194 var ary = v.match(/^lib-resource:(\S+)/);
195 if (ary) {
196 terminal.bellAudio_.setAttribute('src',
197 lib.resource.getDataUrl(ary[1]));
198 } else {
199 terminal.bellAudio_.setAttribute('src', v);
200 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700201 },
202
203 'background-color': function(v) {
204 terminal.setBackgroundColor(v);
205 },
206
207 'background-image': function(v) {
208 terminal.scrollPort_.setBackgroundImage(v);
209 },
210
211 'background-size': function(v) {
212 terminal.scrollPort_.setBackgroundSize(v);
213 },
214
215 'background-position': function(v) {
216 terminal.scrollPort_.setBackgroundPosition(v);
217 },
218
219 'backspace-sends-backspace': function(v) {
220 terminal.keyboard.backspaceSendsBackspace = v;
221 },
222
223 'cursor-blink': function(v) {
224 terminal.setCursorBlink(!!v);
225 },
226
227 'cursor-color': function(v) {
228 terminal.setCursorColor(v);
229 },
230
231 'color-palette-overrides': function(v) {
232 if (!(v == null || v instanceof Object || v instanceof Array)) {
233 console.warn('Preference color-palette-overrides is not an array or ' +
234 'object: ' + v);
235 return;
rginda9f5222b2012-03-05 11:53:28 -0800236 }
rginda9f5222b2012-03-05 11:53:28 -0800237
Robert Ginda57f03b42012-09-13 11:02:48 -0700238 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700239
Robert Ginda57f03b42012-09-13 11:02:48 -0700240 if (v) {
241 for (var key in v) {
242 var i = parseInt(key);
243 if (isNaN(i) || i < 0 || i > 255) {
244 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
245 continue;
246 }
247
248 if (v[i]) {
249 var rgb = lib.colors.normalizeCSS(v[i]);
250 if (rgb)
251 lib.colors.colorPalette[i] = rgb;
252 }
253 }
rginda30f20f62012-04-05 16:36:19 -0700254 }
rginda30f20f62012-04-05 16:36:19 -0700255
Robert Ginda57f03b42012-09-13 11:02:48 -0700256 terminal.primaryScreen_.textAttributes.resetColorPalette()
257 terminal.alternateScreen_.textAttributes.resetColorPalette();
258 },
rginda30f20f62012-04-05 16:36:19 -0700259
Robert Ginda57f03b42012-09-13 11:02:48 -0700260 'copy-on-select': function(v) {
261 terminal.copyOnSelect = !!v;
262 },
rginda9f5222b2012-03-05 11:53:28 -0800263
Robert Ginda7e5e9522014-03-14 12:23:58 -0700264 'ctrl-plus-minus-zero-zoom': function(v) {
265 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
266 },
267
Robert Gindafb5a3f92014-05-13 14:12:00 -0700268 'ctrl-c-copy': function(v) {
269 terminal.keyboard.ctrlCCopy = v;
270 },
271
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100272 'ctrl-v-paste': function(v) {
273 terminal.keyboard.ctrlVPaste = v;
274 },
275
Masaya Suzuki273aa982014-05-31 07:25:55 +0900276 'east-asian-ambiguous-as-two-column': function(v) {
277 lib.wc.regardCjkAmbiguous = v;
278 },
279
Robert Ginda57f03b42012-09-13 11:02:48 -0700280 'enable-8-bit-control': function(v) {
281 terminal.vt.enable8BitControl = !!v;
282 },
rginda30f20f62012-04-05 16:36:19 -0700283
Robert Ginda57f03b42012-09-13 11:02:48 -0700284 'enable-bold': function(v) {
285 terminal.syncBoldSafeState();
286 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400287
Robert Ginda3e278d72014-03-25 13:18:51 -0700288 'enable-bold-as-bright': function(v) {
289 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
290 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
291 },
292
Robert Ginda57f03b42012-09-13 11:02:48 -0700293 'enable-clipboard-write': function(v) {
294 terminal.vt.enableClipboardWrite = !!v;
295 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400296
Robert Ginda3755e752013-05-31 13:34:09 -0700297 'enable-dec12': function(v) {
298 terminal.vt.enableDec12 = !!v;
299 },
300
Robert Ginda57f03b42012-09-13 11:02:48 -0700301 'font-family': function(v) {
302 terminal.syncFontFamily();
303 },
rginda30f20f62012-04-05 16:36:19 -0700304
Robert Ginda57f03b42012-09-13 11:02:48 -0700305 'font-size': function(v) {
306 terminal.setFontSize(v);
307 },
rginda9875d902012-08-20 16:21:57 -0700308
Robert Ginda57f03b42012-09-13 11:02:48 -0700309 'font-smoothing': function(v) {
310 terminal.syncFontFamily();
311 },
rgindade84e382012-04-20 15:39:31 -0700312
Robert Ginda57f03b42012-09-13 11:02:48 -0700313 'foreground-color': function(v) {
314 terminal.setForegroundColor(v);
315 },
rginda30f20f62012-04-05 16:36:19 -0700316
Robert Ginda57f03b42012-09-13 11:02:48 -0700317 'home-keys-scroll': function(v) {
318 terminal.keyboard.homeKeysScroll = v;
319 },
rginda4bba5e12012-06-20 16:15:30 -0700320
Robert Ginda57f03b42012-09-13 11:02:48 -0700321 'max-string-sequence': function(v) {
322 terminal.vt.maxStringSequence = v;
323 },
rginda11057d52012-04-25 12:29:56 -0700324
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700325 'media-keys-are-fkeys': function(v) {
326 terminal.keyboard.mediaKeysAreFKeys = v;
327 },
328
Robert Ginda57f03b42012-09-13 11:02:48 -0700329 'meta-sends-escape': function(v) {
330 terminal.keyboard.metaSendsEscape = v;
331 },
rginda30f20f62012-04-05 16:36:19 -0700332
Robert Ginda57f03b42012-09-13 11:02:48 -0700333 'mouse-paste-button': function(v) {
334 terminal.syncMousePasteButton();
335 },
rgindaa8ba17d2012-08-15 14:41:10 -0700336
Robert Gindae76aa9f2014-03-14 12:29:12 -0700337 'page-keys-scroll': function(v) {
338 terminal.keyboard.pageKeysScroll = v;
339 },
340
Robert Ginda40932892012-12-10 17:26:40 -0800341 'pass-alt-number': function(v) {
342 if (v == null) {
343 var osx = window.navigator.userAgent.match(/Mac OS X/);
344
345 // Let Alt-1..9 pass to the browser (to control tab switching) on
346 // non-OS X systems, or if hterm is not opened in an app window.
347 v = (!osx && hterm.windowType != 'popup');
348 }
349
350 terminal.passAltNumber = v;
351 },
352
353 'pass-ctrl-number': function(v) {
354 if (v == null) {
355 var osx = window.navigator.userAgent.match(/Mac OS X/);
356
357 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
358 // non-OS X systems, or if hterm is not opened in an app window.
359 v = (!osx && hterm.windowType != 'popup');
360 }
361
362 terminal.passCtrlNumber = v;
363 },
364
365 'pass-meta-number': function(v) {
366 if (v == null) {
367 var osx = window.navigator.userAgent.match(/Mac OS X/);
368
369 // Let Meta-1..9 pass to the browser (to control tab switching) on
370 // OS X systems, or if hterm is not opened in an app window.
371 v = (osx && hterm.windowType != 'popup');
372 }
373
374 terminal.passMetaNumber = v;
375 },
376
Marius Schilder77857b32014-05-14 16:21:26 -0700377 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700378 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700379 },
380
Robert Ginda8cb7d902013-06-20 14:37:18 -0700381 'receive-encoding': function(v) {
382 if (!(/^(utf-8|raw)$/).test(v)) {
383 console.warn('Invalid value for "receive-encoding": ' + v);
384 v = 'utf-8';
385 }
386
387 terminal.vt.characterEncoding = v;
388 },
389
Robert Ginda57f03b42012-09-13 11:02:48 -0700390 'scroll-on-keystroke': function(v) {
391 terminal.scrollOnKeystroke_ = v;
392 },
rginda9f5222b2012-03-05 11:53:28 -0800393
Robert Ginda57f03b42012-09-13 11:02:48 -0700394 'scroll-on-output': function(v) {
395 terminal.scrollOnOutput_ = v;
396 },
rginda30f20f62012-04-05 16:36:19 -0700397
Robert Ginda57f03b42012-09-13 11:02:48 -0700398 'scrollbar-visible': function(v) {
399 terminal.setScrollbarVisible(v);
400 },
rginda9f5222b2012-03-05 11:53:28 -0800401
Robert Ginda8cb7d902013-06-20 14:37:18 -0700402 'send-encoding': function(v) {
403 if (!(/^(utf-8|raw)$/).test(v)) {
404 console.warn('Invalid value for "send-encoding": ' + v);
405 v = 'utf-8';
406 }
407
408 terminal.keyboard.characterEncoding = v;
409 },
410
Robert Ginda57f03b42012-09-13 11:02:48 -0700411 'shift-insert-paste': function(v) {
412 terminal.keyboard.shiftInsertPaste = v;
413 },
rginda9f5222b2012-03-05 11:53:28 -0800414
Robert Gindae76aa9f2014-03-14 12:29:12 -0700415 'user-css': function(v) {
416 terminal.scrollPort_.setUserCss(v);
Robert Ginda57f03b42012-09-13 11:02:48 -0700417 }
418 });
rginda30f20f62012-04-05 16:36:19 -0700419
Robert Ginda57f03b42012-09-13 11:02:48 -0700420 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800421 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700422
423 if (opt_callback)
424 opt_callback();
425 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800426};
427
Rob Spies56953412014-04-28 14:09:47 -0700428
429/**
430 * Returns the preferences manager used for configuring this terminal.
431 */
432hterm.Terminal.prototype.getPrefs = function() {
433 return this.prefs_;
434};
435
436
rginda8e92a692012-05-20 19:37:20 -0700437/**
438 * Set the color for the cursor.
439 *
440 * If you want this setting to persist, set it through prefs_, rather than
441 * with this method.
442 */
443hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700444 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700445 this.cursorNode_.style.backgroundColor = color;
446 this.cursorNode_.style.borderColor = color;
447};
448
449/**
450 * Return the current cursor color as a string.
451 */
452hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700453 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700454};
455
456/**
rgindad5613292012-06-19 15:40:37 -0700457 * Enable or disable mouse based text selection in the terminal.
458 */
459hterm.Terminal.prototype.setSelectionEnabled = function(state) {
460 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700461};
462
463/**
rginda8e92a692012-05-20 19:37:20 -0700464 * Set the background color.
465 *
466 * If you want this setting to persist, set it through prefs_, rather than
467 * with this method.
468 */
469hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700470 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700471 this.primaryScreen_.textAttributes.setDefaults(
472 this.foregroundColor_, this.backgroundColor_);
473 this.alternateScreen_.textAttributes.setDefaults(
474 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700475 this.scrollPort_.setBackgroundColor(color);
476};
477
rginda9f5222b2012-03-05 11:53:28 -0800478/**
479 * Return the current terminal background color.
480 *
481 * Intended for use by other classes, so we don't have to expose the entire
482 * prefs_ object.
483 */
484hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700485 return this.backgroundColor_;
486};
487
488/**
489 * Set the foreground color.
490 *
491 * If you want this setting to persist, set it through prefs_, rather than
492 * with this method.
493 */
494hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700495 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700496 this.primaryScreen_.textAttributes.setDefaults(
497 this.foregroundColor_, this.backgroundColor_);
498 this.alternateScreen_.textAttributes.setDefaults(
499 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700500 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800501};
502
503/**
504 * Return the current terminal foreground color.
505 *
506 * Intended for use by other classes, so we don't have to expose the entire
507 * prefs_ object.
508 */
509hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700510 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800511};
512
513/**
rginda87b86462011-12-14 13:48:03 -0800514 * Create a new instance of a terminal command and run it with a given
515 * argument string.
516 *
517 * @param {function} commandClass The constructor for a terminal command.
518 * @param {string} argString The argument string to pass to the command.
519 */
520hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700521 var environment = this.prefs_.get('environment');
522 if (typeof environment != 'object' || environment == null)
523 environment = {};
524
rginda87b86462011-12-14 13:48:03 -0800525 var self = this;
526 this.command = new commandClass(
527 { argString: argString || '',
528 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700529 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800530 onExit: function(code) {
531 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800532 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700533 if (self.prefs_.get('close-on-exit'))
534 window.close();
rginda87b86462011-12-14 13:48:03 -0800535 }
536 });
537
rgindafeaf3142012-01-31 15:14:20 -0800538 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800539 this.command.run();
540};
541
542/**
rgindafeaf3142012-01-31 15:14:20 -0800543 * Returns true if the current screen is the primary screen, false otherwise.
544 */
545hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700546 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800547};
548
549/**
550 * Install the keyboard handler for this terminal.
551 *
552 * This will prevent the browser from seeing any keystrokes sent to the
553 * terminal.
554 */
555hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700556 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800557}
558
559/**
560 * Uninstall the keyboard handler for this terminal.
561 */
562hterm.Terminal.prototype.uninstallKeyboard = function() {
563 this.keyboard.installKeyboard(null);
564}
565
566/**
rginda35c456b2012-02-09 17:29:05 -0800567 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800568 *
569 * Call setFontSize(0) to reset to the default font size.
570 *
571 * This function does not modify the font-size preference.
572 *
573 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800574 */
575hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800576 if (px === 0)
577 px = this.prefs_.get('font-size');
578
rginda35c456b2012-02-09 17:29:05 -0800579 this.scrollPort_.setFontSize(px);
Ricky Liang48f05cb2013-12-31 23:35:29 +0800580 if (this.wcCssRule_) {
581 this.wcCssRule_.style.width = this.scrollPort_.characterSize.width * 2 +
582 'px';
583 }
rginda35c456b2012-02-09 17:29:05 -0800584};
585
586/**
587 * Get the current font size.
588 */
589hterm.Terminal.prototype.getFontSize = function() {
590 return this.scrollPort_.getFontSize();
591};
592
593/**
rginda8e92a692012-05-20 19:37:20 -0700594 * Get the current font family.
595 */
596hterm.Terminal.prototype.getFontFamily = function() {
597 return this.scrollPort_.getFontFamily();
598};
599
600/**
rginda35c456b2012-02-09 17:29:05 -0800601 * Set the CSS "font-family" for this terminal.
602 */
rginda9f5222b2012-03-05 11:53:28 -0800603hterm.Terminal.prototype.syncFontFamily = function() {
604 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
605 this.prefs_.get('font-smoothing'));
606 this.syncBoldSafeState();
607};
608
rginda4bba5e12012-06-20 16:15:30 -0700609/**
610 * Set this.mousePasteButton based on the mouse-paste-button pref,
611 * autodetecting if necessary.
612 */
613hterm.Terminal.prototype.syncMousePasteButton = function() {
614 var button = this.prefs_.get('mouse-paste-button');
615 if (typeof button == 'number') {
616 this.mousePasteButton = button;
617 return;
618 }
619
620 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
621 if (!ary || ary[2] == 'CrOS') {
622 this.mousePasteButton = 2;
623 } else {
624 this.mousePasteButton = 3;
625 }
626};
627
628/**
629 * Enable or disable bold based on the enable-bold pref, autodetecting if
630 * necessary.
631 */
rginda9f5222b2012-03-05 11:53:28 -0800632hterm.Terminal.prototype.syncBoldSafeState = function() {
633 var enableBold = this.prefs_.get('enable-bold');
634 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700635 this.primaryScreen_.textAttributes.enableBold = enableBold;
636 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800637 return;
638 }
639
rgindaf7521392012-02-28 17:20:34 -0800640 var normalSize = this.scrollPort_.measureCharacterSize();
641 var boldSize = this.scrollPort_.measureCharacterSize('bold');
642
643 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800644 if (!isBoldSafe) {
645 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700646 'from normal. Font family is: ' +
647 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800648 }
rginda9f5222b2012-03-05 11:53:28 -0800649
Robert Gindaed016262012-10-26 16:27:09 -0700650 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
651 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800652};
653
654/**
rginda87b86462011-12-14 13:48:03 -0800655 * Return a copy of the current cursor position.
656 *
657 * @return {hterm.RowCol} The RowCol object representing the current position.
658 */
659hterm.Terminal.prototype.saveCursor = function() {
660 return this.screen_.cursorPosition.clone();
661};
662
rgindaa19afe22012-01-25 15:40:22 -0800663hterm.Terminal.prototype.getTextAttributes = function() {
664 return this.screen_.textAttributes;
665};
666
rginda1a09aa02012-06-18 21:11:25 -0700667hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
668 this.screen_.textAttributes = textAttributes;
669};
670
rginda87b86462011-12-14 13:48:03 -0800671/**
rgindaf522ce02012-04-17 17:49:17 -0700672 * Return the current browser zoom factor applied to the terminal.
673 *
674 * @return {number} The current browser zoom factor.
675 */
676hterm.Terminal.prototype.getZoomFactor = function() {
677 return this.scrollPort_.characterSize.zoomFactor;
678};
679
680/**
rginda9846e2f2012-01-27 13:53:33 -0800681 * Change the title of this terminal's window.
682 */
683hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800684 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800685};
686
687/**
rginda87b86462011-12-14 13:48:03 -0800688 * Restore a previously saved cursor position.
689 *
690 * @param {hterm.RowCol} cursor The position to restore.
691 */
692hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700693 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
694 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800695 this.screen_.setCursorPosition(row, column);
696 if (cursor.column > column ||
697 cursor.column == column && cursor.overflow) {
698 this.screen_.cursorPosition.overflow = true;
699 }
rginda87b86462011-12-14 13:48:03 -0800700};
701
702/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400703 * Clear the cursor's overflow flag.
704 */
705hterm.Terminal.prototype.clearCursorOverflow = function() {
706 this.screen_.cursorPosition.overflow = false;
707};
708
709/**
Robert Ginda830583c2013-08-07 13:20:46 -0700710 * Sets the cursor shape
711 */
712hterm.Terminal.prototype.setCursorShape = function(shape) {
713 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800714 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700715}
716
717/**
718 * Get the cursor shape
719 */
720hterm.Terminal.prototype.getCursorShape = function() {
721 return this.cursorShape_;
722}
723
724/**
rginda87b86462011-12-14 13:48:03 -0800725 * Set the width of the terminal, resizing the UI to match.
726 */
727hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800728 if (columnCount == null) {
729 this.div_.style.width = '100%';
730 return;
731 }
732
rginda35c456b2012-02-09 17:29:05 -0800733 this.div_.style.width = this.scrollPort_.characterSize.width *
Robert Ginda97769282013-02-01 15:30:30 -0800734 columnCount + this.scrollPort_.currentScrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400735 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800736 this.scheduleSyncCursorPosition_();
737};
rginda87b86462011-12-14 13:48:03 -0800738
rgindac9bc5502012-01-18 11:48:44 -0800739/**
rginda35c456b2012-02-09 17:29:05 -0800740 * Set the height of the terminal, resizing the UI to match.
741 */
742hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800743 if (rowCount == null) {
744 this.div_.style.height = '100%';
745 return;
746 }
747
rginda35c456b2012-02-09 17:29:05 -0800748 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700749 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800750 this.realizeSize_(this.screenSize.width, rowCount);
751 this.scheduleSyncCursorPosition_();
752};
753
754/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400755 * Deal with terminal size changes.
756 *
757 */
758hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
759 if (columnCount != this.screenSize.width)
760 this.realizeWidth_(columnCount);
761
762 if (rowCount != this.screenSize.height)
763 this.realizeHeight_(rowCount);
764
765 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700766 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400767};
768
769/**
rgindac9bc5502012-01-18 11:48:44 -0800770 * Deal with terminal width changes.
771 *
772 * This function does what needs to be done when the terminal width changes
773 * out from under us. It happens here rather than in onResize_() because this
774 * code may need to run synchronously to handle programmatic changes of
775 * terminal width.
776 *
777 * Relying on the browser to send us an async resize event means we may not be
778 * in the correct state yet when the next escape sequence hits.
779 */
780hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700781 if (columnCount <= 0)
782 throw new Error('Attempt to realize bad width: ' + columnCount);
783
rgindac9bc5502012-01-18 11:48:44 -0800784 var deltaColumns = columnCount - this.screen_.getWidth();
785
rginda87b86462011-12-14 13:48:03 -0800786 this.screenSize.width = columnCount;
787 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800788
789 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400790 if (this.defaultTabStops)
791 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800792 } else {
793 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400794 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800795 break;
796
797 this.tabStops_.pop();
798 }
799 }
800
801 this.screen_.setColumnCount(this.screenSize.width);
802};
803
804/**
805 * Deal with terminal height changes.
806 *
807 * This function does what needs to be done when the terminal height changes
808 * out from under us. It happens here rather than in onResize_() because this
809 * code may need to run synchronously to handle programmatic changes of
810 * terminal height.
811 *
812 * Relying on the browser to send us an async resize event means we may not be
813 * in the correct state yet when the next escape sequence hits.
814 */
815hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700816 if (rowCount <= 0)
817 throw new Error('Attempt to realize bad height: ' + rowCount);
818
rgindac9bc5502012-01-18 11:48:44 -0800819 var deltaRows = rowCount - this.screen_.getHeight();
820
821 this.screenSize.height = rowCount;
822
823 var cursor = this.saveCursor();
824
825 if (deltaRows < 0) {
826 // Screen got smaller.
827 deltaRows *= -1;
828 while (deltaRows) {
829 var lastRow = this.getRowCount() - 1;
830 if (lastRow - this.scrollbackRows_.length == cursor.row)
831 break;
832
833 if (this.getRowText(lastRow))
834 break;
835
836 this.screen_.popRow();
837 deltaRows--;
838 }
839
840 var ary = this.screen_.shiftRows(deltaRows);
841 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
842
843 // We just removed rows from the top of the screen, we need to update
844 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800845 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800846 } else if (deltaRows > 0) {
847 // Screen got larger.
848
849 if (deltaRows <= this.scrollbackRows_.length) {
850 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
851 var rows = this.scrollbackRows_.splice(
852 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
853 this.screen_.unshiftRows(rows);
854 deltaRows -= scrollbackCount;
855 cursor.row += scrollbackCount;
856 }
857
858 if (deltaRows)
859 this.appendRows_(deltaRows);
860 }
861
rginda35c456b2012-02-09 17:29:05 -0800862 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800863 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800864};
865
866/**
867 * Scroll the terminal to the top of the scrollback buffer.
868 */
869hterm.Terminal.prototype.scrollHome = function() {
870 this.scrollPort_.scrollRowToTop(0);
871};
872
873/**
874 * Scroll the terminal to the end.
875 */
876hterm.Terminal.prototype.scrollEnd = function() {
877 this.scrollPort_.scrollRowToBottom(this.getRowCount());
878};
879
880/**
881 * Scroll the terminal one page up (minus one line) relative to the current
882 * position.
883 */
884hterm.Terminal.prototype.scrollPageUp = function() {
885 var i = this.scrollPort_.getTopRowIndex();
886 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
887};
888
889/**
890 * Scroll the terminal one page down (minus one line) relative to the current
891 * position.
892 */
893hterm.Terminal.prototype.scrollPageDown = function() {
894 var i = this.scrollPort_.getTopRowIndex();
895 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800896};
897
rgindac9bc5502012-01-18 11:48:44 -0800898/**
Robert Ginda40932892012-12-10 17:26:40 -0800899 * Clear primary screen, secondary screen, and the scrollback buffer.
900 */
901hterm.Terminal.prototype.wipeContents = function() {
902 this.scrollbackRows_.length = 0;
903 this.scrollPort_.resetCache();
904
905 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
906 var bottom = screen.getHeight();
907 if (bottom > 0) {
908 this.renumberRows_(0, bottom);
909 this.clearHome(screen);
910 }
911 }.bind(this));
912
913 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -0700914 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -0800915};
916
917/**
rgindac9bc5502012-01-18 11:48:44 -0800918 * Full terminal reset.
919 */
rginda87b86462011-12-14 13:48:03 -0800920hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800921 this.clearAllTabStops();
922 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700923
924 this.clearHome(this.primaryScreen_);
925 this.primaryScreen_.textAttributes.reset();
926
927 this.clearHome(this.alternateScreen_);
928 this.alternateScreen_.textAttributes.reset();
929
rgindab8bc8932012-04-27 12:45:03 -0700930 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
931
Robert Ginda92e18102013-03-14 13:56:37 -0700932 this.vt.reset();
933
rgindac9bc5502012-01-18 11:48:44 -0800934 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800935};
936
rgindac9bc5502012-01-18 11:48:44 -0800937/**
938 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700939 *
940 * Perform a soft reset to the default values listed in
941 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800942 */
rginda0f5c0292012-01-13 11:00:13 -0800943hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700944 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800945 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700946
rgindab8bc8932012-04-27 12:45:03 -0700947 // Xterm also resets the color palette on soft reset, even though it doesn't
948 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700949 this.primaryScreen_.textAttributes.resetColorPalette();
950 this.alternateScreen_.textAttributes.resetColorPalette();
951
rgindab8bc8932012-04-27 12:45:03 -0700952 // The xterm man page explicitly says this will happen on soft reset.
953 this.setVTScrollRegion(null, null);
954
955 // Xterm also shows the cursor on soft reset, but does not alter the blink
956 // state.
rgindaa19afe22012-01-25 15:40:22 -0800957 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800958};
959
rgindac9bc5502012-01-18 11:48:44 -0800960/**
961 * Move the cursor forward to the next tab stop, or to the last column
962 * if no more tab stops are set.
963 */
964hterm.Terminal.prototype.forwardTabStop = function() {
965 var column = this.screen_.cursorPosition.column;
966
967 for (var i = 0; i < this.tabStops_.length; i++) {
968 if (this.tabStops_[i] > column) {
969 this.setCursorColumn(this.tabStops_[i]);
970 return;
971 }
972 }
973
David Benjamin66e954d2012-05-05 21:08:12 -0400974 // xterm does not clear the overflow flag on HT or CHT.
975 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800976 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400977 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800978};
979
rgindac9bc5502012-01-18 11:48:44 -0800980/**
981 * Move the cursor backward to the previous tab stop, or to the first column
982 * if no previous tab stops are set.
983 */
984hterm.Terminal.prototype.backwardTabStop = function() {
985 var column = this.screen_.cursorPosition.column;
986
987 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
988 if (this.tabStops_[i] < column) {
989 this.setCursorColumn(this.tabStops_[i]);
990 return;
991 }
992 }
993
994 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800995};
996
rgindac9bc5502012-01-18 11:48:44 -0800997/**
998 * Set a tab stop at the given column.
999 *
1000 * @param {int} column Zero based column.
1001 */
1002hterm.Terminal.prototype.setTabStop = function(column) {
1003 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1004 if (this.tabStops_[i] == column)
1005 return;
1006
1007 if (this.tabStops_[i] < column) {
1008 this.tabStops_.splice(i + 1, 0, column);
1009 return;
1010 }
1011 }
1012
1013 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001014};
1015
rgindac9bc5502012-01-18 11:48:44 -08001016/**
1017 * Clear the tab stop at the current cursor position.
1018 *
1019 * No effect if there is no tab stop at the current cursor position.
1020 */
1021hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1022 var column = this.screen_.cursorPosition.column;
1023
1024 var i = this.tabStops_.indexOf(column);
1025 if (i == -1)
1026 return;
1027
1028 this.tabStops_.splice(i, 1);
1029};
1030
1031/**
1032 * Clear all tab stops.
1033 */
1034hterm.Terminal.prototype.clearAllTabStops = function() {
1035 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001036 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001037};
1038
1039/**
1040 * Set up the default tab stops, starting from a given column.
1041 *
1042 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001043 * from the specified column, or 0 if no column is provided. It also flags
1044 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001045 *
1046 * This does not clear the existing tab stops first, use clearAllTabStops
1047 * for that.
1048 *
1049 * @param {int} opt_start Optional starting zero based starting column, useful
1050 * for filling out missing tab stops when the terminal is resized.
1051 */
1052hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1053 var start = opt_start || 0;
1054 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001055 // Round start up to a default tab stop.
1056 start = start - 1 - ((start - 1) % w) + w;
1057 for (var i = start; i < this.screenSize.width; i += w) {
1058 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001059 }
David Benjamin66e954d2012-05-05 21:08:12 -04001060
1061 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001062};
1063
rginda6d397402012-01-17 10:58:29 -08001064/**
rginda8ba33642011-12-14 12:31:31 -08001065 * Interpret a sequence of characters.
1066 *
1067 * Incomplete escape sequences are buffered until the next call.
1068 *
1069 * @param {string} str Sequence of characters to interpret or pass through.
1070 */
1071hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001072 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001073 this.scheduleSyncCursorPosition_();
1074};
1075
1076/**
1077 * Take over the given DIV for use as the terminal display.
1078 *
1079 * @param {HTMLDivElement} div The div to use as the terminal display.
1080 */
1081hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001082 this.div_ = div;
1083
rginda8ba33642011-12-14 12:31:31 -08001084 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001085 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001086 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1087 this.scrollPort_.setBackgroundPosition(
1088 this.prefs_.get('background-position'));
Robert Gindae76aa9f2014-03-14 12:29:12 -07001089 this.scrollPort_.setUserCss(this.prefs_.get('user-css'));
rginda30f20f62012-04-05 16:36:19 -07001090
rginda0918b652012-04-04 11:26:24 -07001091 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001092
rginda9f5222b2012-03-05 11:53:28 -08001093 this.setFontSize(this.prefs_.get('font-size'));
1094 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001095
David Reveman8f552492012-03-28 12:18:41 -04001096 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1097
rginda8ba33642011-12-14 12:31:31 -08001098 this.document_ = this.scrollPort_.getDocument();
1099
rginda4bba5e12012-06-20 16:15:30 -07001100 this.document_.body.oncontextmenu = function() { return false };
1101
1102 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001103 var screenNode = this.scrollPort_.getScreenNode();
1104 screenNode.addEventListener('mousedown', onMouse);
1105 screenNode.addEventListener('mouseup', onMouse);
1106 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001107 this.scrollPort_.onScrollWheel = onMouse;
1108
Toni Barzic0bfa8922013-11-22 11:18:35 -08001109 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001110 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001111 // Listen for mousedown events on the screenNode as in FF the focus
1112 // events don't bubble.
1113 screenNode.addEventListener('mousedown', function() {
1114 setTimeout(this.onFocusChange_.bind(this, true));
1115 }.bind(this));
1116
Toni Barzic0bfa8922013-11-22 11:18:35 -08001117 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001118 'blur', this.onFocusChange_.bind(this, false));
1119
1120 var style = this.document_.createElement('style');
1121 style.textContent =
1122 ('.cursor-node[focus="false"] {' +
1123 ' box-sizing: border-box;' +
1124 ' background-color: transparent !important;' +
1125 ' border-width: 2px;' +
1126 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001127 '}' +
1128 '.wc-node {' +
1129 ' display: inline-block;' +
1130 ' text-align: center;' +
1131 ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001132 '}');
1133 this.document_.head.appendChild(style);
1134
Ricky Liang48f05cb2013-12-31 23:35:29 +08001135 var styleSheets = this.document_.styleSheets;
1136 var cssRules = styleSheets[styleSheets.length - 1].cssRules;
1137 this.wcCssRule_ = cssRules[cssRules.length - 1];
1138
rginda8ba33642011-12-14 12:31:31 -08001139 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001140 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001141 this.cursorNode_.style.cssText =
1142 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001143 'top: -99px;' +
1144 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001145 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1146 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
Rob Spies06533ba2014-04-24 11:20:37 -07001147 '-webkit-transition: opacity, background-color 100ms linear;' +
1148 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001149
rginda8e92a692012-05-20 19:37:20 -07001150 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001151 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1152 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001153
rginda8ba33642011-12-14 12:31:31 -08001154 this.document_.body.appendChild(this.cursorNode_);
1155
rgindad5613292012-06-19 15:40:37 -07001156 // When 'enableMouseDragScroll' is off we reposition this element directly
1157 // under the mouse cursor after a click. This makes Chrome associate
1158 // subsequent mousemove events with the scroll-blocker. Since the
1159 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1160 // events do not cause the scrollport to scroll.
1161 //
1162 // It's a hack, but it's the cleanest way I could find.
1163 this.scrollBlockerNode_ = this.document_.createElement('div');
1164 this.scrollBlockerNode_.style.cssText =
1165 ('position: absolute;' +
1166 'top: -99px;' +
1167 'display: block;' +
1168 'width: 10px;' +
1169 'height: 10px;');
1170 this.document_.body.appendChild(this.scrollBlockerNode_);
1171
1172 var onMouse = this.onMouse_.bind(this);
1173 this.scrollPort_.onScrollWheel = onMouse;
1174 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1175 ].forEach(function(event) {
1176 this.scrollBlockerNode_.addEventListener(event, onMouse);
1177 this.cursorNode_.addEventListener(event, onMouse);
1178 this.document_.addEventListener(event, onMouse);
1179 }.bind(this));
1180
1181 this.cursorNode_.addEventListener('mousedown', function() {
1182 setTimeout(this.focus.bind(this));
1183 }.bind(this));
1184
rginda8ba33642011-12-14 12:31:31 -08001185 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001186
rginda87b86462011-12-14 13:48:03 -08001187 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001188 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001189};
1190
rginda0918b652012-04-04 11:26:24 -07001191/**
1192 * Return the HTML document that contains the terminal DOM nodes.
1193 */
rginda87b86462011-12-14 13:48:03 -08001194hterm.Terminal.prototype.getDocument = function() {
1195 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001196};
1197
1198/**
rginda0918b652012-04-04 11:26:24 -07001199 * Focus the terminal.
1200 */
1201hterm.Terminal.prototype.focus = function() {
1202 this.scrollPort_.focus();
1203};
1204
1205/**
rginda8ba33642011-12-14 12:31:31 -08001206 * Return the HTML Element for a given row index.
1207 *
1208 * This is a method from the RowProvider interface. The ScrollPort uses
1209 * it to fetch rows on demand as they are scrolled into view.
1210 *
1211 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1212 * pairs to conserve memory.
1213 *
1214 * @param {integer} index The zero-based row index, measured relative to the
1215 * start of the scrollback buffer. On-screen rows will always have the
1216 * largest indicies.
1217 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1218 */
1219hterm.Terminal.prototype.getRowNode = function(index) {
1220 if (index < this.scrollbackRows_.length)
1221 return this.scrollbackRows_[index];
1222
1223 var screenIndex = index - this.scrollbackRows_.length;
1224 return this.screen_.rowsArray[screenIndex];
1225};
1226
1227/**
1228 * Return the text content for a given range of rows.
1229 *
1230 * This is a method from the RowProvider interface. The ScrollPort uses
1231 * it to fetch text content on demand when the user attempts to copy their
1232 * selection to the clipboard.
1233 *
1234 * @param {integer} start The zero-based row index to start from, measured
1235 * relative to the start of the scrollback buffer. On-screen rows will
1236 * always have the largest indicies.
1237 * @param {integer} end The zero-based row index to end on, measured
1238 * relative to the start of the scrollback buffer.
1239 * @return {string} A single string containing the text value of the range of
1240 * rows. Lines will be newline delimited, with no trailing newline.
1241 */
1242hterm.Terminal.prototype.getRowsText = function(start, end) {
1243 var ary = [];
1244 for (var i = start; i < end; i++) {
1245 var node = this.getRowNode(i);
1246 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001247 if (i < end - 1 && !node.getAttribute('line-overflow'))
1248 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001249 }
1250
rgindaa09e7332012-08-17 12:49:51 -07001251 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001252};
1253
1254/**
1255 * Return the text content for a given row.
1256 *
1257 * This is a method from the RowProvider interface. The ScrollPort uses
1258 * it to fetch text content on demand when the user attempts to copy their
1259 * selection to the clipboard.
1260 *
1261 * @param {integer} index The zero-based row index to return, measured
1262 * relative to the start of the scrollback buffer. On-screen rows will
1263 * always have the largest indicies.
1264 * @return {string} A string containing the text value of the selected row.
1265 */
1266hterm.Terminal.prototype.getRowText = function(index) {
1267 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001268 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001269};
1270
1271/**
1272 * Return the total number of rows in the addressable screen and in the
1273 * scrollback buffer of this terminal.
1274 *
1275 * This is a method from the RowProvider interface. The ScrollPort uses
1276 * it to compute the size of the scrollbar.
1277 *
1278 * @return {integer} The number of rows in this terminal.
1279 */
1280hterm.Terminal.prototype.getRowCount = function() {
1281 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1282};
1283
1284/**
1285 * Create DOM nodes for new rows and append them to the end of the terminal.
1286 *
1287 * This is the only correct way to add a new DOM node for a row. Notice that
1288 * the new row is appended to the bottom of the list of rows, and does not
1289 * require renumbering (of the rowIndex property) of previous rows.
1290 *
1291 * If you think you want a new blank row somewhere in the middle of the
1292 * terminal, look into moveRows_().
1293 *
1294 * This method does not pay attention to vtScrollTop/Bottom, since you should
1295 * be using moveRows() in cases where they would matter.
1296 *
1297 * The cursor will be positioned at column 0 of the first inserted line.
1298 */
1299hterm.Terminal.prototype.appendRows_ = function(count) {
1300 var cursorRow = this.screen_.rowsArray.length;
1301 var offset = this.scrollbackRows_.length + cursorRow;
1302 for (var i = 0; i < count; i++) {
1303 var row = this.document_.createElement('x-row');
1304 row.appendChild(this.document_.createTextNode(''));
1305 row.rowIndex = offset + i;
1306 this.screen_.pushRow(row);
1307 }
1308
1309 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1310 if (extraRows > 0) {
1311 var ary = this.screen_.shiftRows(extraRows);
1312 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001313 if (this.scrollPort_.isScrolledEnd)
1314 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001315 }
1316
1317 if (cursorRow >= this.screen_.rowsArray.length)
1318 cursorRow = this.screen_.rowsArray.length - 1;
1319
rginda87b86462011-12-14 13:48:03 -08001320 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001321};
1322
1323/**
1324 * Relocate rows from one part of the addressable screen to another.
1325 *
1326 * This is used to recycle rows during VT scrolls (those which are driven
1327 * by VT commands, rather than by the user manipulating the scrollbar.)
1328 *
1329 * In this case, the blank lines scrolled into the scroll region are made of
1330 * the nodes we scrolled off. These have their rowIndex properties carefully
1331 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001332 */
1333hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1334 var ary = this.screen_.removeRows(fromIndex, count);
1335 this.screen_.insertRows(toIndex, ary);
1336
1337 var start, end;
1338 if (fromIndex < toIndex) {
1339 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001340 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001341 } else {
1342 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001343 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001344 }
1345
1346 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001347 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001348};
1349
1350/**
1351 * Renumber the rowIndex property of the given range of rows.
1352 *
1353 * The start and end indicies are relative to the screen, not the scrollback.
1354 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001355 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001356 * no need to renumber scrollback rows.
1357 */
Robert Ginda40932892012-12-10 17:26:40 -08001358hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1359 var screen = opt_screen || this.screen_;
1360
rginda8ba33642011-12-14 12:31:31 -08001361 var offset = this.scrollbackRows_.length;
1362 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001363 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001364 }
1365};
1366
1367/**
1368 * Print a string to the terminal.
1369 *
1370 * This respects the current insert and wraparound modes. It will add new lines
1371 * to the end of the terminal, scrolling off the top into the scrollback buffer
1372 * if necessary.
1373 *
1374 * The string is *not* parsed for escape codes. Use the interpret() method if
1375 * that's what you're after.
1376 *
1377 * @param{string} str The string to print.
1378 */
1379hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001380 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001381
Ricky Liang48f05cb2013-12-31 23:35:29 +08001382 var strWidth = lib.wc.strWidth(str);
1383
1384 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001385 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1386 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001387 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001388 }
rgindaa19afe22012-01-25 15:40:22 -08001389
Ricky Liang48f05cb2013-12-31 23:35:29 +08001390 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001391 var didOverflow = false;
1392 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001393
rgindaa9abdd82012-08-06 18:05:09 -07001394 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1395 didOverflow = true;
1396 count = this.screenSize.width - this.screen_.cursorPosition.column;
1397 }
rgindaa19afe22012-01-25 15:40:22 -08001398
rgindaa9abdd82012-08-06 18:05:09 -07001399 if (didOverflow && !this.options_.wraparound) {
1400 // If the string overflowed the line but wraparound is off, then the
1401 // last printed character should be the last of the string.
1402 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001403 substr = lib.wc.substr(str, startOffset, count - 1) +
1404 lib.wc.substr(str, strWidth - 1);
1405 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001406 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001407 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001408 }
rgindaa19afe22012-01-25 15:40:22 -08001409
Ricky Liang48f05cb2013-12-31 23:35:29 +08001410 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1411 for (var i = 0; i < tokens.length; i++) {
1412 if (tokens[i].wcNode)
1413 this.screen_.textAttributes.wcNode = true;
1414
1415 if (this.options_.insertMode) {
1416 this.screen_.insertString(tokens[i].str);
1417 } else {
1418 this.screen_.overwriteString(tokens[i].str);
1419 }
1420 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001421 }
1422
1423 this.screen_.maybeClipCurrentRow();
1424 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001425 }
rginda8ba33642011-12-14 12:31:31 -08001426
1427 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001428
rginda9f5222b2012-03-05 11:53:28 -08001429 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001430 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001431};
1432
1433/**
rginda87b86462011-12-14 13:48:03 -08001434 * Set the VT scroll region.
1435 *
rginda87b86462011-12-14 13:48:03 -08001436 * This also resets the cursor position to the absolute (0, 0) position, since
1437 * that's what xterm appears to do.
1438 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001439 * Setting the scroll region to the full height of the terminal will clear
1440 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1441 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1442 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1443 * continue to work as most users would expect.
1444 *
rginda87b86462011-12-14 13:48:03 -08001445 * @param {integer} scrollTop The zero-based top of the scroll region.
1446 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1447 * inclusive.
1448 */
1449hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001450 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001451 this.vtScrollTop_ = null;
1452 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001453 } else {
1454 this.vtScrollTop_ = scrollTop;
1455 this.vtScrollBottom_ = scrollBottom;
1456 }
rginda87b86462011-12-14 13:48:03 -08001457};
1458
1459/**
rginda8ba33642011-12-14 12:31:31 -08001460 * Return the top row index according to the VT.
1461 *
1462 * This will return 0 unless the terminal has been told to restrict scrolling
1463 * to some lower row. It is used for some VT cursor positioning and scrolling
1464 * commands.
1465 *
1466 * @return {integer} The topmost row in the terminal's scroll region.
1467 */
1468hterm.Terminal.prototype.getVTScrollTop = function() {
1469 if (this.vtScrollTop_ != null)
1470 return this.vtScrollTop_;
1471
1472 return 0;
rginda87b86462011-12-14 13:48:03 -08001473};
rginda8ba33642011-12-14 12:31:31 -08001474
1475/**
1476 * Return the bottom row index according to the VT.
1477 *
1478 * This will return the height of the terminal unless the it has been told to
1479 * restrict scrolling to some higher row. It is used for some VT cursor
1480 * positioning and scrolling commands.
1481 *
1482 * @return {integer} The bottommost row in the terminal's scroll region.
1483 */
1484hterm.Terminal.prototype.getVTScrollBottom = function() {
1485 if (this.vtScrollBottom_ != null)
1486 return this.vtScrollBottom_;
1487
rginda87b86462011-12-14 13:48:03 -08001488 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001489}
1490
1491/**
1492 * Process a '\n' character.
1493 *
1494 * If the cursor is on the final row of the terminal this will append a new
1495 * blank row to the screen and scroll the topmost row into the scrollback
1496 * buffer.
1497 *
1498 * Otherwise, this moves the cursor to column zero of the next row.
1499 */
1500hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001501 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1502 this.screen_.rowsArray.length - 1);
1503
1504 if (this.vtScrollBottom_ != null) {
1505 // A VT Scroll region is active, we never append new rows.
1506 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1507 // We're at the end of the VT Scroll Region, perform a VT scroll.
1508 this.vtScrollUp(1);
1509 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1510 } else if (cursorAtEndOfScreen) {
1511 // We're at the end of the screen, the only thing to do is put the
1512 // cursor to column 0.
1513 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1514 } else {
1515 // Anywhere else, advance the cursor row, and reset the column.
1516 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1517 }
1518 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001519 // We're at the end of the screen. Append a new row to the terminal,
1520 // shifting the top row into the scrollback.
1521 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001522 } else {
rginda87b86462011-12-14 13:48:03 -08001523 // Anywhere else in the screen just moves the cursor.
1524 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001525 }
1526};
1527
1528/**
1529 * Like newLine(), except maintain the cursor column.
1530 */
1531hterm.Terminal.prototype.lineFeed = function() {
1532 var column = this.screen_.cursorPosition.column;
1533 this.newLine();
1534 this.setCursorColumn(column);
1535};
1536
1537/**
rginda87b86462011-12-14 13:48:03 -08001538 * If autoCarriageReturn is set then newLine(), else lineFeed().
1539 */
1540hterm.Terminal.prototype.formFeed = function() {
1541 if (this.options_.autoCarriageReturn) {
1542 this.newLine();
1543 } else {
1544 this.lineFeed();
1545 }
1546};
1547
1548/**
1549 * Move the cursor up one row, possibly inserting a blank line.
1550 *
1551 * The cursor column is not changed.
1552 */
1553hterm.Terminal.prototype.reverseLineFeed = function() {
1554 var scrollTop = this.getVTScrollTop();
1555 var currentRow = this.screen_.cursorPosition.row;
1556
1557 if (currentRow == scrollTop) {
1558 this.insertLines(1);
1559 } else {
1560 this.setAbsoluteCursorRow(currentRow - 1);
1561 }
1562};
1563
1564/**
rginda8ba33642011-12-14 12:31:31 -08001565 * Replace all characters to the left of the current cursor with the space
1566 * character.
1567 *
1568 * TODO(rginda): This should probably *remove* the characters (not just replace
1569 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001570 * position.
rginda8ba33642011-12-14 12:31:31 -08001571 */
1572hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001573 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001574 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001575 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001576 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001577};
1578
1579/**
David Benjamin684a9b72012-05-01 17:19:58 -04001580 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001581 *
1582 * The cursor position is unchanged.
1583 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001584 * If the current background color is not the default background color this
1585 * will insert spaces rather than delete. This is unfortunate because the
1586 * trailing space will affect text selection, but it's difficult to come up
1587 * with a way to style empty space that wouldn't trip up the hterm.Screen
1588 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001589 *
1590 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1591 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1592 * crbug.com/232390 for details.
rginda8ba33642011-12-14 12:31:31 -08001593 */
1594hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001595 if (this.screen_.cursorPosition.overflow)
1596 return;
1597
Robert Ginda7fd57082012-09-25 14:41:47 -07001598 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1599 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001600
1601 if (this.screen_.textAttributes.background ===
1602 this.screen_.textAttributes.DEFAULT_COLOR) {
1603 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001604 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001605 this.screen_.cursorPosition.column + count) {
1606 this.screen_.deleteChars(count);
1607 this.clearCursorOverflow();
1608 return;
1609 }
1610 }
1611
rginda87b86462011-12-14 13:48:03 -08001612 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001613 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001614 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001615 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001616};
1617
1618/**
1619 * Erase the current line.
1620 *
1621 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001622 */
1623hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001624 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001625 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001626 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001627 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001628};
1629
1630/**
David Benjamina08d78f2012-05-05 00:28:49 -04001631 * Erase all characters from the start of the screen to the current cursor
1632 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001633 *
1634 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001635 */
1636hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001637 var cursor = this.saveCursor();
1638
1639 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001640
David Benjamina08d78f2012-05-05 00:28:49 -04001641 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001642 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001643 this.screen_.clearCursorRow();
1644 }
1645
rginda87b86462011-12-14 13:48:03 -08001646 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001647 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001648};
1649
1650/**
1651 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001652 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001653 *
1654 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001655 */
1656hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001657 var cursor = this.saveCursor();
1658
1659 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001660
David Benjamina08d78f2012-05-05 00:28:49 -04001661 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001662 for (var i = cursor.row + 1; i <= bottom; i++) {
1663 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001664 this.screen_.clearCursorRow();
1665 }
1666
rginda87b86462011-12-14 13:48:03 -08001667 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001668 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001669};
1670
1671/**
1672 * Fill the terminal with a given character.
1673 *
1674 * This methods does not respect the VT scroll region.
1675 *
1676 * @param {string} ch The character to use for the fill.
1677 */
1678hterm.Terminal.prototype.fill = function(ch) {
1679 var cursor = this.saveCursor();
1680
1681 this.setAbsoluteCursorPosition(0, 0);
1682 for (var row = 0; row < this.screenSize.height; row++) {
1683 for (var col = 0; col < this.screenSize.width; col++) {
1684 this.setAbsoluteCursorPosition(row, col);
1685 this.screen_.overwriteString(ch);
1686 }
1687 }
1688
1689 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001690};
1691
1692/**
rginda9ea433c2012-03-16 11:57:00 -07001693 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001694 *
rginda9ea433c2012-03-16 11:57:00 -07001695 * This does not respect the scroll region.
1696 *
1697 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1698 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001699 */
rginda9ea433c2012-03-16 11:57:00 -07001700hterm.Terminal.prototype.clearHome = function(opt_screen) {
1701 var screen = opt_screen || this.screen_;
1702 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001703
rginda11057d52012-04-25 12:29:56 -07001704 if (bottom == 0) {
1705 // Empty screen, nothing to do.
1706 return;
1707 }
1708
rgindae4d29232012-01-19 10:47:13 -08001709 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001710 screen.setCursorPosition(i, 0);
1711 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001712 }
1713
rginda9ea433c2012-03-16 11:57:00 -07001714 screen.setCursorPosition(0, 0);
1715};
1716
1717/**
1718 * Erase the entire display without changing the cursor position.
1719 *
1720 * The cursor position is unchanged. This does not respect the scroll
1721 * region.
1722 *
1723 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1724 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001725 */
1726hterm.Terminal.prototype.clear = function(opt_screen) {
1727 var screen = opt_screen || this.screen_;
1728 var cursor = screen.cursorPosition.clone();
1729 this.clearHome(screen);
1730 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001731};
1732
1733/**
1734 * VT command to insert lines at the current cursor row.
1735 *
1736 * This respects the current scroll region. Rows pushed off the bottom are
1737 * lost (they won't show up in the scrollback buffer).
1738 *
rginda8ba33642011-12-14 12:31:31 -08001739 * @param {integer} count The number of lines to insert.
1740 */
1741hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001742 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001743
1744 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001745 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001746
Robert Ginda579186b2012-09-26 11:40:04 -07001747 // The moveCount is the number of rows we need to relocate to make room for
1748 // the new row(s). The count is the distance to move them.
1749 var moveCount = bottom - cursorRow - count + 1;
1750 if (moveCount)
1751 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001752
Robert Ginda579186b2012-09-26 11:40:04 -07001753 for (var i = count - 1; i >= 0; i--) {
1754 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001755 this.screen_.clearCursorRow();
1756 }
rginda8ba33642011-12-14 12:31:31 -08001757};
1758
1759/**
1760 * VT command to delete lines at the current cursor row.
1761 *
1762 * New rows are added to the bottom of scroll region to take their place. New
1763 * rows are strictly there to take up space and have no content or style.
1764 */
1765hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001766 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001767
rginda87b86462011-12-14 13:48:03 -08001768 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001769 var bottom = this.getVTScrollBottom();
1770
rginda87b86462011-12-14 13:48:03 -08001771 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001772 count = Math.min(count, maxCount);
1773
rginda87b86462011-12-14 13:48:03 -08001774 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001775 if (count != maxCount)
1776 this.moveRows_(top, count, moveStart);
1777
1778 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001779 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001780 this.screen_.clearCursorRow();
1781 }
1782
rginda87b86462011-12-14 13:48:03 -08001783 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001784 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001785};
1786
1787/**
1788 * Inserts the given number of spaces at the current cursor position.
1789 *
rginda87b86462011-12-14 13:48:03 -08001790 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001791 */
1792hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001793 var cursor = this.saveCursor();
1794
rgindacbbd7482012-06-13 15:06:16 -07001795 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001796 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001797 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001798
1799 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001800 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001801};
1802
1803/**
1804 * Forward-delete the specified number of characters starting at the cursor
1805 * position.
1806 *
1807 * @param {integer} count The number of characters to delete.
1808 */
1809hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001810 var deleted = this.screen_.deleteChars(count);
1811 if (deleted && !this.screen_.textAttributes.isDefault()) {
1812 var cursor = this.saveCursor();
1813 this.setCursorColumn(this.screenSize.width - deleted);
1814 this.screen_.insertString(lib.f.getWhitespace(deleted));
1815 this.restoreCursor(cursor);
1816 }
1817
David Benjamin54e8bf62012-06-01 22:31:40 -04001818 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001819};
1820
1821/**
1822 * Shift rows in the scroll region upwards by a given number of lines.
1823 *
1824 * New rows are inserted at the bottom of the scroll region to fill the
1825 * vacated rows. The new rows not filled out with the current text attributes.
1826 *
1827 * This function does not affect the scrollback rows at all. Rows shifted
1828 * off the top are lost.
1829 *
rginda87b86462011-12-14 13:48:03 -08001830 * The cursor position is not altered.
1831 *
rginda8ba33642011-12-14 12:31:31 -08001832 * @param {integer} count The number of rows to scroll.
1833 */
1834hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001835 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001836
rginda87b86462011-12-14 13:48:03 -08001837 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001838 this.deleteLines(count);
1839
rginda87b86462011-12-14 13:48:03 -08001840 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001841};
1842
1843/**
1844 * Shift rows below the cursor down by a given number of lines.
1845 *
1846 * This function respects the current scroll region.
1847 *
1848 * New rows are inserted at the top 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 bottom are lost.
1853 *
1854 * @param {integer} count The number of rows to scroll.
1855 */
1856hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001857 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001858
rginda87b86462011-12-14 13:48:03 -08001859 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001860 this.insertLines(opt_count);
1861
rginda87b86462011-12-14 13:48:03 -08001862 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001863};
1864
rginda87b86462011-12-14 13:48:03 -08001865
rginda8ba33642011-12-14 12:31:31 -08001866/**
1867 * Set the cursor position.
1868 *
1869 * The cursor row is relative to the scroll region if the terminal has
1870 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1871 *
1872 * @param {integer} row The new zero-based cursor row.
1873 * @param {integer} row The new zero-based cursor column.
1874 */
1875hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1876 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001877 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001878 } else {
rginda87b86462011-12-14 13:48:03 -08001879 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001880 }
rginda87b86462011-12-14 13:48:03 -08001881};
rginda8ba33642011-12-14 12:31:31 -08001882
rginda87b86462011-12-14 13:48:03 -08001883hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1884 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001885 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1886 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001887 this.screen_.setCursorPosition(row, column);
1888};
1889
1890hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001891 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1892 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001893 this.screen_.setCursorPosition(row, column);
1894};
1895
1896/**
1897 * Set the cursor column.
1898 *
1899 * @param {integer} column The new zero-based cursor column.
1900 */
1901hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001902 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001903};
1904
1905/**
1906 * Return the cursor column.
1907 *
1908 * @return {integer} The zero-based cursor column.
1909 */
1910hterm.Terminal.prototype.getCursorColumn = function() {
1911 return this.screen_.cursorPosition.column;
1912};
1913
1914/**
1915 * Set the cursor row.
1916 *
1917 * The cursor row is relative to the scroll region if the terminal has
1918 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1919 *
1920 * @param {integer} row The new cursor row.
1921 */
rginda87b86462011-12-14 13:48:03 -08001922hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1923 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001924};
1925
1926/**
1927 * Return the cursor row.
1928 *
1929 * @return {integer} The zero-based cursor row.
1930 */
1931hterm.Terminal.prototype.getCursorRow = function(row) {
1932 return this.screen_.cursorPosition.row;
1933};
1934
1935/**
1936 * Request that the ScrollPort redraw itself soon.
1937 *
1938 * The redraw will happen asynchronously, soon after the call stack winds down.
1939 * Multiple calls will be coalesced into a single redraw.
1940 */
1941hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001942 if (this.timeouts_.redraw)
1943 return;
rginda8ba33642011-12-14 12:31:31 -08001944
1945 var self = this;
rginda87b86462011-12-14 13:48:03 -08001946 this.timeouts_.redraw = setTimeout(function() {
1947 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001948 self.scrollPort_.redraw_();
1949 }, 0);
1950};
1951
1952/**
1953 * Request that the ScrollPort be scrolled to the bottom.
1954 *
1955 * The scroll will happen asynchronously, soon after the call stack winds down.
1956 * Multiple calls will be coalesced into a single scroll.
1957 *
1958 * This affects the scrollbar position of the ScrollPort, and has nothing to
1959 * do with the VT scroll commands.
1960 */
1961hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1962 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001963 return;
rginda8ba33642011-12-14 12:31:31 -08001964
1965 var self = this;
1966 this.timeouts_.scrollDown = setTimeout(function() {
1967 delete self.timeouts_.scrollDown;
1968 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1969 }, 10);
1970};
1971
1972/**
1973 * Move the cursor up a specified number of rows.
1974 *
1975 * @param {integer} count The number of rows to move the cursor.
1976 */
1977hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001978 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001979};
1980
1981/**
1982 * Move the cursor down a specified number of rows.
1983 *
1984 * @param {integer} count The number of rows to move the cursor.
1985 */
1986hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001987 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001988 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1989 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1990 this.screenSize.height - 1);
1991
rgindacbbd7482012-06-13 15:06:16 -07001992 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001993 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001994 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001995};
1996
1997/**
1998 * Move the cursor left a specified number of columns.
1999 *
2000 * @param {integer} count The number of columns to move the cursor.
2001 */
2002hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002003 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002004};
2005
2006/**
2007 * Move the cursor right a specified number of columns.
2008 *
2009 * @param {integer} count The number of columns to move the cursor.
2010 */
2011hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002012 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07002013 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002014 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002015 this.setCursorColumn(column);
2016};
2017
2018/**
2019 * Reverse the foreground and background colors of the terminal.
2020 *
2021 * This only affects text that was drawn with no attributes.
2022 *
2023 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2024 * been drawn with attributes that happen to coincide with the default
2025 * 'no-attribute' colors. My guess is probably not.
2026 */
2027hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002028 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002029 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002030 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2031 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002032 } else {
rginda9f5222b2012-03-05 11:53:28 -08002033 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2034 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002035 }
2036};
2037
2038/**
rginda87b86462011-12-14 13:48:03 -08002039 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002040 *
2041 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002042 */
2043hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002044 this.cursorNode_.style.backgroundColor =
2045 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002046
2047 var self = this;
2048 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002049 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002050 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002051
2052 if (this.bellAudio_.getAttribute('src')) {
Robert Gindaa6331372013-03-19 10:35:39 -07002053 if (this.bellSquelchTimeout_)
Robert Ginda92e18102013-03-14 13:56:37 -07002054 return;
2055
2056 this.bellAudio_.play();
2057
2058 this.bellSequelchTimeout_ = setTimeout(function() {
2059 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002060 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002061 } else {
2062 delete this.bellSquelchTimeout_;
2063 }
rginda87b86462011-12-14 13:48:03 -08002064};
2065
2066/**
rginda8ba33642011-12-14 12:31:31 -08002067 * Set the origin mode bit.
2068 *
2069 * If origin mode is on, certain VT cursor and scrolling commands measure their
2070 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2071 * to the top of the addressable screen.
2072 *
2073 * Defaults to off.
2074 *
2075 * @param {boolean} state True to set origin mode, false to unset.
2076 */
2077hterm.Terminal.prototype.setOriginMode = function(state) {
2078 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002079 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002080};
2081
2082/**
2083 * Set the insert mode bit.
2084 *
2085 * If insert mode is on, existing text beyond the cursor position will be
2086 * shifted right to make room for new text. Otherwise, new text overwrites
2087 * any existing text.
2088 *
2089 * Defaults to off.
2090 *
2091 * @param {boolean} state True to set insert mode, false to unset.
2092 */
2093hterm.Terminal.prototype.setInsertMode = function(state) {
2094 this.options_.insertMode = state;
2095};
2096
2097/**
rginda87b86462011-12-14 13:48:03 -08002098 * Set the auto carriage return bit.
2099 *
2100 * If auto carriage return is on then a formfeed character is interpreted
2101 * as a newline, otherwise it's the same as a linefeed. The difference boils
2102 * down to whether or not the cursor column is reset.
2103 */
2104hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2105 this.options_.autoCarriageReturn = state;
2106};
2107
2108/**
rginda8ba33642011-12-14 12:31:31 -08002109 * Set the wraparound mode bit.
2110 *
2111 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2112 * to the start of the following row. Otherwise, the cursor is clamped to the
2113 * end of the screen and attempts to write past it are ignored.
2114 *
2115 * Defaults to on.
2116 *
2117 * @param {boolean} state True to set wraparound mode, false to unset.
2118 */
2119hterm.Terminal.prototype.setWraparound = function(state) {
2120 this.options_.wraparound = state;
2121};
2122
2123/**
2124 * Set the reverse-wraparound mode bit.
2125 *
2126 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2127 * to the end of the previous row. Otherwise, the cursor is clamped to column
2128 * 0.
2129 *
2130 * Defaults to off.
2131 *
2132 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2133 */
2134hterm.Terminal.prototype.setReverseWraparound = function(state) {
2135 this.options_.reverseWraparound = state;
2136};
2137
2138/**
2139 * Selects between the primary and alternate screens.
2140 *
2141 * If alternate mode is on, the alternate screen is active. Otherwise the
2142 * primary screen is active.
2143 *
2144 * Swapping screens has no effect on the scrollback buffer.
2145 *
2146 * Each screen maintains its own cursor position.
2147 *
2148 * Defaults to off.
2149 *
2150 * @param {boolean} state True to set alternate mode, false to unset.
2151 */
2152hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002153 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002154 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2155
rginda35c456b2012-02-09 17:29:05 -08002156 if (this.screen_.rowsArray.length &&
2157 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2158 // If the screen changed sizes while we were away, our rowIndexes may
2159 // be incorrect.
2160 var offset = this.scrollbackRows_.length;
2161 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002162 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002163 ary[i].rowIndex = offset + i;
2164 }
2165 }
rginda8ba33642011-12-14 12:31:31 -08002166
rginda35c456b2012-02-09 17:29:05 -08002167 this.realizeWidth_(this.screenSize.width);
2168 this.realizeHeight_(this.screenSize.height);
2169 this.scrollPort_.syncScrollHeight();
2170 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002171
rginda6d397402012-01-17 10:58:29 -08002172 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002173 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002174};
2175
2176/**
2177 * Set the cursor-blink mode bit.
2178 *
2179 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2180 * a visible cursor does not blink.
2181 *
2182 * You should make sure to turn blinking off if you're going to dispose of a
2183 * terminal, otherwise you'll leak a timeout.
2184 *
2185 * Defaults to on.
2186 *
2187 * @param {boolean} state True to set cursor-blink mode, false to unset.
2188 */
2189hterm.Terminal.prototype.setCursorBlink = function(state) {
2190 this.options_.cursorBlink = state;
2191
2192 if (!state && this.timeouts_.cursorBlink) {
2193 clearTimeout(this.timeouts_.cursorBlink);
2194 delete this.timeouts_.cursorBlink;
2195 }
2196
2197 if (this.options_.cursorVisible)
2198 this.setCursorVisible(true);
2199};
2200
2201/**
2202 * Set the cursor-visible mode bit.
2203 *
2204 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2205 *
2206 * Defaults to on.
2207 *
2208 * @param {boolean} state True to set cursor-visible mode, false to unset.
2209 */
2210hterm.Terminal.prototype.setCursorVisible = function(state) {
2211 this.options_.cursorVisible = state;
2212
2213 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002214 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002215 return;
2216 }
2217
rginda87b86462011-12-14 13:48:03 -08002218 this.syncCursorPosition_();
2219
2220 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002221
2222 if (this.options_.cursorBlink) {
2223 if (this.timeouts_.cursorBlink)
2224 return;
2225
2226 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2227 500);
2228 } else {
2229 if (this.timeouts_.cursorBlink) {
2230 clearTimeout(this.timeouts_.cursorBlink);
2231 delete this.timeouts_.cursorBlink;
2232 }
2233 }
2234};
2235
2236/**
rginda87b86462011-12-14 13:48:03 -08002237 * Synchronizes the visible cursor and document selection with the current
2238 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002239 */
2240hterm.Terminal.prototype.syncCursorPosition_ = function() {
2241 var topRowIndex = this.scrollPort_.getTopRowIndex();
2242 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2243 var cursorRowIndex = this.scrollbackRows_.length +
2244 this.screen_.cursorPosition.row;
2245
2246 if (cursorRowIndex > bottomRowIndex) {
2247 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002248 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002249 return;
2250 }
2251
2252 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002253 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2254 'px';
2255 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2256 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002257
2258 this.cursorNode_.setAttribute('title',
2259 '(' + this.screen_.cursorPosition.row +
2260 ', ' + this.screen_.cursorPosition.column +
2261 ')');
2262
2263 // Update the caret for a11y purposes.
2264 var selection = this.document_.getSelection();
2265 if (selection && selection.isCollapsed)
2266 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002267};
2268
Robert Gindafb1be6a2013-12-11 11:56:22 -08002269/**
2270 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2271 * and character cell dimensions.
2272 */
Robert Ginda830583c2013-08-07 13:20:46 -07002273hterm.Terminal.prototype.restyleCursor_ = function() {
2274 var shape = this.cursorShape_;
2275
2276 if (this.cursorNode_.getAttribute('focus') == 'false') {
2277 // Always show a block cursor when unfocused.
2278 shape = hterm.Terminal.cursorShape.BLOCK;
2279 }
2280
2281 var style = this.cursorNode_.style;
2282
Robert Gindafb1be6a2013-12-11 11:56:22 -08002283 style.width = this.scrollPort_.characterSize.width + 'px';
2284
Robert Ginda830583c2013-08-07 13:20:46 -07002285 switch (shape) {
2286 case hterm.Terminal.cursorShape.BEAM:
2287 style.height = this.scrollPort_.characterSize.height + 'px';
2288 style.backgroundColor = 'transparent';
2289 style.borderBottomStyle = null;
2290 style.borderLeftStyle = 'solid';
2291 break;
2292
2293 case hterm.Terminal.cursorShape.UNDERLINE:
2294 style.height = this.scrollPort_.characterSize.baseline + 'px';
2295 style.backgroundColor = 'transparent';
2296 style.borderBottomStyle = 'solid';
2297 // correct the size to put it exactly at the baseline
2298 style.borderLeftStyle = null;
2299 break;
2300
2301 default:
2302 style.height = this.scrollPort_.characterSize.height + 'px';
2303 style.backgroundColor = this.cursorColor_;
2304 style.borderBottomStyle = null;
2305 style.borderLeftStyle = null;
2306 break;
2307 }
2308};
2309
rginda8ba33642011-12-14 12:31:31 -08002310/**
2311 * Synchronizes the visible cursor with the current cursor coordinates.
2312 *
2313 * The sync will happen asynchronously, soon after the call stack winds down.
2314 * Multiple calls will be coalesced into a single sync.
2315 */
2316hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2317 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002318 return;
rginda8ba33642011-12-14 12:31:31 -08002319
2320 var self = this;
2321 this.timeouts_.syncCursor = setTimeout(function() {
2322 self.syncCursorPosition_();
2323 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002324 }, 0);
2325};
2326
rgindacc2996c2012-02-24 14:59:31 -08002327/**
rgindaf522ce02012-04-17 17:49:17 -07002328 * Show or hide the zoom warning.
2329 *
2330 * The zoom warning is a message warning the user that their browser zoom must
2331 * be set to 100% in order for hterm to function properly.
2332 *
2333 * @param {boolean} state True to show the message, false to hide it.
2334 */
2335hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2336 if (!this.zoomWarningNode_) {
2337 if (!state)
2338 return;
2339
2340 this.zoomWarningNode_ = this.document_.createElement('div');
2341 this.zoomWarningNode_.style.cssText = (
2342 'color: black;' +
2343 'background-color: #ff2222;' +
2344 'font-size: large;' +
2345 'border-radius: 8px;' +
2346 'opacity: 0.75;' +
2347 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2348 'top: 0.5em;' +
2349 'right: 1.2em;' +
2350 'position: absolute;' +
2351 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002352 '-webkit-user-select: none;' +
2353 '-moz-text-size-adjust: none;' +
2354 '-moz-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002355 }
2356
Robert Gindab4839c22013-02-28 16:52:10 -08002357 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2358 hterm.zoomWarningMessage,
2359 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2360
rgindaf522ce02012-04-17 17:49:17 -07002361 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2362
2363 if (state) {
2364 if (!this.zoomWarningNode_.parentNode)
2365 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2366 } else if (this.zoomWarningNode_.parentNode) {
2367 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2368 }
2369};
2370
2371/**
rgindacc2996c2012-02-24 14:59:31 -08002372 * Show the terminal overlay for a given amount of time.
2373 *
2374 * The terminal overlay appears in inverse video in a large font, centered
2375 * over the terminal. You should probably keep the overlay message brief,
2376 * since it's in a large font and you probably aren't going to check the size
2377 * of the terminal first.
2378 *
2379 * @param {string} msg The text (not HTML) message to display in the overlay.
2380 * @param {number} opt_timeout The amount of time to wait before fading out
2381 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2382 * stay up forever (or until the next overlay).
2383 */
2384hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002385 if (!this.overlayNode_) {
2386 if (!this.div_)
2387 return;
2388
2389 this.overlayNode_ = this.document_.createElement('div');
2390 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002391 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002392 'font-size: xx-large;' +
2393 'opacity: 0.75;' +
2394 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2395 'position: absolute;' +
2396 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002397 '-webkit-transition: opacity 180ms ease-in;' +
2398 '-moz-user-select: none;' +
2399 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002400
2401 this.overlayNode_.addEventListener('mousedown', function(e) {
2402 e.preventDefault();
2403 e.stopPropagation();
2404 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002405 }
2406
rginda9f5222b2012-03-05 11:53:28 -08002407 this.overlayNode_.style.color = this.prefs_.get('background-color');
2408 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2409 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2410
rgindaf0090c92012-02-10 14:58:52 -08002411 this.overlayNode_.textContent = msg;
2412 this.overlayNode_.style.opacity = '0.75';
2413
2414 if (!this.overlayNode_.parentNode)
2415 this.div_.appendChild(this.overlayNode_);
2416
Robert Ginda97769282013-02-01 15:30:30 -08002417 var divSize = hterm.getClientSize(this.div_);
2418 var overlaySize = hterm.getClientSize(this.overlayNode_);
2419
2420 this.overlayNode_.style.top = (divSize.height - overlaySize.height) / 2;
2421 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
2422 this.scrollPort_.currentScrollbarWidthPx) / 2;
rgindaf0090c92012-02-10 14:58:52 -08002423
2424 var self = this;
2425
2426 if (this.overlayTimeout_)
2427 clearTimeout(this.overlayTimeout_);
2428
rgindacc2996c2012-02-24 14:59:31 -08002429 if (opt_timeout === null)
2430 return;
2431
rgindaf0090c92012-02-10 14:58:52 -08002432 this.overlayTimeout_ = setTimeout(function() {
2433 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002434 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002435 if (self.overlayNode_.parentNode)
2436 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002437 self.overlayTimeout_ = null;
2438 self.overlayNode_.style.opacity = '0.75';
2439 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002440 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002441};
2442
rginda4bba5e12012-06-20 16:15:30 -07002443/**
2444 * Paste from the system clipboard to the terminal.
2445 */
2446hterm.Terminal.prototype.paste = function() {
2447 hterm.pasteFromClipboard(this.document_);
2448};
2449
2450/**
2451 * Copy a string to the system clipboard.
2452 *
2453 * Note: If there is a selected range in the terminal, it'll be cleared.
2454 */
2455hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002456 if (this.prefs_.get('enable-clipboard-notice'))
2457 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2458
rgindaa09e7332012-08-17 12:49:51 -07002459 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002460 copySource.textContent = str;
2461 copySource.style.cssText = (
2462 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002463 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002464 'position: absolute;' +
2465 'top: -99px');
2466
2467 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002468
rginda4bba5e12012-06-20 16:15:30 -07002469 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002470 var anchorNode = selection.anchorNode;
2471 var anchorOffset = selection.anchorOffset;
2472 var focusNode = selection.focusNode;
2473 var focusOffset = selection.focusOffset;
2474
rginda4bba5e12012-06-20 16:15:30 -07002475 selection.selectAllChildren(copySource);
2476
rgindaa09e7332012-08-17 12:49:51 -07002477 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002478
Rob Spies56953412014-04-28 14:09:47 -07002479 // IE doesn't support selection.extend. This means that the selection
2480 // won't return on IE.
2481 if (selection.extend) {
2482 selection.collapse(anchorNode, anchorOffset);
2483 selection.extend(focusNode, focusOffset);
2484 }
rgindafaa74742012-08-21 13:34:03 -07002485
rginda4bba5e12012-06-20 16:15:30 -07002486 copySource.parentNode.removeChild(copySource);
2487};
2488
rgindaa09e7332012-08-17 12:49:51 -07002489hterm.Terminal.prototype.getSelectionText = function() {
2490 var selection = this.scrollPort_.selection;
2491 selection.sync();
2492
2493 if (selection.isCollapsed)
2494 return null;
2495
2496
2497 // Start offset measures from the beginning of the line.
2498 var startOffset = selection.startOffset;
2499 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002500
Robert Gindafdbb3f22012-09-06 20:23:06 -07002501 if (node.nodeName != 'X-ROW') {
2502 // If the selection doesn't start on an x-row node, then it must be
2503 // somewhere inside the x-row. Add any characters from previous siblings
2504 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002505
2506 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2507 // If node is the text node in a styled span, move up to the span node.
2508 node = node.parentNode;
2509 }
2510
Robert Gindafdbb3f22012-09-06 20:23:06 -07002511 while (node.previousSibling) {
2512 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002513 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002514 }
rgindaa09e7332012-08-17 12:49:51 -07002515 }
2516
2517 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002518 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2519 selection.endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002520 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002521
Robert Gindafdbb3f22012-09-06 20:23:06 -07002522 if (node.nodeName != 'X-ROW') {
2523 // If the selection doesn't end on an x-row node, then it must be
2524 // somewhere inside the x-row. Add any characters from following siblings
2525 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002526
2527 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2528 // If node is the text node in a styled span, move up to the span node.
2529 node = node.parentNode;
2530 }
2531
Robert Gindafdbb3f22012-09-06 20:23:06 -07002532 while (node.nextSibling) {
2533 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002534 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002535 }
rgindaa09e7332012-08-17 12:49:51 -07002536 }
2537
2538 var rv = this.getRowsText(selection.startRow.rowIndex,
2539 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002540 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002541};
2542
rginda4bba5e12012-06-20 16:15:30 -07002543/**
2544 * Copy the current selection to the system clipboard, then clear it after a
2545 * short delay.
2546 */
2547hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002548 var text = this.getSelectionText();
2549 if (text != null)
2550 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002551};
2552
rgindaf0090c92012-02-10 14:58:52 -08002553hterm.Terminal.prototype.overlaySize = function() {
2554 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2555};
2556
rginda87b86462011-12-14 13:48:03 -08002557/**
2558 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2559 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002560 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002561 */
2562hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002563 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002564 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2565
Robert Ginda8cb7d902013-06-20 14:37:18 -07002566 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002567};
2568
2569/**
rgindad5613292012-06-19 15:40:37 -07002570 * Add the terminalRow and terminalColumn properties to mouse events and
2571 * then forward on to onMouse().
2572 *
2573 * The terminalRow and terminalColumn properties contain the (row, column)
2574 * coordinates for the mouse event.
2575 */
2576hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002577 if (e.processedByTerminalHandler_) {
2578 // We register our event handlers on the document, as well as the cursor
2579 // and the scroll blocker. Mouse events that occur on the cursor or
2580 // scroll blocker will also appear on the document, but we don't want to
2581 // process them twice.
2582 //
2583 // We can't just prevent bubbling because that has other side effects, so
2584 // we decorate the event object with this property instead.
2585 return;
2586 }
2587
2588 e.processedByTerminalHandler_ = true;
2589
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002590 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2591 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002592 return;
2593 }
2594
rgindad5613292012-06-19 15:40:37 -07002595 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2596 this.scrollPort_.characterSize.height) + 1;
2597 e.terminalColumn = parseInt(e.clientX /
2598 this.scrollPort_.characterSize.width) + 1;
2599
Robert Ginda928cf632014-03-05 15:07:41 -08002600 if (e.type == 'mousedown') {
2601 if (e.altKey || this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED) {
2602 // If VT mouse reporting is disabled, or has been defeated with
2603 // alt-mousedown, then the mouse will act on the local selection.
2604 this.reportMouseEvents_ = false;
2605 this.setSelectionEnabled(true);
2606 } else {
2607 // Otherwise we defer ownership of the mouse to the VT.
2608 this.reportMouseEvents_ = true;
Robert Ginda3ae37822014-05-15 13:05:35 -07002609 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08002610 this.setSelectionEnabled(false);
2611 e.preventDefault();
2612 }
2613 }
2614
2615 if (!this.reportMouseEvents_) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002616 if (e.type == 'dblclick') {
2617 this.screen_.expandSelection(this.document_.getSelection());
2618 hterm.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07002619 }
2620
Robert Ginda928cf632014-03-05 15:07:41 -08002621 if (e.type == 'mousedown' && e.which == this.mousePasteButton)
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002622 this.paste();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002623
2624 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2625 !this.document_.getSelection().isCollapsed) {
2626 hterm.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002627 }
2628
2629 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
2630 this.scrollBlockerNode_.engaged) {
2631 // Disengage the scroll-blocker after one of these events.
2632 this.scrollBlockerNode_.engaged = false;
2633 this.scrollBlockerNode_.style.top = '-99px';
2634 }
2635
Robert Ginda928cf632014-03-05 15:07:41 -08002636 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002637 if (!this.scrollBlockerNode_.engaged) {
2638 if (e.type == 'mousedown') {
2639 // Move the scroll-blocker into place if we want to keep the scrollport
2640 // from scrolling.
2641 this.scrollBlockerNode_.engaged = true;
2642 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2643 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2644 } else if (e.type == 'mousemove') {
2645 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
2646 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07002647 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002648 e.preventDefault();
2649 }
2650 }
Robert Ginda928cf632014-03-05 15:07:41 -08002651
2652 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002653 }
2654
Robert Ginda928cf632014-03-05 15:07:41 -08002655 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
2656 // Restore this on mouseup in case it was temporarily defeated with a
2657 // alt-mousedown. Only do this when the selection is empty so that
2658 // we don't immediately kill the users selection.
2659 this.reportMouseEvents_ = (this.vt.mouseReport !=
2660 this.vt.MOUSE_REPORT_DISABLED);
2661 }
rgindad5613292012-06-19 15:40:37 -07002662};
2663
2664/**
2665 * Clients should override this if they care to know about mouse events.
2666 *
2667 * The event parameter will be a normal DOM mouse click event with additional
2668 * 'terminalRow' and 'terminalColumn' properties.
2669 */
2670hterm.Terminal.prototype.onMouse = function(e) { };
2671
2672/**
rginda8e92a692012-05-20 19:37:20 -07002673 * React when focus changes.
2674 */
Rob Spies06533ba2014-04-24 11:20:37 -07002675hterm.Terminal.prototype.onFocusChange_ = function(focused) {
2676 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07002677 this.restyleCursor_();
rginda8e92a692012-05-20 19:37:20 -07002678};
2679
2680/**
rginda8ba33642011-12-14 12:31:31 -08002681 * React when the ScrollPort is scrolled.
2682 */
2683hterm.Terminal.prototype.onScroll_ = function() {
2684 this.scheduleSyncCursorPosition_();
2685};
2686
2687/**
rginda9846e2f2012-01-27 13:53:33 -08002688 * React when text is pasted into the scrollPort.
2689 */
2690hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Ginda8cb7d902013-06-20 14:37:18 -07002691 this.onVTKeystroke(e.text.replace(/\n/mg, '\r'));
rginda9846e2f2012-01-27 13:53:33 -08002692};
2693
2694/**
rgindaa09e7332012-08-17 12:49:51 -07002695 * React when the user tries to copy from the scrollPort.
2696 */
2697hterm.Terminal.prototype.onCopy_ = function(e) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002698 e.preventDefault();
2699 setTimeout(this.copySelectionToClipboard.bind(this), 0);
rgindaa09e7332012-08-17 12:49:51 -07002700};
2701
2702/**
rginda8ba33642011-12-14 12:31:31 -08002703 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002704 *
2705 * Note: This function should not directly contain code that alters the internal
2706 * state of the terminal. That kind of code belongs in realizeWidth or
2707 * realizeHeight, so that it can be executed synchronously in the case of a
2708 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002709 */
2710hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08002711 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002712 this.scrollPort_.characterSize.width);
Robert Ginda19f61292014-03-04 14:07:57 -08002713 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
rginda35c456b2012-02-09 17:29:05 -08002714 this.scrollPort_.characterSize.height);
2715
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002716 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002717 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002718 // gets removed from the document or during the initial load, and we can't
2719 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002720 return;
2721 }
2722
rgindaa8ba17d2012-08-15 14:41:10 -07002723 var isNewSize = (columnCount != this.screenSize.width ||
2724 rowCount != this.screenSize.height);
2725
2726 // We do this even if the size didn't change, just to be sure everything is
2727 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002728 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002729 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002730
2731 if (isNewSize)
2732 this.overlaySize();
2733
Robert Gindafb1be6a2013-12-11 11:56:22 -08002734 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07002735 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002736};
2737
2738/**
2739 * Service the cursor blink timeout.
2740 */
2741hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Ginda830583c2013-08-07 13:20:46 -07002742 if (this.cursorNode_.getAttribute('focus') == 'false' ||
2743 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08002744 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002745 } else {
rginda87b86462011-12-14 13:48:03 -08002746 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002747 }
2748};
David Reveman8f552492012-03-28 12:18:41 -04002749
2750/**
2751 * Set the scrollbar-visible mode bit.
2752 *
2753 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2754 * Otherwise it will not.
2755 *
2756 * Defaults to on.
2757 *
2758 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2759 */
2760hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2761 this.scrollPort_.setScrollbarVisible(state);
2762};