blob: 5561bcba8b676e5e3fa9ad1c28a630263f90314c [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
rginda8e92a692012-05-20 19:37:20 -0700416/**
417 * Set the color for the cursor.
418 *
419 * If you want this setting to persist, set it through prefs_, rather than
420 * with this method.
421 */
422hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700423 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700424 this.cursorNode_.style.backgroundColor = color;
425 this.cursorNode_.style.borderColor = color;
426};
427
428/**
429 * Return the current cursor color as a string.
430 */
431hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700432 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700433};
434
435/**
rgindad5613292012-06-19 15:40:37 -0700436 * Enable or disable mouse based text selection in the terminal.
437 */
438hterm.Terminal.prototype.setSelectionEnabled = function(state) {
439 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700440};
441
442/**
rginda8e92a692012-05-20 19:37:20 -0700443 * Set the background color.
444 *
445 * If you want this setting to persist, set it through prefs_, rather than
446 * with this method.
447 */
448hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700449 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700450 this.primaryScreen_.textAttributes.setDefaults(
451 this.foregroundColor_, this.backgroundColor_);
452 this.alternateScreen_.textAttributes.setDefaults(
453 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700454 this.scrollPort_.setBackgroundColor(color);
455};
456
rginda9f5222b2012-03-05 11:53:28 -0800457/**
458 * Return the current terminal background color.
459 *
460 * Intended for use by other classes, so we don't have to expose the entire
461 * prefs_ object.
462 */
463hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700464 return this.backgroundColor_;
465};
466
467/**
468 * Set the foreground color.
469 *
470 * If you want this setting to persist, set it through prefs_, rather than
471 * with this method.
472 */
473hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700474 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700475 this.primaryScreen_.textAttributes.setDefaults(
476 this.foregroundColor_, this.backgroundColor_);
477 this.alternateScreen_.textAttributes.setDefaults(
478 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700479 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800480};
481
482/**
483 * Return the current terminal foreground color.
484 *
485 * Intended for use by other classes, so we don't have to expose the entire
486 * prefs_ object.
487 */
488hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700489 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800490};
491
492/**
rginda87b86462011-12-14 13:48:03 -0800493 * Create a new instance of a terminal command and run it with a given
494 * argument string.
495 *
496 * @param {function} commandClass The constructor for a terminal command.
497 * @param {string} argString The argument string to pass to the command.
498 */
499hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700500 var environment = this.prefs_.get('environment');
501 if (typeof environment != 'object' || environment == null)
502 environment = {};
503
rginda87b86462011-12-14 13:48:03 -0800504 var self = this;
505 this.command = new commandClass(
506 { argString: argString || '',
507 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700508 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800509 onExit: function(code) {
510 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800511 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700512 if (self.prefs_.get('close-on-exit'))
513 window.close();
rginda87b86462011-12-14 13:48:03 -0800514 }
515 });
516
rgindafeaf3142012-01-31 15:14:20 -0800517 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800518 this.command.run();
519};
520
521/**
rgindafeaf3142012-01-31 15:14:20 -0800522 * Returns true if the current screen is the primary screen, false otherwise.
523 */
524hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700525 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800526};
527
528/**
529 * Install the keyboard handler for this terminal.
530 *
531 * This will prevent the browser from seeing any keystrokes sent to the
532 * terminal.
533 */
534hterm.Terminal.prototype.installKeyboard = function() {
Toni Barzic0bfa8922013-11-22 11:18:35 -0800535 this.keyboard.installKeyboard(this.scrollPort_.getScreenNode());
rgindafeaf3142012-01-31 15:14:20 -0800536}
537
538/**
539 * Uninstall the keyboard handler for this terminal.
540 */
541hterm.Terminal.prototype.uninstallKeyboard = function() {
542 this.keyboard.installKeyboard(null);
543}
544
545/**
rginda35c456b2012-02-09 17:29:05 -0800546 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800547 *
548 * Call setFontSize(0) to reset to the default font size.
549 *
550 * This function does not modify the font-size preference.
551 *
552 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800553 */
554hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800555 if (px === 0)
556 px = this.prefs_.get('font-size');
557
rginda35c456b2012-02-09 17:29:05 -0800558 this.scrollPort_.setFontSize(px);
Ricky Liang48f05cb2013-12-31 23:35:29 +0800559 if (this.wcCssRule_) {
560 this.wcCssRule_.style.width = this.scrollPort_.characterSize.width * 2 +
561 'px';
562 }
rginda35c456b2012-02-09 17:29:05 -0800563};
564
565/**
566 * Get the current font size.
567 */
568hterm.Terminal.prototype.getFontSize = function() {
569 return this.scrollPort_.getFontSize();
570};
571
572/**
rginda8e92a692012-05-20 19:37:20 -0700573 * Get the current font family.
574 */
575hterm.Terminal.prototype.getFontFamily = function() {
576 return this.scrollPort_.getFontFamily();
577};
578
579/**
rginda35c456b2012-02-09 17:29:05 -0800580 * Set the CSS "font-family" for this terminal.
581 */
rginda9f5222b2012-03-05 11:53:28 -0800582hterm.Terminal.prototype.syncFontFamily = function() {
583 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
584 this.prefs_.get('font-smoothing'));
585 this.syncBoldSafeState();
586};
587
rginda4bba5e12012-06-20 16:15:30 -0700588/**
589 * Set this.mousePasteButton based on the mouse-paste-button pref,
590 * autodetecting if necessary.
591 */
592hterm.Terminal.prototype.syncMousePasteButton = function() {
593 var button = this.prefs_.get('mouse-paste-button');
594 if (typeof button == 'number') {
595 this.mousePasteButton = button;
596 return;
597 }
598
599 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
600 if (!ary || ary[2] == 'CrOS') {
601 this.mousePasteButton = 2;
602 } else {
603 this.mousePasteButton = 3;
604 }
605};
606
607/**
608 * Enable or disable bold based on the enable-bold pref, autodetecting if
609 * necessary.
610 */
rginda9f5222b2012-03-05 11:53:28 -0800611hterm.Terminal.prototype.syncBoldSafeState = function() {
612 var enableBold = this.prefs_.get('enable-bold');
613 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700614 this.primaryScreen_.textAttributes.enableBold = enableBold;
615 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800616 return;
617 }
618
rgindaf7521392012-02-28 17:20:34 -0800619 var normalSize = this.scrollPort_.measureCharacterSize();
620 var boldSize = this.scrollPort_.measureCharacterSize('bold');
621
622 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800623 if (!isBoldSafe) {
624 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700625 'from normal. Font family is: ' +
626 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800627 }
rginda9f5222b2012-03-05 11:53:28 -0800628
Robert Gindaed016262012-10-26 16:27:09 -0700629 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
630 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800631};
632
633/**
rginda87b86462011-12-14 13:48:03 -0800634 * Return a copy of the current cursor position.
635 *
636 * @return {hterm.RowCol} The RowCol object representing the current position.
637 */
638hterm.Terminal.prototype.saveCursor = function() {
639 return this.screen_.cursorPosition.clone();
640};
641
rgindaa19afe22012-01-25 15:40:22 -0800642hterm.Terminal.prototype.getTextAttributes = function() {
643 return this.screen_.textAttributes;
644};
645
rginda1a09aa02012-06-18 21:11:25 -0700646hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
647 this.screen_.textAttributes = textAttributes;
648};
649
rginda87b86462011-12-14 13:48:03 -0800650/**
rgindaf522ce02012-04-17 17:49:17 -0700651 * Return the current browser zoom factor applied to the terminal.
652 *
653 * @return {number} The current browser zoom factor.
654 */
655hterm.Terminal.prototype.getZoomFactor = function() {
656 return this.scrollPort_.characterSize.zoomFactor;
657};
658
659/**
rginda9846e2f2012-01-27 13:53:33 -0800660 * Change the title of this terminal's window.
661 */
662hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800663 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800664};
665
666/**
rginda87b86462011-12-14 13:48:03 -0800667 * Restore a previously saved cursor position.
668 *
669 * @param {hterm.RowCol} cursor The position to restore.
670 */
671hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700672 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
673 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800674 this.screen_.setCursorPosition(row, column);
675 if (cursor.column > column ||
676 cursor.column == column && cursor.overflow) {
677 this.screen_.cursorPosition.overflow = true;
678 }
rginda87b86462011-12-14 13:48:03 -0800679};
680
681/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400682 * Clear the cursor's overflow flag.
683 */
684hterm.Terminal.prototype.clearCursorOverflow = function() {
685 this.screen_.cursorPosition.overflow = false;
686};
687
688/**
Robert Ginda830583c2013-08-07 13:20:46 -0700689 * Sets the cursor shape
690 */
691hterm.Terminal.prototype.setCursorShape = function(shape) {
692 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800693 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700694}
695
696/**
697 * Get the cursor shape
698 */
699hterm.Terminal.prototype.getCursorShape = function() {
700 return this.cursorShape_;
701}
702
703/**
rginda87b86462011-12-14 13:48:03 -0800704 * Set the width of the terminal, resizing the UI to match.
705 */
706hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800707 if (columnCount == null) {
708 this.div_.style.width = '100%';
709 return;
710 }
711
rginda35c456b2012-02-09 17:29:05 -0800712 this.div_.style.width = this.scrollPort_.characterSize.width *
Robert Ginda97769282013-02-01 15:30:30 -0800713 columnCount + this.scrollPort_.currentScrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400714 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800715 this.scheduleSyncCursorPosition_();
716};
rginda87b86462011-12-14 13:48:03 -0800717
rgindac9bc5502012-01-18 11:48:44 -0800718/**
rginda35c456b2012-02-09 17:29:05 -0800719 * Set the height of the terminal, resizing the UI to match.
720 */
721hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800722 if (rowCount == null) {
723 this.div_.style.height = '100%';
724 return;
725 }
726
rginda35c456b2012-02-09 17:29:05 -0800727 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700728 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800729 this.realizeSize_(this.screenSize.width, rowCount);
730 this.scheduleSyncCursorPosition_();
731};
732
733/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400734 * Deal with terminal size changes.
735 *
736 */
737hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
738 if (columnCount != this.screenSize.width)
739 this.realizeWidth_(columnCount);
740
741 if (rowCount != this.screenSize.height)
742 this.realizeHeight_(rowCount);
743
744 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700745 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400746};
747
748/**
rgindac9bc5502012-01-18 11:48:44 -0800749 * Deal with terminal width changes.
750 *
751 * This function does what needs to be done when the terminal width changes
752 * out from under us. It happens here rather than in onResize_() because this
753 * code may need to run synchronously to handle programmatic changes of
754 * terminal width.
755 *
756 * Relying on the browser to send us an async resize event means we may not be
757 * in the correct state yet when the next escape sequence hits.
758 */
759hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700760 if (columnCount <= 0)
761 throw new Error('Attempt to realize bad width: ' + columnCount);
762
rgindac9bc5502012-01-18 11:48:44 -0800763 var deltaColumns = columnCount - this.screen_.getWidth();
764
rginda87b86462011-12-14 13:48:03 -0800765 this.screenSize.width = columnCount;
766 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800767
768 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400769 if (this.defaultTabStops)
770 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800771 } else {
772 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400773 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800774 break;
775
776 this.tabStops_.pop();
777 }
778 }
779
780 this.screen_.setColumnCount(this.screenSize.width);
781};
782
783/**
784 * Deal with terminal height changes.
785 *
786 * This function does what needs to be done when the terminal height changes
787 * out from under us. It happens here rather than in onResize_() because this
788 * code may need to run synchronously to handle programmatic changes of
789 * terminal height.
790 *
791 * Relying on the browser to send us an async resize event means we may not be
792 * in the correct state yet when the next escape sequence hits.
793 */
794hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700795 if (rowCount <= 0)
796 throw new Error('Attempt to realize bad height: ' + rowCount);
797
rgindac9bc5502012-01-18 11:48:44 -0800798 var deltaRows = rowCount - this.screen_.getHeight();
799
800 this.screenSize.height = rowCount;
801
802 var cursor = this.saveCursor();
803
804 if (deltaRows < 0) {
805 // Screen got smaller.
806 deltaRows *= -1;
807 while (deltaRows) {
808 var lastRow = this.getRowCount() - 1;
809 if (lastRow - this.scrollbackRows_.length == cursor.row)
810 break;
811
812 if (this.getRowText(lastRow))
813 break;
814
815 this.screen_.popRow();
816 deltaRows--;
817 }
818
819 var ary = this.screen_.shiftRows(deltaRows);
820 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
821
822 // We just removed rows from the top of the screen, we need to update
823 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800824 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800825 } else if (deltaRows > 0) {
826 // Screen got larger.
827
828 if (deltaRows <= this.scrollbackRows_.length) {
829 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
830 var rows = this.scrollbackRows_.splice(
831 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
832 this.screen_.unshiftRows(rows);
833 deltaRows -= scrollbackCount;
834 cursor.row += scrollbackCount;
835 }
836
837 if (deltaRows)
838 this.appendRows_(deltaRows);
839 }
840
rginda35c456b2012-02-09 17:29:05 -0800841 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800842 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800843};
844
845/**
846 * Scroll the terminal to the top of the scrollback buffer.
847 */
848hterm.Terminal.prototype.scrollHome = function() {
849 this.scrollPort_.scrollRowToTop(0);
850};
851
852/**
853 * Scroll the terminal to the end.
854 */
855hterm.Terminal.prototype.scrollEnd = function() {
856 this.scrollPort_.scrollRowToBottom(this.getRowCount());
857};
858
859/**
860 * Scroll the terminal one page up (minus one line) relative to the current
861 * position.
862 */
863hterm.Terminal.prototype.scrollPageUp = function() {
864 var i = this.scrollPort_.getTopRowIndex();
865 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
866};
867
868/**
869 * Scroll the terminal one page down (minus one line) relative to the current
870 * position.
871 */
872hterm.Terminal.prototype.scrollPageDown = function() {
873 var i = this.scrollPort_.getTopRowIndex();
874 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800875};
876
rgindac9bc5502012-01-18 11:48:44 -0800877/**
Robert Ginda40932892012-12-10 17:26:40 -0800878 * Clear primary screen, secondary screen, and the scrollback buffer.
879 */
880hterm.Terminal.prototype.wipeContents = function() {
881 this.scrollbackRows_.length = 0;
882 this.scrollPort_.resetCache();
883
884 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
885 var bottom = screen.getHeight();
886 if (bottom > 0) {
887 this.renumberRows_(0, bottom);
888 this.clearHome(screen);
889 }
890 }.bind(this));
891
892 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -0700893 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -0800894};
895
896/**
rgindac9bc5502012-01-18 11:48:44 -0800897 * Full terminal reset.
898 */
rginda87b86462011-12-14 13:48:03 -0800899hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800900 this.clearAllTabStops();
901 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700902
903 this.clearHome(this.primaryScreen_);
904 this.primaryScreen_.textAttributes.reset();
905
906 this.clearHome(this.alternateScreen_);
907 this.alternateScreen_.textAttributes.reset();
908
rgindab8bc8932012-04-27 12:45:03 -0700909 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
910
Robert Ginda92e18102013-03-14 13:56:37 -0700911 this.vt.reset();
912
rgindac9bc5502012-01-18 11:48:44 -0800913 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800914};
915
rgindac9bc5502012-01-18 11:48:44 -0800916/**
917 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700918 *
919 * Perform a soft reset to the default values listed in
920 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800921 */
rginda0f5c0292012-01-13 11:00:13 -0800922hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700923 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800924 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700925
rgindab8bc8932012-04-27 12:45:03 -0700926 // Xterm also resets the color palette on soft reset, even though it doesn't
927 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700928 this.primaryScreen_.textAttributes.resetColorPalette();
929 this.alternateScreen_.textAttributes.resetColorPalette();
930
rgindab8bc8932012-04-27 12:45:03 -0700931 // The xterm man page explicitly says this will happen on soft reset.
932 this.setVTScrollRegion(null, null);
933
934 // Xterm also shows the cursor on soft reset, but does not alter the blink
935 // state.
rgindaa19afe22012-01-25 15:40:22 -0800936 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800937};
938
rgindac9bc5502012-01-18 11:48:44 -0800939/**
940 * Move the cursor forward to the next tab stop, or to the last column
941 * if no more tab stops are set.
942 */
943hterm.Terminal.prototype.forwardTabStop = function() {
944 var column = this.screen_.cursorPosition.column;
945
946 for (var i = 0; i < this.tabStops_.length; i++) {
947 if (this.tabStops_[i] > column) {
948 this.setCursorColumn(this.tabStops_[i]);
949 return;
950 }
951 }
952
David Benjamin66e954d2012-05-05 21:08:12 -0400953 // xterm does not clear the overflow flag on HT or CHT.
954 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800955 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400956 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800957};
958
rgindac9bc5502012-01-18 11:48:44 -0800959/**
960 * Move the cursor backward to the previous tab stop, or to the first column
961 * if no previous tab stops are set.
962 */
963hterm.Terminal.prototype.backwardTabStop = function() {
964 var column = this.screen_.cursorPosition.column;
965
966 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
967 if (this.tabStops_[i] < column) {
968 this.setCursorColumn(this.tabStops_[i]);
969 return;
970 }
971 }
972
973 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800974};
975
rgindac9bc5502012-01-18 11:48:44 -0800976/**
977 * Set a tab stop at the given column.
978 *
979 * @param {int} column Zero based column.
980 */
981hterm.Terminal.prototype.setTabStop = function(column) {
982 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
983 if (this.tabStops_[i] == column)
984 return;
985
986 if (this.tabStops_[i] < column) {
987 this.tabStops_.splice(i + 1, 0, column);
988 return;
989 }
990 }
991
992 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800993};
994
rgindac9bc5502012-01-18 11:48:44 -0800995/**
996 * Clear the tab stop at the current cursor position.
997 *
998 * No effect if there is no tab stop at the current cursor position.
999 */
1000hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1001 var column = this.screen_.cursorPosition.column;
1002
1003 var i = this.tabStops_.indexOf(column);
1004 if (i == -1)
1005 return;
1006
1007 this.tabStops_.splice(i, 1);
1008};
1009
1010/**
1011 * Clear all tab stops.
1012 */
1013hterm.Terminal.prototype.clearAllTabStops = function() {
1014 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001015 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001016};
1017
1018/**
1019 * Set up the default tab stops, starting from a given column.
1020 *
1021 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001022 * from the specified column, or 0 if no column is provided. It also flags
1023 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001024 *
1025 * This does not clear the existing tab stops first, use clearAllTabStops
1026 * for that.
1027 *
1028 * @param {int} opt_start Optional starting zero based starting column, useful
1029 * for filling out missing tab stops when the terminal is resized.
1030 */
1031hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1032 var start = opt_start || 0;
1033 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001034 // Round start up to a default tab stop.
1035 start = start - 1 - ((start - 1) % w) + w;
1036 for (var i = start; i < this.screenSize.width; i += w) {
1037 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001038 }
David Benjamin66e954d2012-05-05 21:08:12 -04001039
1040 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001041};
1042
rginda6d397402012-01-17 10:58:29 -08001043/**
rginda8ba33642011-12-14 12:31:31 -08001044 * Interpret a sequence of characters.
1045 *
1046 * Incomplete escape sequences are buffered until the next call.
1047 *
1048 * @param {string} str Sequence of characters to interpret or pass through.
1049 */
1050hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001051 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001052 this.scheduleSyncCursorPosition_();
1053};
1054
1055/**
1056 * Take over the given DIV for use as the terminal display.
1057 *
1058 * @param {HTMLDivElement} div The div to use as the terminal display.
1059 */
1060hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001061 this.div_ = div;
1062
rginda8ba33642011-12-14 12:31:31 -08001063 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001064 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001065 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1066 this.scrollPort_.setBackgroundPosition(
1067 this.prefs_.get('background-position'));
Robert Gindae76aa9f2014-03-14 12:29:12 -07001068 this.scrollPort_.setUserCss(this.prefs_.get('user-css'));
rginda30f20f62012-04-05 16:36:19 -07001069
rginda0918b652012-04-04 11:26:24 -07001070 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001071
rginda9f5222b2012-03-05 11:53:28 -08001072 this.setFontSize(this.prefs_.get('font-size'));
1073 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001074
David Reveman8f552492012-03-28 12:18:41 -04001075 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1076
rginda8ba33642011-12-14 12:31:31 -08001077 this.document_ = this.scrollPort_.getDocument();
1078
rginda4bba5e12012-06-20 16:15:30 -07001079 this.document_.body.oncontextmenu = function() { return false };
1080
1081 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001082 var screenNode = this.scrollPort_.getScreenNode();
1083 screenNode.addEventListener('mousedown', onMouse);
1084 screenNode.addEventListener('mouseup', onMouse);
1085 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001086 this.scrollPort_.onScrollWheel = onMouse;
1087
Toni Barzic0bfa8922013-11-22 11:18:35 -08001088 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001089 'focus', this.onFocusChange_.bind(this, true));
Toni Barzic0bfa8922013-11-22 11:18:35 -08001090 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001091 'blur', this.onFocusChange_.bind(this, false));
1092
1093 var style = this.document_.createElement('style');
1094 style.textContent =
1095 ('.cursor-node[focus="false"] {' +
1096 ' box-sizing: border-box;' +
1097 ' background-color: transparent !important;' +
1098 ' border-width: 2px;' +
1099 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001100 '}' +
1101 '.wc-node {' +
1102 ' display: inline-block;' +
1103 ' text-align: center;' +
1104 ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001105 '}');
1106 this.document_.head.appendChild(style);
1107
Ricky Liang48f05cb2013-12-31 23:35:29 +08001108 var styleSheets = this.document_.styleSheets;
1109 var cssRules = styleSheets[styleSheets.length - 1].cssRules;
1110 this.wcCssRule_ = cssRules[cssRules.length - 1];
1111
rginda8ba33642011-12-14 12:31:31 -08001112 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001113 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001114 this.cursorNode_.style.cssText =
1115 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001116 'top: -99px;' +
1117 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001118 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1119 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001120 '-webkit-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001121
rginda8e92a692012-05-20 19:37:20 -07001122 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001123 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1124 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001125
rginda8ba33642011-12-14 12:31:31 -08001126 this.document_.body.appendChild(this.cursorNode_);
1127
rgindad5613292012-06-19 15:40:37 -07001128 // When 'enableMouseDragScroll' is off we reposition this element directly
1129 // under the mouse cursor after a click. This makes Chrome associate
1130 // subsequent mousemove events with the scroll-blocker. Since the
1131 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1132 // events do not cause the scrollport to scroll.
1133 //
1134 // It's a hack, but it's the cleanest way I could find.
1135 this.scrollBlockerNode_ = this.document_.createElement('div');
1136 this.scrollBlockerNode_.style.cssText =
1137 ('position: absolute;' +
1138 'top: -99px;' +
1139 'display: block;' +
1140 'width: 10px;' +
1141 'height: 10px;');
1142 this.document_.body.appendChild(this.scrollBlockerNode_);
1143
1144 var onMouse = this.onMouse_.bind(this);
1145 this.scrollPort_.onScrollWheel = onMouse;
1146 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1147 ].forEach(function(event) {
1148 this.scrollBlockerNode_.addEventListener(event, onMouse);
1149 this.cursorNode_.addEventListener(event, onMouse);
1150 this.document_.addEventListener(event, onMouse);
1151 }.bind(this));
1152
1153 this.cursorNode_.addEventListener('mousedown', function() {
1154 setTimeout(this.focus.bind(this));
1155 }.bind(this));
1156
rginda8ba33642011-12-14 12:31:31 -08001157 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001158
rginda87b86462011-12-14 13:48:03 -08001159 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001160 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001161};
1162
rginda0918b652012-04-04 11:26:24 -07001163/**
1164 * Return the HTML document that contains the terminal DOM nodes.
1165 */
rginda87b86462011-12-14 13:48:03 -08001166hterm.Terminal.prototype.getDocument = function() {
1167 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001168};
1169
1170/**
rginda0918b652012-04-04 11:26:24 -07001171 * Focus the terminal.
1172 */
1173hterm.Terminal.prototype.focus = function() {
1174 this.scrollPort_.focus();
1175};
1176
1177/**
rginda8ba33642011-12-14 12:31:31 -08001178 * Return the HTML Element for a given row index.
1179 *
1180 * This is a method from the RowProvider interface. The ScrollPort uses
1181 * it to fetch rows on demand as they are scrolled into view.
1182 *
1183 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1184 * pairs to conserve memory.
1185 *
1186 * @param {integer} index The zero-based row index, measured relative to the
1187 * start of the scrollback buffer. On-screen rows will always have the
1188 * largest indicies.
1189 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1190 */
1191hterm.Terminal.prototype.getRowNode = function(index) {
1192 if (index < this.scrollbackRows_.length)
1193 return this.scrollbackRows_[index];
1194
1195 var screenIndex = index - this.scrollbackRows_.length;
1196 return this.screen_.rowsArray[screenIndex];
1197};
1198
1199/**
1200 * Return the text content for a given range of rows.
1201 *
1202 * This is a method from the RowProvider interface. The ScrollPort uses
1203 * it to fetch text content on demand when the user attempts to copy their
1204 * selection to the clipboard.
1205 *
1206 * @param {integer} start The zero-based row index to start from, measured
1207 * relative to the start of the scrollback buffer. On-screen rows will
1208 * always have the largest indicies.
1209 * @param {integer} end The zero-based row index to end on, measured
1210 * relative to the start of the scrollback buffer.
1211 * @return {string} A single string containing the text value of the range of
1212 * rows. Lines will be newline delimited, with no trailing newline.
1213 */
1214hterm.Terminal.prototype.getRowsText = function(start, end) {
1215 var ary = [];
1216 for (var i = start; i < end; i++) {
1217 var node = this.getRowNode(i);
1218 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001219 if (i < end - 1 && !node.getAttribute('line-overflow'))
1220 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001221 }
1222
rgindaa09e7332012-08-17 12:49:51 -07001223 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001224};
1225
1226/**
1227 * Return the text content for a given row.
1228 *
1229 * This is a method from the RowProvider interface. The ScrollPort uses
1230 * it to fetch text content on demand when the user attempts to copy their
1231 * selection to the clipboard.
1232 *
1233 * @param {integer} index The zero-based row index to return, measured
1234 * relative to the start of the scrollback buffer. On-screen rows will
1235 * always have the largest indicies.
1236 * @return {string} A string containing the text value of the selected row.
1237 */
1238hterm.Terminal.prototype.getRowText = function(index) {
1239 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001240 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001241};
1242
1243/**
1244 * Return the total number of rows in the addressable screen and in the
1245 * scrollback buffer of this terminal.
1246 *
1247 * This is a method from the RowProvider interface. The ScrollPort uses
1248 * it to compute the size of the scrollbar.
1249 *
1250 * @return {integer} The number of rows in this terminal.
1251 */
1252hterm.Terminal.prototype.getRowCount = function() {
1253 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1254};
1255
1256/**
1257 * Create DOM nodes for new rows and append them to the end of the terminal.
1258 *
1259 * This is the only correct way to add a new DOM node for a row. Notice that
1260 * the new row is appended to the bottom of the list of rows, and does not
1261 * require renumbering (of the rowIndex property) of previous rows.
1262 *
1263 * If you think you want a new blank row somewhere in the middle of the
1264 * terminal, look into moveRows_().
1265 *
1266 * This method does not pay attention to vtScrollTop/Bottom, since you should
1267 * be using moveRows() in cases where they would matter.
1268 *
1269 * The cursor will be positioned at column 0 of the first inserted line.
1270 */
1271hterm.Terminal.prototype.appendRows_ = function(count) {
1272 var cursorRow = this.screen_.rowsArray.length;
1273 var offset = this.scrollbackRows_.length + cursorRow;
1274 for (var i = 0; i < count; i++) {
1275 var row = this.document_.createElement('x-row');
1276 row.appendChild(this.document_.createTextNode(''));
1277 row.rowIndex = offset + i;
1278 this.screen_.pushRow(row);
1279 }
1280
1281 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1282 if (extraRows > 0) {
1283 var ary = this.screen_.shiftRows(extraRows);
1284 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001285 if (this.scrollPort_.isScrolledEnd)
1286 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001287 }
1288
1289 if (cursorRow >= this.screen_.rowsArray.length)
1290 cursorRow = this.screen_.rowsArray.length - 1;
1291
rginda87b86462011-12-14 13:48:03 -08001292 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001293};
1294
1295/**
1296 * Relocate rows from one part of the addressable screen to another.
1297 *
1298 * This is used to recycle rows during VT scrolls (those which are driven
1299 * by VT commands, rather than by the user manipulating the scrollbar.)
1300 *
1301 * In this case, the blank lines scrolled into the scroll region are made of
1302 * the nodes we scrolled off. These have their rowIndex properties carefully
1303 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001304 */
1305hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1306 var ary = this.screen_.removeRows(fromIndex, count);
1307 this.screen_.insertRows(toIndex, ary);
1308
1309 var start, end;
1310 if (fromIndex < toIndex) {
1311 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001312 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001313 } else {
1314 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001315 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001316 }
1317
1318 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001319 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001320};
1321
1322/**
1323 * Renumber the rowIndex property of the given range of rows.
1324 *
1325 * The start and end indicies are relative to the screen, not the scrollback.
1326 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001327 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001328 * no need to renumber scrollback rows.
1329 */
Robert Ginda40932892012-12-10 17:26:40 -08001330hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1331 var screen = opt_screen || this.screen_;
1332
rginda8ba33642011-12-14 12:31:31 -08001333 var offset = this.scrollbackRows_.length;
1334 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001335 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001336 }
1337};
1338
1339/**
1340 * Print a string to the terminal.
1341 *
1342 * This respects the current insert and wraparound modes. It will add new lines
1343 * to the end of the terminal, scrolling off the top into the scrollback buffer
1344 * if necessary.
1345 *
1346 * The string is *not* parsed for escape codes. Use the interpret() method if
1347 * that's what you're after.
1348 *
1349 * @param{string} str The string to print.
1350 */
1351hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001352 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001353
Ricky Liang48f05cb2013-12-31 23:35:29 +08001354 var strWidth = lib.wc.strWidth(str);
1355
1356 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001357 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1358 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001359 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001360 }
rgindaa19afe22012-01-25 15:40:22 -08001361
Ricky Liang48f05cb2013-12-31 23:35:29 +08001362 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001363 var didOverflow = false;
1364 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001365
rgindaa9abdd82012-08-06 18:05:09 -07001366 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1367 didOverflow = true;
1368 count = this.screenSize.width - this.screen_.cursorPosition.column;
1369 }
rgindaa19afe22012-01-25 15:40:22 -08001370
rgindaa9abdd82012-08-06 18:05:09 -07001371 if (didOverflow && !this.options_.wraparound) {
1372 // If the string overflowed the line but wraparound is off, then the
1373 // last printed character should be the last of the string.
1374 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001375 substr = lib.wc.substr(str, startOffset, count - 1) +
1376 lib.wc.substr(str, strWidth - 1);
1377 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001378 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001379 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001380 }
rgindaa19afe22012-01-25 15:40:22 -08001381
Ricky Liang48f05cb2013-12-31 23:35:29 +08001382 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1383 for (var i = 0; i < tokens.length; i++) {
1384 if (tokens[i].wcNode)
1385 this.screen_.textAttributes.wcNode = true;
1386
1387 if (this.options_.insertMode) {
1388 this.screen_.insertString(tokens[i].str);
1389 } else {
1390 this.screen_.overwriteString(tokens[i].str);
1391 }
1392 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001393 }
1394
1395 this.screen_.maybeClipCurrentRow();
1396 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001397 }
rginda8ba33642011-12-14 12:31:31 -08001398
1399 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001400
rginda9f5222b2012-03-05 11:53:28 -08001401 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001402 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001403};
1404
1405/**
rginda87b86462011-12-14 13:48:03 -08001406 * Set the VT scroll region.
1407 *
rginda87b86462011-12-14 13:48:03 -08001408 * This also resets the cursor position to the absolute (0, 0) position, since
1409 * that's what xterm appears to do.
1410 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001411 * Setting the scroll region to the full height of the terminal will clear
1412 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1413 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1414 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1415 * continue to work as most users would expect.
1416 *
rginda87b86462011-12-14 13:48:03 -08001417 * @param {integer} scrollTop The zero-based top of the scroll region.
1418 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1419 * inclusive.
1420 */
1421hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001422 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001423 this.vtScrollTop_ = null;
1424 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001425 } else {
1426 this.vtScrollTop_ = scrollTop;
1427 this.vtScrollBottom_ = scrollBottom;
1428 }
rginda87b86462011-12-14 13:48:03 -08001429};
1430
1431/**
rginda8ba33642011-12-14 12:31:31 -08001432 * Return the top row index according to the VT.
1433 *
1434 * This will return 0 unless the terminal has been told to restrict scrolling
1435 * to some lower row. It is used for some VT cursor positioning and scrolling
1436 * commands.
1437 *
1438 * @return {integer} The topmost row in the terminal's scroll region.
1439 */
1440hterm.Terminal.prototype.getVTScrollTop = function() {
1441 if (this.vtScrollTop_ != null)
1442 return this.vtScrollTop_;
1443
1444 return 0;
rginda87b86462011-12-14 13:48:03 -08001445};
rginda8ba33642011-12-14 12:31:31 -08001446
1447/**
1448 * Return the bottom row index according to the VT.
1449 *
1450 * This will return the height of the terminal unless the it has been told to
1451 * restrict scrolling to some higher row. It is used for some VT cursor
1452 * positioning and scrolling commands.
1453 *
1454 * @return {integer} The bottommost row in the terminal's scroll region.
1455 */
1456hterm.Terminal.prototype.getVTScrollBottom = function() {
1457 if (this.vtScrollBottom_ != null)
1458 return this.vtScrollBottom_;
1459
rginda87b86462011-12-14 13:48:03 -08001460 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001461}
1462
1463/**
1464 * Process a '\n' character.
1465 *
1466 * If the cursor is on the final row of the terminal this will append a new
1467 * blank row to the screen and scroll the topmost row into the scrollback
1468 * buffer.
1469 *
1470 * Otherwise, this moves the cursor to column zero of the next row.
1471 */
1472hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001473 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1474 this.screen_.rowsArray.length - 1);
1475
1476 if (this.vtScrollBottom_ != null) {
1477 // A VT Scroll region is active, we never append new rows.
1478 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1479 // We're at the end of the VT Scroll Region, perform a VT scroll.
1480 this.vtScrollUp(1);
1481 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1482 } else if (cursorAtEndOfScreen) {
1483 // We're at the end of the screen, the only thing to do is put the
1484 // cursor to column 0.
1485 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1486 } else {
1487 // Anywhere else, advance the cursor row, and reset the column.
1488 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1489 }
1490 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001491 // We're at the end of the screen. Append a new row to the terminal,
1492 // shifting the top row into the scrollback.
1493 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001494 } else {
rginda87b86462011-12-14 13:48:03 -08001495 // Anywhere else in the screen just moves the cursor.
1496 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001497 }
1498};
1499
1500/**
1501 * Like newLine(), except maintain the cursor column.
1502 */
1503hterm.Terminal.prototype.lineFeed = function() {
1504 var column = this.screen_.cursorPosition.column;
1505 this.newLine();
1506 this.setCursorColumn(column);
1507};
1508
1509/**
rginda87b86462011-12-14 13:48:03 -08001510 * If autoCarriageReturn is set then newLine(), else lineFeed().
1511 */
1512hterm.Terminal.prototype.formFeed = function() {
1513 if (this.options_.autoCarriageReturn) {
1514 this.newLine();
1515 } else {
1516 this.lineFeed();
1517 }
1518};
1519
1520/**
1521 * Move the cursor up one row, possibly inserting a blank line.
1522 *
1523 * The cursor column is not changed.
1524 */
1525hterm.Terminal.prototype.reverseLineFeed = function() {
1526 var scrollTop = this.getVTScrollTop();
1527 var currentRow = this.screen_.cursorPosition.row;
1528
1529 if (currentRow == scrollTop) {
1530 this.insertLines(1);
1531 } else {
1532 this.setAbsoluteCursorRow(currentRow - 1);
1533 }
1534};
1535
1536/**
rginda8ba33642011-12-14 12:31:31 -08001537 * Replace all characters to the left of the current cursor with the space
1538 * character.
1539 *
1540 * TODO(rginda): This should probably *remove* the characters (not just replace
1541 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001542 * position.
rginda8ba33642011-12-14 12:31:31 -08001543 */
1544hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001545 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001546 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001547 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001548 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001549};
1550
1551/**
David Benjamin684a9b72012-05-01 17:19:58 -04001552 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001553 *
1554 * The cursor position is unchanged.
1555 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001556 * If the current background color is not the default background color this
1557 * will insert spaces rather than delete. This is unfortunate because the
1558 * trailing space will affect text selection, but it's difficult to come up
1559 * with a way to style empty space that wouldn't trip up the hterm.Screen
1560 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001561 *
1562 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1563 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1564 * crbug.com/232390 for details.
rginda8ba33642011-12-14 12:31:31 -08001565 */
1566hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001567 if (this.screen_.cursorPosition.overflow)
1568 return;
1569
Robert Ginda7fd57082012-09-25 14:41:47 -07001570 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1571 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001572
1573 if (this.screen_.textAttributes.background ===
1574 this.screen_.textAttributes.DEFAULT_COLOR) {
1575 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001576 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001577 this.screen_.cursorPosition.column + count) {
1578 this.screen_.deleteChars(count);
1579 this.clearCursorOverflow();
1580 return;
1581 }
1582 }
1583
rginda87b86462011-12-14 13:48:03 -08001584 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001585 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001586 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001587 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001588};
1589
1590/**
1591 * Erase the current line.
1592 *
1593 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001594 */
1595hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001596 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001597 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001598 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001599 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001600};
1601
1602/**
David Benjamina08d78f2012-05-05 00:28:49 -04001603 * Erase all characters from the start of the screen to the current cursor
1604 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001605 *
1606 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001607 */
1608hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001609 var cursor = this.saveCursor();
1610
1611 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001612
David Benjamina08d78f2012-05-05 00:28:49 -04001613 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001614 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001615 this.screen_.clearCursorRow();
1616 }
1617
rginda87b86462011-12-14 13:48:03 -08001618 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001619 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001620};
1621
1622/**
1623 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001624 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001625 *
1626 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001627 */
1628hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001629 var cursor = this.saveCursor();
1630
1631 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001632
David Benjamina08d78f2012-05-05 00:28:49 -04001633 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001634 for (var i = cursor.row + 1; i <= bottom; i++) {
1635 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001636 this.screen_.clearCursorRow();
1637 }
1638
rginda87b86462011-12-14 13:48:03 -08001639 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001640 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001641};
1642
1643/**
1644 * Fill the terminal with a given character.
1645 *
1646 * This methods does not respect the VT scroll region.
1647 *
1648 * @param {string} ch The character to use for the fill.
1649 */
1650hterm.Terminal.prototype.fill = function(ch) {
1651 var cursor = this.saveCursor();
1652
1653 this.setAbsoluteCursorPosition(0, 0);
1654 for (var row = 0; row < this.screenSize.height; row++) {
1655 for (var col = 0; col < this.screenSize.width; col++) {
1656 this.setAbsoluteCursorPosition(row, col);
1657 this.screen_.overwriteString(ch);
1658 }
1659 }
1660
1661 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001662};
1663
1664/**
rginda9ea433c2012-03-16 11:57:00 -07001665 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001666 *
rginda9ea433c2012-03-16 11:57:00 -07001667 * This does not respect the scroll region.
1668 *
1669 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1670 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001671 */
rginda9ea433c2012-03-16 11:57:00 -07001672hterm.Terminal.prototype.clearHome = function(opt_screen) {
1673 var screen = opt_screen || this.screen_;
1674 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001675
rginda11057d52012-04-25 12:29:56 -07001676 if (bottom == 0) {
1677 // Empty screen, nothing to do.
1678 return;
1679 }
1680
rgindae4d29232012-01-19 10:47:13 -08001681 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001682 screen.setCursorPosition(i, 0);
1683 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001684 }
1685
rginda9ea433c2012-03-16 11:57:00 -07001686 screen.setCursorPosition(0, 0);
1687};
1688
1689/**
1690 * Erase the entire display without changing the cursor position.
1691 *
1692 * The cursor position is unchanged. This does not respect the scroll
1693 * region.
1694 *
1695 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1696 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001697 */
1698hterm.Terminal.prototype.clear = function(opt_screen) {
1699 var screen = opt_screen || this.screen_;
1700 var cursor = screen.cursorPosition.clone();
1701 this.clearHome(screen);
1702 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001703};
1704
1705/**
1706 * VT command to insert lines at the current cursor row.
1707 *
1708 * This respects the current scroll region. Rows pushed off the bottom are
1709 * lost (they won't show up in the scrollback buffer).
1710 *
rginda8ba33642011-12-14 12:31:31 -08001711 * @param {integer} count The number of lines to insert.
1712 */
1713hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001714 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001715
1716 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001717 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001718
Robert Ginda579186b2012-09-26 11:40:04 -07001719 // The moveCount is the number of rows we need to relocate to make room for
1720 // the new row(s). The count is the distance to move them.
1721 var moveCount = bottom - cursorRow - count + 1;
1722 if (moveCount)
1723 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001724
Robert Ginda579186b2012-09-26 11:40:04 -07001725 for (var i = count - 1; i >= 0; i--) {
1726 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001727 this.screen_.clearCursorRow();
1728 }
rginda8ba33642011-12-14 12:31:31 -08001729};
1730
1731/**
1732 * VT command to delete lines at the current cursor row.
1733 *
1734 * New rows are added to the bottom of scroll region to take their place. New
1735 * rows are strictly there to take up space and have no content or style.
1736 */
1737hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001738 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001739
rginda87b86462011-12-14 13:48:03 -08001740 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001741 var bottom = this.getVTScrollBottom();
1742
rginda87b86462011-12-14 13:48:03 -08001743 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001744 count = Math.min(count, maxCount);
1745
rginda87b86462011-12-14 13:48:03 -08001746 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001747 if (count != maxCount)
1748 this.moveRows_(top, count, moveStart);
1749
1750 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001751 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001752 this.screen_.clearCursorRow();
1753 }
1754
rginda87b86462011-12-14 13:48:03 -08001755 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001756 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001757};
1758
1759/**
1760 * Inserts the given number of spaces at the current cursor position.
1761 *
rginda87b86462011-12-14 13:48:03 -08001762 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001763 */
1764hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001765 var cursor = this.saveCursor();
1766
rgindacbbd7482012-06-13 15:06:16 -07001767 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001768 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001769 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001770
1771 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001772 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001773};
1774
1775/**
1776 * Forward-delete the specified number of characters starting at the cursor
1777 * position.
1778 *
1779 * @param {integer} count The number of characters to delete.
1780 */
1781hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001782 var deleted = this.screen_.deleteChars(count);
1783 if (deleted && !this.screen_.textAttributes.isDefault()) {
1784 var cursor = this.saveCursor();
1785 this.setCursorColumn(this.screenSize.width - deleted);
1786 this.screen_.insertString(lib.f.getWhitespace(deleted));
1787 this.restoreCursor(cursor);
1788 }
1789
David Benjamin54e8bf62012-06-01 22:31:40 -04001790 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001791};
1792
1793/**
1794 * Shift rows in the scroll region upwards by a given number of lines.
1795 *
1796 * New rows are inserted at the bottom of the scroll region to fill the
1797 * vacated rows. The new rows not filled out with the current text attributes.
1798 *
1799 * This function does not affect the scrollback rows at all. Rows shifted
1800 * off the top are lost.
1801 *
rginda87b86462011-12-14 13:48:03 -08001802 * The cursor position is not altered.
1803 *
rginda8ba33642011-12-14 12:31:31 -08001804 * @param {integer} count The number of rows to scroll.
1805 */
1806hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001807 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001808
rginda87b86462011-12-14 13:48:03 -08001809 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001810 this.deleteLines(count);
1811
rginda87b86462011-12-14 13:48:03 -08001812 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001813};
1814
1815/**
1816 * Shift rows below the cursor down by a given number of lines.
1817 *
1818 * This function respects the current scroll region.
1819 *
1820 * New rows are inserted at the top of the scroll region to fill the
1821 * vacated rows. The new rows not filled out with the current text attributes.
1822 *
1823 * This function does not affect the scrollback rows at all. Rows shifted
1824 * off the bottom are lost.
1825 *
1826 * @param {integer} count The number of rows to scroll.
1827 */
1828hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001829 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001830
rginda87b86462011-12-14 13:48:03 -08001831 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001832 this.insertLines(opt_count);
1833
rginda87b86462011-12-14 13:48:03 -08001834 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001835};
1836
rginda87b86462011-12-14 13:48:03 -08001837
rginda8ba33642011-12-14 12:31:31 -08001838/**
1839 * Set the cursor position.
1840 *
1841 * The cursor row is relative to the scroll region if the terminal has
1842 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1843 *
1844 * @param {integer} row The new zero-based cursor row.
1845 * @param {integer} row The new zero-based cursor column.
1846 */
1847hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1848 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001849 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001850 } else {
rginda87b86462011-12-14 13:48:03 -08001851 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001852 }
rginda87b86462011-12-14 13:48:03 -08001853};
rginda8ba33642011-12-14 12:31:31 -08001854
rginda87b86462011-12-14 13:48:03 -08001855hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1856 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001857 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1858 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001859 this.screen_.setCursorPosition(row, column);
1860};
1861
1862hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001863 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1864 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001865 this.screen_.setCursorPosition(row, column);
1866};
1867
1868/**
1869 * Set the cursor column.
1870 *
1871 * @param {integer} column The new zero-based cursor column.
1872 */
1873hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001874 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001875};
1876
1877/**
1878 * Return the cursor column.
1879 *
1880 * @return {integer} The zero-based cursor column.
1881 */
1882hterm.Terminal.prototype.getCursorColumn = function() {
1883 return this.screen_.cursorPosition.column;
1884};
1885
1886/**
1887 * Set the cursor row.
1888 *
1889 * The cursor row is relative to the scroll region if the terminal has
1890 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1891 *
1892 * @param {integer} row The new cursor row.
1893 */
rginda87b86462011-12-14 13:48:03 -08001894hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1895 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001896};
1897
1898/**
1899 * Return the cursor row.
1900 *
1901 * @return {integer} The zero-based cursor row.
1902 */
1903hterm.Terminal.prototype.getCursorRow = function(row) {
1904 return this.screen_.cursorPosition.row;
1905};
1906
1907/**
1908 * Request that the ScrollPort redraw itself soon.
1909 *
1910 * The redraw will happen asynchronously, soon after the call stack winds down.
1911 * Multiple calls will be coalesced into a single redraw.
1912 */
1913hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001914 if (this.timeouts_.redraw)
1915 return;
rginda8ba33642011-12-14 12:31:31 -08001916
1917 var self = this;
rginda87b86462011-12-14 13:48:03 -08001918 this.timeouts_.redraw = setTimeout(function() {
1919 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001920 self.scrollPort_.redraw_();
1921 }, 0);
1922};
1923
1924/**
1925 * Request that the ScrollPort be scrolled to the bottom.
1926 *
1927 * The scroll will happen asynchronously, soon after the call stack winds down.
1928 * Multiple calls will be coalesced into a single scroll.
1929 *
1930 * This affects the scrollbar position of the ScrollPort, and has nothing to
1931 * do with the VT scroll commands.
1932 */
1933hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1934 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001935 return;
rginda8ba33642011-12-14 12:31:31 -08001936
1937 var self = this;
1938 this.timeouts_.scrollDown = setTimeout(function() {
1939 delete self.timeouts_.scrollDown;
1940 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1941 }, 10);
1942};
1943
1944/**
1945 * Move the cursor up a specified number of rows.
1946 *
1947 * @param {integer} count The number of rows to move the cursor.
1948 */
1949hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001950 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001951};
1952
1953/**
1954 * Move the cursor down a specified number of rows.
1955 *
1956 * @param {integer} count The number of rows to move the cursor.
1957 */
1958hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001959 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001960 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1961 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1962 this.screenSize.height - 1);
1963
rgindacbbd7482012-06-13 15:06:16 -07001964 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001965 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001966 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001967};
1968
1969/**
1970 * Move the cursor left a specified number of columns.
1971 *
1972 * @param {integer} count The number of columns to move the cursor.
1973 */
1974hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001975 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001976};
1977
1978/**
1979 * Move the cursor right a specified number of columns.
1980 *
1981 * @param {integer} count The number of columns to move the cursor.
1982 */
1983hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001984 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001985 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001986 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001987 this.setCursorColumn(column);
1988};
1989
1990/**
1991 * Reverse the foreground and background colors of the terminal.
1992 *
1993 * This only affects text that was drawn with no attributes.
1994 *
1995 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1996 * been drawn with attributes that happen to coincide with the default
1997 * 'no-attribute' colors. My guess is probably not.
1998 */
1999hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002000 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002001 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002002 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2003 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002004 } else {
rginda9f5222b2012-03-05 11:53:28 -08002005 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2006 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002007 }
2008};
2009
2010/**
rginda87b86462011-12-14 13:48:03 -08002011 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002012 *
2013 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002014 */
2015hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002016 this.cursorNode_.style.backgroundColor =
2017 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002018
2019 var self = this;
2020 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002021 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002022 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002023
2024 if (this.bellAudio_.getAttribute('src')) {
Robert Gindaa6331372013-03-19 10:35:39 -07002025 if (this.bellSquelchTimeout_)
Robert Ginda92e18102013-03-14 13:56:37 -07002026 return;
2027
2028 this.bellAudio_.play();
2029
2030 this.bellSequelchTimeout_ = setTimeout(function() {
2031 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002032 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002033 } else {
2034 delete this.bellSquelchTimeout_;
2035 }
rginda87b86462011-12-14 13:48:03 -08002036};
2037
2038/**
rginda8ba33642011-12-14 12:31:31 -08002039 * Set the origin mode bit.
2040 *
2041 * If origin mode is on, certain VT cursor and scrolling commands measure their
2042 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2043 * to the top of the addressable screen.
2044 *
2045 * Defaults to off.
2046 *
2047 * @param {boolean} state True to set origin mode, false to unset.
2048 */
2049hterm.Terminal.prototype.setOriginMode = function(state) {
2050 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002051 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002052};
2053
2054/**
2055 * Set the insert mode bit.
2056 *
2057 * If insert mode is on, existing text beyond the cursor position will be
2058 * shifted right to make room for new text. Otherwise, new text overwrites
2059 * any existing text.
2060 *
2061 * Defaults to off.
2062 *
2063 * @param {boolean} state True to set insert mode, false to unset.
2064 */
2065hterm.Terminal.prototype.setInsertMode = function(state) {
2066 this.options_.insertMode = state;
2067};
2068
2069/**
rginda87b86462011-12-14 13:48:03 -08002070 * Set the auto carriage return bit.
2071 *
2072 * If auto carriage return is on then a formfeed character is interpreted
2073 * as a newline, otherwise it's the same as a linefeed. The difference boils
2074 * down to whether or not the cursor column is reset.
2075 */
2076hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2077 this.options_.autoCarriageReturn = state;
2078};
2079
2080/**
rginda8ba33642011-12-14 12:31:31 -08002081 * Set the wraparound mode bit.
2082 *
2083 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2084 * to the start of the following row. Otherwise, the cursor is clamped to the
2085 * end of the screen and attempts to write past it are ignored.
2086 *
2087 * Defaults to on.
2088 *
2089 * @param {boolean} state True to set wraparound mode, false to unset.
2090 */
2091hterm.Terminal.prototype.setWraparound = function(state) {
2092 this.options_.wraparound = state;
2093};
2094
2095/**
2096 * Set the reverse-wraparound mode bit.
2097 *
2098 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2099 * to the end of the previous row. Otherwise, the cursor is clamped to column
2100 * 0.
2101 *
2102 * Defaults to off.
2103 *
2104 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2105 */
2106hterm.Terminal.prototype.setReverseWraparound = function(state) {
2107 this.options_.reverseWraparound = state;
2108};
2109
2110/**
2111 * Selects between the primary and alternate screens.
2112 *
2113 * If alternate mode is on, the alternate screen is active. Otherwise the
2114 * primary screen is active.
2115 *
2116 * Swapping screens has no effect on the scrollback buffer.
2117 *
2118 * Each screen maintains its own cursor position.
2119 *
2120 * Defaults to off.
2121 *
2122 * @param {boolean} state True to set alternate mode, false to unset.
2123 */
2124hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002125 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002126 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2127
rginda35c456b2012-02-09 17:29:05 -08002128 if (this.screen_.rowsArray.length &&
2129 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2130 // If the screen changed sizes while we were away, our rowIndexes may
2131 // be incorrect.
2132 var offset = this.scrollbackRows_.length;
2133 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002134 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002135 ary[i].rowIndex = offset + i;
2136 }
2137 }
rginda8ba33642011-12-14 12:31:31 -08002138
rginda35c456b2012-02-09 17:29:05 -08002139 this.realizeWidth_(this.screenSize.width);
2140 this.realizeHeight_(this.screenSize.height);
2141 this.scrollPort_.syncScrollHeight();
2142 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002143
rginda6d397402012-01-17 10:58:29 -08002144 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002145 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002146};
2147
2148/**
2149 * Set the cursor-blink mode bit.
2150 *
2151 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2152 * a visible cursor does not blink.
2153 *
2154 * You should make sure to turn blinking off if you're going to dispose of a
2155 * terminal, otherwise you'll leak a timeout.
2156 *
2157 * Defaults to on.
2158 *
2159 * @param {boolean} state True to set cursor-blink mode, false to unset.
2160 */
2161hterm.Terminal.prototype.setCursorBlink = function(state) {
2162 this.options_.cursorBlink = state;
2163
2164 if (!state && this.timeouts_.cursorBlink) {
2165 clearTimeout(this.timeouts_.cursorBlink);
2166 delete this.timeouts_.cursorBlink;
2167 }
2168
2169 if (this.options_.cursorVisible)
2170 this.setCursorVisible(true);
2171};
2172
2173/**
2174 * Set the cursor-visible mode bit.
2175 *
2176 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2177 *
2178 * Defaults to on.
2179 *
2180 * @param {boolean} state True to set cursor-visible mode, false to unset.
2181 */
2182hterm.Terminal.prototype.setCursorVisible = function(state) {
2183 this.options_.cursorVisible = state;
2184
2185 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002186 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002187 return;
2188 }
2189
rginda87b86462011-12-14 13:48:03 -08002190 this.syncCursorPosition_();
2191
2192 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002193
2194 if (this.options_.cursorBlink) {
2195 if (this.timeouts_.cursorBlink)
2196 return;
2197
2198 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2199 500);
2200 } else {
2201 if (this.timeouts_.cursorBlink) {
2202 clearTimeout(this.timeouts_.cursorBlink);
2203 delete this.timeouts_.cursorBlink;
2204 }
2205 }
2206};
2207
2208/**
rginda87b86462011-12-14 13:48:03 -08002209 * Synchronizes the visible cursor and document selection with the current
2210 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002211 */
2212hterm.Terminal.prototype.syncCursorPosition_ = function() {
2213 var topRowIndex = this.scrollPort_.getTopRowIndex();
2214 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2215 var cursorRowIndex = this.scrollbackRows_.length +
2216 this.screen_.cursorPosition.row;
2217
2218 if (cursorRowIndex > bottomRowIndex) {
2219 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002220 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002221 return;
2222 }
2223
2224 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002225 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2226 'px';
2227 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2228 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002229
2230 this.cursorNode_.setAttribute('title',
2231 '(' + this.screen_.cursorPosition.row +
2232 ', ' + this.screen_.cursorPosition.column +
2233 ')');
2234
2235 // Update the caret for a11y purposes.
2236 var selection = this.document_.getSelection();
2237 if (selection && selection.isCollapsed)
2238 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002239};
2240
Robert Gindafb1be6a2013-12-11 11:56:22 -08002241/**
2242 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2243 * and character cell dimensions.
2244 */
Robert Ginda830583c2013-08-07 13:20:46 -07002245hterm.Terminal.prototype.restyleCursor_ = function() {
2246 var shape = this.cursorShape_;
2247
2248 if (this.cursorNode_.getAttribute('focus') == 'false') {
2249 // Always show a block cursor when unfocused.
2250 shape = hterm.Terminal.cursorShape.BLOCK;
2251 }
2252
2253 var style = this.cursorNode_.style;
2254
Robert Gindafb1be6a2013-12-11 11:56:22 -08002255 style.width = this.scrollPort_.characterSize.width + 'px';
2256
Robert Ginda830583c2013-08-07 13:20:46 -07002257 switch (shape) {
2258 case hterm.Terminal.cursorShape.BEAM:
2259 style.height = this.scrollPort_.characterSize.height + 'px';
2260 style.backgroundColor = 'transparent';
2261 style.borderBottomStyle = null;
2262 style.borderLeftStyle = 'solid';
2263 break;
2264
2265 case hterm.Terminal.cursorShape.UNDERLINE:
2266 style.height = this.scrollPort_.characterSize.baseline + 'px';
2267 style.backgroundColor = 'transparent';
2268 style.borderBottomStyle = 'solid';
2269 // correct the size to put it exactly at the baseline
2270 style.borderLeftStyle = null;
2271 break;
2272
2273 default:
2274 style.height = this.scrollPort_.characterSize.height + 'px';
2275 style.backgroundColor = this.cursorColor_;
2276 style.borderBottomStyle = null;
2277 style.borderLeftStyle = null;
2278 break;
2279 }
2280};
2281
rginda8ba33642011-12-14 12:31:31 -08002282/**
2283 * Synchronizes the visible cursor with the current cursor coordinates.
2284 *
2285 * The sync will happen asynchronously, soon after the call stack winds down.
2286 * Multiple calls will be coalesced into a single sync.
2287 */
2288hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2289 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002290 return;
rginda8ba33642011-12-14 12:31:31 -08002291
2292 var self = this;
2293 this.timeouts_.syncCursor = setTimeout(function() {
2294 self.syncCursorPosition_();
2295 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002296 }, 0);
2297};
2298
rgindacc2996c2012-02-24 14:59:31 -08002299/**
rgindaf522ce02012-04-17 17:49:17 -07002300 * Show or hide the zoom warning.
2301 *
2302 * The zoom warning is a message warning the user that their browser zoom must
2303 * be set to 100% in order for hterm to function properly.
2304 *
2305 * @param {boolean} state True to show the message, false to hide it.
2306 */
2307hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2308 if (!this.zoomWarningNode_) {
2309 if (!state)
2310 return;
2311
2312 this.zoomWarningNode_ = this.document_.createElement('div');
2313 this.zoomWarningNode_.style.cssText = (
2314 'color: black;' +
2315 'background-color: #ff2222;' +
2316 'font-size: large;' +
2317 'border-radius: 8px;' +
2318 'opacity: 0.75;' +
2319 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2320 'top: 0.5em;' +
2321 'right: 1.2em;' +
2322 'position: absolute;' +
2323 '-webkit-text-size-adjust: none;' +
2324 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002325 }
2326
Robert Gindab4839c22013-02-28 16:52:10 -08002327 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2328 hterm.zoomWarningMessage,
2329 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2330
rgindaf522ce02012-04-17 17:49:17 -07002331 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2332
2333 if (state) {
2334 if (!this.zoomWarningNode_.parentNode)
2335 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2336 } else if (this.zoomWarningNode_.parentNode) {
2337 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2338 }
2339};
2340
2341/**
rgindacc2996c2012-02-24 14:59:31 -08002342 * Show the terminal overlay for a given amount of time.
2343 *
2344 * The terminal overlay appears in inverse video in a large font, centered
2345 * over the terminal. You should probably keep the overlay message brief,
2346 * since it's in a large font and you probably aren't going to check the size
2347 * of the terminal first.
2348 *
2349 * @param {string} msg The text (not HTML) message to display in the overlay.
2350 * @param {number} opt_timeout The amount of time to wait before fading out
2351 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2352 * stay up forever (or until the next overlay).
2353 */
2354hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002355 if (!this.overlayNode_) {
2356 if (!this.div_)
2357 return;
2358
2359 this.overlayNode_ = this.document_.createElement('div');
2360 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002361 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002362 'font-size: xx-large;' +
2363 'opacity: 0.75;' +
2364 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2365 'position: absolute;' +
2366 '-webkit-user-select: none;' +
2367 '-webkit-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002368
2369 this.overlayNode_.addEventListener('mousedown', function(e) {
2370 e.preventDefault();
2371 e.stopPropagation();
2372 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002373 }
2374
rginda9f5222b2012-03-05 11:53:28 -08002375 this.overlayNode_.style.color = this.prefs_.get('background-color');
2376 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2377 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2378
rgindaf0090c92012-02-10 14:58:52 -08002379 this.overlayNode_.textContent = msg;
2380 this.overlayNode_.style.opacity = '0.75';
2381
2382 if (!this.overlayNode_.parentNode)
2383 this.div_.appendChild(this.overlayNode_);
2384
Robert Ginda97769282013-02-01 15:30:30 -08002385 var divSize = hterm.getClientSize(this.div_);
2386 var overlaySize = hterm.getClientSize(this.overlayNode_);
2387
2388 this.overlayNode_.style.top = (divSize.height - overlaySize.height) / 2;
2389 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
2390 this.scrollPort_.currentScrollbarWidthPx) / 2;
rgindaf0090c92012-02-10 14:58:52 -08002391
2392 var self = this;
2393
2394 if (this.overlayTimeout_)
2395 clearTimeout(this.overlayTimeout_);
2396
rgindacc2996c2012-02-24 14:59:31 -08002397 if (opt_timeout === null)
2398 return;
2399
rgindaf0090c92012-02-10 14:58:52 -08002400 this.overlayTimeout_ = setTimeout(function() {
2401 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002402 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002403 if (self.overlayNode_.parentNode)
2404 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002405 self.overlayTimeout_ = null;
2406 self.overlayNode_.style.opacity = '0.75';
2407 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002408 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002409};
2410
rginda4bba5e12012-06-20 16:15:30 -07002411/**
2412 * Paste from the system clipboard to the terminal.
2413 */
2414hterm.Terminal.prototype.paste = function() {
2415 hterm.pasteFromClipboard(this.document_);
2416};
2417
2418/**
2419 * Copy a string to the system clipboard.
2420 *
2421 * Note: If there is a selected range in the terminal, it'll be cleared.
2422 */
2423hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002424 if (this.prefs_.get('enable-clipboard-notice'))
2425 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2426
rgindaa09e7332012-08-17 12:49:51 -07002427 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002428 copySource.textContent = str;
2429 copySource.style.cssText = (
2430 '-webkit-user-select: text;' +
2431 'position: absolute;' +
2432 'top: -99px');
2433
2434 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002435
rginda4bba5e12012-06-20 16:15:30 -07002436 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002437 var anchorNode = selection.anchorNode;
2438 var anchorOffset = selection.anchorOffset;
2439 var focusNode = selection.focusNode;
2440 var focusOffset = selection.focusOffset;
2441
rginda4bba5e12012-06-20 16:15:30 -07002442 selection.selectAllChildren(copySource);
2443
rgindaa09e7332012-08-17 12:49:51 -07002444 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002445
rgindafaa74742012-08-21 13:34:03 -07002446 selection.collapse(anchorNode, anchorOffset);
2447 selection.extend(focusNode, focusOffset);
2448
rginda4bba5e12012-06-20 16:15:30 -07002449 copySource.parentNode.removeChild(copySource);
2450};
2451
rgindaa09e7332012-08-17 12:49:51 -07002452hterm.Terminal.prototype.getSelectionText = function() {
2453 var selection = this.scrollPort_.selection;
2454 selection.sync();
2455
2456 if (selection.isCollapsed)
2457 return null;
2458
2459
2460 // Start offset measures from the beginning of the line.
2461 var startOffset = selection.startOffset;
2462 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002463
Robert Gindafdbb3f22012-09-06 20:23:06 -07002464 if (node.nodeName != 'X-ROW') {
2465 // If the selection doesn't start on an x-row node, then it must be
2466 // somewhere inside the x-row. Add any characters from previous siblings
2467 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002468
2469 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2470 // If node is the text node in a styled span, move up to the span node.
2471 node = node.parentNode;
2472 }
2473
Robert Gindafdbb3f22012-09-06 20:23:06 -07002474 while (node.previousSibling) {
2475 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002476 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002477 }
rgindaa09e7332012-08-17 12:49:51 -07002478 }
2479
2480 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002481 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2482 selection.endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002483 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002484
Robert Gindafdbb3f22012-09-06 20:23:06 -07002485 if (node.nodeName != 'X-ROW') {
2486 // If the selection doesn't end on an x-row node, then it must be
2487 // somewhere inside the x-row. Add any characters from following siblings
2488 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002489
2490 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2491 // If node is the text node in a styled span, move up to the span node.
2492 node = node.parentNode;
2493 }
2494
Robert Gindafdbb3f22012-09-06 20:23:06 -07002495 while (node.nextSibling) {
2496 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002497 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002498 }
rgindaa09e7332012-08-17 12:49:51 -07002499 }
2500
2501 var rv = this.getRowsText(selection.startRow.rowIndex,
2502 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002503 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002504};
2505
rginda4bba5e12012-06-20 16:15:30 -07002506/**
2507 * Copy the current selection to the system clipboard, then clear it after a
2508 * short delay.
2509 */
2510hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002511 var text = this.getSelectionText();
2512 if (text != null)
2513 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002514};
2515
rgindaf0090c92012-02-10 14:58:52 -08002516hterm.Terminal.prototype.overlaySize = function() {
2517 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2518};
2519
rginda87b86462011-12-14 13:48:03 -08002520/**
2521 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2522 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002523 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002524 */
2525hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002526 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002527 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2528
Robert Ginda8cb7d902013-06-20 14:37:18 -07002529 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002530};
2531
2532/**
rgindad5613292012-06-19 15:40:37 -07002533 * Add the terminalRow and terminalColumn properties to mouse events and
2534 * then forward on to onMouse().
2535 *
2536 * The terminalRow and terminalColumn properties contain the (row, column)
2537 * coordinates for the mouse event.
2538 */
2539hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002540 if (e.processedByTerminalHandler_) {
2541 // We register our event handlers on the document, as well as the cursor
2542 // and the scroll blocker. Mouse events that occur on the cursor or
2543 // scroll blocker will also appear on the document, but we don't want to
2544 // process them twice.
2545 //
2546 // We can't just prevent bubbling because that has other side effects, so
2547 // we decorate the event object with this property instead.
2548 return;
2549 }
2550
2551 e.processedByTerminalHandler_ = true;
2552
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002553 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2554 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002555 return;
2556 }
2557
rgindad5613292012-06-19 15:40:37 -07002558 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2559 this.scrollPort_.characterSize.height) + 1;
2560 e.terminalColumn = parseInt(e.clientX /
2561 this.scrollPort_.characterSize.width) + 1;
2562
Robert Ginda928cf632014-03-05 15:07:41 -08002563 if (e.type == 'mousedown') {
2564 if (e.altKey || this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED) {
2565 // If VT mouse reporting is disabled, or has been defeated with
2566 // alt-mousedown, then the mouse will act on the local selection.
2567 this.reportMouseEvents_ = false;
2568 this.setSelectionEnabled(true);
2569 } else {
2570 // Otherwise we defer ownership of the mouse to the VT.
2571 this.reportMouseEvents_ = true;
2572 this.document_.getSelection().collapse();
2573 this.setSelectionEnabled(false);
2574 e.preventDefault();
2575 }
2576 }
2577
2578 if (!this.reportMouseEvents_) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002579 if (e.type == 'dblclick') {
2580 this.screen_.expandSelection(this.document_.getSelection());
2581 hterm.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07002582 }
2583
Robert Ginda928cf632014-03-05 15:07:41 -08002584 if (e.type == 'mousedown' && e.which == this.mousePasteButton)
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002585 this.paste();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002586
2587 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2588 !this.document_.getSelection().isCollapsed) {
2589 hterm.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002590 }
2591
2592 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
2593 this.scrollBlockerNode_.engaged) {
2594 // Disengage the scroll-blocker after one of these events.
2595 this.scrollBlockerNode_.engaged = false;
2596 this.scrollBlockerNode_.style.top = '-99px';
2597 }
2598
Robert Ginda928cf632014-03-05 15:07:41 -08002599 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002600 if (!this.scrollBlockerNode_.engaged) {
2601 if (e.type == 'mousedown') {
2602 // Move the scroll-blocker into place if we want to keep the scrollport
2603 // from scrolling.
2604 this.scrollBlockerNode_.engaged = true;
2605 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2606 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2607 } else if (e.type == 'mousemove') {
2608 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
2609 // in which case it's too late to engage the scroll-blocker.
2610 this.document_.getSelection().collapse();
2611 e.preventDefault();
2612 }
2613 }
Robert Ginda928cf632014-03-05 15:07:41 -08002614
2615 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002616 }
2617
Robert Ginda928cf632014-03-05 15:07:41 -08002618 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
2619 // Restore this on mouseup in case it was temporarily defeated with a
2620 // alt-mousedown. Only do this when the selection is empty so that
2621 // we don't immediately kill the users selection.
2622 this.reportMouseEvents_ = (this.vt.mouseReport !=
2623 this.vt.MOUSE_REPORT_DISABLED);
2624 }
rgindad5613292012-06-19 15:40:37 -07002625};
2626
2627/**
2628 * Clients should override this if they care to know about mouse events.
2629 *
2630 * The event parameter will be a normal DOM mouse click event with additional
2631 * 'terminalRow' and 'terminalColumn' properties.
2632 */
2633hterm.Terminal.prototype.onMouse = function(e) { };
2634
2635/**
rginda8e92a692012-05-20 19:37:20 -07002636 * React when focus changes.
2637 */
2638hterm.Terminal.prototype.onFocusChange_ = function(state) {
2639 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
Robert Ginda830583c2013-08-07 13:20:46 -07002640 this.restyleCursor_();
rginda8e92a692012-05-20 19:37:20 -07002641};
2642
2643/**
rginda8ba33642011-12-14 12:31:31 -08002644 * React when the ScrollPort is scrolled.
2645 */
2646hterm.Terminal.prototype.onScroll_ = function() {
2647 this.scheduleSyncCursorPosition_();
2648};
2649
2650/**
rginda9846e2f2012-01-27 13:53:33 -08002651 * React when text is pasted into the scrollPort.
2652 */
2653hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Ginda8cb7d902013-06-20 14:37:18 -07002654 this.onVTKeystroke(e.text.replace(/\n/mg, '\r'));
rginda9846e2f2012-01-27 13:53:33 -08002655};
2656
2657/**
rgindaa09e7332012-08-17 12:49:51 -07002658 * React when the user tries to copy from the scrollPort.
2659 */
2660hterm.Terminal.prototype.onCopy_ = function(e) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002661 e.preventDefault();
2662 setTimeout(this.copySelectionToClipboard.bind(this), 0);
rgindaa09e7332012-08-17 12:49:51 -07002663};
2664
2665/**
rginda8ba33642011-12-14 12:31:31 -08002666 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002667 *
2668 * Note: This function should not directly contain code that alters the internal
2669 * state of the terminal. That kind of code belongs in realizeWidth or
2670 * realizeHeight, so that it can be executed synchronously in the case of a
2671 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002672 */
2673hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08002674 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002675 this.scrollPort_.characterSize.width);
Robert Ginda19f61292014-03-04 14:07:57 -08002676 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
rginda35c456b2012-02-09 17:29:05 -08002677 this.scrollPort_.characterSize.height);
2678
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002679 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002680 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002681 // gets removed from the document or during the initial load, and we can't
2682 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002683 return;
2684 }
2685
rgindaa8ba17d2012-08-15 14:41:10 -07002686 var isNewSize = (columnCount != this.screenSize.width ||
2687 rowCount != this.screenSize.height);
2688
2689 // We do this even if the size didn't change, just to be sure everything is
2690 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002691 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002692 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002693
2694 if (isNewSize)
2695 this.overlaySize();
2696
Robert Gindafb1be6a2013-12-11 11:56:22 -08002697 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07002698 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002699};
2700
2701/**
2702 * Service the cursor blink timeout.
2703 */
2704hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Ginda830583c2013-08-07 13:20:46 -07002705 if (this.cursorNode_.getAttribute('focus') == 'false' ||
2706 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08002707 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002708 } else {
rginda87b86462011-12-14 13:48:03 -08002709 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002710 }
2711};
David Reveman8f552492012-03-28 12:18:41 -04002712
2713/**
2714 * Set the scrollbar-visible mode bit.
2715 *
2716 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2717 * Otherwise it will not.
2718 *
2719 * Defaults to on.
2720 *
2721 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2722 */
2723hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2724 this.scrollPort_.setScrollbarVisible(state);
2725};