blob: 9be742ad1890fd0be914ee487a63119023c000c5 [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 Ginda40932892012-12-10 17:26:40 -0800320 'pass-alt-number': function(v) {
321 if (v == null) {
322 var osx = window.navigator.userAgent.match(/Mac OS X/);
323
324 // Let Alt-1..9 pass to the browser (to control tab switching) on
325 // non-OS X systems, or if hterm is not opened in an app window.
326 v = (!osx && hterm.windowType != 'popup');
327 }
328
329 terminal.passAltNumber = v;
330 },
331
332 'pass-ctrl-number': function(v) {
333 if (v == null) {
334 var osx = window.navigator.userAgent.match(/Mac OS X/);
335
336 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
337 // non-OS X systems, or if hterm is not opened in an app window.
338 v = (!osx && hterm.windowType != 'popup');
339 }
340
341 terminal.passCtrlNumber = v;
342 },
343
344 'pass-meta-number': function(v) {
345 if (v == null) {
346 var osx = window.navigator.userAgent.match(/Mac OS X/);
347
348 // Let Meta-1..9 pass to the browser (to control tab switching) on
349 // OS X systems, or if hterm is not opened in an app window.
350 v = (osx && hterm.windowType != 'popup');
351 }
352
353 terminal.passMetaNumber = v;
354 },
355
Robert Ginda8cb7d902013-06-20 14:37:18 -0700356 'receive-encoding': function(v) {
357 if (!(/^(utf-8|raw)$/).test(v)) {
358 console.warn('Invalid value for "receive-encoding": ' + v);
359 v = 'utf-8';
360 }
361
362 terminal.vt.characterEncoding = v;
363 },
364
Robert Ginda57f03b42012-09-13 11:02:48 -0700365 'scroll-on-keystroke': function(v) {
366 terminal.scrollOnKeystroke_ = v;
367 },
rginda9f5222b2012-03-05 11:53:28 -0800368
Robert Ginda57f03b42012-09-13 11:02:48 -0700369 'scroll-on-output': function(v) {
370 terminal.scrollOnOutput_ = v;
371 },
rginda30f20f62012-04-05 16:36:19 -0700372
Robert Ginda57f03b42012-09-13 11:02:48 -0700373 'scrollbar-visible': function(v) {
374 terminal.setScrollbarVisible(v);
375 },
rginda9f5222b2012-03-05 11:53:28 -0800376
Robert Ginda8cb7d902013-06-20 14:37:18 -0700377 'send-encoding': function(v) {
378 if (!(/^(utf-8|raw)$/).test(v)) {
379 console.warn('Invalid value for "send-encoding": ' + v);
380 v = 'utf-8';
381 }
382
383 terminal.keyboard.characterEncoding = v;
384 },
385
Robert Ginda57f03b42012-09-13 11:02:48 -0700386 'shift-insert-paste': function(v) {
387 terminal.keyboard.shiftInsertPaste = v;
388 },
rginda9f5222b2012-03-05 11:53:28 -0800389
Ricky Liang48f05cb2013-12-31 23:35:29 +0800390 'page-keys-scroll': function(v) {
391 terminal.keyboard.pageKeysScroll = v;
Robert Ginda57f03b42012-09-13 11:02:48 -0700392 }
393 });
rginda30f20f62012-04-05 16:36:19 -0700394
Robert Ginda57f03b42012-09-13 11:02:48 -0700395 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800396 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700397
398 if (opt_callback)
399 opt_callback();
400 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800401};
402
rginda8e92a692012-05-20 19:37:20 -0700403/**
404 * Set the color for the cursor.
405 *
406 * If you want this setting to persist, set it through prefs_, rather than
407 * with this method.
408 */
409hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700410 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700411 this.cursorNode_.style.backgroundColor = color;
412 this.cursorNode_.style.borderColor = color;
413};
414
415/**
416 * Return the current cursor color as a string.
417 */
418hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700419 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700420};
421
422/**
rgindad5613292012-06-19 15:40:37 -0700423 * Enable or disable mouse based text selection in the terminal.
424 */
425hterm.Terminal.prototype.setSelectionEnabled = function(state) {
426 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700427};
428
429/**
rginda8e92a692012-05-20 19:37:20 -0700430 * Set the background color.
431 *
432 * If you want this setting to persist, set it through prefs_, rather than
433 * with this method.
434 */
435hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700436 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700437 this.primaryScreen_.textAttributes.setDefaults(
438 this.foregroundColor_, this.backgroundColor_);
439 this.alternateScreen_.textAttributes.setDefaults(
440 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700441 this.scrollPort_.setBackgroundColor(color);
442};
443
rginda9f5222b2012-03-05 11:53:28 -0800444/**
445 * Return the current terminal background color.
446 *
447 * Intended for use by other classes, so we don't have to expose the entire
448 * prefs_ object.
449 */
450hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700451 return this.backgroundColor_;
452};
453
454/**
455 * Set the foreground color.
456 *
457 * If you want this setting to persist, set it through prefs_, rather than
458 * with this method.
459 */
460hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700461 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700462 this.primaryScreen_.textAttributes.setDefaults(
463 this.foregroundColor_, this.backgroundColor_);
464 this.alternateScreen_.textAttributes.setDefaults(
465 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700466 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800467};
468
469/**
470 * Return the current terminal foreground color.
471 *
472 * Intended for use by other classes, so we don't have to expose the entire
473 * prefs_ object.
474 */
475hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700476 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800477};
478
479/**
rginda87b86462011-12-14 13:48:03 -0800480 * Create a new instance of a terminal command and run it with a given
481 * argument string.
482 *
483 * @param {function} commandClass The constructor for a terminal command.
484 * @param {string} argString The argument string to pass to the command.
485 */
486hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700487 var environment = this.prefs_.get('environment');
488 if (typeof environment != 'object' || environment == null)
489 environment = {};
490
rginda87b86462011-12-14 13:48:03 -0800491 var self = this;
492 this.command = new commandClass(
493 { argString: argString || '',
494 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700495 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800496 onExit: function(code) {
497 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800498 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700499 if (self.prefs_.get('close-on-exit'))
500 window.close();
rginda87b86462011-12-14 13:48:03 -0800501 }
502 });
503
rgindafeaf3142012-01-31 15:14:20 -0800504 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800505 this.command.run();
506};
507
508/**
rgindafeaf3142012-01-31 15:14:20 -0800509 * Returns true if the current screen is the primary screen, false otherwise.
510 */
511hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700512 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800513};
514
515/**
516 * Install the keyboard handler for this terminal.
517 *
518 * This will prevent the browser from seeing any keystrokes sent to the
519 * terminal.
520 */
521hterm.Terminal.prototype.installKeyboard = function() {
Toni Barzic0bfa8922013-11-22 11:18:35 -0800522 this.keyboard.installKeyboard(this.scrollPort_.getScreenNode());
rgindafeaf3142012-01-31 15:14:20 -0800523}
524
525/**
526 * Uninstall the keyboard handler for this terminal.
527 */
528hterm.Terminal.prototype.uninstallKeyboard = function() {
529 this.keyboard.installKeyboard(null);
530}
531
532/**
rginda35c456b2012-02-09 17:29:05 -0800533 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800534 *
535 * Call setFontSize(0) to reset to the default font size.
536 *
537 * This function does not modify the font-size preference.
538 *
539 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800540 */
541hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800542 if (px === 0)
543 px = this.prefs_.get('font-size');
544
rginda35c456b2012-02-09 17:29:05 -0800545 this.scrollPort_.setFontSize(px);
Ricky Liang48f05cb2013-12-31 23:35:29 +0800546 if (this.wcCssRule_) {
547 this.wcCssRule_.style.width = this.scrollPort_.characterSize.width * 2 +
548 'px';
549 }
rginda35c456b2012-02-09 17:29:05 -0800550};
551
552/**
553 * Get the current font size.
554 */
555hterm.Terminal.prototype.getFontSize = function() {
556 return this.scrollPort_.getFontSize();
557};
558
559/**
rginda8e92a692012-05-20 19:37:20 -0700560 * Get the current font family.
561 */
562hterm.Terminal.prototype.getFontFamily = function() {
563 return this.scrollPort_.getFontFamily();
564};
565
566/**
rginda35c456b2012-02-09 17:29:05 -0800567 * Set the CSS "font-family" for this terminal.
568 */
rginda9f5222b2012-03-05 11:53:28 -0800569hterm.Terminal.prototype.syncFontFamily = function() {
570 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
571 this.prefs_.get('font-smoothing'));
572 this.syncBoldSafeState();
573};
574
rginda4bba5e12012-06-20 16:15:30 -0700575/**
576 * Set this.mousePasteButton based on the mouse-paste-button pref,
577 * autodetecting if necessary.
578 */
579hterm.Terminal.prototype.syncMousePasteButton = function() {
580 var button = this.prefs_.get('mouse-paste-button');
581 if (typeof button == 'number') {
582 this.mousePasteButton = button;
583 return;
584 }
585
586 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
587 if (!ary || ary[2] == 'CrOS') {
588 this.mousePasteButton = 2;
589 } else {
590 this.mousePasteButton = 3;
591 }
592};
593
594/**
595 * Enable or disable bold based on the enable-bold pref, autodetecting if
596 * necessary.
597 */
rginda9f5222b2012-03-05 11:53:28 -0800598hterm.Terminal.prototype.syncBoldSafeState = function() {
599 var enableBold = this.prefs_.get('enable-bold');
600 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700601 this.primaryScreen_.textAttributes.enableBold = enableBold;
602 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800603 return;
604 }
605
rgindaf7521392012-02-28 17:20:34 -0800606 var normalSize = this.scrollPort_.measureCharacterSize();
607 var boldSize = this.scrollPort_.measureCharacterSize('bold');
608
609 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800610 if (!isBoldSafe) {
611 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700612 'from normal. Font family is: ' +
613 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800614 }
rginda9f5222b2012-03-05 11:53:28 -0800615
Robert Gindaed016262012-10-26 16:27:09 -0700616 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
617 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800618};
619
620/**
rginda87b86462011-12-14 13:48:03 -0800621 * Return a copy of the current cursor position.
622 *
623 * @return {hterm.RowCol} The RowCol object representing the current position.
624 */
625hterm.Terminal.prototype.saveCursor = function() {
626 return this.screen_.cursorPosition.clone();
627};
628
rgindaa19afe22012-01-25 15:40:22 -0800629hterm.Terminal.prototype.getTextAttributes = function() {
630 return this.screen_.textAttributes;
631};
632
rginda1a09aa02012-06-18 21:11:25 -0700633hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
634 this.screen_.textAttributes = textAttributes;
635};
636
rginda87b86462011-12-14 13:48:03 -0800637/**
rgindaf522ce02012-04-17 17:49:17 -0700638 * Return the current browser zoom factor applied to the terminal.
639 *
640 * @return {number} The current browser zoom factor.
641 */
642hterm.Terminal.prototype.getZoomFactor = function() {
643 return this.scrollPort_.characterSize.zoomFactor;
644};
645
646/**
rginda9846e2f2012-01-27 13:53:33 -0800647 * Change the title of this terminal's window.
648 */
649hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800650 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800651};
652
653/**
rginda87b86462011-12-14 13:48:03 -0800654 * Restore a previously saved cursor position.
655 *
656 * @param {hterm.RowCol} cursor The position to restore.
657 */
658hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700659 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
660 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800661 this.screen_.setCursorPosition(row, column);
662 if (cursor.column > column ||
663 cursor.column == column && cursor.overflow) {
664 this.screen_.cursorPosition.overflow = true;
665 }
rginda87b86462011-12-14 13:48:03 -0800666};
667
668/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400669 * Clear the cursor's overflow flag.
670 */
671hterm.Terminal.prototype.clearCursorOverflow = function() {
672 this.screen_.cursorPosition.overflow = false;
673};
674
675/**
Robert Ginda830583c2013-08-07 13:20:46 -0700676 * Sets the cursor shape
677 */
678hterm.Terminal.prototype.setCursorShape = function(shape) {
679 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800680 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700681}
682
683/**
684 * Get the cursor shape
685 */
686hterm.Terminal.prototype.getCursorShape = function() {
687 return this.cursorShape_;
688}
689
690/**
rginda87b86462011-12-14 13:48:03 -0800691 * Set the width of the terminal, resizing the UI to match.
692 */
693hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800694 if (columnCount == null) {
695 this.div_.style.width = '100%';
696 return;
697 }
698
rginda35c456b2012-02-09 17:29:05 -0800699 this.div_.style.width = this.scrollPort_.characterSize.width *
Robert Ginda97769282013-02-01 15:30:30 -0800700 columnCount + this.scrollPort_.currentScrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400701 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800702 this.scheduleSyncCursorPosition_();
703};
rginda87b86462011-12-14 13:48:03 -0800704
rgindac9bc5502012-01-18 11:48:44 -0800705/**
rginda35c456b2012-02-09 17:29:05 -0800706 * Set the height of the terminal, resizing the UI to match.
707 */
708hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800709 if (rowCount == null) {
710 this.div_.style.height = '100%';
711 return;
712 }
713
rginda35c456b2012-02-09 17:29:05 -0800714 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700715 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800716 this.realizeSize_(this.screenSize.width, rowCount);
717 this.scheduleSyncCursorPosition_();
718};
719
720/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400721 * Deal with terminal size changes.
722 *
723 */
724hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
725 if (columnCount != this.screenSize.width)
726 this.realizeWidth_(columnCount);
727
728 if (rowCount != this.screenSize.height)
729 this.realizeHeight_(rowCount);
730
731 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700732 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400733};
734
735/**
rgindac9bc5502012-01-18 11:48:44 -0800736 * Deal with terminal width changes.
737 *
738 * This function does what needs to be done when the terminal width changes
739 * out from under us. It happens here rather than in onResize_() because this
740 * code may need to run synchronously to handle programmatic changes of
741 * terminal width.
742 *
743 * Relying on the browser to send us an async resize event means we may not be
744 * in the correct state yet when the next escape sequence hits.
745 */
746hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700747 if (columnCount <= 0)
748 throw new Error('Attempt to realize bad width: ' + columnCount);
749
rgindac9bc5502012-01-18 11:48:44 -0800750 var deltaColumns = columnCount - this.screen_.getWidth();
751
rginda87b86462011-12-14 13:48:03 -0800752 this.screenSize.width = columnCount;
753 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800754
755 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400756 if (this.defaultTabStops)
757 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800758 } else {
759 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400760 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800761 break;
762
763 this.tabStops_.pop();
764 }
765 }
766
767 this.screen_.setColumnCount(this.screenSize.width);
768};
769
770/**
771 * Deal with terminal height changes.
772 *
773 * This function does what needs to be done when the terminal height changes
774 * out from under us. It happens here rather than in onResize_() because this
775 * code may need to run synchronously to handle programmatic changes of
776 * terminal height.
777 *
778 * Relying on the browser to send us an async resize event means we may not be
779 * in the correct state yet when the next escape sequence hits.
780 */
781hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700782 if (rowCount <= 0)
783 throw new Error('Attempt to realize bad height: ' + rowCount);
784
rgindac9bc5502012-01-18 11:48:44 -0800785 var deltaRows = rowCount - this.screen_.getHeight();
786
787 this.screenSize.height = rowCount;
788
789 var cursor = this.saveCursor();
790
791 if (deltaRows < 0) {
792 // Screen got smaller.
793 deltaRows *= -1;
794 while (deltaRows) {
795 var lastRow = this.getRowCount() - 1;
796 if (lastRow - this.scrollbackRows_.length == cursor.row)
797 break;
798
799 if (this.getRowText(lastRow))
800 break;
801
802 this.screen_.popRow();
803 deltaRows--;
804 }
805
806 var ary = this.screen_.shiftRows(deltaRows);
807 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
808
809 // We just removed rows from the top of the screen, we need to update
810 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800811 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800812 } else if (deltaRows > 0) {
813 // Screen got larger.
814
815 if (deltaRows <= this.scrollbackRows_.length) {
816 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
817 var rows = this.scrollbackRows_.splice(
818 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
819 this.screen_.unshiftRows(rows);
820 deltaRows -= scrollbackCount;
821 cursor.row += scrollbackCount;
822 }
823
824 if (deltaRows)
825 this.appendRows_(deltaRows);
826 }
827
rginda35c456b2012-02-09 17:29:05 -0800828 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800829 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800830};
831
832/**
833 * Scroll the terminal to the top of the scrollback buffer.
834 */
835hterm.Terminal.prototype.scrollHome = function() {
836 this.scrollPort_.scrollRowToTop(0);
837};
838
839/**
840 * Scroll the terminal to the end.
841 */
842hterm.Terminal.prototype.scrollEnd = function() {
843 this.scrollPort_.scrollRowToBottom(this.getRowCount());
844};
845
846/**
847 * Scroll the terminal one page up (minus one line) relative to the current
848 * position.
849 */
850hterm.Terminal.prototype.scrollPageUp = function() {
851 var i = this.scrollPort_.getTopRowIndex();
852 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
853};
854
855/**
856 * Scroll the terminal one page down (minus one line) relative to the current
857 * position.
858 */
859hterm.Terminal.prototype.scrollPageDown = function() {
860 var i = this.scrollPort_.getTopRowIndex();
861 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800862};
863
rgindac9bc5502012-01-18 11:48:44 -0800864/**
Robert Ginda40932892012-12-10 17:26:40 -0800865 * Clear primary screen, secondary screen, and the scrollback buffer.
866 */
867hterm.Terminal.prototype.wipeContents = function() {
868 this.scrollbackRows_.length = 0;
869 this.scrollPort_.resetCache();
870
871 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
872 var bottom = screen.getHeight();
873 if (bottom > 0) {
874 this.renumberRows_(0, bottom);
875 this.clearHome(screen);
876 }
877 }.bind(this));
878
879 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -0700880 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -0800881};
882
883/**
rgindac9bc5502012-01-18 11:48:44 -0800884 * Full terminal reset.
885 */
rginda87b86462011-12-14 13:48:03 -0800886hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800887 this.clearAllTabStops();
888 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700889
890 this.clearHome(this.primaryScreen_);
891 this.primaryScreen_.textAttributes.reset();
892
893 this.clearHome(this.alternateScreen_);
894 this.alternateScreen_.textAttributes.reset();
895
rgindab8bc8932012-04-27 12:45:03 -0700896 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
897
Robert Ginda92e18102013-03-14 13:56:37 -0700898 this.vt.reset();
899
rgindac9bc5502012-01-18 11:48:44 -0800900 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800901};
902
rgindac9bc5502012-01-18 11:48:44 -0800903/**
904 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700905 *
906 * Perform a soft reset to the default values listed in
907 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800908 */
rginda0f5c0292012-01-13 11:00:13 -0800909hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700910 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800911 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700912
rgindab8bc8932012-04-27 12:45:03 -0700913 // Xterm also resets the color palette on soft reset, even though it doesn't
914 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700915 this.primaryScreen_.textAttributes.resetColorPalette();
916 this.alternateScreen_.textAttributes.resetColorPalette();
917
rgindab8bc8932012-04-27 12:45:03 -0700918 // The xterm man page explicitly says this will happen on soft reset.
919 this.setVTScrollRegion(null, null);
920
921 // Xterm also shows the cursor on soft reset, but does not alter the blink
922 // state.
rgindaa19afe22012-01-25 15:40:22 -0800923 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800924};
925
rgindac9bc5502012-01-18 11:48:44 -0800926/**
927 * Move the cursor forward to the next tab stop, or to the last column
928 * if no more tab stops are set.
929 */
930hterm.Terminal.prototype.forwardTabStop = function() {
931 var column = this.screen_.cursorPosition.column;
932
933 for (var i = 0; i < this.tabStops_.length; i++) {
934 if (this.tabStops_[i] > column) {
935 this.setCursorColumn(this.tabStops_[i]);
936 return;
937 }
938 }
939
David Benjamin66e954d2012-05-05 21:08:12 -0400940 // xterm does not clear the overflow flag on HT or CHT.
941 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800942 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400943 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800944};
945
rgindac9bc5502012-01-18 11:48:44 -0800946/**
947 * Move the cursor backward to the previous tab stop, or to the first column
948 * if no previous tab stops are set.
949 */
950hterm.Terminal.prototype.backwardTabStop = function() {
951 var column = this.screen_.cursorPosition.column;
952
953 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
954 if (this.tabStops_[i] < column) {
955 this.setCursorColumn(this.tabStops_[i]);
956 return;
957 }
958 }
959
960 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800961};
962
rgindac9bc5502012-01-18 11:48:44 -0800963/**
964 * Set a tab stop at the given column.
965 *
966 * @param {int} column Zero based column.
967 */
968hterm.Terminal.prototype.setTabStop = function(column) {
969 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
970 if (this.tabStops_[i] == column)
971 return;
972
973 if (this.tabStops_[i] < column) {
974 this.tabStops_.splice(i + 1, 0, column);
975 return;
976 }
977 }
978
979 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800980};
981
rgindac9bc5502012-01-18 11:48:44 -0800982/**
983 * Clear the tab stop at the current cursor position.
984 *
985 * No effect if there is no tab stop at the current cursor position.
986 */
987hterm.Terminal.prototype.clearTabStopAtCursor = function() {
988 var column = this.screen_.cursorPosition.column;
989
990 var i = this.tabStops_.indexOf(column);
991 if (i == -1)
992 return;
993
994 this.tabStops_.splice(i, 1);
995};
996
997/**
998 * Clear all tab stops.
999 */
1000hterm.Terminal.prototype.clearAllTabStops = function() {
1001 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001002 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001003};
1004
1005/**
1006 * Set up the default tab stops, starting from a given column.
1007 *
1008 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001009 * from the specified column, or 0 if no column is provided. It also flags
1010 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001011 *
1012 * This does not clear the existing tab stops first, use clearAllTabStops
1013 * for that.
1014 *
1015 * @param {int} opt_start Optional starting zero based starting column, useful
1016 * for filling out missing tab stops when the terminal is resized.
1017 */
1018hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1019 var start = opt_start || 0;
1020 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001021 // Round start up to a default tab stop.
1022 start = start - 1 - ((start - 1) % w) + w;
1023 for (var i = start; i < this.screenSize.width; i += w) {
1024 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001025 }
David Benjamin66e954d2012-05-05 21:08:12 -04001026
1027 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001028};
1029
rginda6d397402012-01-17 10:58:29 -08001030/**
rginda8ba33642011-12-14 12:31:31 -08001031 * Interpret a sequence of characters.
1032 *
1033 * Incomplete escape sequences are buffered until the next call.
1034 *
1035 * @param {string} str Sequence of characters to interpret or pass through.
1036 */
1037hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001038 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001039 this.scheduleSyncCursorPosition_();
1040};
1041
1042/**
1043 * Take over the given DIV for use as the terminal display.
1044 *
1045 * @param {HTMLDivElement} div The div to use as the terminal display.
1046 */
1047hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001048 this.div_ = div;
1049
rginda8ba33642011-12-14 12:31:31 -08001050 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001051 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001052 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1053 this.scrollPort_.setBackgroundPosition(
1054 this.prefs_.get('background-position'));
rginda30f20f62012-04-05 16:36:19 -07001055
rginda0918b652012-04-04 11:26:24 -07001056 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001057
rginda9f5222b2012-03-05 11:53:28 -08001058 this.setFontSize(this.prefs_.get('font-size'));
1059 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001060
David Reveman8f552492012-03-28 12:18:41 -04001061 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1062
rginda8ba33642011-12-14 12:31:31 -08001063 this.document_ = this.scrollPort_.getDocument();
1064
rginda4bba5e12012-06-20 16:15:30 -07001065 this.document_.body.oncontextmenu = function() { return false };
1066
1067 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001068 var screenNode = this.scrollPort_.getScreenNode();
1069 screenNode.addEventListener('mousedown', onMouse);
1070 screenNode.addEventListener('mouseup', onMouse);
1071 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001072 this.scrollPort_.onScrollWheel = onMouse;
1073
Toni Barzic0bfa8922013-11-22 11:18:35 -08001074 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001075 'focus', this.onFocusChange_.bind(this, true));
Toni Barzic0bfa8922013-11-22 11:18:35 -08001076 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001077 'blur', this.onFocusChange_.bind(this, false));
1078
1079 var style = this.document_.createElement('style');
1080 style.textContent =
1081 ('.cursor-node[focus="false"] {' +
1082 ' box-sizing: border-box;' +
1083 ' background-color: transparent !important;' +
1084 ' border-width: 2px;' +
1085 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001086 '}' +
1087 '.wc-node {' +
1088 ' display: inline-block;' +
1089 ' text-align: center;' +
1090 ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001091 '}');
1092 this.document_.head.appendChild(style);
1093
Ricky Liang48f05cb2013-12-31 23:35:29 +08001094 var styleSheets = this.document_.styleSheets;
1095 var cssRules = styleSheets[styleSheets.length - 1].cssRules;
1096 this.wcCssRule_ = cssRules[cssRules.length - 1];
1097
rginda8ba33642011-12-14 12:31:31 -08001098 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001099 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001100 this.cursorNode_.style.cssText =
1101 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001102 'top: -99px;' +
1103 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001104 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1105 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001106 '-webkit-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001107
rginda8e92a692012-05-20 19:37:20 -07001108 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001109 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1110 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001111
rginda8ba33642011-12-14 12:31:31 -08001112 this.document_.body.appendChild(this.cursorNode_);
1113
rgindad5613292012-06-19 15:40:37 -07001114 // When 'enableMouseDragScroll' is off we reposition this element directly
1115 // under the mouse cursor after a click. This makes Chrome associate
1116 // subsequent mousemove events with the scroll-blocker. Since the
1117 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1118 // events do not cause the scrollport to scroll.
1119 //
1120 // It's a hack, but it's the cleanest way I could find.
1121 this.scrollBlockerNode_ = this.document_.createElement('div');
1122 this.scrollBlockerNode_.style.cssText =
1123 ('position: absolute;' +
1124 'top: -99px;' +
1125 'display: block;' +
1126 'width: 10px;' +
1127 'height: 10px;');
1128 this.document_.body.appendChild(this.scrollBlockerNode_);
1129
1130 var onMouse = this.onMouse_.bind(this);
1131 this.scrollPort_.onScrollWheel = onMouse;
1132 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1133 ].forEach(function(event) {
1134 this.scrollBlockerNode_.addEventListener(event, onMouse);
1135 this.cursorNode_.addEventListener(event, onMouse);
1136 this.document_.addEventListener(event, onMouse);
1137 }.bind(this));
1138
1139 this.cursorNode_.addEventListener('mousedown', function() {
1140 setTimeout(this.focus.bind(this));
1141 }.bind(this));
1142
rginda8ba33642011-12-14 12:31:31 -08001143 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001144
rginda87b86462011-12-14 13:48:03 -08001145 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001146 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001147};
1148
rginda0918b652012-04-04 11:26:24 -07001149/**
1150 * Return the HTML document that contains the terminal DOM nodes.
1151 */
rginda87b86462011-12-14 13:48:03 -08001152hterm.Terminal.prototype.getDocument = function() {
1153 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001154};
1155
1156/**
rginda0918b652012-04-04 11:26:24 -07001157 * Focus the terminal.
1158 */
1159hterm.Terminal.prototype.focus = function() {
1160 this.scrollPort_.focus();
1161};
1162
1163/**
rginda8ba33642011-12-14 12:31:31 -08001164 * Return the HTML Element for a given row index.
1165 *
1166 * This is a method from the RowProvider interface. The ScrollPort uses
1167 * it to fetch rows on demand as they are scrolled into view.
1168 *
1169 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1170 * pairs to conserve memory.
1171 *
1172 * @param {integer} index The zero-based row index, measured relative to the
1173 * start of the scrollback buffer. On-screen rows will always have the
1174 * largest indicies.
1175 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1176 */
1177hterm.Terminal.prototype.getRowNode = function(index) {
1178 if (index < this.scrollbackRows_.length)
1179 return this.scrollbackRows_[index];
1180
1181 var screenIndex = index - this.scrollbackRows_.length;
1182 return this.screen_.rowsArray[screenIndex];
1183};
1184
1185/**
1186 * Return the text content for a given range of rows.
1187 *
1188 * This is a method from the RowProvider interface. The ScrollPort uses
1189 * it to fetch text content on demand when the user attempts to copy their
1190 * selection to the clipboard.
1191 *
1192 * @param {integer} start The zero-based row index to start from, measured
1193 * relative to the start of the scrollback buffer. On-screen rows will
1194 * always have the largest indicies.
1195 * @param {integer} end The zero-based row index to end on, measured
1196 * relative to the start of the scrollback buffer.
1197 * @return {string} A single string containing the text value of the range of
1198 * rows. Lines will be newline delimited, with no trailing newline.
1199 */
1200hterm.Terminal.prototype.getRowsText = function(start, end) {
1201 var ary = [];
1202 for (var i = start; i < end; i++) {
1203 var node = this.getRowNode(i);
1204 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001205 if (i < end - 1 && !node.getAttribute('line-overflow'))
1206 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001207 }
1208
rgindaa09e7332012-08-17 12:49:51 -07001209 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001210};
1211
1212/**
1213 * Return the text content for a given row.
1214 *
1215 * This is a method from the RowProvider interface. The ScrollPort uses
1216 * it to fetch text content on demand when the user attempts to copy their
1217 * selection to the clipboard.
1218 *
1219 * @param {integer} index The zero-based row index to return, measured
1220 * relative to the start of the scrollback buffer. On-screen rows will
1221 * always have the largest indicies.
1222 * @return {string} A string containing the text value of the selected row.
1223 */
1224hterm.Terminal.prototype.getRowText = function(index) {
1225 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001226 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001227};
1228
1229/**
1230 * Return the total number of rows in the addressable screen and in the
1231 * scrollback buffer of this terminal.
1232 *
1233 * This is a method from the RowProvider interface. The ScrollPort uses
1234 * it to compute the size of the scrollbar.
1235 *
1236 * @return {integer} The number of rows in this terminal.
1237 */
1238hterm.Terminal.prototype.getRowCount = function() {
1239 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1240};
1241
1242/**
1243 * Create DOM nodes for new rows and append them to the end of the terminal.
1244 *
1245 * This is the only correct way to add a new DOM node for a row. Notice that
1246 * the new row is appended to the bottom of the list of rows, and does not
1247 * require renumbering (of the rowIndex property) of previous rows.
1248 *
1249 * If you think you want a new blank row somewhere in the middle of the
1250 * terminal, look into moveRows_().
1251 *
1252 * This method does not pay attention to vtScrollTop/Bottom, since you should
1253 * be using moveRows() in cases where they would matter.
1254 *
1255 * The cursor will be positioned at column 0 of the first inserted line.
1256 */
1257hterm.Terminal.prototype.appendRows_ = function(count) {
1258 var cursorRow = this.screen_.rowsArray.length;
1259 var offset = this.scrollbackRows_.length + cursorRow;
1260 for (var i = 0; i < count; i++) {
1261 var row = this.document_.createElement('x-row');
1262 row.appendChild(this.document_.createTextNode(''));
1263 row.rowIndex = offset + i;
1264 this.screen_.pushRow(row);
1265 }
1266
1267 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1268 if (extraRows > 0) {
1269 var ary = this.screen_.shiftRows(extraRows);
1270 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001271 if (this.scrollPort_.isScrolledEnd)
1272 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001273 }
1274
1275 if (cursorRow >= this.screen_.rowsArray.length)
1276 cursorRow = this.screen_.rowsArray.length - 1;
1277
rginda87b86462011-12-14 13:48:03 -08001278 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001279};
1280
1281/**
1282 * Relocate rows from one part of the addressable screen to another.
1283 *
1284 * This is used to recycle rows during VT scrolls (those which are driven
1285 * by VT commands, rather than by the user manipulating the scrollbar.)
1286 *
1287 * In this case, the blank lines scrolled into the scroll region are made of
1288 * the nodes we scrolled off. These have their rowIndex properties carefully
1289 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001290 */
1291hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1292 var ary = this.screen_.removeRows(fromIndex, count);
1293 this.screen_.insertRows(toIndex, ary);
1294
1295 var start, end;
1296 if (fromIndex < toIndex) {
1297 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001298 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001299 } else {
1300 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001301 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001302 }
1303
1304 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001305 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001306};
1307
1308/**
1309 * Renumber the rowIndex property of the given range of rows.
1310 *
1311 * The start and end indicies are relative to the screen, not the scrollback.
1312 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001313 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001314 * no need to renumber scrollback rows.
1315 */
Robert Ginda40932892012-12-10 17:26:40 -08001316hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1317 var screen = opt_screen || this.screen_;
1318
rginda8ba33642011-12-14 12:31:31 -08001319 var offset = this.scrollbackRows_.length;
1320 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001321 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001322 }
1323};
1324
1325/**
1326 * Print a string to the terminal.
1327 *
1328 * This respects the current insert and wraparound modes. It will add new lines
1329 * to the end of the terminal, scrolling off the top into the scrollback buffer
1330 * if necessary.
1331 *
1332 * The string is *not* parsed for escape codes. Use the interpret() method if
1333 * that's what you're after.
1334 *
1335 * @param{string} str The string to print.
1336 */
1337hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001338 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001339
Ricky Liang48f05cb2013-12-31 23:35:29 +08001340 var strWidth = lib.wc.strWidth(str);
1341
1342 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001343 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1344 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001345 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001346 }
rgindaa19afe22012-01-25 15:40:22 -08001347
Ricky Liang48f05cb2013-12-31 23:35:29 +08001348 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001349 var didOverflow = false;
1350 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001351
rgindaa9abdd82012-08-06 18:05:09 -07001352 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1353 didOverflow = true;
1354 count = this.screenSize.width - this.screen_.cursorPosition.column;
1355 }
rgindaa19afe22012-01-25 15:40:22 -08001356
rgindaa9abdd82012-08-06 18:05:09 -07001357 if (didOverflow && !this.options_.wraparound) {
1358 // If the string overflowed the line but wraparound is off, then the
1359 // last printed character should be the last of the string.
1360 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001361 substr = lib.wc.substr(str, startOffset, count - 1) +
1362 lib.wc.substr(str, strWidth - 1);
1363 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001364 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001365 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001366 }
rgindaa19afe22012-01-25 15:40:22 -08001367
Ricky Liang48f05cb2013-12-31 23:35:29 +08001368 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1369 for (var i = 0; i < tokens.length; i++) {
1370 if (tokens[i].wcNode)
1371 this.screen_.textAttributes.wcNode = true;
1372
1373 if (this.options_.insertMode) {
1374 this.screen_.insertString(tokens[i].str);
1375 } else {
1376 this.screen_.overwriteString(tokens[i].str);
1377 }
1378 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001379 }
1380
1381 this.screen_.maybeClipCurrentRow();
1382 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001383 }
rginda8ba33642011-12-14 12:31:31 -08001384
1385 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001386
rginda9f5222b2012-03-05 11:53:28 -08001387 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001388 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001389};
1390
1391/**
rginda87b86462011-12-14 13:48:03 -08001392 * Set the VT scroll region.
1393 *
rginda87b86462011-12-14 13:48:03 -08001394 * This also resets the cursor position to the absolute (0, 0) position, since
1395 * that's what xterm appears to do.
1396 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001397 * Setting the scroll region to the full height of the terminal will clear
1398 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1399 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1400 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1401 * continue to work as most users would expect.
1402 *
rginda87b86462011-12-14 13:48:03 -08001403 * @param {integer} scrollTop The zero-based top of the scroll region.
1404 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1405 * inclusive.
1406 */
1407hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001408 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001409 this.vtScrollTop_ = null;
1410 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001411 } else {
1412 this.vtScrollTop_ = scrollTop;
1413 this.vtScrollBottom_ = scrollBottom;
1414 }
rginda87b86462011-12-14 13:48:03 -08001415};
1416
1417/**
rginda8ba33642011-12-14 12:31:31 -08001418 * Return the top row index according to the VT.
1419 *
1420 * This will return 0 unless the terminal has been told to restrict scrolling
1421 * to some lower row. It is used for some VT cursor positioning and scrolling
1422 * commands.
1423 *
1424 * @return {integer} The topmost row in the terminal's scroll region.
1425 */
1426hterm.Terminal.prototype.getVTScrollTop = function() {
1427 if (this.vtScrollTop_ != null)
1428 return this.vtScrollTop_;
1429
1430 return 0;
rginda87b86462011-12-14 13:48:03 -08001431};
rginda8ba33642011-12-14 12:31:31 -08001432
1433/**
1434 * Return the bottom row index according to the VT.
1435 *
1436 * This will return the height of the terminal unless the it has been told to
1437 * restrict scrolling to some higher row. It is used for some VT cursor
1438 * positioning and scrolling commands.
1439 *
1440 * @return {integer} The bottommost row in the terminal's scroll region.
1441 */
1442hterm.Terminal.prototype.getVTScrollBottom = function() {
1443 if (this.vtScrollBottom_ != null)
1444 return this.vtScrollBottom_;
1445
rginda87b86462011-12-14 13:48:03 -08001446 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001447}
1448
1449/**
1450 * Process a '\n' character.
1451 *
1452 * If the cursor is on the final row of the terminal this will append a new
1453 * blank row to the screen and scroll the topmost row into the scrollback
1454 * buffer.
1455 *
1456 * Otherwise, this moves the cursor to column zero of the next row.
1457 */
1458hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001459 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1460 this.screen_.rowsArray.length - 1);
1461
1462 if (this.vtScrollBottom_ != null) {
1463 // A VT Scroll region is active, we never append new rows.
1464 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1465 // We're at the end of the VT Scroll Region, perform a VT scroll.
1466 this.vtScrollUp(1);
1467 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1468 } else if (cursorAtEndOfScreen) {
1469 // We're at the end of the screen, the only thing to do is put the
1470 // cursor to column 0.
1471 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1472 } else {
1473 // Anywhere else, advance the cursor row, and reset the column.
1474 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1475 }
1476 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001477 // We're at the end of the screen. Append a new row to the terminal,
1478 // shifting the top row into the scrollback.
1479 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001480 } else {
rginda87b86462011-12-14 13:48:03 -08001481 // Anywhere else in the screen just moves the cursor.
1482 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001483 }
1484};
1485
1486/**
1487 * Like newLine(), except maintain the cursor column.
1488 */
1489hterm.Terminal.prototype.lineFeed = function() {
1490 var column = this.screen_.cursorPosition.column;
1491 this.newLine();
1492 this.setCursorColumn(column);
1493};
1494
1495/**
rginda87b86462011-12-14 13:48:03 -08001496 * If autoCarriageReturn is set then newLine(), else lineFeed().
1497 */
1498hterm.Terminal.prototype.formFeed = function() {
1499 if (this.options_.autoCarriageReturn) {
1500 this.newLine();
1501 } else {
1502 this.lineFeed();
1503 }
1504};
1505
1506/**
1507 * Move the cursor up one row, possibly inserting a blank line.
1508 *
1509 * The cursor column is not changed.
1510 */
1511hterm.Terminal.prototype.reverseLineFeed = function() {
1512 var scrollTop = this.getVTScrollTop();
1513 var currentRow = this.screen_.cursorPosition.row;
1514
1515 if (currentRow == scrollTop) {
1516 this.insertLines(1);
1517 } else {
1518 this.setAbsoluteCursorRow(currentRow - 1);
1519 }
1520};
1521
1522/**
rginda8ba33642011-12-14 12:31:31 -08001523 * Replace all characters to the left of the current cursor with the space
1524 * character.
1525 *
1526 * TODO(rginda): This should probably *remove* the characters (not just replace
1527 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001528 * position.
rginda8ba33642011-12-14 12:31:31 -08001529 */
1530hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001531 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001532 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001533 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001534 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001535};
1536
1537/**
David Benjamin684a9b72012-05-01 17:19:58 -04001538 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001539 *
1540 * The cursor position is unchanged.
1541 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001542 * If the current background color is not the default background color this
1543 * will insert spaces rather than delete. This is unfortunate because the
1544 * trailing space will affect text selection, but it's difficult to come up
1545 * with a way to style empty space that wouldn't trip up the hterm.Screen
1546 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001547 *
1548 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1549 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1550 * crbug.com/232390 for details.
rginda8ba33642011-12-14 12:31:31 -08001551 */
1552hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001553 if (this.screen_.cursorPosition.overflow)
1554 return;
1555
Robert Ginda7fd57082012-09-25 14:41:47 -07001556 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1557 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001558
1559 if (this.screen_.textAttributes.background ===
1560 this.screen_.textAttributes.DEFAULT_COLOR) {
1561 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001562 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001563 this.screen_.cursorPosition.column + count) {
1564 this.screen_.deleteChars(count);
1565 this.clearCursorOverflow();
1566 return;
1567 }
1568 }
1569
rginda87b86462011-12-14 13:48:03 -08001570 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001571 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001572 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001573 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001574};
1575
1576/**
1577 * Erase the current line.
1578 *
1579 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001580 */
1581hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001582 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001583 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001584 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001585 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001586};
1587
1588/**
David Benjamina08d78f2012-05-05 00:28:49 -04001589 * Erase all characters from the start of the screen to the current cursor
1590 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001591 *
1592 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001593 */
1594hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001595 var cursor = this.saveCursor();
1596
1597 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001598
David Benjamina08d78f2012-05-05 00:28:49 -04001599 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001600 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001601 this.screen_.clearCursorRow();
1602 }
1603
rginda87b86462011-12-14 13:48:03 -08001604 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001605 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001606};
1607
1608/**
1609 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001610 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001611 *
1612 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001613 */
1614hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001615 var cursor = this.saveCursor();
1616
1617 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001618
David Benjamina08d78f2012-05-05 00:28:49 -04001619 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001620 for (var i = cursor.row + 1; i <= bottom; i++) {
1621 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001622 this.screen_.clearCursorRow();
1623 }
1624
rginda87b86462011-12-14 13:48:03 -08001625 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001626 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001627};
1628
1629/**
1630 * Fill the terminal with a given character.
1631 *
1632 * This methods does not respect the VT scroll region.
1633 *
1634 * @param {string} ch The character to use for the fill.
1635 */
1636hterm.Terminal.prototype.fill = function(ch) {
1637 var cursor = this.saveCursor();
1638
1639 this.setAbsoluteCursorPosition(0, 0);
1640 for (var row = 0; row < this.screenSize.height; row++) {
1641 for (var col = 0; col < this.screenSize.width; col++) {
1642 this.setAbsoluteCursorPosition(row, col);
1643 this.screen_.overwriteString(ch);
1644 }
1645 }
1646
1647 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001648};
1649
1650/**
rginda9ea433c2012-03-16 11:57:00 -07001651 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001652 *
rginda9ea433c2012-03-16 11:57:00 -07001653 * This does not respect the scroll region.
1654 *
1655 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1656 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001657 */
rginda9ea433c2012-03-16 11:57:00 -07001658hterm.Terminal.prototype.clearHome = function(opt_screen) {
1659 var screen = opt_screen || this.screen_;
1660 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001661
rginda11057d52012-04-25 12:29:56 -07001662 if (bottom == 0) {
1663 // Empty screen, nothing to do.
1664 return;
1665 }
1666
rgindae4d29232012-01-19 10:47:13 -08001667 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001668 screen.setCursorPosition(i, 0);
1669 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001670 }
1671
rginda9ea433c2012-03-16 11:57:00 -07001672 screen.setCursorPosition(0, 0);
1673};
1674
1675/**
1676 * Erase the entire display without changing the cursor position.
1677 *
1678 * The cursor position is unchanged. This does not respect the scroll
1679 * region.
1680 *
1681 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1682 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001683 */
1684hterm.Terminal.prototype.clear = function(opt_screen) {
1685 var screen = opt_screen || this.screen_;
1686 var cursor = screen.cursorPosition.clone();
1687 this.clearHome(screen);
1688 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001689};
1690
1691/**
1692 * VT command to insert lines at the current cursor row.
1693 *
1694 * This respects the current scroll region. Rows pushed off the bottom are
1695 * lost (they won't show up in the scrollback buffer).
1696 *
rginda8ba33642011-12-14 12:31:31 -08001697 * @param {integer} count The number of lines to insert.
1698 */
1699hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001700 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001701
1702 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001703 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001704
Robert Ginda579186b2012-09-26 11:40:04 -07001705 // The moveCount is the number of rows we need to relocate to make room for
1706 // the new row(s). The count is the distance to move them.
1707 var moveCount = bottom - cursorRow - count + 1;
1708 if (moveCount)
1709 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001710
Robert Ginda579186b2012-09-26 11:40:04 -07001711 for (var i = count - 1; i >= 0; i--) {
1712 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001713 this.screen_.clearCursorRow();
1714 }
rginda8ba33642011-12-14 12:31:31 -08001715};
1716
1717/**
1718 * VT command to delete lines at the current cursor row.
1719 *
1720 * New rows are added to the bottom of scroll region to take their place. New
1721 * rows are strictly there to take up space and have no content or style.
1722 */
1723hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001724 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001725
rginda87b86462011-12-14 13:48:03 -08001726 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001727 var bottom = this.getVTScrollBottom();
1728
rginda87b86462011-12-14 13:48:03 -08001729 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001730 count = Math.min(count, maxCount);
1731
rginda87b86462011-12-14 13:48:03 -08001732 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001733 if (count != maxCount)
1734 this.moveRows_(top, count, moveStart);
1735
1736 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001737 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001738 this.screen_.clearCursorRow();
1739 }
1740
rginda87b86462011-12-14 13:48:03 -08001741 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001742 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001743};
1744
1745/**
1746 * Inserts the given number of spaces at the current cursor position.
1747 *
rginda87b86462011-12-14 13:48:03 -08001748 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001749 */
1750hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001751 var cursor = this.saveCursor();
1752
rgindacbbd7482012-06-13 15:06:16 -07001753 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001754 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001755 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001756
1757 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001758 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001759};
1760
1761/**
1762 * Forward-delete the specified number of characters starting at the cursor
1763 * position.
1764 *
1765 * @param {integer} count The number of characters to delete.
1766 */
1767hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001768 var deleted = this.screen_.deleteChars(count);
1769 if (deleted && !this.screen_.textAttributes.isDefault()) {
1770 var cursor = this.saveCursor();
1771 this.setCursorColumn(this.screenSize.width - deleted);
1772 this.screen_.insertString(lib.f.getWhitespace(deleted));
1773 this.restoreCursor(cursor);
1774 }
1775
David Benjamin54e8bf62012-06-01 22:31:40 -04001776 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001777};
1778
1779/**
1780 * Shift rows in the scroll region upwards by a given number of lines.
1781 *
1782 * New rows are inserted at the bottom of the scroll region to fill the
1783 * vacated rows. The new rows not filled out with the current text attributes.
1784 *
1785 * This function does not affect the scrollback rows at all. Rows shifted
1786 * off the top are lost.
1787 *
rginda87b86462011-12-14 13:48:03 -08001788 * The cursor position is not altered.
1789 *
rginda8ba33642011-12-14 12:31:31 -08001790 * @param {integer} count The number of rows to scroll.
1791 */
1792hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001793 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001794
rginda87b86462011-12-14 13:48:03 -08001795 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001796 this.deleteLines(count);
1797
rginda87b86462011-12-14 13:48:03 -08001798 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001799};
1800
1801/**
1802 * Shift rows below the cursor down by a given number of lines.
1803 *
1804 * This function respects the current scroll region.
1805 *
1806 * New rows are inserted at the top of the scroll region to fill the
1807 * vacated rows. The new rows not filled out with the current text attributes.
1808 *
1809 * This function does not affect the scrollback rows at all. Rows shifted
1810 * off the bottom are lost.
1811 *
1812 * @param {integer} count The number of rows to scroll.
1813 */
1814hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001815 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001816
rginda87b86462011-12-14 13:48:03 -08001817 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001818 this.insertLines(opt_count);
1819
rginda87b86462011-12-14 13:48:03 -08001820 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001821};
1822
rginda87b86462011-12-14 13:48:03 -08001823
rginda8ba33642011-12-14 12:31:31 -08001824/**
1825 * Set the cursor position.
1826 *
1827 * The cursor row is relative to the scroll region if the terminal has
1828 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1829 *
1830 * @param {integer} row The new zero-based cursor row.
1831 * @param {integer} row The new zero-based cursor column.
1832 */
1833hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1834 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001835 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001836 } else {
rginda87b86462011-12-14 13:48:03 -08001837 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001838 }
rginda87b86462011-12-14 13:48:03 -08001839};
rginda8ba33642011-12-14 12:31:31 -08001840
rginda87b86462011-12-14 13:48:03 -08001841hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1842 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001843 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1844 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001845 this.screen_.setCursorPosition(row, column);
1846};
1847
1848hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001849 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1850 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001851 this.screen_.setCursorPosition(row, column);
1852};
1853
1854/**
1855 * Set the cursor column.
1856 *
1857 * @param {integer} column The new zero-based cursor column.
1858 */
1859hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001860 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001861};
1862
1863/**
1864 * Return the cursor column.
1865 *
1866 * @return {integer} The zero-based cursor column.
1867 */
1868hterm.Terminal.prototype.getCursorColumn = function() {
1869 return this.screen_.cursorPosition.column;
1870};
1871
1872/**
1873 * Set the cursor row.
1874 *
1875 * The cursor row is relative to the scroll region if the terminal has
1876 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1877 *
1878 * @param {integer} row The new cursor row.
1879 */
rginda87b86462011-12-14 13:48:03 -08001880hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1881 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001882};
1883
1884/**
1885 * Return the cursor row.
1886 *
1887 * @return {integer} The zero-based cursor row.
1888 */
1889hterm.Terminal.prototype.getCursorRow = function(row) {
1890 return this.screen_.cursorPosition.row;
1891};
1892
1893/**
1894 * Request that the ScrollPort redraw itself soon.
1895 *
1896 * The redraw will happen asynchronously, soon after the call stack winds down.
1897 * Multiple calls will be coalesced into a single redraw.
1898 */
1899hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001900 if (this.timeouts_.redraw)
1901 return;
rginda8ba33642011-12-14 12:31:31 -08001902
1903 var self = this;
rginda87b86462011-12-14 13:48:03 -08001904 this.timeouts_.redraw = setTimeout(function() {
1905 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001906 self.scrollPort_.redraw_();
1907 }, 0);
1908};
1909
1910/**
1911 * Request that the ScrollPort be scrolled to the bottom.
1912 *
1913 * The scroll will happen asynchronously, soon after the call stack winds down.
1914 * Multiple calls will be coalesced into a single scroll.
1915 *
1916 * This affects the scrollbar position of the ScrollPort, and has nothing to
1917 * do with the VT scroll commands.
1918 */
1919hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1920 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001921 return;
rginda8ba33642011-12-14 12:31:31 -08001922
1923 var self = this;
1924 this.timeouts_.scrollDown = setTimeout(function() {
1925 delete self.timeouts_.scrollDown;
1926 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1927 }, 10);
1928};
1929
1930/**
1931 * Move the cursor up a specified number of rows.
1932 *
1933 * @param {integer} count The number of rows to move the cursor.
1934 */
1935hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001936 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001937};
1938
1939/**
1940 * Move the cursor down a specified number of rows.
1941 *
1942 * @param {integer} count The number of rows to move the cursor.
1943 */
1944hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001945 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001946 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1947 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1948 this.screenSize.height - 1);
1949
rgindacbbd7482012-06-13 15:06:16 -07001950 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001951 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001952 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001953};
1954
1955/**
1956 * Move the cursor left a specified number of columns.
1957 *
1958 * @param {integer} count The number of columns to move the cursor.
1959 */
1960hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001961 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001962};
1963
1964/**
1965 * Move the cursor right a specified number of columns.
1966 *
1967 * @param {integer} count The number of columns to move the cursor.
1968 */
1969hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001970 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001971 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001972 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001973 this.setCursorColumn(column);
1974};
1975
1976/**
1977 * Reverse the foreground and background colors of the terminal.
1978 *
1979 * This only affects text that was drawn with no attributes.
1980 *
1981 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1982 * been drawn with attributes that happen to coincide with the default
1983 * 'no-attribute' colors. My guess is probably not.
1984 */
1985hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001986 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001987 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001988 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1989 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001990 } else {
rginda9f5222b2012-03-05 11:53:28 -08001991 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1992 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001993 }
1994};
1995
1996/**
rginda87b86462011-12-14 13:48:03 -08001997 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07001998 *
1999 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002000 */
2001hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002002 this.cursorNode_.style.backgroundColor =
2003 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002004
2005 var self = this;
2006 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002007 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002008 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002009
2010 if (this.bellAudio_.getAttribute('src')) {
Robert Gindaa6331372013-03-19 10:35:39 -07002011 if (this.bellSquelchTimeout_)
Robert Ginda92e18102013-03-14 13:56:37 -07002012 return;
2013
2014 this.bellAudio_.play();
2015
2016 this.bellSequelchTimeout_ = setTimeout(function() {
2017 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002018 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002019 } else {
2020 delete this.bellSquelchTimeout_;
2021 }
rginda87b86462011-12-14 13:48:03 -08002022};
2023
2024/**
rginda8ba33642011-12-14 12:31:31 -08002025 * Set the origin mode bit.
2026 *
2027 * If origin mode is on, certain VT cursor and scrolling commands measure their
2028 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2029 * to the top of the addressable screen.
2030 *
2031 * Defaults to off.
2032 *
2033 * @param {boolean} state True to set origin mode, false to unset.
2034 */
2035hterm.Terminal.prototype.setOriginMode = function(state) {
2036 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002037 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002038};
2039
2040/**
2041 * Set the insert mode bit.
2042 *
2043 * If insert mode is on, existing text beyond the cursor position will be
2044 * shifted right to make room for new text. Otherwise, new text overwrites
2045 * any existing text.
2046 *
2047 * Defaults to off.
2048 *
2049 * @param {boolean} state True to set insert mode, false to unset.
2050 */
2051hterm.Terminal.prototype.setInsertMode = function(state) {
2052 this.options_.insertMode = state;
2053};
2054
2055/**
rginda87b86462011-12-14 13:48:03 -08002056 * Set the auto carriage return bit.
2057 *
2058 * If auto carriage return is on then a formfeed character is interpreted
2059 * as a newline, otherwise it's the same as a linefeed. The difference boils
2060 * down to whether or not the cursor column is reset.
2061 */
2062hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2063 this.options_.autoCarriageReturn = state;
2064};
2065
2066/**
rginda8ba33642011-12-14 12:31:31 -08002067 * Set the wraparound mode bit.
2068 *
2069 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2070 * to the start of the following row. Otherwise, the cursor is clamped to the
2071 * end of the screen and attempts to write past it are ignored.
2072 *
2073 * Defaults to on.
2074 *
2075 * @param {boolean} state True to set wraparound mode, false to unset.
2076 */
2077hterm.Terminal.prototype.setWraparound = function(state) {
2078 this.options_.wraparound = state;
2079};
2080
2081/**
2082 * Set the reverse-wraparound mode bit.
2083 *
2084 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2085 * to the end of the previous row. Otherwise, the cursor is clamped to column
2086 * 0.
2087 *
2088 * Defaults to off.
2089 *
2090 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2091 */
2092hterm.Terminal.prototype.setReverseWraparound = function(state) {
2093 this.options_.reverseWraparound = state;
2094};
2095
2096/**
2097 * Selects between the primary and alternate screens.
2098 *
2099 * If alternate mode is on, the alternate screen is active. Otherwise the
2100 * primary screen is active.
2101 *
2102 * Swapping screens has no effect on the scrollback buffer.
2103 *
2104 * Each screen maintains its own cursor position.
2105 *
2106 * Defaults to off.
2107 *
2108 * @param {boolean} state True to set alternate mode, false to unset.
2109 */
2110hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002111 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002112 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2113
rginda35c456b2012-02-09 17:29:05 -08002114 if (this.screen_.rowsArray.length &&
2115 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2116 // If the screen changed sizes while we were away, our rowIndexes may
2117 // be incorrect.
2118 var offset = this.scrollbackRows_.length;
2119 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002120 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002121 ary[i].rowIndex = offset + i;
2122 }
2123 }
rginda8ba33642011-12-14 12:31:31 -08002124
rginda35c456b2012-02-09 17:29:05 -08002125 this.realizeWidth_(this.screenSize.width);
2126 this.realizeHeight_(this.screenSize.height);
2127 this.scrollPort_.syncScrollHeight();
2128 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002129
rginda6d397402012-01-17 10:58:29 -08002130 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002131 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002132};
2133
2134/**
2135 * Set the cursor-blink mode bit.
2136 *
2137 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2138 * a visible cursor does not blink.
2139 *
2140 * You should make sure to turn blinking off if you're going to dispose of a
2141 * terminal, otherwise you'll leak a timeout.
2142 *
2143 * Defaults to on.
2144 *
2145 * @param {boolean} state True to set cursor-blink mode, false to unset.
2146 */
2147hterm.Terminal.prototype.setCursorBlink = function(state) {
2148 this.options_.cursorBlink = state;
2149
2150 if (!state && this.timeouts_.cursorBlink) {
2151 clearTimeout(this.timeouts_.cursorBlink);
2152 delete this.timeouts_.cursorBlink;
2153 }
2154
2155 if (this.options_.cursorVisible)
2156 this.setCursorVisible(true);
2157};
2158
2159/**
2160 * Set the cursor-visible mode bit.
2161 *
2162 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2163 *
2164 * Defaults to on.
2165 *
2166 * @param {boolean} state True to set cursor-visible mode, false to unset.
2167 */
2168hterm.Terminal.prototype.setCursorVisible = function(state) {
2169 this.options_.cursorVisible = state;
2170
2171 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002172 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002173 return;
2174 }
2175
rginda87b86462011-12-14 13:48:03 -08002176 this.syncCursorPosition_();
2177
2178 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002179
2180 if (this.options_.cursorBlink) {
2181 if (this.timeouts_.cursorBlink)
2182 return;
2183
2184 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2185 500);
2186 } else {
2187 if (this.timeouts_.cursorBlink) {
2188 clearTimeout(this.timeouts_.cursorBlink);
2189 delete this.timeouts_.cursorBlink;
2190 }
2191 }
2192};
2193
2194/**
rginda87b86462011-12-14 13:48:03 -08002195 * Synchronizes the visible cursor and document selection with the current
2196 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002197 */
2198hterm.Terminal.prototype.syncCursorPosition_ = function() {
2199 var topRowIndex = this.scrollPort_.getTopRowIndex();
2200 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2201 var cursorRowIndex = this.scrollbackRows_.length +
2202 this.screen_.cursorPosition.row;
2203
2204 if (cursorRowIndex > bottomRowIndex) {
2205 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002206 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002207 return;
2208 }
2209
2210 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002211 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2212 'px';
2213 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2214 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002215
2216 this.cursorNode_.setAttribute('title',
2217 '(' + this.screen_.cursorPosition.row +
2218 ', ' + this.screen_.cursorPosition.column +
2219 ')');
2220
2221 // Update the caret for a11y purposes.
2222 var selection = this.document_.getSelection();
2223 if (selection && selection.isCollapsed)
2224 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002225};
2226
Robert Gindafb1be6a2013-12-11 11:56:22 -08002227/**
2228 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2229 * and character cell dimensions.
2230 */
Robert Ginda830583c2013-08-07 13:20:46 -07002231hterm.Terminal.prototype.restyleCursor_ = function() {
2232 var shape = this.cursorShape_;
2233
2234 if (this.cursorNode_.getAttribute('focus') == 'false') {
2235 // Always show a block cursor when unfocused.
2236 shape = hterm.Terminal.cursorShape.BLOCK;
2237 }
2238
2239 var style = this.cursorNode_.style;
2240
Robert Gindafb1be6a2013-12-11 11:56:22 -08002241 style.width = this.scrollPort_.characterSize.width + 'px';
2242
Robert Ginda830583c2013-08-07 13:20:46 -07002243 switch (shape) {
2244 case hterm.Terminal.cursorShape.BEAM:
2245 style.height = this.scrollPort_.characterSize.height + 'px';
2246 style.backgroundColor = 'transparent';
2247 style.borderBottomStyle = null;
2248 style.borderLeftStyle = 'solid';
2249 break;
2250
2251 case hterm.Terminal.cursorShape.UNDERLINE:
2252 style.height = this.scrollPort_.characterSize.baseline + 'px';
2253 style.backgroundColor = 'transparent';
2254 style.borderBottomStyle = 'solid';
2255 // correct the size to put it exactly at the baseline
2256 style.borderLeftStyle = null;
2257 break;
2258
2259 default:
2260 style.height = this.scrollPort_.characterSize.height + 'px';
2261 style.backgroundColor = this.cursorColor_;
2262 style.borderBottomStyle = null;
2263 style.borderLeftStyle = null;
2264 break;
2265 }
2266};
2267
rginda8ba33642011-12-14 12:31:31 -08002268/**
2269 * Synchronizes the visible cursor with the current cursor coordinates.
2270 *
2271 * The sync will happen asynchronously, soon after the call stack winds down.
2272 * Multiple calls will be coalesced into a single sync.
2273 */
2274hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2275 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002276 return;
rginda8ba33642011-12-14 12:31:31 -08002277
2278 var self = this;
2279 this.timeouts_.syncCursor = setTimeout(function() {
2280 self.syncCursorPosition_();
2281 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002282 }, 0);
2283};
2284
rgindacc2996c2012-02-24 14:59:31 -08002285/**
rgindaf522ce02012-04-17 17:49:17 -07002286 * Show or hide the zoom warning.
2287 *
2288 * The zoom warning is a message warning the user that their browser zoom must
2289 * be set to 100% in order for hterm to function properly.
2290 *
2291 * @param {boolean} state True to show the message, false to hide it.
2292 */
2293hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2294 if (!this.zoomWarningNode_) {
2295 if (!state)
2296 return;
2297
2298 this.zoomWarningNode_ = this.document_.createElement('div');
2299 this.zoomWarningNode_.style.cssText = (
2300 'color: black;' +
2301 'background-color: #ff2222;' +
2302 'font-size: large;' +
2303 'border-radius: 8px;' +
2304 'opacity: 0.75;' +
2305 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2306 'top: 0.5em;' +
2307 'right: 1.2em;' +
2308 'position: absolute;' +
2309 '-webkit-text-size-adjust: none;' +
2310 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002311 }
2312
Robert Gindab4839c22013-02-28 16:52:10 -08002313 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2314 hterm.zoomWarningMessage,
2315 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2316
rgindaf522ce02012-04-17 17:49:17 -07002317 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2318
2319 if (state) {
2320 if (!this.zoomWarningNode_.parentNode)
2321 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2322 } else if (this.zoomWarningNode_.parentNode) {
2323 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2324 }
2325};
2326
2327/**
rgindacc2996c2012-02-24 14:59:31 -08002328 * Show the terminal overlay for a given amount of time.
2329 *
2330 * The terminal overlay appears in inverse video in a large font, centered
2331 * over the terminal. You should probably keep the overlay message brief,
2332 * since it's in a large font and you probably aren't going to check the size
2333 * of the terminal first.
2334 *
2335 * @param {string} msg The text (not HTML) message to display in the overlay.
2336 * @param {number} opt_timeout The amount of time to wait before fading out
2337 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2338 * stay up forever (or until the next overlay).
2339 */
2340hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002341 if (!this.overlayNode_) {
2342 if (!this.div_)
2343 return;
2344
2345 this.overlayNode_ = this.document_.createElement('div');
2346 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002347 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002348 'font-size: xx-large;' +
2349 'opacity: 0.75;' +
2350 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2351 'position: absolute;' +
2352 '-webkit-user-select: none;' +
2353 '-webkit-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002354
2355 this.overlayNode_.addEventListener('mousedown', function(e) {
2356 e.preventDefault();
2357 e.stopPropagation();
2358 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002359 }
2360
rginda9f5222b2012-03-05 11:53:28 -08002361 this.overlayNode_.style.color = this.prefs_.get('background-color');
2362 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2363 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2364
rgindaf0090c92012-02-10 14:58:52 -08002365 this.overlayNode_.textContent = msg;
2366 this.overlayNode_.style.opacity = '0.75';
2367
2368 if (!this.overlayNode_.parentNode)
2369 this.div_.appendChild(this.overlayNode_);
2370
Robert Ginda97769282013-02-01 15:30:30 -08002371 var divSize = hterm.getClientSize(this.div_);
2372 var overlaySize = hterm.getClientSize(this.overlayNode_);
2373
2374 this.overlayNode_.style.top = (divSize.height - overlaySize.height) / 2;
2375 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
2376 this.scrollPort_.currentScrollbarWidthPx) / 2;
rgindaf0090c92012-02-10 14:58:52 -08002377
2378 var self = this;
2379
2380 if (this.overlayTimeout_)
2381 clearTimeout(this.overlayTimeout_);
2382
rgindacc2996c2012-02-24 14:59:31 -08002383 if (opt_timeout === null)
2384 return;
2385
rgindaf0090c92012-02-10 14:58:52 -08002386 this.overlayTimeout_ = setTimeout(function() {
2387 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002388 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002389 if (self.overlayNode_.parentNode)
2390 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002391 self.overlayTimeout_ = null;
2392 self.overlayNode_.style.opacity = '0.75';
2393 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002394 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002395};
2396
rginda4bba5e12012-06-20 16:15:30 -07002397/**
2398 * Paste from the system clipboard to the terminal.
2399 */
2400hterm.Terminal.prototype.paste = function() {
2401 hterm.pasteFromClipboard(this.document_);
2402};
2403
2404/**
2405 * Copy a string to the system clipboard.
2406 *
2407 * Note: If there is a selected range in the terminal, it'll be cleared.
2408 */
2409hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002410 if (this.prefs_.get('enable-clipboard-notice'))
2411 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2412
rgindaa09e7332012-08-17 12:49:51 -07002413 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002414 copySource.textContent = str;
2415 copySource.style.cssText = (
2416 '-webkit-user-select: text;' +
2417 'position: absolute;' +
2418 'top: -99px');
2419
2420 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002421
rginda4bba5e12012-06-20 16:15:30 -07002422 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002423 var anchorNode = selection.anchorNode;
2424 var anchorOffset = selection.anchorOffset;
2425 var focusNode = selection.focusNode;
2426 var focusOffset = selection.focusOffset;
2427
rginda4bba5e12012-06-20 16:15:30 -07002428 selection.selectAllChildren(copySource);
2429
rgindaa09e7332012-08-17 12:49:51 -07002430 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002431
rgindafaa74742012-08-21 13:34:03 -07002432 selection.collapse(anchorNode, anchorOffset);
2433 selection.extend(focusNode, focusOffset);
2434
rginda4bba5e12012-06-20 16:15:30 -07002435 copySource.parentNode.removeChild(copySource);
2436};
2437
rgindaa09e7332012-08-17 12:49:51 -07002438hterm.Terminal.prototype.getSelectionText = function() {
2439 var selection = this.scrollPort_.selection;
2440 selection.sync();
2441
2442 if (selection.isCollapsed)
2443 return null;
2444
2445
2446 // Start offset measures from the beginning of the line.
2447 var startOffset = selection.startOffset;
2448 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002449
Robert Gindafdbb3f22012-09-06 20:23:06 -07002450 if (node.nodeName != 'X-ROW') {
2451 // If the selection doesn't start on an x-row node, then it must be
2452 // somewhere inside the x-row. Add any characters from previous siblings
2453 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002454
2455 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2456 // If node is the text node in a styled span, move up to the span node.
2457 node = node.parentNode;
2458 }
2459
Robert Gindafdbb3f22012-09-06 20:23:06 -07002460 while (node.previousSibling) {
2461 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002462 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002463 }
rgindaa09e7332012-08-17 12:49:51 -07002464 }
2465
2466 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002467 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2468 selection.endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002469 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002470
Robert Gindafdbb3f22012-09-06 20:23:06 -07002471 if (node.nodeName != 'X-ROW') {
2472 // If the selection doesn't end on an x-row node, then it must be
2473 // somewhere inside the x-row. Add any characters from following siblings
2474 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002475
2476 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2477 // If node is the text node in a styled span, move up to the span node.
2478 node = node.parentNode;
2479 }
2480
Robert Gindafdbb3f22012-09-06 20:23:06 -07002481 while (node.nextSibling) {
2482 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002483 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002484 }
rgindaa09e7332012-08-17 12:49:51 -07002485 }
2486
2487 var rv = this.getRowsText(selection.startRow.rowIndex,
2488 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002489 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002490};
2491
rginda4bba5e12012-06-20 16:15:30 -07002492/**
2493 * Copy the current selection to the system clipboard, then clear it after a
2494 * short delay.
2495 */
2496hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002497 var text = this.getSelectionText();
2498 if (text != null)
2499 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002500};
2501
rgindaf0090c92012-02-10 14:58:52 -08002502hterm.Terminal.prototype.overlaySize = function() {
2503 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2504};
2505
rginda87b86462011-12-14 13:48:03 -08002506/**
2507 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2508 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002509 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002510 */
2511hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002512 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002513 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2514
Robert Ginda8cb7d902013-06-20 14:37:18 -07002515 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002516};
2517
2518/**
rgindad5613292012-06-19 15:40:37 -07002519 * Add the terminalRow and terminalColumn properties to mouse events and
2520 * then forward on to onMouse().
2521 *
2522 * The terminalRow and terminalColumn properties contain the (row, column)
2523 * coordinates for the mouse event.
2524 */
2525hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002526 if (e.processedByTerminalHandler_) {
2527 // We register our event handlers on the document, as well as the cursor
2528 // and the scroll blocker. Mouse events that occur on the cursor or
2529 // scroll blocker will also appear on the document, but we don't want to
2530 // process them twice.
2531 //
2532 // We can't just prevent bubbling because that has other side effects, so
2533 // we decorate the event object with this property instead.
2534 return;
2535 }
2536
2537 e.processedByTerminalHandler_ = true;
2538
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002539 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2540 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002541 return;
2542 }
2543
rgindad5613292012-06-19 15:40:37 -07002544 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2545 this.scrollPort_.characterSize.height) + 1;
2546 e.terminalColumn = parseInt(e.clientX /
2547 this.scrollPort_.characterSize.width) + 1;
2548
Robert Ginda928cf632014-03-05 15:07:41 -08002549 if (e.type == 'mousedown') {
2550 if (e.altKey || this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED) {
2551 // If VT mouse reporting is disabled, or has been defeated with
2552 // alt-mousedown, then the mouse will act on the local selection.
2553 this.reportMouseEvents_ = false;
2554 this.setSelectionEnabled(true);
2555 } else {
2556 // Otherwise we defer ownership of the mouse to the VT.
2557 this.reportMouseEvents_ = true;
2558 this.document_.getSelection().collapse();
2559 this.setSelectionEnabled(false);
2560 e.preventDefault();
2561 }
2562 }
2563
2564 if (!this.reportMouseEvents_) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002565 if (e.type == 'dblclick') {
2566 this.screen_.expandSelection(this.document_.getSelection());
2567 hterm.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07002568 }
2569
Robert Ginda928cf632014-03-05 15:07:41 -08002570 if (e.type == 'mousedown' && e.which == this.mousePasteButton)
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002571 this.paste();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002572
2573 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2574 !this.document_.getSelection().isCollapsed) {
2575 hterm.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002576 }
2577
2578 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
2579 this.scrollBlockerNode_.engaged) {
2580 // Disengage the scroll-blocker after one of these events.
2581 this.scrollBlockerNode_.engaged = false;
2582 this.scrollBlockerNode_.style.top = '-99px';
2583 }
2584
Robert Ginda928cf632014-03-05 15:07:41 -08002585 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002586 if (!this.scrollBlockerNode_.engaged) {
2587 if (e.type == 'mousedown') {
2588 // Move the scroll-blocker into place if we want to keep the scrollport
2589 // from scrolling.
2590 this.scrollBlockerNode_.engaged = true;
2591 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2592 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2593 } else if (e.type == 'mousemove') {
2594 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
2595 // in which case it's too late to engage the scroll-blocker.
2596 this.document_.getSelection().collapse();
2597 e.preventDefault();
2598 }
2599 }
Robert Ginda928cf632014-03-05 15:07:41 -08002600
2601 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002602 }
2603
Robert Ginda928cf632014-03-05 15:07:41 -08002604 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
2605 // Restore this on mouseup in case it was temporarily defeated with a
2606 // alt-mousedown. Only do this when the selection is empty so that
2607 // we don't immediately kill the users selection.
2608 this.reportMouseEvents_ = (this.vt.mouseReport !=
2609 this.vt.MOUSE_REPORT_DISABLED);
2610 }
rgindad5613292012-06-19 15:40:37 -07002611};
2612
2613/**
2614 * Clients should override this if they care to know about mouse events.
2615 *
2616 * The event parameter will be a normal DOM mouse click event with additional
2617 * 'terminalRow' and 'terminalColumn' properties.
2618 */
2619hterm.Terminal.prototype.onMouse = function(e) { };
2620
2621/**
rginda8e92a692012-05-20 19:37:20 -07002622 * React when focus changes.
2623 */
2624hterm.Terminal.prototype.onFocusChange_ = function(state) {
2625 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
Robert Ginda830583c2013-08-07 13:20:46 -07002626 this.restyleCursor_();
rginda8e92a692012-05-20 19:37:20 -07002627};
2628
2629/**
rginda8ba33642011-12-14 12:31:31 -08002630 * React when the ScrollPort is scrolled.
2631 */
2632hterm.Terminal.prototype.onScroll_ = function() {
2633 this.scheduleSyncCursorPosition_();
2634};
2635
2636/**
rginda9846e2f2012-01-27 13:53:33 -08002637 * React when text is pasted into the scrollPort.
2638 */
2639hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Ginda8cb7d902013-06-20 14:37:18 -07002640 this.onVTKeystroke(e.text.replace(/\n/mg, '\r'));
rginda9846e2f2012-01-27 13:53:33 -08002641};
2642
2643/**
rgindaa09e7332012-08-17 12:49:51 -07002644 * React when the user tries to copy from the scrollPort.
2645 */
2646hterm.Terminal.prototype.onCopy_ = function(e) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002647 e.preventDefault();
2648 setTimeout(this.copySelectionToClipboard.bind(this), 0);
rgindaa09e7332012-08-17 12:49:51 -07002649};
2650
2651/**
rginda8ba33642011-12-14 12:31:31 -08002652 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002653 *
2654 * Note: This function should not directly contain code that alters the internal
2655 * state of the terminal. That kind of code belongs in realizeWidth or
2656 * realizeHeight, so that it can be executed synchronously in the case of a
2657 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002658 */
2659hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08002660 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002661 this.scrollPort_.characterSize.width);
Robert Ginda19f61292014-03-04 14:07:57 -08002662 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
rginda35c456b2012-02-09 17:29:05 -08002663 this.scrollPort_.characterSize.height);
2664
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002665 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002666 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002667 // gets removed from the document or during the initial load, and we can't
2668 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002669 return;
2670 }
2671
rgindaa8ba17d2012-08-15 14:41:10 -07002672 var isNewSize = (columnCount != this.screenSize.width ||
2673 rowCount != this.screenSize.height);
2674
2675 // We do this even if the size didn't change, just to be sure everything is
2676 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002677 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002678 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002679
2680 if (isNewSize)
2681 this.overlaySize();
2682
Robert Gindafb1be6a2013-12-11 11:56:22 -08002683 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07002684 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002685};
2686
2687/**
2688 * Service the cursor blink timeout.
2689 */
2690hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Ginda830583c2013-08-07 13:20:46 -07002691 if (this.cursorNode_.getAttribute('focus') == 'false' ||
2692 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08002693 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002694 } else {
rginda87b86462011-12-14 13:48:03 -08002695 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002696 }
2697};
David Reveman8f552492012-03-28 12:18:41 -04002698
2699/**
2700 * Set the scrollbar-visible mode bit.
2701 *
2702 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2703 * Otherwise it will not.
2704 *
2705 * Defaults to on.
2706 *
2707 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2708 */
2709hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2710 this.scrollPort_.setScrollbarVisible(state);
2711};