blob: bfaba222f531b1035a9eae3cd96513db26f644d0 [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
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100264 'ctrl-v-paste': function(v) {
265 terminal.keyboard.ctrlVPaste = v;
266 },
267
Robert Ginda57f03b42012-09-13 11:02:48 -0700268 'enable-8-bit-control': function(v) {
269 terminal.vt.enable8BitControl = !!v;
270 },
rginda30f20f62012-04-05 16:36:19 -0700271
Robert Ginda57f03b42012-09-13 11:02:48 -0700272 'enable-bold': function(v) {
273 terminal.syncBoldSafeState();
274 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400275
Robert Ginda57f03b42012-09-13 11:02:48 -0700276 'enable-clipboard-write': function(v) {
277 terminal.vt.enableClipboardWrite = !!v;
278 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400279
Robert Ginda3755e752013-05-31 13:34:09 -0700280 'enable-dec12': function(v) {
281 terminal.vt.enableDec12 = !!v;
282 },
283
Robert Ginda57f03b42012-09-13 11:02:48 -0700284 'font-family': function(v) {
285 terminal.syncFontFamily();
286 },
rginda30f20f62012-04-05 16:36:19 -0700287
Robert Ginda57f03b42012-09-13 11:02:48 -0700288 'font-size': function(v) {
289 terminal.setFontSize(v);
290 },
rginda9875d902012-08-20 16:21:57 -0700291
Robert Ginda57f03b42012-09-13 11:02:48 -0700292 'font-smoothing': function(v) {
293 terminal.syncFontFamily();
294 },
rgindade84e382012-04-20 15:39:31 -0700295
Robert Ginda57f03b42012-09-13 11:02:48 -0700296 'foreground-color': function(v) {
297 terminal.setForegroundColor(v);
298 },
rginda30f20f62012-04-05 16:36:19 -0700299
Robert Ginda57f03b42012-09-13 11:02:48 -0700300 'home-keys-scroll': function(v) {
301 terminal.keyboard.homeKeysScroll = v;
302 },
rginda4bba5e12012-06-20 16:15:30 -0700303
Robert Ginda57f03b42012-09-13 11:02:48 -0700304 'max-string-sequence': function(v) {
305 terminal.vt.maxStringSequence = v;
306 },
rginda11057d52012-04-25 12:29:56 -0700307
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700308 'media-keys-are-fkeys': function(v) {
309 terminal.keyboard.mediaKeysAreFKeys = v;
310 },
311
Robert Ginda57f03b42012-09-13 11:02:48 -0700312 'meta-sends-escape': function(v) {
313 terminal.keyboard.metaSendsEscape = v;
314 },
rginda30f20f62012-04-05 16:36:19 -0700315
Robert Ginda57f03b42012-09-13 11:02:48 -0700316 'mouse-paste-button': function(v) {
317 terminal.syncMousePasteButton();
318 },
rgindaa8ba17d2012-08-15 14:41:10 -0700319
Robert Gindae76aa9f2014-03-14 12:29:12 -0700320 'page-keys-scroll': function(v) {
321 terminal.keyboard.pageKeysScroll = v;
322 },
323
Robert Ginda40932892012-12-10 17:26:40 -0800324 'pass-alt-number': function(v) {
325 if (v == null) {
326 var osx = window.navigator.userAgent.match(/Mac OS X/);
327
328 // Let Alt-1..9 pass to the browser (to control tab switching) on
329 // non-OS X systems, or if hterm is not opened in an app window.
330 v = (!osx && hterm.windowType != 'popup');
331 }
332
333 terminal.passAltNumber = v;
334 },
335
336 'pass-ctrl-number': function(v) {
337 if (v == null) {
338 var osx = window.navigator.userAgent.match(/Mac OS X/);
339
340 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
341 // non-OS X systems, or if hterm is not opened in an app window.
342 v = (!osx && hterm.windowType != 'popup');
343 }
344
345 terminal.passCtrlNumber = v;
346 },
347
348 'pass-meta-number': function(v) {
349 if (v == null) {
350 var osx = window.navigator.userAgent.match(/Mac OS X/);
351
352 // Let Meta-1..9 pass to the browser (to control tab switching) on
353 // OS X systems, or if hterm is not opened in an app window.
354 v = (osx && hterm.windowType != 'popup');
355 }
356
357 terminal.passMetaNumber = v;
358 },
359
Robert Ginda8cb7d902013-06-20 14:37:18 -0700360 'receive-encoding': function(v) {
361 if (!(/^(utf-8|raw)$/).test(v)) {
362 console.warn('Invalid value for "receive-encoding": ' + v);
363 v = 'utf-8';
364 }
365
366 terminal.vt.characterEncoding = v;
367 },
368
Robert Ginda57f03b42012-09-13 11:02:48 -0700369 'scroll-on-keystroke': function(v) {
370 terminal.scrollOnKeystroke_ = v;
371 },
rginda9f5222b2012-03-05 11:53:28 -0800372
Robert Ginda57f03b42012-09-13 11:02:48 -0700373 'scroll-on-output': function(v) {
374 terminal.scrollOnOutput_ = v;
375 },
rginda30f20f62012-04-05 16:36:19 -0700376
Robert Ginda57f03b42012-09-13 11:02:48 -0700377 'scrollbar-visible': function(v) {
378 terminal.setScrollbarVisible(v);
379 },
rginda9f5222b2012-03-05 11:53:28 -0800380
Robert Ginda8cb7d902013-06-20 14:37:18 -0700381 'send-encoding': function(v) {
382 if (!(/^(utf-8|raw)$/).test(v)) {
383 console.warn('Invalid value for "send-encoding": ' + v);
384 v = 'utf-8';
385 }
386
387 terminal.keyboard.characterEncoding = v;
388 },
389
Robert Ginda57f03b42012-09-13 11:02:48 -0700390 'shift-insert-paste': function(v) {
391 terminal.keyboard.shiftInsertPaste = v;
392 },
rginda9f5222b2012-03-05 11:53:28 -0800393
Robert Gindae76aa9f2014-03-14 12:29:12 -0700394 'user-css': function(v) {
395 terminal.scrollPort_.setUserCss(v);
Robert Ginda57f03b42012-09-13 11:02:48 -0700396 }
397 });
rginda30f20f62012-04-05 16:36:19 -0700398
Robert Ginda57f03b42012-09-13 11:02:48 -0700399 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800400 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700401
402 if (opt_callback)
403 opt_callback();
404 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800405};
406
rginda8e92a692012-05-20 19:37:20 -0700407/**
408 * Set the color for the cursor.
409 *
410 * If you want this setting to persist, set it through prefs_, rather than
411 * with this method.
412 */
413hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700414 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700415 this.cursorNode_.style.backgroundColor = color;
416 this.cursorNode_.style.borderColor = color;
417};
418
419/**
420 * Return the current cursor color as a string.
421 */
422hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700423 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700424};
425
426/**
rgindad5613292012-06-19 15:40:37 -0700427 * Enable or disable mouse based text selection in the terminal.
428 */
429hterm.Terminal.prototype.setSelectionEnabled = function(state) {
430 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700431};
432
433/**
rginda8e92a692012-05-20 19:37:20 -0700434 * Set the background color.
435 *
436 * If you want this setting to persist, set it through prefs_, rather than
437 * with this method.
438 */
439hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700440 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700441 this.primaryScreen_.textAttributes.setDefaults(
442 this.foregroundColor_, this.backgroundColor_);
443 this.alternateScreen_.textAttributes.setDefaults(
444 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700445 this.scrollPort_.setBackgroundColor(color);
446};
447
rginda9f5222b2012-03-05 11:53:28 -0800448/**
449 * Return the current terminal background color.
450 *
451 * Intended for use by other classes, so we don't have to expose the entire
452 * prefs_ object.
453 */
454hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700455 return this.backgroundColor_;
456};
457
458/**
459 * Set the foreground color.
460 *
461 * If you want this setting to persist, set it through prefs_, rather than
462 * with this method.
463 */
464hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700465 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700466 this.primaryScreen_.textAttributes.setDefaults(
467 this.foregroundColor_, this.backgroundColor_);
468 this.alternateScreen_.textAttributes.setDefaults(
469 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700470 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800471};
472
473/**
474 * Return the current terminal foreground color.
475 *
476 * Intended for use by other classes, so we don't have to expose the entire
477 * prefs_ object.
478 */
479hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700480 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800481};
482
483/**
rginda87b86462011-12-14 13:48:03 -0800484 * Create a new instance of a terminal command and run it with a given
485 * argument string.
486 *
487 * @param {function} commandClass The constructor for a terminal command.
488 * @param {string} argString The argument string to pass to the command.
489 */
490hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700491 var environment = this.prefs_.get('environment');
492 if (typeof environment != 'object' || environment == null)
493 environment = {};
494
rginda87b86462011-12-14 13:48:03 -0800495 var self = this;
496 this.command = new commandClass(
497 { argString: argString || '',
498 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700499 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800500 onExit: function(code) {
501 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800502 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700503 if (self.prefs_.get('close-on-exit'))
504 window.close();
rginda87b86462011-12-14 13:48:03 -0800505 }
506 });
507
rgindafeaf3142012-01-31 15:14:20 -0800508 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800509 this.command.run();
510};
511
512/**
rgindafeaf3142012-01-31 15:14:20 -0800513 * Returns true if the current screen is the primary screen, false otherwise.
514 */
515hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700516 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800517};
518
519/**
520 * Install the keyboard handler for this terminal.
521 *
522 * This will prevent the browser from seeing any keystrokes sent to the
523 * terminal.
524 */
525hterm.Terminal.prototype.installKeyboard = function() {
Toni Barzic0bfa8922013-11-22 11:18:35 -0800526 this.keyboard.installKeyboard(this.scrollPort_.getScreenNode());
rgindafeaf3142012-01-31 15:14:20 -0800527}
528
529/**
530 * Uninstall the keyboard handler for this terminal.
531 */
532hterm.Terminal.prototype.uninstallKeyboard = function() {
533 this.keyboard.installKeyboard(null);
534}
535
536/**
rginda35c456b2012-02-09 17:29:05 -0800537 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800538 *
539 * Call setFontSize(0) to reset to the default font size.
540 *
541 * This function does not modify the font-size preference.
542 *
543 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800544 */
545hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800546 if (px === 0)
547 px = this.prefs_.get('font-size');
548
rginda35c456b2012-02-09 17:29:05 -0800549 this.scrollPort_.setFontSize(px);
Ricky Liang48f05cb2013-12-31 23:35:29 +0800550 if (this.wcCssRule_) {
551 this.wcCssRule_.style.width = this.scrollPort_.characterSize.width * 2 +
552 'px';
553 }
rginda35c456b2012-02-09 17:29:05 -0800554};
555
556/**
557 * Get the current font size.
558 */
559hterm.Terminal.prototype.getFontSize = function() {
560 return this.scrollPort_.getFontSize();
561};
562
563/**
rginda8e92a692012-05-20 19:37:20 -0700564 * Get the current font family.
565 */
566hterm.Terminal.prototype.getFontFamily = function() {
567 return this.scrollPort_.getFontFamily();
568};
569
570/**
rginda35c456b2012-02-09 17:29:05 -0800571 * Set the CSS "font-family" for this terminal.
572 */
rginda9f5222b2012-03-05 11:53:28 -0800573hterm.Terminal.prototype.syncFontFamily = function() {
574 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
575 this.prefs_.get('font-smoothing'));
576 this.syncBoldSafeState();
577};
578
rginda4bba5e12012-06-20 16:15:30 -0700579/**
580 * Set this.mousePasteButton based on the mouse-paste-button pref,
581 * autodetecting if necessary.
582 */
583hterm.Terminal.prototype.syncMousePasteButton = function() {
584 var button = this.prefs_.get('mouse-paste-button');
585 if (typeof button == 'number') {
586 this.mousePasteButton = button;
587 return;
588 }
589
590 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
591 if (!ary || ary[2] == 'CrOS') {
592 this.mousePasteButton = 2;
593 } else {
594 this.mousePasteButton = 3;
595 }
596};
597
598/**
599 * Enable or disable bold based on the enable-bold pref, autodetecting if
600 * necessary.
601 */
rginda9f5222b2012-03-05 11:53:28 -0800602hterm.Terminal.prototype.syncBoldSafeState = function() {
603 var enableBold = this.prefs_.get('enable-bold');
604 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700605 this.primaryScreen_.textAttributes.enableBold = enableBold;
606 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800607 return;
608 }
609
rgindaf7521392012-02-28 17:20:34 -0800610 var normalSize = this.scrollPort_.measureCharacterSize();
611 var boldSize = this.scrollPort_.measureCharacterSize('bold');
612
613 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800614 if (!isBoldSafe) {
615 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700616 'from normal. Font family is: ' +
617 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800618 }
rginda9f5222b2012-03-05 11:53:28 -0800619
Robert Gindaed016262012-10-26 16:27:09 -0700620 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
621 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800622};
623
624/**
rginda87b86462011-12-14 13:48:03 -0800625 * Return a copy of the current cursor position.
626 *
627 * @return {hterm.RowCol} The RowCol object representing the current position.
628 */
629hterm.Terminal.prototype.saveCursor = function() {
630 return this.screen_.cursorPosition.clone();
631};
632
rgindaa19afe22012-01-25 15:40:22 -0800633hterm.Terminal.prototype.getTextAttributes = function() {
634 return this.screen_.textAttributes;
635};
636
rginda1a09aa02012-06-18 21:11:25 -0700637hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
638 this.screen_.textAttributes = textAttributes;
639};
640
rginda87b86462011-12-14 13:48:03 -0800641/**
rgindaf522ce02012-04-17 17:49:17 -0700642 * Return the current browser zoom factor applied to the terminal.
643 *
644 * @return {number} The current browser zoom factor.
645 */
646hterm.Terminal.prototype.getZoomFactor = function() {
647 return this.scrollPort_.characterSize.zoomFactor;
648};
649
650/**
rginda9846e2f2012-01-27 13:53:33 -0800651 * Change the title of this terminal's window.
652 */
653hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800654 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800655};
656
657/**
rginda87b86462011-12-14 13:48:03 -0800658 * Restore a previously saved cursor position.
659 *
660 * @param {hterm.RowCol} cursor The position to restore.
661 */
662hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700663 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
664 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800665 this.screen_.setCursorPosition(row, column);
666 if (cursor.column > column ||
667 cursor.column == column && cursor.overflow) {
668 this.screen_.cursorPosition.overflow = true;
669 }
rginda87b86462011-12-14 13:48:03 -0800670};
671
672/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400673 * Clear the cursor's overflow flag.
674 */
675hterm.Terminal.prototype.clearCursorOverflow = function() {
676 this.screen_.cursorPosition.overflow = false;
677};
678
679/**
Robert Ginda830583c2013-08-07 13:20:46 -0700680 * Sets the cursor shape
681 */
682hterm.Terminal.prototype.setCursorShape = function(shape) {
683 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800684 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700685}
686
687/**
688 * Get the cursor shape
689 */
690hterm.Terminal.prototype.getCursorShape = function() {
691 return this.cursorShape_;
692}
693
694/**
rginda87b86462011-12-14 13:48:03 -0800695 * Set the width of the terminal, resizing the UI to match.
696 */
697hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800698 if (columnCount == null) {
699 this.div_.style.width = '100%';
700 return;
701 }
702
rginda35c456b2012-02-09 17:29:05 -0800703 this.div_.style.width = this.scrollPort_.characterSize.width *
Robert Ginda97769282013-02-01 15:30:30 -0800704 columnCount + this.scrollPort_.currentScrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400705 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800706 this.scheduleSyncCursorPosition_();
707};
rginda87b86462011-12-14 13:48:03 -0800708
rgindac9bc5502012-01-18 11:48:44 -0800709/**
rginda35c456b2012-02-09 17:29:05 -0800710 * Set the height of the terminal, resizing the UI to match.
711 */
712hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800713 if (rowCount == null) {
714 this.div_.style.height = '100%';
715 return;
716 }
717
rginda35c456b2012-02-09 17:29:05 -0800718 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700719 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800720 this.realizeSize_(this.screenSize.width, rowCount);
721 this.scheduleSyncCursorPosition_();
722};
723
724/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400725 * Deal with terminal size changes.
726 *
727 */
728hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
729 if (columnCount != this.screenSize.width)
730 this.realizeWidth_(columnCount);
731
732 if (rowCount != this.screenSize.height)
733 this.realizeHeight_(rowCount);
734
735 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700736 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400737};
738
739/**
rgindac9bc5502012-01-18 11:48:44 -0800740 * Deal with terminal width changes.
741 *
742 * This function does what needs to be done when the terminal width changes
743 * out from under us. It happens here rather than in onResize_() because this
744 * code may need to run synchronously to handle programmatic changes of
745 * terminal width.
746 *
747 * Relying on the browser to send us an async resize event means we may not be
748 * in the correct state yet when the next escape sequence hits.
749 */
750hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700751 if (columnCount <= 0)
752 throw new Error('Attempt to realize bad width: ' + columnCount);
753
rgindac9bc5502012-01-18 11:48:44 -0800754 var deltaColumns = columnCount - this.screen_.getWidth();
755
rginda87b86462011-12-14 13:48:03 -0800756 this.screenSize.width = columnCount;
757 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800758
759 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400760 if (this.defaultTabStops)
761 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800762 } else {
763 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400764 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800765 break;
766
767 this.tabStops_.pop();
768 }
769 }
770
771 this.screen_.setColumnCount(this.screenSize.width);
772};
773
774/**
775 * Deal with terminal height changes.
776 *
777 * This function does what needs to be done when the terminal height changes
778 * out from under us. It happens here rather than in onResize_() because this
779 * code may need to run synchronously to handle programmatic changes of
780 * terminal height.
781 *
782 * Relying on the browser to send us an async resize event means we may not be
783 * in the correct state yet when the next escape sequence hits.
784 */
785hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700786 if (rowCount <= 0)
787 throw new Error('Attempt to realize bad height: ' + rowCount);
788
rgindac9bc5502012-01-18 11:48:44 -0800789 var deltaRows = rowCount - this.screen_.getHeight();
790
791 this.screenSize.height = rowCount;
792
793 var cursor = this.saveCursor();
794
795 if (deltaRows < 0) {
796 // Screen got smaller.
797 deltaRows *= -1;
798 while (deltaRows) {
799 var lastRow = this.getRowCount() - 1;
800 if (lastRow - this.scrollbackRows_.length == cursor.row)
801 break;
802
803 if (this.getRowText(lastRow))
804 break;
805
806 this.screen_.popRow();
807 deltaRows--;
808 }
809
810 var ary = this.screen_.shiftRows(deltaRows);
811 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
812
813 // We just removed rows from the top of the screen, we need to update
814 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800815 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800816 } else if (deltaRows > 0) {
817 // Screen got larger.
818
819 if (deltaRows <= this.scrollbackRows_.length) {
820 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
821 var rows = this.scrollbackRows_.splice(
822 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
823 this.screen_.unshiftRows(rows);
824 deltaRows -= scrollbackCount;
825 cursor.row += scrollbackCount;
826 }
827
828 if (deltaRows)
829 this.appendRows_(deltaRows);
830 }
831
rginda35c456b2012-02-09 17:29:05 -0800832 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800833 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800834};
835
836/**
837 * Scroll the terminal to the top of the scrollback buffer.
838 */
839hterm.Terminal.prototype.scrollHome = function() {
840 this.scrollPort_.scrollRowToTop(0);
841};
842
843/**
844 * Scroll the terminal to the end.
845 */
846hterm.Terminal.prototype.scrollEnd = function() {
847 this.scrollPort_.scrollRowToBottom(this.getRowCount());
848};
849
850/**
851 * Scroll the terminal one page up (minus one line) relative to the current
852 * position.
853 */
854hterm.Terminal.prototype.scrollPageUp = function() {
855 var i = this.scrollPort_.getTopRowIndex();
856 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
857};
858
859/**
860 * Scroll the terminal one page down (minus one line) relative to the current
861 * position.
862 */
863hterm.Terminal.prototype.scrollPageDown = function() {
864 var i = this.scrollPort_.getTopRowIndex();
865 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800866};
867
rgindac9bc5502012-01-18 11:48:44 -0800868/**
Robert Ginda40932892012-12-10 17:26:40 -0800869 * Clear primary screen, secondary screen, and the scrollback buffer.
870 */
871hterm.Terminal.prototype.wipeContents = function() {
872 this.scrollbackRows_.length = 0;
873 this.scrollPort_.resetCache();
874
875 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
876 var bottom = screen.getHeight();
877 if (bottom > 0) {
878 this.renumberRows_(0, bottom);
879 this.clearHome(screen);
880 }
881 }.bind(this));
882
883 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -0700884 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -0800885};
886
887/**
rgindac9bc5502012-01-18 11:48:44 -0800888 * Full terminal reset.
889 */
rginda87b86462011-12-14 13:48:03 -0800890hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800891 this.clearAllTabStops();
892 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700893
894 this.clearHome(this.primaryScreen_);
895 this.primaryScreen_.textAttributes.reset();
896
897 this.clearHome(this.alternateScreen_);
898 this.alternateScreen_.textAttributes.reset();
899
rgindab8bc8932012-04-27 12:45:03 -0700900 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
901
Robert Ginda92e18102013-03-14 13:56:37 -0700902 this.vt.reset();
903
rgindac9bc5502012-01-18 11:48:44 -0800904 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800905};
906
rgindac9bc5502012-01-18 11:48:44 -0800907/**
908 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700909 *
910 * Perform a soft reset to the default values listed in
911 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800912 */
rginda0f5c0292012-01-13 11:00:13 -0800913hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700914 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800915 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700916
rgindab8bc8932012-04-27 12:45:03 -0700917 // Xterm also resets the color palette on soft reset, even though it doesn't
918 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700919 this.primaryScreen_.textAttributes.resetColorPalette();
920 this.alternateScreen_.textAttributes.resetColorPalette();
921
rgindab8bc8932012-04-27 12:45:03 -0700922 // The xterm man page explicitly says this will happen on soft reset.
923 this.setVTScrollRegion(null, null);
924
925 // Xterm also shows the cursor on soft reset, but does not alter the blink
926 // state.
rgindaa19afe22012-01-25 15:40:22 -0800927 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800928};
929
rgindac9bc5502012-01-18 11:48:44 -0800930/**
931 * Move the cursor forward to the next tab stop, or to the last column
932 * if no more tab stops are set.
933 */
934hterm.Terminal.prototype.forwardTabStop = function() {
935 var column = this.screen_.cursorPosition.column;
936
937 for (var i = 0; i < this.tabStops_.length; i++) {
938 if (this.tabStops_[i] > column) {
939 this.setCursorColumn(this.tabStops_[i]);
940 return;
941 }
942 }
943
David Benjamin66e954d2012-05-05 21:08:12 -0400944 // xterm does not clear the overflow flag on HT or CHT.
945 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800946 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400947 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800948};
949
rgindac9bc5502012-01-18 11:48:44 -0800950/**
951 * Move the cursor backward to the previous tab stop, or to the first column
952 * if no previous tab stops are set.
953 */
954hterm.Terminal.prototype.backwardTabStop = function() {
955 var column = this.screen_.cursorPosition.column;
956
957 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
958 if (this.tabStops_[i] < column) {
959 this.setCursorColumn(this.tabStops_[i]);
960 return;
961 }
962 }
963
964 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800965};
966
rgindac9bc5502012-01-18 11:48:44 -0800967/**
968 * Set a tab stop at the given column.
969 *
970 * @param {int} column Zero based column.
971 */
972hterm.Terminal.prototype.setTabStop = function(column) {
973 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
974 if (this.tabStops_[i] == column)
975 return;
976
977 if (this.tabStops_[i] < column) {
978 this.tabStops_.splice(i + 1, 0, column);
979 return;
980 }
981 }
982
983 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800984};
985
rgindac9bc5502012-01-18 11:48:44 -0800986/**
987 * Clear the tab stop at the current cursor position.
988 *
989 * No effect if there is no tab stop at the current cursor position.
990 */
991hterm.Terminal.prototype.clearTabStopAtCursor = function() {
992 var column = this.screen_.cursorPosition.column;
993
994 var i = this.tabStops_.indexOf(column);
995 if (i == -1)
996 return;
997
998 this.tabStops_.splice(i, 1);
999};
1000
1001/**
1002 * Clear all tab stops.
1003 */
1004hterm.Terminal.prototype.clearAllTabStops = function() {
1005 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001006 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001007};
1008
1009/**
1010 * Set up the default tab stops, starting from a given column.
1011 *
1012 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001013 * from the specified column, or 0 if no column is provided. It also flags
1014 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001015 *
1016 * This does not clear the existing tab stops first, use clearAllTabStops
1017 * for that.
1018 *
1019 * @param {int} opt_start Optional starting zero based starting column, useful
1020 * for filling out missing tab stops when the terminal is resized.
1021 */
1022hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1023 var start = opt_start || 0;
1024 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001025 // Round start up to a default tab stop.
1026 start = start - 1 - ((start - 1) % w) + w;
1027 for (var i = start; i < this.screenSize.width; i += w) {
1028 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001029 }
David Benjamin66e954d2012-05-05 21:08:12 -04001030
1031 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001032};
1033
rginda6d397402012-01-17 10:58:29 -08001034/**
rginda8ba33642011-12-14 12:31:31 -08001035 * Interpret a sequence of characters.
1036 *
1037 * Incomplete escape sequences are buffered until the next call.
1038 *
1039 * @param {string} str Sequence of characters to interpret or pass through.
1040 */
1041hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001042 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001043 this.scheduleSyncCursorPosition_();
1044};
1045
1046/**
1047 * Take over the given DIV for use as the terminal display.
1048 *
1049 * @param {HTMLDivElement} div The div to use as the terminal display.
1050 */
1051hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001052 this.div_ = div;
1053
rginda8ba33642011-12-14 12:31:31 -08001054 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001055 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001056 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1057 this.scrollPort_.setBackgroundPosition(
1058 this.prefs_.get('background-position'));
Robert Gindae76aa9f2014-03-14 12:29:12 -07001059 this.scrollPort_.setUserCss(this.prefs_.get('user-css'));
rginda30f20f62012-04-05 16:36:19 -07001060
rginda0918b652012-04-04 11:26:24 -07001061 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001062
rginda9f5222b2012-03-05 11:53:28 -08001063 this.setFontSize(this.prefs_.get('font-size'));
1064 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001065
David Reveman8f552492012-03-28 12:18:41 -04001066 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1067
rginda8ba33642011-12-14 12:31:31 -08001068 this.document_ = this.scrollPort_.getDocument();
1069
rginda4bba5e12012-06-20 16:15:30 -07001070 this.document_.body.oncontextmenu = function() { return false };
1071
1072 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001073 var screenNode = this.scrollPort_.getScreenNode();
1074 screenNode.addEventListener('mousedown', onMouse);
1075 screenNode.addEventListener('mouseup', onMouse);
1076 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001077 this.scrollPort_.onScrollWheel = onMouse;
1078
Toni Barzic0bfa8922013-11-22 11:18:35 -08001079 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001080 'focus', this.onFocusChange_.bind(this, true));
Toni Barzic0bfa8922013-11-22 11:18:35 -08001081 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001082 'blur', this.onFocusChange_.bind(this, false));
1083
1084 var style = this.document_.createElement('style');
1085 style.textContent =
1086 ('.cursor-node[focus="false"] {' +
1087 ' box-sizing: border-box;' +
1088 ' background-color: transparent !important;' +
1089 ' border-width: 2px;' +
1090 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001091 '}' +
1092 '.wc-node {' +
1093 ' display: inline-block;' +
1094 ' text-align: center;' +
1095 ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001096 '}');
1097 this.document_.head.appendChild(style);
1098
Ricky Liang48f05cb2013-12-31 23:35:29 +08001099 var styleSheets = this.document_.styleSheets;
1100 var cssRules = styleSheets[styleSheets.length - 1].cssRules;
1101 this.wcCssRule_ = cssRules[cssRules.length - 1];
1102
rginda8ba33642011-12-14 12:31:31 -08001103 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001104 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001105 this.cursorNode_.style.cssText =
1106 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001107 'top: -99px;' +
1108 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001109 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1110 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001111 '-webkit-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001112
rginda8e92a692012-05-20 19:37:20 -07001113 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001114 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1115 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001116
rginda8ba33642011-12-14 12:31:31 -08001117 this.document_.body.appendChild(this.cursorNode_);
1118
rgindad5613292012-06-19 15:40:37 -07001119 // When 'enableMouseDragScroll' is off we reposition this element directly
1120 // under the mouse cursor after a click. This makes Chrome associate
1121 // subsequent mousemove events with the scroll-blocker. Since the
1122 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1123 // events do not cause the scrollport to scroll.
1124 //
1125 // It's a hack, but it's the cleanest way I could find.
1126 this.scrollBlockerNode_ = this.document_.createElement('div');
1127 this.scrollBlockerNode_.style.cssText =
1128 ('position: absolute;' +
1129 'top: -99px;' +
1130 'display: block;' +
1131 'width: 10px;' +
1132 'height: 10px;');
1133 this.document_.body.appendChild(this.scrollBlockerNode_);
1134
1135 var onMouse = this.onMouse_.bind(this);
1136 this.scrollPort_.onScrollWheel = onMouse;
1137 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1138 ].forEach(function(event) {
1139 this.scrollBlockerNode_.addEventListener(event, onMouse);
1140 this.cursorNode_.addEventListener(event, onMouse);
1141 this.document_.addEventListener(event, onMouse);
1142 }.bind(this));
1143
1144 this.cursorNode_.addEventListener('mousedown', function() {
1145 setTimeout(this.focus.bind(this));
1146 }.bind(this));
1147
rginda8ba33642011-12-14 12:31:31 -08001148 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001149
rginda87b86462011-12-14 13:48:03 -08001150 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001151 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001152};
1153
rginda0918b652012-04-04 11:26:24 -07001154/**
1155 * Return the HTML document that contains the terminal DOM nodes.
1156 */
rginda87b86462011-12-14 13:48:03 -08001157hterm.Terminal.prototype.getDocument = function() {
1158 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001159};
1160
1161/**
rginda0918b652012-04-04 11:26:24 -07001162 * Focus the terminal.
1163 */
1164hterm.Terminal.prototype.focus = function() {
1165 this.scrollPort_.focus();
1166};
1167
1168/**
rginda8ba33642011-12-14 12:31:31 -08001169 * Return the HTML Element for a given row index.
1170 *
1171 * This is a method from the RowProvider interface. The ScrollPort uses
1172 * it to fetch rows on demand as they are scrolled into view.
1173 *
1174 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1175 * pairs to conserve memory.
1176 *
1177 * @param {integer} index The zero-based row index, measured relative to the
1178 * start of the scrollback buffer. On-screen rows will always have the
1179 * largest indicies.
1180 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1181 */
1182hterm.Terminal.prototype.getRowNode = function(index) {
1183 if (index < this.scrollbackRows_.length)
1184 return this.scrollbackRows_[index];
1185
1186 var screenIndex = index - this.scrollbackRows_.length;
1187 return this.screen_.rowsArray[screenIndex];
1188};
1189
1190/**
1191 * Return the text content for a given range of rows.
1192 *
1193 * This is a method from the RowProvider interface. The ScrollPort uses
1194 * it to fetch text content on demand when the user attempts to copy their
1195 * selection to the clipboard.
1196 *
1197 * @param {integer} start The zero-based row index to start from, measured
1198 * relative to the start of the scrollback buffer. On-screen rows will
1199 * always have the largest indicies.
1200 * @param {integer} end The zero-based row index to end on, measured
1201 * relative to the start of the scrollback buffer.
1202 * @return {string} A single string containing the text value of the range of
1203 * rows. Lines will be newline delimited, with no trailing newline.
1204 */
1205hterm.Terminal.prototype.getRowsText = function(start, end) {
1206 var ary = [];
1207 for (var i = start; i < end; i++) {
1208 var node = this.getRowNode(i);
1209 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001210 if (i < end - 1 && !node.getAttribute('line-overflow'))
1211 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001212 }
1213
rgindaa09e7332012-08-17 12:49:51 -07001214 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001215};
1216
1217/**
1218 * Return the text content for a given row.
1219 *
1220 * This is a method from the RowProvider interface. The ScrollPort uses
1221 * it to fetch text content on demand when the user attempts to copy their
1222 * selection to the clipboard.
1223 *
1224 * @param {integer} index The zero-based row index to return, measured
1225 * relative to the start of the scrollback buffer. On-screen rows will
1226 * always have the largest indicies.
1227 * @return {string} A string containing the text value of the selected row.
1228 */
1229hterm.Terminal.prototype.getRowText = function(index) {
1230 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001231 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001232};
1233
1234/**
1235 * Return the total number of rows in the addressable screen and in the
1236 * scrollback buffer of this terminal.
1237 *
1238 * This is a method from the RowProvider interface. The ScrollPort uses
1239 * it to compute the size of the scrollbar.
1240 *
1241 * @return {integer} The number of rows in this terminal.
1242 */
1243hterm.Terminal.prototype.getRowCount = function() {
1244 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1245};
1246
1247/**
1248 * Create DOM nodes for new rows and append them to the end of the terminal.
1249 *
1250 * This is the only correct way to add a new DOM node for a row. Notice that
1251 * the new row is appended to the bottom of the list of rows, and does not
1252 * require renumbering (of the rowIndex property) of previous rows.
1253 *
1254 * If you think you want a new blank row somewhere in the middle of the
1255 * terminal, look into moveRows_().
1256 *
1257 * This method does not pay attention to vtScrollTop/Bottom, since you should
1258 * be using moveRows() in cases where they would matter.
1259 *
1260 * The cursor will be positioned at column 0 of the first inserted line.
1261 */
1262hterm.Terminal.prototype.appendRows_ = function(count) {
1263 var cursorRow = this.screen_.rowsArray.length;
1264 var offset = this.scrollbackRows_.length + cursorRow;
1265 for (var i = 0; i < count; i++) {
1266 var row = this.document_.createElement('x-row');
1267 row.appendChild(this.document_.createTextNode(''));
1268 row.rowIndex = offset + i;
1269 this.screen_.pushRow(row);
1270 }
1271
1272 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1273 if (extraRows > 0) {
1274 var ary = this.screen_.shiftRows(extraRows);
1275 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001276 if (this.scrollPort_.isScrolledEnd)
1277 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001278 }
1279
1280 if (cursorRow >= this.screen_.rowsArray.length)
1281 cursorRow = this.screen_.rowsArray.length - 1;
1282
rginda87b86462011-12-14 13:48:03 -08001283 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001284};
1285
1286/**
1287 * Relocate rows from one part of the addressable screen to another.
1288 *
1289 * This is used to recycle rows during VT scrolls (those which are driven
1290 * by VT commands, rather than by the user manipulating the scrollbar.)
1291 *
1292 * In this case, the blank lines scrolled into the scroll region are made of
1293 * the nodes we scrolled off. These have their rowIndex properties carefully
1294 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001295 */
1296hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1297 var ary = this.screen_.removeRows(fromIndex, count);
1298 this.screen_.insertRows(toIndex, ary);
1299
1300 var start, end;
1301 if (fromIndex < toIndex) {
1302 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001303 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001304 } else {
1305 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001306 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001307 }
1308
1309 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001310 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001311};
1312
1313/**
1314 * Renumber the rowIndex property of the given range of rows.
1315 *
1316 * The start and end indicies are relative to the screen, not the scrollback.
1317 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001318 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001319 * no need to renumber scrollback rows.
1320 */
Robert Ginda40932892012-12-10 17:26:40 -08001321hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1322 var screen = opt_screen || this.screen_;
1323
rginda8ba33642011-12-14 12:31:31 -08001324 var offset = this.scrollbackRows_.length;
1325 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001326 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001327 }
1328};
1329
1330/**
1331 * Print a string to the terminal.
1332 *
1333 * This respects the current insert and wraparound modes. It will add new lines
1334 * to the end of the terminal, scrolling off the top into the scrollback buffer
1335 * if necessary.
1336 *
1337 * The string is *not* parsed for escape codes. Use the interpret() method if
1338 * that's what you're after.
1339 *
1340 * @param{string} str The string to print.
1341 */
1342hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001343 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001344
Ricky Liang48f05cb2013-12-31 23:35:29 +08001345 var strWidth = lib.wc.strWidth(str);
1346
1347 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001348 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1349 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001350 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001351 }
rgindaa19afe22012-01-25 15:40:22 -08001352
Ricky Liang48f05cb2013-12-31 23:35:29 +08001353 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001354 var didOverflow = false;
1355 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001356
rgindaa9abdd82012-08-06 18:05:09 -07001357 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1358 didOverflow = true;
1359 count = this.screenSize.width - this.screen_.cursorPosition.column;
1360 }
rgindaa19afe22012-01-25 15:40:22 -08001361
rgindaa9abdd82012-08-06 18:05:09 -07001362 if (didOverflow && !this.options_.wraparound) {
1363 // If the string overflowed the line but wraparound is off, then the
1364 // last printed character should be the last of the string.
1365 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001366 substr = lib.wc.substr(str, startOffset, count - 1) +
1367 lib.wc.substr(str, strWidth - 1);
1368 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001369 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001370 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001371 }
rgindaa19afe22012-01-25 15:40:22 -08001372
Ricky Liang48f05cb2013-12-31 23:35:29 +08001373 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1374 for (var i = 0; i < tokens.length; i++) {
1375 if (tokens[i].wcNode)
1376 this.screen_.textAttributes.wcNode = true;
1377
1378 if (this.options_.insertMode) {
1379 this.screen_.insertString(tokens[i].str);
1380 } else {
1381 this.screen_.overwriteString(tokens[i].str);
1382 }
1383 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001384 }
1385
1386 this.screen_.maybeClipCurrentRow();
1387 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001388 }
rginda8ba33642011-12-14 12:31:31 -08001389
1390 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001391
rginda9f5222b2012-03-05 11:53:28 -08001392 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001393 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001394};
1395
1396/**
rginda87b86462011-12-14 13:48:03 -08001397 * Set the VT scroll region.
1398 *
rginda87b86462011-12-14 13:48:03 -08001399 * This also resets the cursor position to the absolute (0, 0) position, since
1400 * that's what xterm appears to do.
1401 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001402 * Setting the scroll region to the full height of the terminal will clear
1403 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1404 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1405 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1406 * continue to work as most users would expect.
1407 *
rginda87b86462011-12-14 13:48:03 -08001408 * @param {integer} scrollTop The zero-based top of the scroll region.
1409 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1410 * inclusive.
1411 */
1412hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001413 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001414 this.vtScrollTop_ = null;
1415 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001416 } else {
1417 this.vtScrollTop_ = scrollTop;
1418 this.vtScrollBottom_ = scrollBottom;
1419 }
rginda87b86462011-12-14 13:48:03 -08001420};
1421
1422/**
rginda8ba33642011-12-14 12:31:31 -08001423 * Return the top row index according to the VT.
1424 *
1425 * This will return 0 unless the terminal has been told to restrict scrolling
1426 * to some lower row. It is used for some VT cursor positioning and scrolling
1427 * commands.
1428 *
1429 * @return {integer} The topmost row in the terminal's scroll region.
1430 */
1431hterm.Terminal.prototype.getVTScrollTop = function() {
1432 if (this.vtScrollTop_ != null)
1433 return this.vtScrollTop_;
1434
1435 return 0;
rginda87b86462011-12-14 13:48:03 -08001436};
rginda8ba33642011-12-14 12:31:31 -08001437
1438/**
1439 * Return the bottom row index according to the VT.
1440 *
1441 * This will return the height of the terminal unless the it has been told to
1442 * restrict scrolling to some higher row. It is used for some VT cursor
1443 * positioning and scrolling commands.
1444 *
1445 * @return {integer} The bottommost row in the terminal's scroll region.
1446 */
1447hterm.Terminal.prototype.getVTScrollBottom = function() {
1448 if (this.vtScrollBottom_ != null)
1449 return this.vtScrollBottom_;
1450
rginda87b86462011-12-14 13:48:03 -08001451 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001452}
1453
1454/**
1455 * Process a '\n' character.
1456 *
1457 * If the cursor is on the final row of the terminal this will append a new
1458 * blank row to the screen and scroll the topmost row into the scrollback
1459 * buffer.
1460 *
1461 * Otherwise, this moves the cursor to column zero of the next row.
1462 */
1463hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001464 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1465 this.screen_.rowsArray.length - 1);
1466
1467 if (this.vtScrollBottom_ != null) {
1468 // A VT Scroll region is active, we never append new rows.
1469 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1470 // We're at the end of the VT Scroll Region, perform a VT scroll.
1471 this.vtScrollUp(1);
1472 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1473 } else if (cursorAtEndOfScreen) {
1474 // We're at the end of the screen, the only thing to do is put the
1475 // cursor to column 0.
1476 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1477 } else {
1478 // Anywhere else, advance the cursor row, and reset the column.
1479 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1480 }
1481 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001482 // We're at the end of the screen. Append a new row to the terminal,
1483 // shifting the top row into the scrollback.
1484 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001485 } else {
rginda87b86462011-12-14 13:48:03 -08001486 // Anywhere else in the screen just moves the cursor.
1487 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001488 }
1489};
1490
1491/**
1492 * Like newLine(), except maintain the cursor column.
1493 */
1494hterm.Terminal.prototype.lineFeed = function() {
1495 var column = this.screen_.cursorPosition.column;
1496 this.newLine();
1497 this.setCursorColumn(column);
1498};
1499
1500/**
rginda87b86462011-12-14 13:48:03 -08001501 * If autoCarriageReturn is set then newLine(), else lineFeed().
1502 */
1503hterm.Terminal.prototype.formFeed = function() {
1504 if (this.options_.autoCarriageReturn) {
1505 this.newLine();
1506 } else {
1507 this.lineFeed();
1508 }
1509};
1510
1511/**
1512 * Move the cursor up one row, possibly inserting a blank line.
1513 *
1514 * The cursor column is not changed.
1515 */
1516hterm.Terminal.prototype.reverseLineFeed = function() {
1517 var scrollTop = this.getVTScrollTop();
1518 var currentRow = this.screen_.cursorPosition.row;
1519
1520 if (currentRow == scrollTop) {
1521 this.insertLines(1);
1522 } else {
1523 this.setAbsoluteCursorRow(currentRow - 1);
1524 }
1525};
1526
1527/**
rginda8ba33642011-12-14 12:31:31 -08001528 * Replace all characters to the left of the current cursor with the space
1529 * character.
1530 *
1531 * TODO(rginda): This should probably *remove* the characters (not just replace
1532 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001533 * position.
rginda8ba33642011-12-14 12:31:31 -08001534 */
1535hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001536 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001537 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001538 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001539 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001540};
1541
1542/**
David Benjamin684a9b72012-05-01 17:19:58 -04001543 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001544 *
1545 * The cursor position is unchanged.
1546 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001547 * If the current background color is not the default background color this
1548 * will insert spaces rather than delete. This is unfortunate because the
1549 * trailing space will affect text selection, but it's difficult to come up
1550 * with a way to style empty space that wouldn't trip up the hterm.Screen
1551 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001552 *
1553 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1554 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1555 * crbug.com/232390 for details.
rginda8ba33642011-12-14 12:31:31 -08001556 */
1557hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001558 if (this.screen_.cursorPosition.overflow)
1559 return;
1560
Robert Ginda7fd57082012-09-25 14:41:47 -07001561 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1562 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001563
1564 if (this.screen_.textAttributes.background ===
1565 this.screen_.textAttributes.DEFAULT_COLOR) {
1566 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001567 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001568 this.screen_.cursorPosition.column + count) {
1569 this.screen_.deleteChars(count);
1570 this.clearCursorOverflow();
1571 return;
1572 }
1573 }
1574
rginda87b86462011-12-14 13:48:03 -08001575 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001576 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001577 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001578 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001579};
1580
1581/**
1582 * Erase the current line.
1583 *
1584 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001585 */
1586hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001587 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001588 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001589 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001590 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001591};
1592
1593/**
David Benjamina08d78f2012-05-05 00:28:49 -04001594 * Erase all characters from the start of the screen to the current cursor
1595 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001596 *
1597 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001598 */
1599hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001600 var cursor = this.saveCursor();
1601
1602 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001603
David Benjamina08d78f2012-05-05 00:28:49 -04001604 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001605 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001606 this.screen_.clearCursorRow();
1607 }
1608
rginda87b86462011-12-14 13:48:03 -08001609 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001610 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001611};
1612
1613/**
1614 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001615 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001616 *
1617 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001618 */
1619hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001620 var cursor = this.saveCursor();
1621
1622 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001623
David Benjamina08d78f2012-05-05 00:28:49 -04001624 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001625 for (var i = cursor.row + 1; i <= bottom; i++) {
1626 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001627 this.screen_.clearCursorRow();
1628 }
1629
rginda87b86462011-12-14 13:48:03 -08001630 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001631 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001632};
1633
1634/**
1635 * Fill the terminal with a given character.
1636 *
1637 * This methods does not respect the VT scroll region.
1638 *
1639 * @param {string} ch The character to use for the fill.
1640 */
1641hterm.Terminal.prototype.fill = function(ch) {
1642 var cursor = this.saveCursor();
1643
1644 this.setAbsoluteCursorPosition(0, 0);
1645 for (var row = 0; row < this.screenSize.height; row++) {
1646 for (var col = 0; col < this.screenSize.width; col++) {
1647 this.setAbsoluteCursorPosition(row, col);
1648 this.screen_.overwriteString(ch);
1649 }
1650 }
1651
1652 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001653};
1654
1655/**
rginda9ea433c2012-03-16 11:57:00 -07001656 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001657 *
rginda9ea433c2012-03-16 11:57:00 -07001658 * This does not respect the scroll region.
1659 *
1660 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1661 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001662 */
rginda9ea433c2012-03-16 11:57:00 -07001663hterm.Terminal.prototype.clearHome = function(opt_screen) {
1664 var screen = opt_screen || this.screen_;
1665 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001666
rginda11057d52012-04-25 12:29:56 -07001667 if (bottom == 0) {
1668 // Empty screen, nothing to do.
1669 return;
1670 }
1671
rgindae4d29232012-01-19 10:47:13 -08001672 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001673 screen.setCursorPosition(i, 0);
1674 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001675 }
1676
rginda9ea433c2012-03-16 11:57:00 -07001677 screen.setCursorPosition(0, 0);
1678};
1679
1680/**
1681 * Erase the entire display without changing the cursor position.
1682 *
1683 * The cursor position is unchanged. This does not respect the scroll
1684 * region.
1685 *
1686 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1687 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001688 */
1689hterm.Terminal.prototype.clear = function(opt_screen) {
1690 var screen = opt_screen || this.screen_;
1691 var cursor = screen.cursorPosition.clone();
1692 this.clearHome(screen);
1693 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001694};
1695
1696/**
1697 * VT command to insert lines at the current cursor row.
1698 *
1699 * This respects the current scroll region. Rows pushed off the bottom are
1700 * lost (they won't show up in the scrollback buffer).
1701 *
rginda8ba33642011-12-14 12:31:31 -08001702 * @param {integer} count The number of lines to insert.
1703 */
1704hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001705 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001706
1707 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001708 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001709
Robert Ginda579186b2012-09-26 11:40:04 -07001710 // The moveCount is the number of rows we need to relocate to make room for
1711 // the new row(s). The count is the distance to move them.
1712 var moveCount = bottom - cursorRow - count + 1;
1713 if (moveCount)
1714 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001715
Robert Ginda579186b2012-09-26 11:40:04 -07001716 for (var i = count - 1; i >= 0; i--) {
1717 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001718 this.screen_.clearCursorRow();
1719 }
rginda8ba33642011-12-14 12:31:31 -08001720};
1721
1722/**
1723 * VT command to delete lines at the current cursor row.
1724 *
1725 * New rows are added to the bottom of scroll region to take their place. New
1726 * rows are strictly there to take up space and have no content or style.
1727 */
1728hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001729 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001730
rginda87b86462011-12-14 13:48:03 -08001731 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001732 var bottom = this.getVTScrollBottom();
1733
rginda87b86462011-12-14 13:48:03 -08001734 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001735 count = Math.min(count, maxCount);
1736
rginda87b86462011-12-14 13:48:03 -08001737 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001738 if (count != maxCount)
1739 this.moveRows_(top, count, moveStart);
1740
1741 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001742 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001743 this.screen_.clearCursorRow();
1744 }
1745
rginda87b86462011-12-14 13:48:03 -08001746 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001747 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001748};
1749
1750/**
1751 * Inserts the given number of spaces at the current cursor position.
1752 *
rginda87b86462011-12-14 13:48:03 -08001753 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001754 */
1755hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001756 var cursor = this.saveCursor();
1757
rgindacbbd7482012-06-13 15:06:16 -07001758 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001759 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001760 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001761
1762 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001763 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001764};
1765
1766/**
1767 * Forward-delete the specified number of characters starting at the cursor
1768 * position.
1769 *
1770 * @param {integer} count The number of characters to delete.
1771 */
1772hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001773 var deleted = this.screen_.deleteChars(count);
1774 if (deleted && !this.screen_.textAttributes.isDefault()) {
1775 var cursor = this.saveCursor();
1776 this.setCursorColumn(this.screenSize.width - deleted);
1777 this.screen_.insertString(lib.f.getWhitespace(deleted));
1778 this.restoreCursor(cursor);
1779 }
1780
David Benjamin54e8bf62012-06-01 22:31:40 -04001781 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001782};
1783
1784/**
1785 * Shift rows in the scroll region upwards by a given number of lines.
1786 *
1787 * New rows are inserted at the bottom of the scroll region to fill the
1788 * vacated rows. The new rows not filled out with the current text attributes.
1789 *
1790 * This function does not affect the scrollback rows at all. Rows shifted
1791 * off the top are lost.
1792 *
rginda87b86462011-12-14 13:48:03 -08001793 * The cursor position is not altered.
1794 *
rginda8ba33642011-12-14 12:31:31 -08001795 * @param {integer} count The number of rows to scroll.
1796 */
1797hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001798 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001799
rginda87b86462011-12-14 13:48:03 -08001800 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001801 this.deleteLines(count);
1802
rginda87b86462011-12-14 13:48:03 -08001803 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001804};
1805
1806/**
1807 * Shift rows below the cursor down by a given number of lines.
1808 *
1809 * This function respects the current scroll region.
1810 *
1811 * New rows are inserted at the top of the scroll region to fill the
1812 * vacated rows. The new rows not filled out with the current text attributes.
1813 *
1814 * This function does not affect the scrollback rows at all. Rows shifted
1815 * off the bottom are lost.
1816 *
1817 * @param {integer} count The number of rows to scroll.
1818 */
1819hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001820 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001821
rginda87b86462011-12-14 13:48:03 -08001822 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001823 this.insertLines(opt_count);
1824
rginda87b86462011-12-14 13:48:03 -08001825 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001826};
1827
rginda87b86462011-12-14 13:48:03 -08001828
rginda8ba33642011-12-14 12:31:31 -08001829/**
1830 * Set the cursor position.
1831 *
1832 * The cursor row is relative to the scroll region if the terminal has
1833 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1834 *
1835 * @param {integer} row The new zero-based cursor row.
1836 * @param {integer} row The new zero-based cursor column.
1837 */
1838hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1839 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001840 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001841 } else {
rginda87b86462011-12-14 13:48:03 -08001842 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001843 }
rginda87b86462011-12-14 13:48:03 -08001844};
rginda8ba33642011-12-14 12:31:31 -08001845
rginda87b86462011-12-14 13:48:03 -08001846hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1847 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001848 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1849 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001850 this.screen_.setCursorPosition(row, column);
1851};
1852
1853hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001854 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1855 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001856 this.screen_.setCursorPosition(row, column);
1857};
1858
1859/**
1860 * Set the cursor column.
1861 *
1862 * @param {integer} column The new zero-based cursor column.
1863 */
1864hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001865 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001866};
1867
1868/**
1869 * Return the cursor column.
1870 *
1871 * @return {integer} The zero-based cursor column.
1872 */
1873hterm.Terminal.prototype.getCursorColumn = function() {
1874 return this.screen_.cursorPosition.column;
1875};
1876
1877/**
1878 * Set the cursor row.
1879 *
1880 * The cursor row is relative to the scroll region if the terminal has
1881 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1882 *
1883 * @param {integer} row The new cursor row.
1884 */
rginda87b86462011-12-14 13:48:03 -08001885hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1886 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001887};
1888
1889/**
1890 * Return the cursor row.
1891 *
1892 * @return {integer} The zero-based cursor row.
1893 */
1894hterm.Terminal.prototype.getCursorRow = function(row) {
1895 return this.screen_.cursorPosition.row;
1896};
1897
1898/**
1899 * Request that the ScrollPort redraw itself soon.
1900 *
1901 * The redraw will happen asynchronously, soon after the call stack winds down.
1902 * Multiple calls will be coalesced into a single redraw.
1903 */
1904hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001905 if (this.timeouts_.redraw)
1906 return;
rginda8ba33642011-12-14 12:31:31 -08001907
1908 var self = this;
rginda87b86462011-12-14 13:48:03 -08001909 this.timeouts_.redraw = setTimeout(function() {
1910 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001911 self.scrollPort_.redraw_();
1912 }, 0);
1913};
1914
1915/**
1916 * Request that the ScrollPort be scrolled to the bottom.
1917 *
1918 * The scroll will happen asynchronously, soon after the call stack winds down.
1919 * Multiple calls will be coalesced into a single scroll.
1920 *
1921 * This affects the scrollbar position of the ScrollPort, and has nothing to
1922 * do with the VT scroll commands.
1923 */
1924hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1925 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001926 return;
rginda8ba33642011-12-14 12:31:31 -08001927
1928 var self = this;
1929 this.timeouts_.scrollDown = setTimeout(function() {
1930 delete self.timeouts_.scrollDown;
1931 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1932 }, 10);
1933};
1934
1935/**
1936 * Move the cursor up a specified number of rows.
1937 *
1938 * @param {integer} count The number of rows to move the cursor.
1939 */
1940hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001941 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001942};
1943
1944/**
1945 * Move the cursor down a specified number of rows.
1946 *
1947 * @param {integer} count The number of rows to move the cursor.
1948 */
1949hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001950 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001951 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1952 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1953 this.screenSize.height - 1);
1954
rgindacbbd7482012-06-13 15:06:16 -07001955 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001956 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001957 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001958};
1959
1960/**
1961 * Move the cursor left a specified number of columns.
1962 *
1963 * @param {integer} count The number of columns to move the cursor.
1964 */
1965hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001966 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001967};
1968
1969/**
1970 * Move the cursor right a specified number of columns.
1971 *
1972 * @param {integer} count The number of columns to move the cursor.
1973 */
1974hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001975 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001976 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001977 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001978 this.setCursorColumn(column);
1979};
1980
1981/**
1982 * Reverse the foreground and background colors of the terminal.
1983 *
1984 * This only affects text that was drawn with no attributes.
1985 *
1986 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1987 * been drawn with attributes that happen to coincide with the default
1988 * 'no-attribute' colors. My guess is probably not.
1989 */
1990hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001991 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001992 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001993 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1994 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001995 } else {
rginda9f5222b2012-03-05 11:53:28 -08001996 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1997 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001998 }
1999};
2000
2001/**
rginda87b86462011-12-14 13:48:03 -08002002 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002003 *
2004 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002005 */
2006hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002007 this.cursorNode_.style.backgroundColor =
2008 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002009
2010 var self = this;
2011 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002012 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002013 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002014
2015 if (this.bellAudio_.getAttribute('src')) {
Robert Gindaa6331372013-03-19 10:35:39 -07002016 if (this.bellSquelchTimeout_)
Robert Ginda92e18102013-03-14 13:56:37 -07002017 return;
2018
2019 this.bellAudio_.play();
2020
2021 this.bellSequelchTimeout_ = setTimeout(function() {
2022 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002023 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002024 } else {
2025 delete this.bellSquelchTimeout_;
2026 }
rginda87b86462011-12-14 13:48:03 -08002027};
2028
2029/**
rginda8ba33642011-12-14 12:31:31 -08002030 * Set the origin mode bit.
2031 *
2032 * If origin mode is on, certain VT cursor and scrolling commands measure their
2033 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2034 * to the top of the addressable screen.
2035 *
2036 * Defaults to off.
2037 *
2038 * @param {boolean} state True to set origin mode, false to unset.
2039 */
2040hterm.Terminal.prototype.setOriginMode = function(state) {
2041 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002042 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002043};
2044
2045/**
2046 * Set the insert mode bit.
2047 *
2048 * If insert mode is on, existing text beyond the cursor position will be
2049 * shifted right to make room for new text. Otherwise, new text overwrites
2050 * any existing text.
2051 *
2052 * Defaults to off.
2053 *
2054 * @param {boolean} state True to set insert mode, false to unset.
2055 */
2056hterm.Terminal.prototype.setInsertMode = function(state) {
2057 this.options_.insertMode = state;
2058};
2059
2060/**
rginda87b86462011-12-14 13:48:03 -08002061 * Set the auto carriage return bit.
2062 *
2063 * If auto carriage return is on then a formfeed character is interpreted
2064 * as a newline, otherwise it's the same as a linefeed. The difference boils
2065 * down to whether or not the cursor column is reset.
2066 */
2067hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2068 this.options_.autoCarriageReturn = state;
2069};
2070
2071/**
rginda8ba33642011-12-14 12:31:31 -08002072 * Set the wraparound mode bit.
2073 *
2074 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2075 * to the start of the following row. Otherwise, the cursor is clamped to the
2076 * end of the screen and attempts to write past it are ignored.
2077 *
2078 * Defaults to on.
2079 *
2080 * @param {boolean} state True to set wraparound mode, false to unset.
2081 */
2082hterm.Terminal.prototype.setWraparound = function(state) {
2083 this.options_.wraparound = state;
2084};
2085
2086/**
2087 * Set the reverse-wraparound mode bit.
2088 *
2089 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2090 * to the end of the previous row. Otherwise, the cursor is clamped to column
2091 * 0.
2092 *
2093 * Defaults to off.
2094 *
2095 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2096 */
2097hterm.Terminal.prototype.setReverseWraparound = function(state) {
2098 this.options_.reverseWraparound = state;
2099};
2100
2101/**
2102 * Selects between the primary and alternate screens.
2103 *
2104 * If alternate mode is on, the alternate screen is active. Otherwise the
2105 * primary screen is active.
2106 *
2107 * Swapping screens has no effect on the scrollback buffer.
2108 *
2109 * Each screen maintains its own cursor position.
2110 *
2111 * Defaults to off.
2112 *
2113 * @param {boolean} state True to set alternate mode, false to unset.
2114 */
2115hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002116 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002117 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2118
rginda35c456b2012-02-09 17:29:05 -08002119 if (this.screen_.rowsArray.length &&
2120 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2121 // If the screen changed sizes while we were away, our rowIndexes may
2122 // be incorrect.
2123 var offset = this.scrollbackRows_.length;
2124 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002125 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002126 ary[i].rowIndex = offset + i;
2127 }
2128 }
rginda8ba33642011-12-14 12:31:31 -08002129
rginda35c456b2012-02-09 17:29:05 -08002130 this.realizeWidth_(this.screenSize.width);
2131 this.realizeHeight_(this.screenSize.height);
2132 this.scrollPort_.syncScrollHeight();
2133 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002134
rginda6d397402012-01-17 10:58:29 -08002135 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002136 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002137};
2138
2139/**
2140 * Set the cursor-blink mode bit.
2141 *
2142 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2143 * a visible cursor does not blink.
2144 *
2145 * You should make sure to turn blinking off if you're going to dispose of a
2146 * terminal, otherwise you'll leak a timeout.
2147 *
2148 * Defaults to on.
2149 *
2150 * @param {boolean} state True to set cursor-blink mode, false to unset.
2151 */
2152hterm.Terminal.prototype.setCursorBlink = function(state) {
2153 this.options_.cursorBlink = state;
2154
2155 if (!state && this.timeouts_.cursorBlink) {
2156 clearTimeout(this.timeouts_.cursorBlink);
2157 delete this.timeouts_.cursorBlink;
2158 }
2159
2160 if (this.options_.cursorVisible)
2161 this.setCursorVisible(true);
2162};
2163
2164/**
2165 * Set the cursor-visible mode bit.
2166 *
2167 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2168 *
2169 * Defaults to on.
2170 *
2171 * @param {boolean} state True to set cursor-visible mode, false to unset.
2172 */
2173hterm.Terminal.prototype.setCursorVisible = function(state) {
2174 this.options_.cursorVisible = state;
2175
2176 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002177 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002178 return;
2179 }
2180
rginda87b86462011-12-14 13:48:03 -08002181 this.syncCursorPosition_();
2182
2183 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002184
2185 if (this.options_.cursorBlink) {
2186 if (this.timeouts_.cursorBlink)
2187 return;
2188
2189 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2190 500);
2191 } else {
2192 if (this.timeouts_.cursorBlink) {
2193 clearTimeout(this.timeouts_.cursorBlink);
2194 delete this.timeouts_.cursorBlink;
2195 }
2196 }
2197};
2198
2199/**
rginda87b86462011-12-14 13:48:03 -08002200 * Synchronizes the visible cursor and document selection with the current
2201 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002202 */
2203hterm.Terminal.prototype.syncCursorPosition_ = function() {
2204 var topRowIndex = this.scrollPort_.getTopRowIndex();
2205 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2206 var cursorRowIndex = this.scrollbackRows_.length +
2207 this.screen_.cursorPosition.row;
2208
2209 if (cursorRowIndex > bottomRowIndex) {
2210 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002211 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002212 return;
2213 }
2214
2215 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002216 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2217 'px';
2218 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2219 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002220
2221 this.cursorNode_.setAttribute('title',
2222 '(' + this.screen_.cursorPosition.row +
2223 ', ' + this.screen_.cursorPosition.column +
2224 ')');
2225
2226 // Update the caret for a11y purposes.
2227 var selection = this.document_.getSelection();
2228 if (selection && selection.isCollapsed)
2229 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002230};
2231
Robert Gindafb1be6a2013-12-11 11:56:22 -08002232/**
2233 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2234 * and character cell dimensions.
2235 */
Robert Ginda830583c2013-08-07 13:20:46 -07002236hterm.Terminal.prototype.restyleCursor_ = function() {
2237 var shape = this.cursorShape_;
2238
2239 if (this.cursorNode_.getAttribute('focus') == 'false') {
2240 // Always show a block cursor when unfocused.
2241 shape = hterm.Terminal.cursorShape.BLOCK;
2242 }
2243
2244 var style = this.cursorNode_.style;
2245
Robert Gindafb1be6a2013-12-11 11:56:22 -08002246 style.width = this.scrollPort_.characterSize.width + 'px';
2247
Robert Ginda830583c2013-08-07 13:20:46 -07002248 switch (shape) {
2249 case hterm.Terminal.cursorShape.BEAM:
2250 style.height = this.scrollPort_.characterSize.height + 'px';
2251 style.backgroundColor = 'transparent';
2252 style.borderBottomStyle = null;
2253 style.borderLeftStyle = 'solid';
2254 break;
2255
2256 case hterm.Terminal.cursorShape.UNDERLINE:
2257 style.height = this.scrollPort_.characterSize.baseline + 'px';
2258 style.backgroundColor = 'transparent';
2259 style.borderBottomStyle = 'solid';
2260 // correct the size to put it exactly at the baseline
2261 style.borderLeftStyle = null;
2262 break;
2263
2264 default:
2265 style.height = this.scrollPort_.characterSize.height + 'px';
2266 style.backgroundColor = this.cursorColor_;
2267 style.borderBottomStyle = null;
2268 style.borderLeftStyle = null;
2269 break;
2270 }
2271};
2272
rginda8ba33642011-12-14 12:31:31 -08002273/**
2274 * Synchronizes the visible cursor with the current cursor coordinates.
2275 *
2276 * The sync will happen asynchronously, soon after the call stack winds down.
2277 * Multiple calls will be coalesced into a single sync.
2278 */
2279hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2280 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002281 return;
rginda8ba33642011-12-14 12:31:31 -08002282
2283 var self = this;
2284 this.timeouts_.syncCursor = setTimeout(function() {
2285 self.syncCursorPosition_();
2286 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002287 }, 0);
2288};
2289
rgindacc2996c2012-02-24 14:59:31 -08002290/**
rgindaf522ce02012-04-17 17:49:17 -07002291 * Show or hide the zoom warning.
2292 *
2293 * The zoom warning is a message warning the user that their browser zoom must
2294 * be set to 100% in order for hterm to function properly.
2295 *
2296 * @param {boolean} state True to show the message, false to hide it.
2297 */
2298hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2299 if (!this.zoomWarningNode_) {
2300 if (!state)
2301 return;
2302
2303 this.zoomWarningNode_ = this.document_.createElement('div');
2304 this.zoomWarningNode_.style.cssText = (
2305 'color: black;' +
2306 'background-color: #ff2222;' +
2307 'font-size: large;' +
2308 'border-radius: 8px;' +
2309 'opacity: 0.75;' +
2310 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2311 'top: 0.5em;' +
2312 'right: 1.2em;' +
2313 'position: absolute;' +
2314 '-webkit-text-size-adjust: none;' +
2315 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002316 }
2317
Robert Gindab4839c22013-02-28 16:52:10 -08002318 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2319 hterm.zoomWarningMessage,
2320 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2321
rgindaf522ce02012-04-17 17:49:17 -07002322 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2323
2324 if (state) {
2325 if (!this.zoomWarningNode_.parentNode)
2326 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2327 } else if (this.zoomWarningNode_.parentNode) {
2328 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2329 }
2330};
2331
2332/**
rgindacc2996c2012-02-24 14:59:31 -08002333 * Show the terminal overlay for a given amount of time.
2334 *
2335 * The terminal overlay appears in inverse video in a large font, centered
2336 * over the terminal. You should probably keep the overlay message brief,
2337 * since it's in a large font and you probably aren't going to check the size
2338 * of the terminal first.
2339 *
2340 * @param {string} msg The text (not HTML) message to display in the overlay.
2341 * @param {number} opt_timeout The amount of time to wait before fading out
2342 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2343 * stay up forever (or until the next overlay).
2344 */
2345hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002346 if (!this.overlayNode_) {
2347 if (!this.div_)
2348 return;
2349
2350 this.overlayNode_ = this.document_.createElement('div');
2351 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002352 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002353 'font-size: xx-large;' +
2354 'opacity: 0.75;' +
2355 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2356 'position: absolute;' +
2357 '-webkit-user-select: none;' +
2358 '-webkit-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002359
2360 this.overlayNode_.addEventListener('mousedown', function(e) {
2361 e.preventDefault();
2362 e.stopPropagation();
2363 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002364 }
2365
rginda9f5222b2012-03-05 11:53:28 -08002366 this.overlayNode_.style.color = this.prefs_.get('background-color');
2367 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2368 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2369
rgindaf0090c92012-02-10 14:58:52 -08002370 this.overlayNode_.textContent = msg;
2371 this.overlayNode_.style.opacity = '0.75';
2372
2373 if (!this.overlayNode_.parentNode)
2374 this.div_.appendChild(this.overlayNode_);
2375
Robert Ginda97769282013-02-01 15:30:30 -08002376 var divSize = hterm.getClientSize(this.div_);
2377 var overlaySize = hterm.getClientSize(this.overlayNode_);
2378
2379 this.overlayNode_.style.top = (divSize.height - overlaySize.height) / 2;
2380 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
2381 this.scrollPort_.currentScrollbarWidthPx) / 2;
rgindaf0090c92012-02-10 14:58:52 -08002382
2383 var self = this;
2384
2385 if (this.overlayTimeout_)
2386 clearTimeout(this.overlayTimeout_);
2387
rgindacc2996c2012-02-24 14:59:31 -08002388 if (opt_timeout === null)
2389 return;
2390
rgindaf0090c92012-02-10 14:58:52 -08002391 this.overlayTimeout_ = setTimeout(function() {
2392 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002393 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002394 if (self.overlayNode_.parentNode)
2395 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002396 self.overlayTimeout_ = null;
2397 self.overlayNode_.style.opacity = '0.75';
2398 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002399 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002400};
2401
rginda4bba5e12012-06-20 16:15:30 -07002402/**
2403 * Paste from the system clipboard to the terminal.
2404 */
2405hterm.Terminal.prototype.paste = function() {
2406 hterm.pasteFromClipboard(this.document_);
2407};
2408
2409/**
2410 * Copy a string to the system clipboard.
2411 *
2412 * Note: If there is a selected range in the terminal, it'll be cleared.
2413 */
2414hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002415 if (this.prefs_.get('enable-clipboard-notice'))
2416 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2417
rgindaa09e7332012-08-17 12:49:51 -07002418 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002419 copySource.textContent = str;
2420 copySource.style.cssText = (
2421 '-webkit-user-select: text;' +
2422 'position: absolute;' +
2423 'top: -99px');
2424
2425 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002426
rginda4bba5e12012-06-20 16:15:30 -07002427 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002428 var anchorNode = selection.anchorNode;
2429 var anchorOffset = selection.anchorOffset;
2430 var focusNode = selection.focusNode;
2431 var focusOffset = selection.focusOffset;
2432
rginda4bba5e12012-06-20 16:15:30 -07002433 selection.selectAllChildren(copySource);
2434
rgindaa09e7332012-08-17 12:49:51 -07002435 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002436
rgindafaa74742012-08-21 13:34:03 -07002437 selection.collapse(anchorNode, anchorOffset);
2438 selection.extend(focusNode, focusOffset);
2439
rginda4bba5e12012-06-20 16:15:30 -07002440 copySource.parentNode.removeChild(copySource);
2441};
2442
rgindaa09e7332012-08-17 12:49:51 -07002443hterm.Terminal.prototype.getSelectionText = function() {
2444 var selection = this.scrollPort_.selection;
2445 selection.sync();
2446
2447 if (selection.isCollapsed)
2448 return null;
2449
2450
2451 // Start offset measures from the beginning of the line.
2452 var startOffset = selection.startOffset;
2453 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002454
Robert Gindafdbb3f22012-09-06 20:23:06 -07002455 if (node.nodeName != 'X-ROW') {
2456 // If the selection doesn't start on an x-row node, then it must be
2457 // somewhere inside the x-row. Add any characters from previous siblings
2458 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002459
2460 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2461 // If node is the text node in a styled span, move up to the span node.
2462 node = node.parentNode;
2463 }
2464
Robert Gindafdbb3f22012-09-06 20:23:06 -07002465 while (node.previousSibling) {
2466 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002467 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002468 }
rgindaa09e7332012-08-17 12:49:51 -07002469 }
2470
2471 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002472 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2473 selection.endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002474 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002475
Robert Gindafdbb3f22012-09-06 20:23:06 -07002476 if (node.nodeName != 'X-ROW') {
2477 // If the selection doesn't end on an x-row node, then it must be
2478 // somewhere inside the x-row. Add any characters from following siblings
2479 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002480
2481 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2482 // If node is the text node in a styled span, move up to the span node.
2483 node = node.parentNode;
2484 }
2485
Robert Gindafdbb3f22012-09-06 20:23:06 -07002486 while (node.nextSibling) {
2487 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002488 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002489 }
rgindaa09e7332012-08-17 12:49:51 -07002490 }
2491
2492 var rv = this.getRowsText(selection.startRow.rowIndex,
2493 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002494 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002495};
2496
rginda4bba5e12012-06-20 16:15:30 -07002497/**
2498 * Copy the current selection to the system clipboard, then clear it after a
2499 * short delay.
2500 */
2501hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002502 var text = this.getSelectionText();
2503 if (text != null)
2504 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002505};
2506
rgindaf0090c92012-02-10 14:58:52 -08002507hterm.Terminal.prototype.overlaySize = function() {
2508 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2509};
2510
rginda87b86462011-12-14 13:48:03 -08002511/**
2512 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2513 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002514 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002515 */
2516hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002517 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002518 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2519
Robert Ginda8cb7d902013-06-20 14:37:18 -07002520 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002521};
2522
2523/**
rgindad5613292012-06-19 15:40:37 -07002524 * Add the terminalRow and terminalColumn properties to mouse events and
2525 * then forward on to onMouse().
2526 *
2527 * The terminalRow and terminalColumn properties contain the (row, column)
2528 * coordinates for the mouse event.
2529 */
2530hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002531 if (e.processedByTerminalHandler_) {
2532 // We register our event handlers on the document, as well as the cursor
2533 // and the scroll blocker. Mouse events that occur on the cursor or
2534 // scroll blocker will also appear on the document, but we don't want to
2535 // process them twice.
2536 //
2537 // We can't just prevent bubbling because that has other side effects, so
2538 // we decorate the event object with this property instead.
2539 return;
2540 }
2541
2542 e.processedByTerminalHandler_ = true;
2543
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002544 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2545 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002546 return;
2547 }
2548
rgindad5613292012-06-19 15:40:37 -07002549 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2550 this.scrollPort_.characterSize.height) + 1;
2551 e.terminalColumn = parseInt(e.clientX /
2552 this.scrollPort_.characterSize.width) + 1;
2553
Robert Ginda928cf632014-03-05 15:07:41 -08002554 if (e.type == 'mousedown') {
2555 if (e.altKey || this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED) {
2556 // If VT mouse reporting is disabled, or has been defeated with
2557 // alt-mousedown, then the mouse will act on the local selection.
2558 this.reportMouseEvents_ = false;
2559 this.setSelectionEnabled(true);
2560 } else {
2561 // Otherwise we defer ownership of the mouse to the VT.
2562 this.reportMouseEvents_ = true;
2563 this.document_.getSelection().collapse();
2564 this.setSelectionEnabled(false);
2565 e.preventDefault();
2566 }
2567 }
2568
2569 if (!this.reportMouseEvents_) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002570 if (e.type == 'dblclick') {
2571 this.screen_.expandSelection(this.document_.getSelection());
2572 hterm.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07002573 }
2574
Robert Ginda928cf632014-03-05 15:07:41 -08002575 if (e.type == 'mousedown' && e.which == this.mousePasteButton)
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002576 this.paste();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002577
2578 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2579 !this.document_.getSelection().isCollapsed) {
2580 hterm.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002581 }
2582
2583 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
2584 this.scrollBlockerNode_.engaged) {
2585 // Disengage the scroll-blocker after one of these events.
2586 this.scrollBlockerNode_.engaged = false;
2587 this.scrollBlockerNode_.style.top = '-99px';
2588 }
2589
Robert Ginda928cf632014-03-05 15:07:41 -08002590 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002591 if (!this.scrollBlockerNode_.engaged) {
2592 if (e.type == 'mousedown') {
2593 // Move the scroll-blocker into place if we want to keep the scrollport
2594 // from scrolling.
2595 this.scrollBlockerNode_.engaged = true;
2596 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2597 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2598 } else if (e.type == 'mousemove') {
2599 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
2600 // in which case it's too late to engage the scroll-blocker.
2601 this.document_.getSelection().collapse();
2602 e.preventDefault();
2603 }
2604 }
Robert Ginda928cf632014-03-05 15:07:41 -08002605
2606 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002607 }
2608
Robert Ginda928cf632014-03-05 15:07:41 -08002609 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
2610 // Restore this on mouseup in case it was temporarily defeated with a
2611 // alt-mousedown. Only do this when the selection is empty so that
2612 // we don't immediately kill the users selection.
2613 this.reportMouseEvents_ = (this.vt.mouseReport !=
2614 this.vt.MOUSE_REPORT_DISABLED);
2615 }
rgindad5613292012-06-19 15:40:37 -07002616};
2617
2618/**
2619 * Clients should override this if they care to know about mouse events.
2620 *
2621 * The event parameter will be a normal DOM mouse click event with additional
2622 * 'terminalRow' and 'terminalColumn' properties.
2623 */
2624hterm.Terminal.prototype.onMouse = function(e) { };
2625
2626/**
rginda8e92a692012-05-20 19:37:20 -07002627 * React when focus changes.
2628 */
2629hterm.Terminal.prototype.onFocusChange_ = function(state) {
2630 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
Robert Ginda830583c2013-08-07 13:20:46 -07002631 this.restyleCursor_();
rginda8e92a692012-05-20 19:37:20 -07002632};
2633
2634/**
rginda8ba33642011-12-14 12:31:31 -08002635 * React when the ScrollPort is scrolled.
2636 */
2637hterm.Terminal.prototype.onScroll_ = function() {
2638 this.scheduleSyncCursorPosition_();
2639};
2640
2641/**
rginda9846e2f2012-01-27 13:53:33 -08002642 * React when text is pasted into the scrollPort.
2643 */
2644hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Ginda8cb7d902013-06-20 14:37:18 -07002645 this.onVTKeystroke(e.text.replace(/\n/mg, '\r'));
rginda9846e2f2012-01-27 13:53:33 -08002646};
2647
2648/**
rgindaa09e7332012-08-17 12:49:51 -07002649 * React when the user tries to copy from the scrollPort.
2650 */
2651hterm.Terminal.prototype.onCopy_ = function(e) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002652 e.preventDefault();
2653 setTimeout(this.copySelectionToClipboard.bind(this), 0);
rgindaa09e7332012-08-17 12:49:51 -07002654};
2655
2656/**
rginda8ba33642011-12-14 12:31:31 -08002657 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002658 *
2659 * Note: This function should not directly contain code that alters the internal
2660 * state of the terminal. That kind of code belongs in realizeWidth or
2661 * realizeHeight, so that it can be executed synchronously in the case of a
2662 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002663 */
2664hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08002665 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002666 this.scrollPort_.characterSize.width);
Robert Ginda19f61292014-03-04 14:07:57 -08002667 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
rginda35c456b2012-02-09 17:29:05 -08002668 this.scrollPort_.characterSize.height);
2669
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002670 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002671 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002672 // gets removed from the document or during the initial load, and we can't
2673 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002674 return;
2675 }
2676
rgindaa8ba17d2012-08-15 14:41:10 -07002677 var isNewSize = (columnCount != this.screenSize.width ||
2678 rowCount != this.screenSize.height);
2679
2680 // We do this even if the size didn't change, just to be sure everything is
2681 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002682 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002683 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002684
2685 if (isNewSize)
2686 this.overlaySize();
2687
Robert Gindafb1be6a2013-12-11 11:56:22 -08002688 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07002689 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002690};
2691
2692/**
2693 * Service the cursor blink timeout.
2694 */
2695hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Ginda830583c2013-08-07 13:20:46 -07002696 if (this.cursorNode_.getAttribute('focus') == 'false' ||
2697 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08002698 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002699 } else {
rginda87b86462011-12-14 13:48:03 -08002700 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002701 }
2702};
David Reveman8f552492012-03-28 12:18:41 -04002703
2704/**
2705 * Set the scrollbar-visible mode bit.
2706 *
2707 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2708 * Otherwise it will not.
2709 *
2710 * Defaults to on.
2711 *
2712 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2713 */
2714hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2715 this.scrollPort_.setScrollbarVisible(state);
2716};