blob: 65d820dfefe2f86d8f117b9928cb18e150931b2b [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
Robert Gindab4839c22013-02-28 16:52:10 -08007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource',
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
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100268 'ctrl-v-paste': function(v) {
269 terminal.keyboard.ctrlVPaste = v;
270 },
271
Robert Ginda57f03b42012-09-13 11:02:48 -0700272 'enable-8-bit-control': function(v) {
273 terminal.vt.enable8BitControl = !!v;
274 },
rginda30f20f62012-04-05 16:36:19 -0700275
Robert Ginda57f03b42012-09-13 11:02:48 -0700276 'enable-bold': function(v) {
277 terminal.syncBoldSafeState();
278 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400279
Robert Ginda3e278d72014-03-25 13:18:51 -0700280 'enable-bold-as-bright': function(v) {
281 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
282 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
283 },
284
Robert Ginda57f03b42012-09-13 11:02:48 -0700285 'enable-clipboard-write': function(v) {
286 terminal.vt.enableClipboardWrite = !!v;
287 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400288
Robert Ginda3755e752013-05-31 13:34:09 -0700289 'enable-dec12': function(v) {
290 terminal.vt.enableDec12 = !!v;
291 },
292
Robert Ginda57f03b42012-09-13 11:02:48 -0700293 'font-family': function(v) {
294 terminal.syncFontFamily();
295 },
rginda30f20f62012-04-05 16:36:19 -0700296
Robert Ginda57f03b42012-09-13 11:02:48 -0700297 'font-size': function(v) {
298 terminal.setFontSize(v);
299 },
rginda9875d902012-08-20 16:21:57 -0700300
Robert Ginda57f03b42012-09-13 11:02:48 -0700301 'font-smoothing': function(v) {
302 terminal.syncFontFamily();
303 },
rgindade84e382012-04-20 15:39:31 -0700304
Robert Ginda57f03b42012-09-13 11:02:48 -0700305 'foreground-color': function(v) {
306 terminal.setForegroundColor(v);
307 },
rginda30f20f62012-04-05 16:36:19 -0700308
Robert Ginda57f03b42012-09-13 11:02:48 -0700309 'home-keys-scroll': function(v) {
310 terminal.keyboard.homeKeysScroll = v;
311 },
rginda4bba5e12012-06-20 16:15:30 -0700312
Robert Ginda57f03b42012-09-13 11:02:48 -0700313 'max-string-sequence': function(v) {
314 terminal.vt.maxStringSequence = v;
315 },
rginda11057d52012-04-25 12:29:56 -0700316
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700317 'media-keys-are-fkeys': function(v) {
318 terminal.keyboard.mediaKeysAreFKeys = v;
319 },
320
Robert Ginda57f03b42012-09-13 11:02:48 -0700321 'meta-sends-escape': function(v) {
322 terminal.keyboard.metaSendsEscape = v;
323 },
rginda30f20f62012-04-05 16:36:19 -0700324
Robert Ginda57f03b42012-09-13 11:02:48 -0700325 'mouse-paste-button': function(v) {
326 terminal.syncMousePasteButton();
327 },
rgindaa8ba17d2012-08-15 14:41:10 -0700328
Robert Gindae76aa9f2014-03-14 12:29:12 -0700329 'page-keys-scroll': function(v) {
330 terminal.keyboard.pageKeysScroll = v;
331 },
332
Robert Ginda40932892012-12-10 17:26:40 -0800333 'pass-alt-number': function(v) {
334 if (v == null) {
335 var osx = window.navigator.userAgent.match(/Mac OS X/);
336
337 // Let Alt-1..9 pass to the browser (to control tab switching) on
338 // non-OS X systems, or if hterm is not opened in an app window.
339 v = (!osx && hterm.windowType != 'popup');
340 }
341
342 terminal.passAltNumber = v;
343 },
344
345 'pass-ctrl-number': function(v) {
346 if (v == null) {
347 var osx = window.navigator.userAgent.match(/Mac OS X/);
348
349 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
350 // non-OS X systems, or if hterm is not opened in an app window.
351 v = (!osx && hterm.windowType != 'popup');
352 }
353
354 terminal.passCtrlNumber = v;
355 },
356
357 'pass-meta-number': function(v) {
358 if (v == null) {
359 var osx = window.navigator.userAgent.match(/Mac OS X/);
360
361 // Let Meta-1..9 pass to the browser (to control tab switching) on
362 // OS X systems, or if hterm is not opened in an app window.
363 v = (osx && hterm.windowType != 'popup');
364 }
365
366 terminal.passMetaNumber = v;
367 },
368
Robert Ginda8cb7d902013-06-20 14:37:18 -0700369 'receive-encoding': function(v) {
370 if (!(/^(utf-8|raw)$/).test(v)) {
371 console.warn('Invalid value for "receive-encoding": ' + v);
372 v = 'utf-8';
373 }
374
375 terminal.vt.characterEncoding = v;
376 },
377
Robert Ginda57f03b42012-09-13 11:02:48 -0700378 'scroll-on-keystroke': function(v) {
379 terminal.scrollOnKeystroke_ = v;
380 },
rginda9f5222b2012-03-05 11:53:28 -0800381
Robert Ginda57f03b42012-09-13 11:02:48 -0700382 'scroll-on-output': function(v) {
383 terminal.scrollOnOutput_ = v;
384 },
rginda30f20f62012-04-05 16:36:19 -0700385
Robert Ginda57f03b42012-09-13 11:02:48 -0700386 'scrollbar-visible': function(v) {
387 terminal.setScrollbarVisible(v);
388 },
rginda9f5222b2012-03-05 11:53:28 -0800389
Robert Ginda8cb7d902013-06-20 14:37:18 -0700390 'send-encoding': function(v) {
391 if (!(/^(utf-8|raw)$/).test(v)) {
392 console.warn('Invalid value for "send-encoding": ' + v);
393 v = 'utf-8';
394 }
395
396 terminal.keyboard.characterEncoding = v;
397 },
398
Robert Ginda57f03b42012-09-13 11:02:48 -0700399 'shift-insert-paste': function(v) {
400 terminal.keyboard.shiftInsertPaste = v;
401 },
rginda9f5222b2012-03-05 11:53:28 -0800402
Robert Gindae76aa9f2014-03-14 12:29:12 -0700403 'user-css': function(v) {
404 terminal.scrollPort_.setUserCss(v);
Robert Ginda57f03b42012-09-13 11:02:48 -0700405 }
406 });
rginda30f20f62012-04-05 16:36:19 -0700407
Robert Ginda57f03b42012-09-13 11:02:48 -0700408 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800409 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700410
411 if (opt_callback)
412 opt_callback();
413 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800414};
415
Rob Spies56953412014-04-28 14:09:47 -0700416
417/**
418 * Returns the preferences manager used for configuring this terminal.
419 */
420hterm.Terminal.prototype.getPrefs = function() {
421 return this.prefs_;
422};
423
424
rginda8e92a692012-05-20 19:37:20 -0700425/**
426 * Set the color for the cursor.
427 *
428 * If you want this setting to persist, set it through prefs_, rather than
429 * with this method.
430 */
431hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700432 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700433 this.cursorNode_.style.backgroundColor = color;
434 this.cursorNode_.style.borderColor = color;
435};
436
437/**
438 * Return the current cursor color as a string.
439 */
440hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700441 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700442};
443
444/**
rgindad5613292012-06-19 15:40:37 -0700445 * Enable or disable mouse based text selection in the terminal.
446 */
447hterm.Terminal.prototype.setSelectionEnabled = function(state) {
448 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700449};
450
451/**
rginda8e92a692012-05-20 19:37:20 -0700452 * Set the background color.
453 *
454 * If you want this setting to persist, set it through prefs_, rather than
455 * with this method.
456 */
457hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700458 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700459 this.primaryScreen_.textAttributes.setDefaults(
460 this.foregroundColor_, this.backgroundColor_);
461 this.alternateScreen_.textAttributes.setDefaults(
462 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700463 this.scrollPort_.setBackgroundColor(color);
464};
465
rginda9f5222b2012-03-05 11:53:28 -0800466/**
467 * Return the current terminal background color.
468 *
469 * Intended for use by other classes, so we don't have to expose the entire
470 * prefs_ object.
471 */
472hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700473 return this.backgroundColor_;
474};
475
476/**
477 * Set the foreground color.
478 *
479 * If you want this setting to persist, set it through prefs_, rather than
480 * with this method.
481 */
482hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700483 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700484 this.primaryScreen_.textAttributes.setDefaults(
485 this.foregroundColor_, this.backgroundColor_);
486 this.alternateScreen_.textAttributes.setDefaults(
487 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700488 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800489};
490
491/**
492 * Return the current terminal foreground color.
493 *
494 * Intended for use by other classes, so we don't have to expose the entire
495 * prefs_ object.
496 */
497hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700498 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800499};
500
501/**
rginda87b86462011-12-14 13:48:03 -0800502 * Create a new instance of a terminal command and run it with a given
503 * argument string.
504 *
505 * @param {function} commandClass The constructor for a terminal command.
506 * @param {string} argString The argument string to pass to the command.
507 */
508hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700509 var environment = this.prefs_.get('environment');
510 if (typeof environment != 'object' || environment == null)
511 environment = {};
512
rginda87b86462011-12-14 13:48:03 -0800513 var self = this;
514 this.command = new commandClass(
515 { argString: argString || '',
516 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700517 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800518 onExit: function(code) {
519 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800520 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700521 if (self.prefs_.get('close-on-exit'))
522 window.close();
rginda87b86462011-12-14 13:48:03 -0800523 }
524 });
525
rgindafeaf3142012-01-31 15:14:20 -0800526 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800527 this.command.run();
528};
529
530/**
rgindafeaf3142012-01-31 15:14:20 -0800531 * Returns true if the current screen is the primary screen, false otherwise.
532 */
533hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700534 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800535};
536
537/**
538 * Install the keyboard handler for this terminal.
539 *
540 * This will prevent the browser from seeing any keystrokes sent to the
541 * terminal.
542 */
543hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700544 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800545}
546
547/**
548 * Uninstall the keyboard handler for this terminal.
549 */
550hterm.Terminal.prototype.uninstallKeyboard = function() {
551 this.keyboard.installKeyboard(null);
552}
553
554/**
rginda35c456b2012-02-09 17:29:05 -0800555 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800556 *
557 * Call setFontSize(0) to reset to the default font size.
558 *
559 * This function does not modify the font-size preference.
560 *
561 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800562 */
563hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800564 if (px === 0)
565 px = this.prefs_.get('font-size');
566
rginda35c456b2012-02-09 17:29:05 -0800567 this.scrollPort_.setFontSize(px);
Ricky Liang48f05cb2013-12-31 23:35:29 +0800568 if (this.wcCssRule_) {
569 this.wcCssRule_.style.width = this.scrollPort_.characterSize.width * 2 +
570 'px';
571 }
rginda35c456b2012-02-09 17:29:05 -0800572};
573
574/**
575 * Get the current font size.
576 */
577hterm.Terminal.prototype.getFontSize = function() {
578 return this.scrollPort_.getFontSize();
579};
580
581/**
rginda8e92a692012-05-20 19:37:20 -0700582 * Get the current font family.
583 */
584hterm.Terminal.prototype.getFontFamily = function() {
585 return this.scrollPort_.getFontFamily();
586};
587
588/**
rginda35c456b2012-02-09 17:29:05 -0800589 * Set the CSS "font-family" for this terminal.
590 */
rginda9f5222b2012-03-05 11:53:28 -0800591hterm.Terminal.prototype.syncFontFamily = function() {
592 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
593 this.prefs_.get('font-smoothing'));
594 this.syncBoldSafeState();
595};
596
rginda4bba5e12012-06-20 16:15:30 -0700597/**
598 * Set this.mousePasteButton based on the mouse-paste-button pref,
599 * autodetecting if necessary.
600 */
601hterm.Terminal.prototype.syncMousePasteButton = function() {
602 var button = this.prefs_.get('mouse-paste-button');
603 if (typeof button == 'number') {
604 this.mousePasteButton = button;
605 return;
606 }
607
608 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
609 if (!ary || ary[2] == 'CrOS') {
610 this.mousePasteButton = 2;
611 } else {
612 this.mousePasteButton = 3;
613 }
614};
615
616/**
617 * Enable or disable bold based on the enable-bold pref, autodetecting if
618 * necessary.
619 */
rginda9f5222b2012-03-05 11:53:28 -0800620hterm.Terminal.prototype.syncBoldSafeState = function() {
621 var enableBold = this.prefs_.get('enable-bold');
622 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700623 this.primaryScreen_.textAttributes.enableBold = enableBold;
624 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800625 return;
626 }
627
rgindaf7521392012-02-28 17:20:34 -0800628 var normalSize = this.scrollPort_.measureCharacterSize();
629 var boldSize = this.scrollPort_.measureCharacterSize('bold');
630
631 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800632 if (!isBoldSafe) {
633 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700634 'from normal. Font family is: ' +
635 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800636 }
rginda9f5222b2012-03-05 11:53:28 -0800637
Robert Gindaed016262012-10-26 16:27:09 -0700638 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
639 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800640};
641
642/**
rginda87b86462011-12-14 13:48:03 -0800643 * Return a copy of the current cursor position.
644 *
645 * @return {hterm.RowCol} The RowCol object representing the current position.
646 */
647hterm.Terminal.prototype.saveCursor = function() {
648 return this.screen_.cursorPosition.clone();
649};
650
rgindaa19afe22012-01-25 15:40:22 -0800651hterm.Terminal.prototype.getTextAttributes = function() {
652 return this.screen_.textAttributes;
653};
654
rginda1a09aa02012-06-18 21:11:25 -0700655hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
656 this.screen_.textAttributes = textAttributes;
657};
658
rginda87b86462011-12-14 13:48:03 -0800659/**
rgindaf522ce02012-04-17 17:49:17 -0700660 * Return the current browser zoom factor applied to the terminal.
661 *
662 * @return {number} The current browser zoom factor.
663 */
664hterm.Terminal.prototype.getZoomFactor = function() {
665 return this.scrollPort_.characterSize.zoomFactor;
666};
667
668/**
rginda9846e2f2012-01-27 13:53:33 -0800669 * Change the title of this terminal's window.
670 */
671hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800672 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800673};
674
675/**
rginda87b86462011-12-14 13:48:03 -0800676 * Restore a previously saved cursor position.
677 *
678 * @param {hterm.RowCol} cursor The position to restore.
679 */
680hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700681 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
682 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800683 this.screen_.setCursorPosition(row, column);
684 if (cursor.column > column ||
685 cursor.column == column && cursor.overflow) {
686 this.screen_.cursorPosition.overflow = true;
687 }
rginda87b86462011-12-14 13:48:03 -0800688};
689
690/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400691 * Clear the cursor's overflow flag.
692 */
693hterm.Terminal.prototype.clearCursorOverflow = function() {
694 this.screen_.cursorPosition.overflow = false;
695};
696
697/**
Robert Ginda830583c2013-08-07 13:20:46 -0700698 * Sets the cursor shape
699 */
700hterm.Terminal.prototype.setCursorShape = function(shape) {
701 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800702 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700703}
704
705/**
706 * Get the cursor shape
707 */
708hterm.Terminal.prototype.getCursorShape = function() {
709 return this.cursorShape_;
710}
711
712/**
rginda87b86462011-12-14 13:48:03 -0800713 * Set the width of the terminal, resizing the UI to match.
714 */
715hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800716 if (columnCount == null) {
717 this.div_.style.width = '100%';
718 return;
719 }
720
rginda35c456b2012-02-09 17:29:05 -0800721 this.div_.style.width = this.scrollPort_.characterSize.width *
Robert Ginda97769282013-02-01 15:30:30 -0800722 columnCount + this.scrollPort_.currentScrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400723 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800724 this.scheduleSyncCursorPosition_();
725};
rginda87b86462011-12-14 13:48:03 -0800726
rgindac9bc5502012-01-18 11:48:44 -0800727/**
rginda35c456b2012-02-09 17:29:05 -0800728 * Set the height of the terminal, resizing the UI to match.
729 */
730hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800731 if (rowCount == null) {
732 this.div_.style.height = '100%';
733 return;
734 }
735
rginda35c456b2012-02-09 17:29:05 -0800736 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700737 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800738 this.realizeSize_(this.screenSize.width, rowCount);
739 this.scheduleSyncCursorPosition_();
740};
741
742/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400743 * Deal with terminal size changes.
744 *
745 */
746hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
747 if (columnCount != this.screenSize.width)
748 this.realizeWidth_(columnCount);
749
750 if (rowCount != this.screenSize.height)
751 this.realizeHeight_(rowCount);
752
753 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700754 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400755};
756
757/**
rgindac9bc5502012-01-18 11:48:44 -0800758 * Deal with terminal width changes.
759 *
760 * This function does what needs to be done when the terminal width changes
761 * out from under us. It happens here rather than in onResize_() because this
762 * code may need to run synchronously to handle programmatic changes of
763 * terminal width.
764 *
765 * Relying on the browser to send us an async resize event means we may not be
766 * in the correct state yet when the next escape sequence hits.
767 */
768hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700769 if (columnCount <= 0)
770 throw new Error('Attempt to realize bad width: ' + columnCount);
771
rgindac9bc5502012-01-18 11:48:44 -0800772 var deltaColumns = columnCount - this.screen_.getWidth();
773
rginda87b86462011-12-14 13:48:03 -0800774 this.screenSize.width = columnCount;
775 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800776
777 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400778 if (this.defaultTabStops)
779 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800780 } else {
781 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400782 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800783 break;
784
785 this.tabStops_.pop();
786 }
787 }
788
789 this.screen_.setColumnCount(this.screenSize.width);
790};
791
792/**
793 * Deal with terminal height changes.
794 *
795 * This function does what needs to be done when the terminal height changes
796 * out from under us. It happens here rather than in onResize_() because this
797 * code may need to run synchronously to handle programmatic changes of
798 * terminal height.
799 *
800 * Relying on the browser to send us an async resize event means we may not be
801 * in the correct state yet when the next escape sequence hits.
802 */
803hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700804 if (rowCount <= 0)
805 throw new Error('Attempt to realize bad height: ' + rowCount);
806
rgindac9bc5502012-01-18 11:48:44 -0800807 var deltaRows = rowCount - this.screen_.getHeight();
808
809 this.screenSize.height = rowCount;
810
811 var cursor = this.saveCursor();
812
813 if (deltaRows < 0) {
814 // Screen got smaller.
815 deltaRows *= -1;
816 while (deltaRows) {
817 var lastRow = this.getRowCount() - 1;
818 if (lastRow - this.scrollbackRows_.length == cursor.row)
819 break;
820
821 if (this.getRowText(lastRow))
822 break;
823
824 this.screen_.popRow();
825 deltaRows--;
826 }
827
828 var ary = this.screen_.shiftRows(deltaRows);
829 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
830
831 // We just removed rows from the top of the screen, we need to update
832 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800833 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800834 } else if (deltaRows > 0) {
835 // Screen got larger.
836
837 if (deltaRows <= this.scrollbackRows_.length) {
838 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
839 var rows = this.scrollbackRows_.splice(
840 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
841 this.screen_.unshiftRows(rows);
842 deltaRows -= scrollbackCount;
843 cursor.row += scrollbackCount;
844 }
845
846 if (deltaRows)
847 this.appendRows_(deltaRows);
848 }
849
rginda35c456b2012-02-09 17:29:05 -0800850 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800851 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800852};
853
854/**
855 * Scroll the terminal to the top of the scrollback buffer.
856 */
857hterm.Terminal.prototype.scrollHome = function() {
858 this.scrollPort_.scrollRowToTop(0);
859};
860
861/**
862 * Scroll the terminal to the end.
863 */
864hterm.Terminal.prototype.scrollEnd = function() {
865 this.scrollPort_.scrollRowToBottom(this.getRowCount());
866};
867
868/**
869 * Scroll the terminal one page up (minus one line) relative to the current
870 * position.
871 */
872hterm.Terminal.prototype.scrollPageUp = function() {
873 var i = this.scrollPort_.getTopRowIndex();
874 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
875};
876
877/**
878 * Scroll the terminal one page down (minus one line) relative to the current
879 * position.
880 */
881hterm.Terminal.prototype.scrollPageDown = function() {
882 var i = this.scrollPort_.getTopRowIndex();
883 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800884};
885
rgindac9bc5502012-01-18 11:48:44 -0800886/**
Robert Ginda40932892012-12-10 17:26:40 -0800887 * Clear primary screen, secondary screen, and the scrollback buffer.
888 */
889hterm.Terminal.prototype.wipeContents = function() {
890 this.scrollbackRows_.length = 0;
891 this.scrollPort_.resetCache();
892
893 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
894 var bottom = screen.getHeight();
895 if (bottom > 0) {
896 this.renumberRows_(0, bottom);
897 this.clearHome(screen);
898 }
899 }.bind(this));
900
901 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -0700902 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -0800903};
904
905/**
rgindac9bc5502012-01-18 11:48:44 -0800906 * Full terminal reset.
907 */
rginda87b86462011-12-14 13:48:03 -0800908hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800909 this.clearAllTabStops();
910 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700911
912 this.clearHome(this.primaryScreen_);
913 this.primaryScreen_.textAttributes.reset();
914
915 this.clearHome(this.alternateScreen_);
916 this.alternateScreen_.textAttributes.reset();
917
rgindab8bc8932012-04-27 12:45:03 -0700918 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
919
Robert Ginda92e18102013-03-14 13:56:37 -0700920 this.vt.reset();
921
rgindac9bc5502012-01-18 11:48:44 -0800922 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800923};
924
rgindac9bc5502012-01-18 11:48:44 -0800925/**
926 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700927 *
928 * Perform a soft reset to the default values listed in
929 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800930 */
rginda0f5c0292012-01-13 11:00:13 -0800931hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700932 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800933 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700934
rgindab8bc8932012-04-27 12:45:03 -0700935 // Xterm also resets the color palette on soft reset, even though it doesn't
936 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700937 this.primaryScreen_.textAttributes.resetColorPalette();
938 this.alternateScreen_.textAttributes.resetColorPalette();
939
rgindab8bc8932012-04-27 12:45:03 -0700940 // The xterm man page explicitly says this will happen on soft reset.
941 this.setVTScrollRegion(null, null);
942
943 // Xterm also shows the cursor on soft reset, but does not alter the blink
944 // state.
rgindaa19afe22012-01-25 15:40:22 -0800945 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800946};
947
rgindac9bc5502012-01-18 11:48:44 -0800948/**
949 * Move the cursor forward to the next tab stop, or to the last column
950 * if no more tab stops are set.
951 */
952hterm.Terminal.prototype.forwardTabStop = function() {
953 var column = this.screen_.cursorPosition.column;
954
955 for (var i = 0; i < this.tabStops_.length; i++) {
956 if (this.tabStops_[i] > column) {
957 this.setCursorColumn(this.tabStops_[i]);
958 return;
959 }
960 }
961
David Benjamin66e954d2012-05-05 21:08:12 -0400962 // xterm does not clear the overflow flag on HT or CHT.
963 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800964 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400965 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800966};
967
rgindac9bc5502012-01-18 11:48:44 -0800968/**
969 * Move the cursor backward to the previous tab stop, or to the first column
970 * if no previous tab stops are set.
971 */
972hterm.Terminal.prototype.backwardTabStop = function() {
973 var column = this.screen_.cursorPosition.column;
974
975 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
976 if (this.tabStops_[i] < column) {
977 this.setCursorColumn(this.tabStops_[i]);
978 return;
979 }
980 }
981
982 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800983};
984
rgindac9bc5502012-01-18 11:48:44 -0800985/**
986 * Set a tab stop at the given column.
987 *
988 * @param {int} column Zero based column.
989 */
990hterm.Terminal.prototype.setTabStop = function(column) {
991 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
992 if (this.tabStops_[i] == column)
993 return;
994
995 if (this.tabStops_[i] < column) {
996 this.tabStops_.splice(i + 1, 0, column);
997 return;
998 }
999 }
1000
1001 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001002};
1003
rgindac9bc5502012-01-18 11:48:44 -08001004/**
1005 * Clear the tab stop at the current cursor position.
1006 *
1007 * No effect if there is no tab stop at the current cursor position.
1008 */
1009hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1010 var column = this.screen_.cursorPosition.column;
1011
1012 var i = this.tabStops_.indexOf(column);
1013 if (i == -1)
1014 return;
1015
1016 this.tabStops_.splice(i, 1);
1017};
1018
1019/**
1020 * Clear all tab stops.
1021 */
1022hterm.Terminal.prototype.clearAllTabStops = function() {
1023 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001024 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001025};
1026
1027/**
1028 * Set up the default tab stops, starting from a given column.
1029 *
1030 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001031 * from the specified column, or 0 if no column is provided. It also flags
1032 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001033 *
1034 * This does not clear the existing tab stops first, use clearAllTabStops
1035 * for that.
1036 *
1037 * @param {int} opt_start Optional starting zero based starting column, useful
1038 * for filling out missing tab stops when the terminal is resized.
1039 */
1040hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1041 var start = opt_start || 0;
1042 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001043 // Round start up to a default tab stop.
1044 start = start - 1 - ((start - 1) % w) + w;
1045 for (var i = start; i < this.screenSize.width; i += w) {
1046 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001047 }
David Benjamin66e954d2012-05-05 21:08:12 -04001048
1049 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001050};
1051
rginda6d397402012-01-17 10:58:29 -08001052/**
rginda8ba33642011-12-14 12:31:31 -08001053 * Interpret a sequence of characters.
1054 *
1055 * Incomplete escape sequences are buffered until the next call.
1056 *
1057 * @param {string} str Sequence of characters to interpret or pass through.
1058 */
1059hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001060 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001061 this.scheduleSyncCursorPosition_();
1062};
1063
1064/**
1065 * Take over the given DIV for use as the terminal display.
1066 *
1067 * @param {HTMLDivElement} div The div to use as the terminal display.
1068 */
1069hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001070 this.div_ = div;
1071
rginda8ba33642011-12-14 12:31:31 -08001072 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001073 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001074 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1075 this.scrollPort_.setBackgroundPosition(
1076 this.prefs_.get('background-position'));
Robert Gindae76aa9f2014-03-14 12:29:12 -07001077 this.scrollPort_.setUserCss(this.prefs_.get('user-css'));
rginda30f20f62012-04-05 16:36:19 -07001078
rginda0918b652012-04-04 11:26:24 -07001079 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001080
rginda9f5222b2012-03-05 11:53:28 -08001081 this.setFontSize(this.prefs_.get('font-size'));
1082 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001083
David Reveman8f552492012-03-28 12:18:41 -04001084 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1085
rginda8ba33642011-12-14 12:31:31 -08001086 this.document_ = this.scrollPort_.getDocument();
1087
rginda4bba5e12012-06-20 16:15:30 -07001088 this.document_.body.oncontextmenu = function() { return false };
1089
1090 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001091 var screenNode = this.scrollPort_.getScreenNode();
1092 screenNode.addEventListener('mousedown', onMouse);
1093 screenNode.addEventListener('mouseup', onMouse);
1094 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001095 this.scrollPort_.onScrollWheel = onMouse;
1096
Toni Barzic0bfa8922013-11-22 11:18:35 -08001097 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001098 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001099 // Listen for mousedown events on the screenNode as in FF the focus
1100 // events don't bubble.
1101 screenNode.addEventListener('mousedown', function() {
1102 setTimeout(this.onFocusChange_.bind(this, true));
1103 }.bind(this));
1104
Toni Barzic0bfa8922013-11-22 11:18:35 -08001105 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001106 'blur', this.onFocusChange_.bind(this, false));
1107
1108 var style = this.document_.createElement('style');
1109 style.textContent =
1110 ('.cursor-node[focus="false"] {' +
1111 ' box-sizing: border-box;' +
1112 ' background-color: transparent !important;' +
1113 ' border-width: 2px;' +
1114 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001115 '}' +
1116 '.wc-node {' +
1117 ' display: inline-block;' +
1118 ' text-align: center;' +
1119 ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001120 '}');
1121 this.document_.head.appendChild(style);
1122
Ricky Liang48f05cb2013-12-31 23:35:29 +08001123 var styleSheets = this.document_.styleSheets;
1124 var cssRules = styleSheets[styleSheets.length - 1].cssRules;
1125 this.wcCssRule_ = cssRules[cssRules.length - 1];
1126
rginda8ba33642011-12-14 12:31:31 -08001127 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001128 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001129 this.cursorNode_.style.cssText =
1130 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001131 'top: -99px;' +
1132 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001133 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1134 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
Rob Spies06533ba2014-04-24 11:20:37 -07001135 '-webkit-transition: opacity, background-color 100ms linear;' +
1136 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001137
rginda8e92a692012-05-20 19:37:20 -07001138 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001139 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1140 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001141
rginda8ba33642011-12-14 12:31:31 -08001142 this.document_.body.appendChild(this.cursorNode_);
1143
rgindad5613292012-06-19 15:40:37 -07001144 // When 'enableMouseDragScroll' is off we reposition this element directly
1145 // under the mouse cursor after a click. This makes Chrome associate
1146 // subsequent mousemove events with the scroll-blocker. Since the
1147 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1148 // events do not cause the scrollport to scroll.
1149 //
1150 // It's a hack, but it's the cleanest way I could find.
1151 this.scrollBlockerNode_ = this.document_.createElement('div');
1152 this.scrollBlockerNode_.style.cssText =
1153 ('position: absolute;' +
1154 'top: -99px;' +
1155 'display: block;' +
1156 'width: 10px;' +
1157 'height: 10px;');
1158 this.document_.body.appendChild(this.scrollBlockerNode_);
1159
1160 var onMouse = this.onMouse_.bind(this);
1161 this.scrollPort_.onScrollWheel = onMouse;
1162 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1163 ].forEach(function(event) {
1164 this.scrollBlockerNode_.addEventListener(event, onMouse);
1165 this.cursorNode_.addEventListener(event, onMouse);
1166 this.document_.addEventListener(event, onMouse);
1167 }.bind(this));
1168
1169 this.cursorNode_.addEventListener('mousedown', function() {
1170 setTimeout(this.focus.bind(this));
1171 }.bind(this));
1172
rginda8ba33642011-12-14 12:31:31 -08001173 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001174
rginda87b86462011-12-14 13:48:03 -08001175 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001176 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001177};
1178
rginda0918b652012-04-04 11:26:24 -07001179/**
1180 * Return the HTML document that contains the terminal DOM nodes.
1181 */
rginda87b86462011-12-14 13:48:03 -08001182hterm.Terminal.prototype.getDocument = function() {
1183 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001184};
1185
1186/**
rginda0918b652012-04-04 11:26:24 -07001187 * Focus the terminal.
1188 */
1189hterm.Terminal.prototype.focus = function() {
1190 this.scrollPort_.focus();
1191};
1192
1193/**
rginda8ba33642011-12-14 12:31:31 -08001194 * Return the HTML Element for a given row index.
1195 *
1196 * This is a method from the RowProvider interface. The ScrollPort uses
1197 * it to fetch rows on demand as they are scrolled into view.
1198 *
1199 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1200 * pairs to conserve memory.
1201 *
1202 * @param {integer} index The zero-based row index, measured relative to the
1203 * start of the scrollback buffer. On-screen rows will always have the
1204 * largest indicies.
1205 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1206 */
1207hterm.Terminal.prototype.getRowNode = function(index) {
1208 if (index < this.scrollbackRows_.length)
1209 return this.scrollbackRows_[index];
1210
1211 var screenIndex = index - this.scrollbackRows_.length;
1212 return this.screen_.rowsArray[screenIndex];
1213};
1214
1215/**
1216 * Return the text content for a given range of rows.
1217 *
1218 * This is a method from the RowProvider interface. The ScrollPort uses
1219 * it to fetch text content on demand when the user attempts to copy their
1220 * selection to the clipboard.
1221 *
1222 * @param {integer} start The zero-based row index to start from, measured
1223 * relative to the start of the scrollback buffer. On-screen rows will
1224 * always have the largest indicies.
1225 * @param {integer} end The zero-based row index to end on, measured
1226 * relative to the start of the scrollback buffer.
1227 * @return {string} A single string containing the text value of the range of
1228 * rows. Lines will be newline delimited, with no trailing newline.
1229 */
1230hterm.Terminal.prototype.getRowsText = function(start, end) {
1231 var ary = [];
1232 for (var i = start; i < end; i++) {
1233 var node = this.getRowNode(i);
1234 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001235 if (i < end - 1 && !node.getAttribute('line-overflow'))
1236 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001237 }
1238
rgindaa09e7332012-08-17 12:49:51 -07001239 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001240};
1241
1242/**
1243 * Return the text content for a given row.
1244 *
1245 * This is a method from the RowProvider interface. The ScrollPort uses
1246 * it to fetch text content on demand when the user attempts to copy their
1247 * selection to the clipboard.
1248 *
1249 * @param {integer} index The zero-based row index to return, measured
1250 * relative to the start of the scrollback buffer. On-screen rows will
1251 * always have the largest indicies.
1252 * @return {string} A string containing the text value of the selected row.
1253 */
1254hterm.Terminal.prototype.getRowText = function(index) {
1255 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001256 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001257};
1258
1259/**
1260 * Return the total number of rows in the addressable screen and in the
1261 * scrollback buffer of this terminal.
1262 *
1263 * This is a method from the RowProvider interface. The ScrollPort uses
1264 * it to compute the size of the scrollbar.
1265 *
1266 * @return {integer} The number of rows in this terminal.
1267 */
1268hterm.Terminal.prototype.getRowCount = function() {
1269 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1270};
1271
1272/**
1273 * Create DOM nodes for new rows and append them to the end of the terminal.
1274 *
1275 * This is the only correct way to add a new DOM node for a row. Notice that
1276 * the new row is appended to the bottom of the list of rows, and does not
1277 * require renumbering (of the rowIndex property) of previous rows.
1278 *
1279 * If you think you want a new blank row somewhere in the middle of the
1280 * terminal, look into moveRows_().
1281 *
1282 * This method does not pay attention to vtScrollTop/Bottom, since you should
1283 * be using moveRows() in cases where they would matter.
1284 *
1285 * The cursor will be positioned at column 0 of the first inserted line.
1286 */
1287hterm.Terminal.prototype.appendRows_ = function(count) {
1288 var cursorRow = this.screen_.rowsArray.length;
1289 var offset = this.scrollbackRows_.length + cursorRow;
1290 for (var i = 0; i < count; i++) {
1291 var row = this.document_.createElement('x-row');
1292 row.appendChild(this.document_.createTextNode(''));
1293 row.rowIndex = offset + i;
1294 this.screen_.pushRow(row);
1295 }
1296
1297 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1298 if (extraRows > 0) {
1299 var ary = this.screen_.shiftRows(extraRows);
1300 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001301 if (this.scrollPort_.isScrolledEnd)
1302 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001303 }
1304
1305 if (cursorRow >= this.screen_.rowsArray.length)
1306 cursorRow = this.screen_.rowsArray.length - 1;
1307
rginda87b86462011-12-14 13:48:03 -08001308 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001309};
1310
1311/**
1312 * Relocate rows from one part of the addressable screen to another.
1313 *
1314 * This is used to recycle rows during VT scrolls (those which are driven
1315 * by VT commands, rather than by the user manipulating the scrollbar.)
1316 *
1317 * In this case, the blank lines scrolled into the scroll region are made of
1318 * the nodes we scrolled off. These have their rowIndex properties carefully
1319 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001320 */
1321hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1322 var ary = this.screen_.removeRows(fromIndex, count);
1323 this.screen_.insertRows(toIndex, ary);
1324
1325 var start, end;
1326 if (fromIndex < toIndex) {
1327 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001328 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001329 } else {
1330 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001331 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001332 }
1333
1334 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001335 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001336};
1337
1338/**
1339 * Renumber the rowIndex property of the given range of rows.
1340 *
1341 * The start and end indicies are relative to the screen, not the scrollback.
1342 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001343 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001344 * no need to renumber scrollback rows.
1345 */
Robert Ginda40932892012-12-10 17:26:40 -08001346hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1347 var screen = opt_screen || this.screen_;
1348
rginda8ba33642011-12-14 12:31:31 -08001349 var offset = this.scrollbackRows_.length;
1350 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001351 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001352 }
1353};
1354
1355/**
1356 * Print a string to the terminal.
1357 *
1358 * This respects the current insert and wraparound modes. It will add new lines
1359 * to the end of the terminal, scrolling off the top into the scrollback buffer
1360 * if necessary.
1361 *
1362 * The string is *not* parsed for escape codes. Use the interpret() method if
1363 * that's what you're after.
1364 *
1365 * @param{string} str The string to print.
1366 */
1367hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001368 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001369
Ricky Liang48f05cb2013-12-31 23:35:29 +08001370 var strWidth = lib.wc.strWidth(str);
1371
1372 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001373 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1374 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001375 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001376 }
rgindaa19afe22012-01-25 15:40:22 -08001377
Ricky Liang48f05cb2013-12-31 23:35:29 +08001378 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001379 var didOverflow = false;
1380 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001381
rgindaa9abdd82012-08-06 18:05:09 -07001382 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1383 didOverflow = true;
1384 count = this.screenSize.width - this.screen_.cursorPosition.column;
1385 }
rgindaa19afe22012-01-25 15:40:22 -08001386
rgindaa9abdd82012-08-06 18:05:09 -07001387 if (didOverflow && !this.options_.wraparound) {
1388 // If the string overflowed the line but wraparound is off, then the
1389 // last printed character should be the last of the string.
1390 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001391 substr = lib.wc.substr(str, startOffset, count - 1) +
1392 lib.wc.substr(str, strWidth - 1);
1393 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001394 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001395 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001396 }
rgindaa19afe22012-01-25 15:40:22 -08001397
Ricky Liang48f05cb2013-12-31 23:35:29 +08001398 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1399 for (var i = 0; i < tokens.length; i++) {
1400 if (tokens[i].wcNode)
1401 this.screen_.textAttributes.wcNode = true;
1402
1403 if (this.options_.insertMode) {
1404 this.screen_.insertString(tokens[i].str);
1405 } else {
1406 this.screen_.overwriteString(tokens[i].str);
1407 }
1408 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001409 }
1410
1411 this.screen_.maybeClipCurrentRow();
1412 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001413 }
rginda8ba33642011-12-14 12:31:31 -08001414
1415 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001416
rginda9f5222b2012-03-05 11:53:28 -08001417 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001418 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001419};
1420
1421/**
rginda87b86462011-12-14 13:48:03 -08001422 * Set the VT scroll region.
1423 *
rginda87b86462011-12-14 13:48:03 -08001424 * This also resets the cursor position to the absolute (0, 0) position, since
1425 * that's what xterm appears to do.
1426 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001427 * Setting the scroll region to the full height of the terminal will clear
1428 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1429 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1430 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1431 * continue to work as most users would expect.
1432 *
rginda87b86462011-12-14 13:48:03 -08001433 * @param {integer} scrollTop The zero-based top of the scroll region.
1434 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1435 * inclusive.
1436 */
1437hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001438 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001439 this.vtScrollTop_ = null;
1440 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001441 } else {
1442 this.vtScrollTop_ = scrollTop;
1443 this.vtScrollBottom_ = scrollBottom;
1444 }
rginda87b86462011-12-14 13:48:03 -08001445};
1446
1447/**
rginda8ba33642011-12-14 12:31:31 -08001448 * Return the top row index according to the VT.
1449 *
1450 * This will return 0 unless the terminal has been told to restrict scrolling
1451 * to some lower row. It is used for some VT cursor positioning and scrolling
1452 * commands.
1453 *
1454 * @return {integer} The topmost row in the terminal's scroll region.
1455 */
1456hterm.Terminal.prototype.getVTScrollTop = function() {
1457 if (this.vtScrollTop_ != null)
1458 return this.vtScrollTop_;
1459
1460 return 0;
rginda87b86462011-12-14 13:48:03 -08001461};
rginda8ba33642011-12-14 12:31:31 -08001462
1463/**
1464 * Return the bottom row index according to the VT.
1465 *
1466 * This will return the height of the terminal unless the it has been told to
1467 * restrict scrolling to some higher row. It is used for some VT cursor
1468 * positioning and scrolling commands.
1469 *
1470 * @return {integer} The bottommost row in the terminal's scroll region.
1471 */
1472hterm.Terminal.prototype.getVTScrollBottom = function() {
1473 if (this.vtScrollBottom_ != null)
1474 return this.vtScrollBottom_;
1475
rginda87b86462011-12-14 13:48:03 -08001476 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001477}
1478
1479/**
1480 * Process a '\n' character.
1481 *
1482 * If the cursor is on the final row of the terminal this will append a new
1483 * blank row to the screen and scroll the topmost row into the scrollback
1484 * buffer.
1485 *
1486 * Otherwise, this moves the cursor to column zero of the next row.
1487 */
1488hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001489 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1490 this.screen_.rowsArray.length - 1);
1491
1492 if (this.vtScrollBottom_ != null) {
1493 // A VT Scroll region is active, we never append new rows.
1494 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1495 // We're at the end of the VT Scroll Region, perform a VT scroll.
1496 this.vtScrollUp(1);
1497 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1498 } else if (cursorAtEndOfScreen) {
1499 // We're at the end of the screen, the only thing to do is put the
1500 // cursor to column 0.
1501 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1502 } else {
1503 // Anywhere else, advance the cursor row, and reset the column.
1504 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1505 }
1506 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001507 // We're at the end of the screen. Append a new row to the terminal,
1508 // shifting the top row into the scrollback.
1509 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001510 } else {
rginda87b86462011-12-14 13:48:03 -08001511 // Anywhere else in the screen just moves the cursor.
1512 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001513 }
1514};
1515
1516/**
1517 * Like newLine(), except maintain the cursor column.
1518 */
1519hterm.Terminal.prototype.lineFeed = function() {
1520 var column = this.screen_.cursorPosition.column;
1521 this.newLine();
1522 this.setCursorColumn(column);
1523};
1524
1525/**
rginda87b86462011-12-14 13:48:03 -08001526 * If autoCarriageReturn is set then newLine(), else lineFeed().
1527 */
1528hterm.Terminal.prototype.formFeed = function() {
1529 if (this.options_.autoCarriageReturn) {
1530 this.newLine();
1531 } else {
1532 this.lineFeed();
1533 }
1534};
1535
1536/**
1537 * Move the cursor up one row, possibly inserting a blank line.
1538 *
1539 * The cursor column is not changed.
1540 */
1541hterm.Terminal.prototype.reverseLineFeed = function() {
1542 var scrollTop = this.getVTScrollTop();
1543 var currentRow = this.screen_.cursorPosition.row;
1544
1545 if (currentRow == scrollTop) {
1546 this.insertLines(1);
1547 } else {
1548 this.setAbsoluteCursorRow(currentRow - 1);
1549 }
1550};
1551
1552/**
rginda8ba33642011-12-14 12:31:31 -08001553 * Replace all characters to the left of the current cursor with the space
1554 * character.
1555 *
1556 * TODO(rginda): This should probably *remove* the characters (not just replace
1557 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001558 * position.
rginda8ba33642011-12-14 12:31:31 -08001559 */
1560hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001561 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001562 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001563 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001564 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001565};
1566
1567/**
David Benjamin684a9b72012-05-01 17:19:58 -04001568 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001569 *
1570 * The cursor position is unchanged.
1571 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001572 * If the current background color is not the default background color this
1573 * will insert spaces rather than delete. This is unfortunate because the
1574 * trailing space will affect text selection, but it's difficult to come up
1575 * with a way to style empty space that wouldn't trip up the hterm.Screen
1576 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001577 *
1578 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1579 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1580 * crbug.com/232390 for details.
rginda8ba33642011-12-14 12:31:31 -08001581 */
1582hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001583 if (this.screen_.cursorPosition.overflow)
1584 return;
1585
Robert Ginda7fd57082012-09-25 14:41:47 -07001586 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1587 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001588
1589 if (this.screen_.textAttributes.background ===
1590 this.screen_.textAttributes.DEFAULT_COLOR) {
1591 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001592 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001593 this.screen_.cursorPosition.column + count) {
1594 this.screen_.deleteChars(count);
1595 this.clearCursorOverflow();
1596 return;
1597 }
1598 }
1599
rginda87b86462011-12-14 13:48:03 -08001600 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001601 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001602 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001603 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001604};
1605
1606/**
1607 * Erase the current line.
1608 *
1609 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001610 */
1611hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001612 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001613 this.screen_.clearCursorRow();
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/**
David Benjamina08d78f2012-05-05 00:28:49 -04001619 * Erase all characters from the start of the screen to the current cursor
1620 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001621 *
1622 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001623 */
1624hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001625 var cursor = this.saveCursor();
1626
1627 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001628
David Benjamina08d78f2012-05-05 00:28:49 -04001629 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001630 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001631 this.screen_.clearCursorRow();
1632 }
1633
rginda87b86462011-12-14 13:48:03 -08001634 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001635 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001636};
1637
1638/**
1639 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001640 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001641 *
1642 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001643 */
1644hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001645 var cursor = this.saveCursor();
1646
1647 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001648
David Benjamina08d78f2012-05-05 00:28:49 -04001649 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001650 for (var i = cursor.row + 1; i <= bottom; i++) {
1651 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001652 this.screen_.clearCursorRow();
1653 }
1654
rginda87b86462011-12-14 13:48:03 -08001655 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001656 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001657};
1658
1659/**
1660 * Fill the terminal with a given character.
1661 *
1662 * This methods does not respect the VT scroll region.
1663 *
1664 * @param {string} ch The character to use for the fill.
1665 */
1666hterm.Terminal.prototype.fill = function(ch) {
1667 var cursor = this.saveCursor();
1668
1669 this.setAbsoluteCursorPosition(0, 0);
1670 for (var row = 0; row < this.screenSize.height; row++) {
1671 for (var col = 0; col < this.screenSize.width; col++) {
1672 this.setAbsoluteCursorPosition(row, col);
1673 this.screen_.overwriteString(ch);
1674 }
1675 }
1676
1677 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001678};
1679
1680/**
rginda9ea433c2012-03-16 11:57:00 -07001681 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001682 *
rginda9ea433c2012-03-16 11:57:00 -07001683 * This does not respect the scroll region.
1684 *
1685 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1686 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001687 */
rginda9ea433c2012-03-16 11:57:00 -07001688hterm.Terminal.prototype.clearHome = function(opt_screen) {
1689 var screen = opt_screen || this.screen_;
1690 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001691
rginda11057d52012-04-25 12:29:56 -07001692 if (bottom == 0) {
1693 // Empty screen, nothing to do.
1694 return;
1695 }
1696
rgindae4d29232012-01-19 10:47:13 -08001697 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001698 screen.setCursorPosition(i, 0);
1699 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001700 }
1701
rginda9ea433c2012-03-16 11:57:00 -07001702 screen.setCursorPosition(0, 0);
1703};
1704
1705/**
1706 * Erase the entire display without changing the cursor position.
1707 *
1708 * The cursor position is unchanged. This does not respect the scroll
1709 * region.
1710 *
1711 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1712 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001713 */
1714hterm.Terminal.prototype.clear = function(opt_screen) {
1715 var screen = opt_screen || this.screen_;
1716 var cursor = screen.cursorPosition.clone();
1717 this.clearHome(screen);
1718 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001719};
1720
1721/**
1722 * VT command to insert lines at the current cursor row.
1723 *
1724 * This respects the current scroll region. Rows pushed off the bottom are
1725 * lost (they won't show up in the scrollback buffer).
1726 *
rginda8ba33642011-12-14 12:31:31 -08001727 * @param {integer} count The number of lines to insert.
1728 */
1729hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001730 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001731
1732 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001733 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001734
Robert Ginda579186b2012-09-26 11:40:04 -07001735 // The moveCount is the number of rows we need to relocate to make room for
1736 // the new row(s). The count is the distance to move them.
1737 var moveCount = bottom - cursorRow - count + 1;
1738 if (moveCount)
1739 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001740
Robert Ginda579186b2012-09-26 11:40:04 -07001741 for (var i = count - 1; i >= 0; i--) {
1742 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001743 this.screen_.clearCursorRow();
1744 }
rginda8ba33642011-12-14 12:31:31 -08001745};
1746
1747/**
1748 * VT command to delete lines at the current cursor row.
1749 *
1750 * New rows are added to the bottom of scroll region to take their place. New
1751 * rows are strictly there to take up space and have no content or style.
1752 */
1753hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001754 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001755
rginda87b86462011-12-14 13:48:03 -08001756 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001757 var bottom = this.getVTScrollBottom();
1758
rginda87b86462011-12-14 13:48:03 -08001759 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001760 count = Math.min(count, maxCount);
1761
rginda87b86462011-12-14 13:48:03 -08001762 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001763 if (count != maxCount)
1764 this.moveRows_(top, count, moveStart);
1765
1766 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001767 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001768 this.screen_.clearCursorRow();
1769 }
1770
rginda87b86462011-12-14 13:48:03 -08001771 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001772 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001773};
1774
1775/**
1776 * Inserts the given number of spaces at the current cursor position.
1777 *
rginda87b86462011-12-14 13:48:03 -08001778 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001779 */
1780hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001781 var cursor = this.saveCursor();
1782
rgindacbbd7482012-06-13 15:06:16 -07001783 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001784 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001785 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001786
1787 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001788 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001789};
1790
1791/**
1792 * Forward-delete the specified number of characters starting at the cursor
1793 * position.
1794 *
1795 * @param {integer} count The number of characters to delete.
1796 */
1797hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001798 var deleted = this.screen_.deleteChars(count);
1799 if (deleted && !this.screen_.textAttributes.isDefault()) {
1800 var cursor = this.saveCursor();
1801 this.setCursorColumn(this.screenSize.width - deleted);
1802 this.screen_.insertString(lib.f.getWhitespace(deleted));
1803 this.restoreCursor(cursor);
1804 }
1805
David Benjamin54e8bf62012-06-01 22:31:40 -04001806 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001807};
1808
1809/**
1810 * Shift rows in the scroll region upwards by a given number of lines.
1811 *
1812 * New rows are inserted at the bottom of the scroll region to fill the
1813 * vacated rows. The new rows not filled out with the current text attributes.
1814 *
1815 * This function does not affect the scrollback rows at all. Rows shifted
1816 * off the top are lost.
1817 *
rginda87b86462011-12-14 13:48:03 -08001818 * The cursor position is not altered.
1819 *
rginda8ba33642011-12-14 12:31:31 -08001820 * @param {integer} count The number of rows to scroll.
1821 */
1822hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001823 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001824
rginda87b86462011-12-14 13:48:03 -08001825 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001826 this.deleteLines(count);
1827
rginda87b86462011-12-14 13:48:03 -08001828 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001829};
1830
1831/**
1832 * Shift rows below the cursor down by a given number of lines.
1833 *
1834 * This function respects the current scroll region.
1835 *
1836 * New rows are inserted at the top of the scroll region to fill the
1837 * vacated rows. The new rows not filled out with the current text attributes.
1838 *
1839 * This function does not affect the scrollback rows at all. Rows shifted
1840 * off the bottom are lost.
1841 *
1842 * @param {integer} count The number of rows to scroll.
1843 */
1844hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001845 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001846
rginda87b86462011-12-14 13:48:03 -08001847 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001848 this.insertLines(opt_count);
1849
rginda87b86462011-12-14 13:48:03 -08001850 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001851};
1852
rginda87b86462011-12-14 13:48:03 -08001853
rginda8ba33642011-12-14 12:31:31 -08001854/**
1855 * Set the cursor position.
1856 *
1857 * The cursor row is relative to the scroll region if the terminal has
1858 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1859 *
1860 * @param {integer} row The new zero-based cursor row.
1861 * @param {integer} row The new zero-based cursor column.
1862 */
1863hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1864 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001865 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001866 } else {
rginda87b86462011-12-14 13:48:03 -08001867 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001868 }
rginda87b86462011-12-14 13:48:03 -08001869};
rginda8ba33642011-12-14 12:31:31 -08001870
rginda87b86462011-12-14 13:48:03 -08001871hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1872 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001873 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1874 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001875 this.screen_.setCursorPosition(row, column);
1876};
1877
1878hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001879 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1880 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001881 this.screen_.setCursorPosition(row, column);
1882};
1883
1884/**
1885 * Set the cursor column.
1886 *
1887 * @param {integer} column The new zero-based cursor column.
1888 */
1889hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001890 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001891};
1892
1893/**
1894 * Return the cursor column.
1895 *
1896 * @return {integer} The zero-based cursor column.
1897 */
1898hterm.Terminal.prototype.getCursorColumn = function() {
1899 return this.screen_.cursorPosition.column;
1900};
1901
1902/**
1903 * Set the cursor row.
1904 *
1905 * The cursor row is relative to the scroll region if the terminal has
1906 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1907 *
1908 * @param {integer} row The new cursor row.
1909 */
rginda87b86462011-12-14 13:48:03 -08001910hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1911 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001912};
1913
1914/**
1915 * Return the cursor row.
1916 *
1917 * @return {integer} The zero-based cursor row.
1918 */
1919hterm.Terminal.prototype.getCursorRow = function(row) {
1920 return this.screen_.cursorPosition.row;
1921};
1922
1923/**
1924 * Request that the ScrollPort redraw itself soon.
1925 *
1926 * The redraw will happen asynchronously, soon after the call stack winds down.
1927 * Multiple calls will be coalesced into a single redraw.
1928 */
1929hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001930 if (this.timeouts_.redraw)
1931 return;
rginda8ba33642011-12-14 12:31:31 -08001932
1933 var self = this;
rginda87b86462011-12-14 13:48:03 -08001934 this.timeouts_.redraw = setTimeout(function() {
1935 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001936 self.scrollPort_.redraw_();
1937 }, 0);
1938};
1939
1940/**
1941 * Request that the ScrollPort be scrolled to the bottom.
1942 *
1943 * The scroll will happen asynchronously, soon after the call stack winds down.
1944 * Multiple calls will be coalesced into a single scroll.
1945 *
1946 * This affects the scrollbar position of the ScrollPort, and has nothing to
1947 * do with the VT scroll commands.
1948 */
1949hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1950 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001951 return;
rginda8ba33642011-12-14 12:31:31 -08001952
1953 var self = this;
1954 this.timeouts_.scrollDown = setTimeout(function() {
1955 delete self.timeouts_.scrollDown;
1956 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1957 }, 10);
1958};
1959
1960/**
1961 * Move the cursor up a specified number of rows.
1962 *
1963 * @param {integer} count The number of rows to move the cursor.
1964 */
1965hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001966 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001967};
1968
1969/**
1970 * Move the cursor down a specified number of rows.
1971 *
1972 * @param {integer} count The number of rows to move the cursor.
1973 */
1974hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001975 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001976 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1977 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1978 this.screenSize.height - 1);
1979
rgindacbbd7482012-06-13 15:06:16 -07001980 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001981 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001982 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001983};
1984
1985/**
1986 * Move the cursor left a specified number of columns.
1987 *
1988 * @param {integer} count The number of columns to move the cursor.
1989 */
1990hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001991 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001992};
1993
1994/**
1995 * Move the cursor right a specified number of columns.
1996 *
1997 * @param {integer} count The number of columns to move the cursor.
1998 */
1999hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002000 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07002001 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002002 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002003 this.setCursorColumn(column);
2004};
2005
2006/**
2007 * Reverse the foreground and background colors of the terminal.
2008 *
2009 * This only affects text that was drawn with no attributes.
2010 *
2011 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2012 * been drawn with attributes that happen to coincide with the default
2013 * 'no-attribute' colors. My guess is probably not.
2014 */
2015hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002016 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002017 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002018 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2019 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002020 } else {
rginda9f5222b2012-03-05 11:53:28 -08002021 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2022 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002023 }
2024};
2025
2026/**
rginda87b86462011-12-14 13:48:03 -08002027 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002028 *
2029 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002030 */
2031hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002032 this.cursorNode_.style.backgroundColor =
2033 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002034
2035 var self = this;
2036 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002037 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002038 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002039
2040 if (this.bellAudio_.getAttribute('src')) {
Robert Gindaa6331372013-03-19 10:35:39 -07002041 if (this.bellSquelchTimeout_)
Robert Ginda92e18102013-03-14 13:56:37 -07002042 return;
2043
2044 this.bellAudio_.play();
2045
2046 this.bellSequelchTimeout_ = setTimeout(function() {
2047 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002048 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002049 } else {
2050 delete this.bellSquelchTimeout_;
2051 }
rginda87b86462011-12-14 13:48:03 -08002052};
2053
2054/**
rginda8ba33642011-12-14 12:31:31 -08002055 * Set the origin mode bit.
2056 *
2057 * If origin mode is on, certain VT cursor and scrolling commands measure their
2058 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2059 * to the top of the addressable screen.
2060 *
2061 * Defaults to off.
2062 *
2063 * @param {boolean} state True to set origin mode, false to unset.
2064 */
2065hterm.Terminal.prototype.setOriginMode = function(state) {
2066 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002067 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002068};
2069
2070/**
2071 * Set the insert mode bit.
2072 *
2073 * If insert mode is on, existing text beyond the cursor position will be
2074 * shifted right to make room for new text. Otherwise, new text overwrites
2075 * any existing text.
2076 *
2077 * Defaults to off.
2078 *
2079 * @param {boolean} state True to set insert mode, false to unset.
2080 */
2081hterm.Terminal.prototype.setInsertMode = function(state) {
2082 this.options_.insertMode = state;
2083};
2084
2085/**
rginda87b86462011-12-14 13:48:03 -08002086 * Set the auto carriage return bit.
2087 *
2088 * If auto carriage return is on then a formfeed character is interpreted
2089 * as a newline, otherwise it's the same as a linefeed. The difference boils
2090 * down to whether or not the cursor column is reset.
2091 */
2092hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2093 this.options_.autoCarriageReturn = state;
2094};
2095
2096/**
rginda8ba33642011-12-14 12:31:31 -08002097 * Set the wraparound mode bit.
2098 *
2099 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2100 * to the start of the following row. Otherwise, the cursor is clamped to the
2101 * end of the screen and attempts to write past it are ignored.
2102 *
2103 * Defaults to on.
2104 *
2105 * @param {boolean} state True to set wraparound mode, false to unset.
2106 */
2107hterm.Terminal.prototype.setWraparound = function(state) {
2108 this.options_.wraparound = state;
2109};
2110
2111/**
2112 * Set the reverse-wraparound mode bit.
2113 *
2114 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2115 * to the end of the previous row. Otherwise, the cursor is clamped to column
2116 * 0.
2117 *
2118 * Defaults to off.
2119 *
2120 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2121 */
2122hterm.Terminal.prototype.setReverseWraparound = function(state) {
2123 this.options_.reverseWraparound = state;
2124};
2125
2126/**
2127 * Selects between the primary and alternate screens.
2128 *
2129 * If alternate mode is on, the alternate screen is active. Otherwise the
2130 * primary screen is active.
2131 *
2132 * Swapping screens has no effect on the scrollback buffer.
2133 *
2134 * Each screen maintains its own cursor position.
2135 *
2136 * Defaults to off.
2137 *
2138 * @param {boolean} state True to set alternate mode, false to unset.
2139 */
2140hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002141 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002142 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2143
rginda35c456b2012-02-09 17:29:05 -08002144 if (this.screen_.rowsArray.length &&
2145 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2146 // If the screen changed sizes while we were away, our rowIndexes may
2147 // be incorrect.
2148 var offset = this.scrollbackRows_.length;
2149 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002150 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002151 ary[i].rowIndex = offset + i;
2152 }
2153 }
rginda8ba33642011-12-14 12:31:31 -08002154
rginda35c456b2012-02-09 17:29:05 -08002155 this.realizeWidth_(this.screenSize.width);
2156 this.realizeHeight_(this.screenSize.height);
2157 this.scrollPort_.syncScrollHeight();
2158 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002159
rginda6d397402012-01-17 10:58:29 -08002160 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002161 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002162};
2163
2164/**
2165 * Set the cursor-blink mode bit.
2166 *
2167 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2168 * a visible cursor does not blink.
2169 *
2170 * You should make sure to turn blinking off if you're going to dispose of a
2171 * terminal, otherwise you'll leak a timeout.
2172 *
2173 * Defaults to on.
2174 *
2175 * @param {boolean} state True to set cursor-blink mode, false to unset.
2176 */
2177hterm.Terminal.prototype.setCursorBlink = function(state) {
2178 this.options_.cursorBlink = state;
2179
2180 if (!state && this.timeouts_.cursorBlink) {
2181 clearTimeout(this.timeouts_.cursorBlink);
2182 delete this.timeouts_.cursorBlink;
2183 }
2184
2185 if (this.options_.cursorVisible)
2186 this.setCursorVisible(true);
2187};
2188
2189/**
2190 * Set the cursor-visible mode bit.
2191 *
2192 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2193 *
2194 * Defaults to on.
2195 *
2196 * @param {boolean} state True to set cursor-visible mode, false to unset.
2197 */
2198hterm.Terminal.prototype.setCursorVisible = function(state) {
2199 this.options_.cursorVisible = state;
2200
2201 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002202 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002203 return;
2204 }
2205
rginda87b86462011-12-14 13:48:03 -08002206 this.syncCursorPosition_();
2207
2208 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002209
2210 if (this.options_.cursorBlink) {
2211 if (this.timeouts_.cursorBlink)
2212 return;
2213
2214 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2215 500);
2216 } else {
2217 if (this.timeouts_.cursorBlink) {
2218 clearTimeout(this.timeouts_.cursorBlink);
2219 delete this.timeouts_.cursorBlink;
2220 }
2221 }
2222};
2223
2224/**
rginda87b86462011-12-14 13:48:03 -08002225 * Synchronizes the visible cursor and document selection with the current
2226 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002227 */
2228hterm.Terminal.prototype.syncCursorPosition_ = function() {
2229 var topRowIndex = this.scrollPort_.getTopRowIndex();
2230 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2231 var cursorRowIndex = this.scrollbackRows_.length +
2232 this.screen_.cursorPosition.row;
2233
2234 if (cursorRowIndex > bottomRowIndex) {
2235 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002236 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002237 return;
2238 }
2239
2240 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002241 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2242 'px';
2243 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2244 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002245
2246 this.cursorNode_.setAttribute('title',
2247 '(' + this.screen_.cursorPosition.row +
2248 ', ' + this.screen_.cursorPosition.column +
2249 ')');
2250
2251 // Update the caret for a11y purposes.
2252 var selection = this.document_.getSelection();
2253 if (selection && selection.isCollapsed)
2254 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002255};
2256
Robert Gindafb1be6a2013-12-11 11:56:22 -08002257/**
2258 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2259 * and character cell dimensions.
2260 */
Robert Ginda830583c2013-08-07 13:20:46 -07002261hterm.Terminal.prototype.restyleCursor_ = function() {
2262 var shape = this.cursorShape_;
2263
2264 if (this.cursorNode_.getAttribute('focus') == 'false') {
2265 // Always show a block cursor when unfocused.
2266 shape = hterm.Terminal.cursorShape.BLOCK;
2267 }
2268
2269 var style = this.cursorNode_.style;
2270
Robert Gindafb1be6a2013-12-11 11:56:22 -08002271 style.width = this.scrollPort_.characterSize.width + 'px';
2272
Robert Ginda830583c2013-08-07 13:20:46 -07002273 switch (shape) {
2274 case hterm.Terminal.cursorShape.BEAM:
2275 style.height = this.scrollPort_.characterSize.height + 'px';
2276 style.backgroundColor = 'transparent';
2277 style.borderBottomStyle = null;
2278 style.borderLeftStyle = 'solid';
2279 break;
2280
2281 case hterm.Terminal.cursorShape.UNDERLINE:
2282 style.height = this.scrollPort_.characterSize.baseline + 'px';
2283 style.backgroundColor = 'transparent';
2284 style.borderBottomStyle = 'solid';
2285 // correct the size to put it exactly at the baseline
2286 style.borderLeftStyle = null;
2287 break;
2288
2289 default:
2290 style.height = this.scrollPort_.characterSize.height + 'px';
2291 style.backgroundColor = this.cursorColor_;
2292 style.borderBottomStyle = null;
2293 style.borderLeftStyle = null;
2294 break;
2295 }
2296};
2297
rginda8ba33642011-12-14 12:31:31 -08002298/**
2299 * Synchronizes the visible cursor with the current cursor coordinates.
2300 *
2301 * The sync will happen asynchronously, soon after the call stack winds down.
2302 * Multiple calls will be coalesced into a single sync.
2303 */
2304hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2305 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002306 return;
rginda8ba33642011-12-14 12:31:31 -08002307
2308 var self = this;
2309 this.timeouts_.syncCursor = setTimeout(function() {
2310 self.syncCursorPosition_();
2311 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002312 }, 0);
2313};
2314
rgindacc2996c2012-02-24 14:59:31 -08002315/**
rgindaf522ce02012-04-17 17:49:17 -07002316 * Show or hide the zoom warning.
2317 *
2318 * The zoom warning is a message warning the user that their browser zoom must
2319 * be set to 100% in order for hterm to function properly.
2320 *
2321 * @param {boolean} state True to show the message, false to hide it.
2322 */
2323hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2324 if (!this.zoomWarningNode_) {
2325 if (!state)
2326 return;
2327
2328 this.zoomWarningNode_ = this.document_.createElement('div');
2329 this.zoomWarningNode_.style.cssText = (
2330 'color: black;' +
2331 'background-color: #ff2222;' +
2332 'font-size: large;' +
2333 'border-radius: 8px;' +
2334 'opacity: 0.75;' +
2335 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2336 'top: 0.5em;' +
2337 'right: 1.2em;' +
2338 'position: absolute;' +
2339 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002340 '-webkit-user-select: none;' +
2341 '-moz-text-size-adjust: none;' +
2342 '-moz-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002343 }
2344
Robert Gindab4839c22013-02-28 16:52:10 -08002345 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2346 hterm.zoomWarningMessage,
2347 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2348
rgindaf522ce02012-04-17 17:49:17 -07002349 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2350
2351 if (state) {
2352 if (!this.zoomWarningNode_.parentNode)
2353 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2354 } else if (this.zoomWarningNode_.parentNode) {
2355 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2356 }
2357};
2358
2359/**
rgindacc2996c2012-02-24 14:59:31 -08002360 * Show the terminal overlay for a given amount of time.
2361 *
2362 * The terminal overlay appears in inverse video in a large font, centered
2363 * over the terminal. You should probably keep the overlay message brief,
2364 * since it's in a large font and you probably aren't going to check the size
2365 * of the terminal first.
2366 *
2367 * @param {string} msg The text (not HTML) message to display in the overlay.
2368 * @param {number} opt_timeout The amount of time to wait before fading out
2369 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2370 * stay up forever (or until the next overlay).
2371 */
2372hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002373 if (!this.overlayNode_) {
2374 if (!this.div_)
2375 return;
2376
2377 this.overlayNode_ = this.document_.createElement('div');
2378 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002379 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002380 'font-size: xx-large;' +
2381 'opacity: 0.75;' +
2382 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2383 'position: absolute;' +
2384 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002385 '-webkit-transition: opacity 180ms ease-in;' +
2386 '-moz-user-select: none;' +
2387 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002388
2389 this.overlayNode_.addEventListener('mousedown', function(e) {
2390 e.preventDefault();
2391 e.stopPropagation();
2392 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002393 }
2394
rginda9f5222b2012-03-05 11:53:28 -08002395 this.overlayNode_.style.color = this.prefs_.get('background-color');
2396 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2397 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2398
rgindaf0090c92012-02-10 14:58:52 -08002399 this.overlayNode_.textContent = msg;
2400 this.overlayNode_.style.opacity = '0.75';
2401
2402 if (!this.overlayNode_.parentNode)
2403 this.div_.appendChild(this.overlayNode_);
2404
Robert Ginda97769282013-02-01 15:30:30 -08002405 var divSize = hterm.getClientSize(this.div_);
2406 var overlaySize = hterm.getClientSize(this.overlayNode_);
2407
2408 this.overlayNode_.style.top = (divSize.height - overlaySize.height) / 2;
2409 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
2410 this.scrollPort_.currentScrollbarWidthPx) / 2;
rgindaf0090c92012-02-10 14:58:52 -08002411
2412 var self = this;
2413
2414 if (this.overlayTimeout_)
2415 clearTimeout(this.overlayTimeout_);
2416
rgindacc2996c2012-02-24 14:59:31 -08002417 if (opt_timeout === null)
2418 return;
2419
rgindaf0090c92012-02-10 14:58:52 -08002420 this.overlayTimeout_ = setTimeout(function() {
2421 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002422 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002423 if (self.overlayNode_.parentNode)
2424 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002425 self.overlayTimeout_ = null;
2426 self.overlayNode_.style.opacity = '0.75';
2427 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002428 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002429};
2430
rginda4bba5e12012-06-20 16:15:30 -07002431/**
2432 * Paste from the system clipboard to the terminal.
2433 */
2434hterm.Terminal.prototype.paste = function() {
2435 hterm.pasteFromClipboard(this.document_);
2436};
2437
2438/**
2439 * Copy a string to the system clipboard.
2440 *
2441 * Note: If there is a selected range in the terminal, it'll be cleared.
2442 */
2443hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002444 if (this.prefs_.get('enable-clipboard-notice'))
2445 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2446
rgindaa09e7332012-08-17 12:49:51 -07002447 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002448 copySource.textContent = str;
2449 copySource.style.cssText = (
2450 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002451 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002452 'position: absolute;' +
2453 'top: -99px');
2454
2455 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002456
rginda4bba5e12012-06-20 16:15:30 -07002457 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002458 var anchorNode = selection.anchorNode;
2459 var anchorOffset = selection.anchorOffset;
2460 var focusNode = selection.focusNode;
2461 var focusOffset = selection.focusOffset;
2462
rginda4bba5e12012-06-20 16:15:30 -07002463 selection.selectAllChildren(copySource);
2464
rgindaa09e7332012-08-17 12:49:51 -07002465 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002466
Rob Spies56953412014-04-28 14:09:47 -07002467 // IE doesn't support selection.extend. This means that the selection
2468 // won't return on IE.
2469 if (selection.extend) {
2470 selection.collapse(anchorNode, anchorOffset);
2471 selection.extend(focusNode, focusOffset);
2472 }
rgindafaa74742012-08-21 13:34:03 -07002473
rginda4bba5e12012-06-20 16:15:30 -07002474 copySource.parentNode.removeChild(copySource);
2475};
2476
rgindaa09e7332012-08-17 12:49:51 -07002477hterm.Terminal.prototype.getSelectionText = function() {
2478 var selection = this.scrollPort_.selection;
2479 selection.sync();
2480
2481 if (selection.isCollapsed)
2482 return null;
2483
2484
2485 // Start offset measures from the beginning of the line.
2486 var startOffset = selection.startOffset;
2487 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002488
Robert Gindafdbb3f22012-09-06 20:23:06 -07002489 if (node.nodeName != 'X-ROW') {
2490 // If the selection doesn't start on an x-row node, then it must be
2491 // somewhere inside the x-row. Add any characters from previous siblings
2492 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002493
2494 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2495 // If node is the text node in a styled span, move up to the span node.
2496 node = node.parentNode;
2497 }
2498
Robert Gindafdbb3f22012-09-06 20:23:06 -07002499 while (node.previousSibling) {
2500 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002501 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002502 }
rgindaa09e7332012-08-17 12:49:51 -07002503 }
2504
2505 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002506 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2507 selection.endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002508 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002509
Robert Gindafdbb3f22012-09-06 20:23:06 -07002510 if (node.nodeName != 'X-ROW') {
2511 // If the selection doesn't end on an x-row node, then it must be
2512 // somewhere inside the x-row. Add any characters from following siblings
2513 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002514
2515 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2516 // If node is the text node in a styled span, move up to the span node.
2517 node = node.parentNode;
2518 }
2519
Robert Gindafdbb3f22012-09-06 20:23:06 -07002520 while (node.nextSibling) {
2521 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002522 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002523 }
rgindaa09e7332012-08-17 12:49:51 -07002524 }
2525
2526 var rv = this.getRowsText(selection.startRow.rowIndex,
2527 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002528 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002529};
2530
rginda4bba5e12012-06-20 16:15:30 -07002531/**
2532 * Copy the current selection to the system clipboard, then clear it after a
2533 * short delay.
2534 */
2535hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002536 var text = this.getSelectionText();
2537 if (text != null)
2538 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002539};
2540
rgindaf0090c92012-02-10 14:58:52 -08002541hterm.Terminal.prototype.overlaySize = function() {
2542 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2543};
2544
rginda87b86462011-12-14 13:48:03 -08002545/**
2546 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2547 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002548 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002549 */
2550hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002551 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002552 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2553
Robert Ginda8cb7d902013-06-20 14:37:18 -07002554 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002555};
2556
2557/**
rgindad5613292012-06-19 15:40:37 -07002558 * Add the terminalRow and terminalColumn properties to mouse events and
2559 * then forward on to onMouse().
2560 *
2561 * The terminalRow and terminalColumn properties contain the (row, column)
2562 * coordinates for the mouse event.
2563 */
2564hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002565 if (e.processedByTerminalHandler_) {
2566 // We register our event handlers on the document, as well as the cursor
2567 // and the scroll blocker. Mouse events that occur on the cursor or
2568 // scroll blocker will also appear on the document, but we don't want to
2569 // process them twice.
2570 //
2571 // We can't just prevent bubbling because that has other side effects, so
2572 // we decorate the event object with this property instead.
2573 return;
2574 }
2575
2576 e.processedByTerminalHandler_ = true;
2577
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002578 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2579 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002580 return;
2581 }
2582
rgindad5613292012-06-19 15:40:37 -07002583 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2584 this.scrollPort_.characterSize.height) + 1;
2585 e.terminalColumn = parseInt(e.clientX /
2586 this.scrollPort_.characterSize.width) + 1;
2587
Robert Ginda928cf632014-03-05 15:07:41 -08002588 if (e.type == 'mousedown') {
2589 if (e.altKey || this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED) {
2590 // If VT mouse reporting is disabled, or has been defeated with
2591 // alt-mousedown, then the mouse will act on the local selection.
2592 this.reportMouseEvents_ = false;
2593 this.setSelectionEnabled(true);
2594 } else {
2595 // Otherwise we defer ownership of the mouse to the VT.
2596 this.reportMouseEvents_ = true;
2597 this.document_.getSelection().collapse();
2598 this.setSelectionEnabled(false);
2599 e.preventDefault();
2600 }
2601 }
2602
2603 if (!this.reportMouseEvents_) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002604 if (e.type == 'dblclick') {
2605 this.screen_.expandSelection(this.document_.getSelection());
2606 hterm.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07002607 }
2608
Robert Ginda928cf632014-03-05 15:07:41 -08002609 if (e.type == 'mousedown' && e.which == this.mousePasteButton)
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002610 this.paste();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002611
2612 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2613 !this.document_.getSelection().isCollapsed) {
2614 hterm.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002615 }
2616
2617 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
2618 this.scrollBlockerNode_.engaged) {
2619 // Disengage the scroll-blocker after one of these events.
2620 this.scrollBlockerNode_.engaged = false;
2621 this.scrollBlockerNode_.style.top = '-99px';
2622 }
2623
Robert Ginda928cf632014-03-05 15:07:41 -08002624 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002625 if (!this.scrollBlockerNode_.engaged) {
2626 if (e.type == 'mousedown') {
2627 // Move the scroll-blocker into place if we want to keep the scrollport
2628 // from scrolling.
2629 this.scrollBlockerNode_.engaged = true;
2630 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2631 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2632 } else if (e.type == 'mousemove') {
2633 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
2634 // in which case it's too late to engage the scroll-blocker.
2635 this.document_.getSelection().collapse();
2636 e.preventDefault();
2637 }
2638 }
Robert Ginda928cf632014-03-05 15:07:41 -08002639
2640 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002641 }
2642
Robert Ginda928cf632014-03-05 15:07:41 -08002643 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
2644 // Restore this on mouseup in case it was temporarily defeated with a
2645 // alt-mousedown. Only do this when the selection is empty so that
2646 // we don't immediately kill the users selection.
2647 this.reportMouseEvents_ = (this.vt.mouseReport !=
2648 this.vt.MOUSE_REPORT_DISABLED);
2649 }
rgindad5613292012-06-19 15:40:37 -07002650};
2651
2652/**
2653 * Clients should override this if they care to know about mouse events.
2654 *
2655 * The event parameter will be a normal DOM mouse click event with additional
2656 * 'terminalRow' and 'terminalColumn' properties.
2657 */
2658hterm.Terminal.prototype.onMouse = function(e) { };
2659
2660/**
rginda8e92a692012-05-20 19:37:20 -07002661 * React when focus changes.
2662 */
Rob Spies06533ba2014-04-24 11:20:37 -07002663hterm.Terminal.prototype.onFocusChange_ = function(focused) {
2664 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07002665 this.restyleCursor_();
rginda8e92a692012-05-20 19:37:20 -07002666};
2667
2668/**
rginda8ba33642011-12-14 12:31:31 -08002669 * React when the ScrollPort is scrolled.
2670 */
2671hterm.Terminal.prototype.onScroll_ = function() {
2672 this.scheduleSyncCursorPosition_();
2673};
2674
2675/**
rginda9846e2f2012-01-27 13:53:33 -08002676 * React when text is pasted into the scrollPort.
2677 */
2678hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Ginda8cb7d902013-06-20 14:37:18 -07002679 this.onVTKeystroke(e.text.replace(/\n/mg, '\r'));
rginda9846e2f2012-01-27 13:53:33 -08002680};
2681
2682/**
rgindaa09e7332012-08-17 12:49:51 -07002683 * React when the user tries to copy from the scrollPort.
2684 */
2685hterm.Terminal.prototype.onCopy_ = function(e) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002686 e.preventDefault();
2687 setTimeout(this.copySelectionToClipboard.bind(this), 0);
rgindaa09e7332012-08-17 12:49:51 -07002688};
2689
2690/**
rginda8ba33642011-12-14 12:31:31 -08002691 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002692 *
2693 * Note: This function should not directly contain code that alters the internal
2694 * state of the terminal. That kind of code belongs in realizeWidth or
2695 * realizeHeight, so that it can be executed synchronously in the case of a
2696 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002697 */
2698hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08002699 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002700 this.scrollPort_.characterSize.width);
Robert Ginda19f61292014-03-04 14:07:57 -08002701 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
rginda35c456b2012-02-09 17:29:05 -08002702 this.scrollPort_.characterSize.height);
2703
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002704 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002705 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002706 // gets removed from the document or during the initial load, and we can't
2707 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002708 return;
2709 }
2710
rgindaa8ba17d2012-08-15 14:41:10 -07002711 var isNewSize = (columnCount != this.screenSize.width ||
2712 rowCount != this.screenSize.height);
2713
2714 // We do this even if the size didn't change, just to be sure everything is
2715 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002716 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002717 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002718
2719 if (isNewSize)
2720 this.overlaySize();
2721
Robert Gindafb1be6a2013-12-11 11:56:22 -08002722 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07002723 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002724};
2725
2726/**
2727 * Service the cursor blink timeout.
2728 */
2729hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Ginda830583c2013-08-07 13:20:46 -07002730 if (this.cursorNode_.getAttribute('focus') == 'false' ||
2731 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08002732 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002733 } else {
rginda87b86462011-12-14 13:48:03 -08002734 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002735 }
2736};
David Reveman8f552492012-03-28 12:18:41 -04002737
2738/**
2739 * Set the scrollbar-visible mode bit.
2740 *
2741 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2742 * Otherwise it will not.
2743 *
2744 * Defaults to on.
2745 *
2746 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2747 */
2748hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2749 this.scrollPort_.setScrollbarVisible(state);
2750};