blob: c32705d536e1f8c7278e6f65bfb73b46e8fd640f [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
Robert Gindab4839c22013-02-28 16:52:10 -08007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource',
Robert Ginda57f03b42012-09-13 11:02:48 -07008 'hterm.Keyboard', 'hterm.Options', 'hterm.PreferenceManager',
Ricky Liang48f05cb2013-12-31 23:35:29 +08009 'hterm.Screen', 'hterm.ScrollPort', 'hterm.Size',
10 'hterm.TextAttributes', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070053 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080054
rginda87b86462011-12-14 13:48:03 -080055 // The div that contains this terminal.
56 this.div_ = null;
57
rgindac9bc5502012-01-18 11:48:44 -080058 // The document that contains the scrollPort. Defaulted to the global
59 // document here so that the terminal is functional even if it hasn't been
60 // inserted into a document yet, but re-set in decorate().
61 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080062
rginda8ba33642011-12-14 12:31:31 -080063 // The rows that have scrolled off screen and are no longer addressable.
64 this.scrollbackRows_ = [];
65
rgindac9bc5502012-01-18 11:48:44 -080066 // Saved tab stops.
67 this.tabStops_ = [];
68
David Benjamin66e954d2012-05-05 21:08:12 -040069 // Keep track of whether default tab stops have been erased; after a TBC
70 // clears all tab stops, defaults aren't restored on resize until a reset.
71 this.defaultTabStops = true;
72
rginda8ba33642011-12-14 12:31:31 -080073 // The VT's notion of the top and bottom rows. Used during some VT
74 // cursor positioning and scrolling commands.
75 this.vtScrollTop_ = null;
76 this.vtScrollBottom_ = null;
77
78 // The DIV element for the visible cursor.
79 this.cursorNode_ = null;
80
Robert Ginda830583c2013-08-07 13:20:46 -070081 // The current cursor shape of the terminal.
82 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
83
84 // The current color of the cursor.
85 this.cursorColor_ = null;
86
rginda9f5222b2012-03-05 11:53:28 -080087 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070088 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070089 this.backgroundColor_ = null;
90 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070091 this.scrollOnOutput_ = null;
92 this.scrollOnKeystroke_ = null;
rginda9f5222b2012-03-05 11:53:28 -080093
Robert Ginda928cf632014-03-05 15:07:41 -080094 // True if we should send mouse events to the vt, false if we want them
95 // to manage the local text selection.
96 this.reportMouseEvents_ = false;
97
rgindaf0090c92012-02-10 14:58:52 -080098 // Terminal bell sound.
99 this.bellAudio_ = this.document_.createElement('audio');
rgindaf0090c92012-02-10 14:58:52 -0800100 this.bellAudio_.setAttribute('preload', 'auto');
101
rginda6d397402012-01-17 10:58:29 -0800102 // Cursor position and attributes saved with DECSC.
103 this.savedOptions_ = {};
104
rginda8ba33642011-12-14 12:31:31 -0800105 // The current mode bits for the terminal.
106 this.options_ = new hterm.Options();
107
108 // Timeouts we might need to clear.
109 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800110
111 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800112 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800113
rgindafeaf3142012-01-31 15:14:20 -0800114 // The keyboard hander.
115 this.keyboard = new hterm.Keyboard(this);
116
rginda87b86462011-12-14 13:48:03 -0800117 // General IO interface that can be given to third parties without exposing
118 // the entire terminal object.
119 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800120
rgindad5613292012-06-19 15:40:37 -0700121 // True if mouse-click-drag should scroll the terminal.
122 this.enableMouseDragScroll = true;
123
Robert Ginda57f03b42012-09-13 11:02:48 -0700124 this.copyOnSelect = null;
rginda4bba5e12012-06-20 16:15:30 -0700125 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700126
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400127 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800128 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700129
130 this.setProfile(opt_profileId || 'default',
131 function() { this.onTerminalReady() }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800132};
133
134/**
Robert Ginda830583c2013-08-07 13:20:46 -0700135 * Possible cursor shapes.
136 */
137hterm.Terminal.cursorShape = {
138 BLOCK: 'BLOCK',
139 BEAM: 'BEAM',
140 UNDERLINE: 'UNDERLINE'
141};
142
143/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700144 * Clients should override this to be notified when the terminal is ready
145 * for use.
146 *
147 * The terminal initialization is asynchronous, and shouldn't be used before
148 * this method is called.
149 */
150hterm.Terminal.prototype.onTerminalReady = function() { };
151
152/**
rginda35c456b2012-02-09 17:29:05 -0800153 * Default tab with of 8 to match xterm.
154 */
155hterm.Terminal.prototype.tabWidth = 8;
156
157/**
rginda9f5222b2012-03-05 11:53:28 -0800158 * Select a preference profile.
159 *
160 * This will load the terminal preferences for the given profile name and
161 * associate subsequent preference changes with the new preference profile.
162 *
163 * @param {string} newName The name of the preference profile. Forward slash
164 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700165 * @param {function} opt_callback Optional callback to invoke when the profile
166 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800167 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700168hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
169 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800170
Robert Ginda57f03b42012-09-13 11:02:48 -0700171 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800172
Robert Ginda57f03b42012-09-13 11:02:48 -0700173 if (this.prefs_)
174 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800175
Robert Ginda57f03b42012-09-13 11:02:48 -0700176 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
177 this.prefs_.addObservers(null, {
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700178 'alt-backspace-is-meta-backspace': function(v) {
179 terminal.keyboard.altBackspaceIsMetaBackspace = v;
180 },
181
Robert Ginda57f03b42012-09-13 11:02:48 -0700182 'alt-is-meta': function(v) {
183 terminal.keyboard.altIsMeta = v;
184 },
185
186 'alt-sends-what': function(v) {
187 if (!/^(escape|8-bit|browser-key)$/.test(v))
188 v = 'escape';
189
190 terminal.keyboard.altSendsWhat = v;
191 },
192
193 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800194 var ary = v.match(/^lib-resource:(\S+)/);
195 if (ary) {
196 terminal.bellAudio_.setAttribute('src',
197 lib.resource.getDataUrl(ary[1]));
198 } else {
199 terminal.bellAudio_.setAttribute('src', v);
200 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700201 },
202
203 'background-color': function(v) {
204 terminal.setBackgroundColor(v);
205 },
206
207 'background-image': function(v) {
208 terminal.scrollPort_.setBackgroundImage(v);
209 },
210
211 'background-size': function(v) {
212 terminal.scrollPort_.setBackgroundSize(v);
213 },
214
215 'background-position': function(v) {
216 terminal.scrollPort_.setBackgroundPosition(v);
217 },
218
219 'backspace-sends-backspace': function(v) {
220 terminal.keyboard.backspaceSendsBackspace = v;
221 },
222
223 'cursor-blink': function(v) {
224 terminal.setCursorBlink(!!v);
225 },
226
227 'cursor-color': function(v) {
228 terminal.setCursorColor(v);
229 },
230
231 'color-palette-overrides': function(v) {
232 if (!(v == null || v instanceof Object || v instanceof Array)) {
233 console.warn('Preference color-palette-overrides is not an array or ' +
234 'object: ' + v);
235 return;
rginda9f5222b2012-03-05 11:53:28 -0800236 }
rginda9f5222b2012-03-05 11:53:28 -0800237
Robert Ginda57f03b42012-09-13 11:02:48 -0700238 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700239
Robert Ginda57f03b42012-09-13 11:02:48 -0700240 if (v) {
241 for (var key in v) {
242 var i = parseInt(key);
243 if (isNaN(i) || i < 0 || i > 255) {
244 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
245 continue;
246 }
247
248 if (v[i]) {
249 var rgb = lib.colors.normalizeCSS(v[i]);
250 if (rgb)
251 lib.colors.colorPalette[i] = rgb;
252 }
253 }
rginda30f20f62012-04-05 16:36:19 -0700254 }
rginda30f20f62012-04-05 16:36:19 -0700255
Robert Ginda57f03b42012-09-13 11:02:48 -0700256 terminal.primaryScreen_.textAttributes.resetColorPalette()
257 terminal.alternateScreen_.textAttributes.resetColorPalette();
258 },
rginda30f20f62012-04-05 16:36:19 -0700259
Robert Ginda57f03b42012-09-13 11:02:48 -0700260 'copy-on-select': function(v) {
261 terminal.copyOnSelect = !!v;
262 },
rginda9f5222b2012-03-05 11:53:28 -0800263
Robert Ginda7e5e9522014-03-14 12:23:58 -0700264 'ctrl-plus-minus-zero-zoom': function(v) {
265 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
266 },
267
Robert Gindafb5a3f92014-05-13 14:12:00 -0700268 'ctrl-c-copy': function(v) {
269 terminal.keyboard.ctrlCCopy = v;
270 },
271
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100272 'ctrl-v-paste': function(v) {
273 terminal.keyboard.ctrlVPaste = v;
274 },
275
Robert Ginda57f03b42012-09-13 11:02:48 -0700276 'enable-8-bit-control': function(v) {
277 terminal.vt.enable8BitControl = !!v;
278 },
rginda30f20f62012-04-05 16:36:19 -0700279
Robert Ginda57f03b42012-09-13 11:02:48 -0700280 'enable-bold': function(v) {
281 terminal.syncBoldSafeState();
282 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400283
Robert Ginda3e278d72014-03-25 13:18:51 -0700284 'enable-bold-as-bright': function(v) {
285 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
286 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
287 },
288
Robert Ginda57f03b42012-09-13 11:02:48 -0700289 'enable-clipboard-write': function(v) {
290 terminal.vt.enableClipboardWrite = !!v;
291 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400292
Robert Ginda3755e752013-05-31 13:34:09 -0700293 'enable-dec12': function(v) {
294 terminal.vt.enableDec12 = !!v;
295 },
296
Robert Ginda57f03b42012-09-13 11:02:48 -0700297 'font-family': function(v) {
298 terminal.syncFontFamily();
299 },
rginda30f20f62012-04-05 16:36:19 -0700300
Robert Ginda57f03b42012-09-13 11:02:48 -0700301 'font-size': function(v) {
302 terminal.setFontSize(v);
303 },
rginda9875d902012-08-20 16:21:57 -0700304
Robert Ginda57f03b42012-09-13 11:02:48 -0700305 'font-smoothing': function(v) {
306 terminal.syncFontFamily();
307 },
rgindade84e382012-04-20 15:39:31 -0700308
Robert Ginda57f03b42012-09-13 11:02:48 -0700309 'foreground-color': function(v) {
310 terminal.setForegroundColor(v);
311 },
rginda30f20f62012-04-05 16:36:19 -0700312
Robert Ginda57f03b42012-09-13 11:02:48 -0700313 'home-keys-scroll': function(v) {
314 terminal.keyboard.homeKeysScroll = v;
315 },
rginda4bba5e12012-06-20 16:15:30 -0700316
Robert Ginda57f03b42012-09-13 11:02:48 -0700317 'max-string-sequence': function(v) {
318 terminal.vt.maxStringSequence = v;
319 },
rginda11057d52012-04-25 12:29:56 -0700320
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700321 'media-keys-are-fkeys': function(v) {
322 terminal.keyboard.mediaKeysAreFKeys = v;
323 },
324
Robert Ginda57f03b42012-09-13 11:02:48 -0700325 'meta-sends-escape': function(v) {
326 terminal.keyboard.metaSendsEscape = v;
327 },
rginda30f20f62012-04-05 16:36:19 -0700328
Robert Ginda57f03b42012-09-13 11:02:48 -0700329 'mouse-paste-button': function(v) {
330 terminal.syncMousePasteButton();
331 },
rgindaa8ba17d2012-08-15 14:41:10 -0700332
Robert Gindae76aa9f2014-03-14 12:29:12 -0700333 'page-keys-scroll': function(v) {
334 terminal.keyboard.pageKeysScroll = v;
335 },
336
Robert Ginda40932892012-12-10 17:26:40 -0800337 'pass-alt-number': function(v) {
338 if (v == null) {
339 var osx = window.navigator.userAgent.match(/Mac OS X/);
340
341 // Let Alt-1..9 pass to the browser (to control tab switching) on
342 // non-OS X systems, or if hterm is not opened in an app window.
343 v = (!osx && hterm.windowType != 'popup');
344 }
345
346 terminal.passAltNumber = v;
347 },
348
349 'pass-ctrl-number': function(v) {
350 if (v == null) {
351 var osx = window.navigator.userAgent.match(/Mac OS X/);
352
353 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
354 // non-OS X systems, or if hterm is not opened in an app window.
355 v = (!osx && hterm.windowType != 'popup');
356 }
357
358 terminal.passCtrlNumber = v;
359 },
360
361 'pass-meta-number': function(v) {
362 if (v == null) {
363 var osx = window.navigator.userAgent.match(/Mac OS X/);
364
365 // Let Meta-1..9 pass to the browser (to control tab switching) on
366 // OS X systems, or if hterm is not opened in an app window.
367 v = (osx && hterm.windowType != 'popup');
368 }
369
370 terminal.passMetaNumber = v;
371 },
372
Robert Ginda8cb7d902013-06-20 14:37:18 -0700373 'receive-encoding': function(v) {
374 if (!(/^(utf-8|raw)$/).test(v)) {
375 console.warn('Invalid value for "receive-encoding": ' + v);
376 v = 'utf-8';
377 }
378
379 terminal.vt.characterEncoding = v;
380 },
381
Robert Ginda57f03b42012-09-13 11:02:48 -0700382 'scroll-on-keystroke': function(v) {
383 terminal.scrollOnKeystroke_ = v;
384 },
rginda9f5222b2012-03-05 11:53:28 -0800385
Robert Ginda57f03b42012-09-13 11:02:48 -0700386 'scroll-on-output': function(v) {
387 terminal.scrollOnOutput_ = v;
388 },
rginda30f20f62012-04-05 16:36:19 -0700389
Robert Ginda57f03b42012-09-13 11:02:48 -0700390 'scrollbar-visible': function(v) {
391 terminal.setScrollbarVisible(v);
392 },
rginda9f5222b2012-03-05 11:53:28 -0800393
Robert Ginda8cb7d902013-06-20 14:37:18 -0700394 'send-encoding': function(v) {
395 if (!(/^(utf-8|raw)$/).test(v)) {
396 console.warn('Invalid value for "send-encoding": ' + v);
397 v = 'utf-8';
398 }
399
400 terminal.keyboard.characterEncoding = v;
401 },
402
Robert Ginda57f03b42012-09-13 11:02:48 -0700403 'shift-insert-paste': function(v) {
404 terminal.keyboard.shiftInsertPaste = v;
405 },
rginda9f5222b2012-03-05 11:53:28 -0800406
Robert Gindae76aa9f2014-03-14 12:29:12 -0700407 'user-css': function(v) {
408 terminal.scrollPort_.setUserCss(v);
Robert Ginda57f03b42012-09-13 11:02:48 -0700409 }
410 });
rginda30f20f62012-04-05 16:36:19 -0700411
Robert Ginda57f03b42012-09-13 11:02:48 -0700412 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800413 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700414
415 if (opt_callback)
416 opt_callback();
417 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800418};
419
Rob Spies56953412014-04-28 14:09:47 -0700420
421/**
422 * Returns the preferences manager used for configuring this terminal.
423 */
424hterm.Terminal.prototype.getPrefs = function() {
425 return this.prefs_;
426};
427
428
rginda8e92a692012-05-20 19:37:20 -0700429/**
430 * Set the color for the cursor.
431 *
432 * If you want this setting to persist, set it through prefs_, rather than
433 * with this method.
434 */
435hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700436 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700437 this.cursorNode_.style.backgroundColor = color;
438 this.cursorNode_.style.borderColor = color;
439};
440
441/**
442 * Return the current cursor color as a string.
443 */
444hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700445 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700446};
447
448/**
rgindad5613292012-06-19 15:40:37 -0700449 * Enable or disable mouse based text selection in the terminal.
450 */
451hterm.Terminal.prototype.setSelectionEnabled = function(state) {
452 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700453};
454
455/**
rginda8e92a692012-05-20 19:37:20 -0700456 * Set the background color.
457 *
458 * If you want this setting to persist, set it through prefs_, rather than
459 * with this method.
460 */
461hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700462 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700463 this.primaryScreen_.textAttributes.setDefaults(
464 this.foregroundColor_, this.backgroundColor_);
465 this.alternateScreen_.textAttributes.setDefaults(
466 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700467 this.scrollPort_.setBackgroundColor(color);
468};
469
rginda9f5222b2012-03-05 11:53:28 -0800470/**
471 * Return the current terminal background color.
472 *
473 * Intended for use by other classes, so we don't have to expose the entire
474 * prefs_ object.
475 */
476hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700477 return this.backgroundColor_;
478};
479
480/**
481 * Set the foreground color.
482 *
483 * If you want this setting to persist, set it through prefs_, rather than
484 * with this method.
485 */
486hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700487 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700488 this.primaryScreen_.textAttributes.setDefaults(
489 this.foregroundColor_, this.backgroundColor_);
490 this.alternateScreen_.textAttributes.setDefaults(
491 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700492 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800493};
494
495/**
496 * Return the current terminal foreground color.
497 *
498 * Intended for use by other classes, so we don't have to expose the entire
499 * prefs_ object.
500 */
501hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700502 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800503};
504
505/**
rginda87b86462011-12-14 13:48:03 -0800506 * Create a new instance of a terminal command and run it with a given
507 * argument string.
508 *
509 * @param {function} commandClass The constructor for a terminal command.
510 * @param {string} argString The argument string to pass to the command.
511 */
512hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700513 var environment = this.prefs_.get('environment');
514 if (typeof environment != 'object' || environment == null)
515 environment = {};
516
rginda87b86462011-12-14 13:48:03 -0800517 var self = this;
518 this.command = new commandClass(
519 { argString: argString || '',
520 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700521 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800522 onExit: function(code) {
523 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800524 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700525 if (self.prefs_.get('close-on-exit'))
526 window.close();
rginda87b86462011-12-14 13:48:03 -0800527 }
528 });
529
rgindafeaf3142012-01-31 15:14:20 -0800530 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800531 this.command.run();
532};
533
534/**
rgindafeaf3142012-01-31 15:14:20 -0800535 * Returns true if the current screen is the primary screen, false otherwise.
536 */
537hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700538 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800539};
540
541/**
542 * Install the keyboard handler for this terminal.
543 *
544 * This will prevent the browser from seeing any keystrokes sent to the
545 * terminal.
546 */
547hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700548 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800549}
550
551/**
552 * Uninstall the keyboard handler for this terminal.
553 */
554hterm.Terminal.prototype.uninstallKeyboard = function() {
555 this.keyboard.installKeyboard(null);
556}
557
558/**
rginda35c456b2012-02-09 17:29:05 -0800559 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800560 *
561 * Call setFontSize(0) to reset to the default font size.
562 *
563 * This function does not modify the font-size preference.
564 *
565 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800566 */
567hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800568 if (px === 0)
569 px = this.prefs_.get('font-size');
570
rginda35c456b2012-02-09 17:29:05 -0800571 this.scrollPort_.setFontSize(px);
Ricky Liang48f05cb2013-12-31 23:35:29 +0800572 if (this.wcCssRule_) {
573 this.wcCssRule_.style.width = this.scrollPort_.characterSize.width * 2 +
574 'px';
575 }
rginda35c456b2012-02-09 17:29:05 -0800576};
577
578/**
579 * Get the current font size.
580 */
581hterm.Terminal.prototype.getFontSize = function() {
582 return this.scrollPort_.getFontSize();
583};
584
585/**
rginda8e92a692012-05-20 19:37:20 -0700586 * Get the current font family.
587 */
588hterm.Terminal.prototype.getFontFamily = function() {
589 return this.scrollPort_.getFontFamily();
590};
591
592/**
rginda35c456b2012-02-09 17:29:05 -0800593 * Set the CSS "font-family" for this terminal.
594 */
rginda9f5222b2012-03-05 11:53:28 -0800595hterm.Terminal.prototype.syncFontFamily = function() {
596 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
597 this.prefs_.get('font-smoothing'));
598 this.syncBoldSafeState();
599};
600
rginda4bba5e12012-06-20 16:15:30 -0700601/**
602 * Set this.mousePasteButton based on the mouse-paste-button pref,
603 * autodetecting if necessary.
604 */
605hterm.Terminal.prototype.syncMousePasteButton = function() {
606 var button = this.prefs_.get('mouse-paste-button');
607 if (typeof button == 'number') {
608 this.mousePasteButton = button;
609 return;
610 }
611
612 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
613 if (!ary || ary[2] == 'CrOS') {
614 this.mousePasteButton = 2;
615 } else {
616 this.mousePasteButton = 3;
617 }
618};
619
620/**
621 * Enable or disable bold based on the enable-bold pref, autodetecting if
622 * necessary.
623 */
rginda9f5222b2012-03-05 11:53:28 -0800624hterm.Terminal.prototype.syncBoldSafeState = function() {
625 var enableBold = this.prefs_.get('enable-bold');
626 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700627 this.primaryScreen_.textAttributes.enableBold = enableBold;
628 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800629 return;
630 }
631
rgindaf7521392012-02-28 17:20:34 -0800632 var normalSize = this.scrollPort_.measureCharacterSize();
633 var boldSize = this.scrollPort_.measureCharacterSize('bold');
634
635 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800636 if (!isBoldSafe) {
637 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700638 'from normal. Font family is: ' +
639 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800640 }
rginda9f5222b2012-03-05 11:53:28 -0800641
Robert Gindaed016262012-10-26 16:27:09 -0700642 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
643 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800644};
645
646/**
rginda87b86462011-12-14 13:48:03 -0800647 * Return a copy of the current cursor position.
648 *
649 * @return {hterm.RowCol} The RowCol object representing the current position.
650 */
651hterm.Terminal.prototype.saveCursor = function() {
652 return this.screen_.cursorPosition.clone();
653};
654
rgindaa19afe22012-01-25 15:40:22 -0800655hterm.Terminal.prototype.getTextAttributes = function() {
656 return this.screen_.textAttributes;
657};
658
rginda1a09aa02012-06-18 21:11:25 -0700659hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
660 this.screen_.textAttributes = textAttributes;
661};
662
rginda87b86462011-12-14 13:48:03 -0800663/**
rgindaf522ce02012-04-17 17:49:17 -0700664 * Return the current browser zoom factor applied to the terminal.
665 *
666 * @return {number} The current browser zoom factor.
667 */
668hterm.Terminal.prototype.getZoomFactor = function() {
669 return this.scrollPort_.characterSize.zoomFactor;
670};
671
672/**
rginda9846e2f2012-01-27 13:53:33 -0800673 * Change the title of this terminal's window.
674 */
675hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800676 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800677};
678
679/**
rginda87b86462011-12-14 13:48:03 -0800680 * Restore a previously saved cursor position.
681 *
682 * @param {hterm.RowCol} cursor The position to restore.
683 */
684hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700685 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
686 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800687 this.screen_.setCursorPosition(row, column);
688 if (cursor.column > column ||
689 cursor.column == column && cursor.overflow) {
690 this.screen_.cursorPosition.overflow = true;
691 }
rginda87b86462011-12-14 13:48:03 -0800692};
693
694/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400695 * Clear the cursor's overflow flag.
696 */
697hterm.Terminal.prototype.clearCursorOverflow = function() {
698 this.screen_.cursorPosition.overflow = false;
699};
700
701/**
Robert Ginda830583c2013-08-07 13:20:46 -0700702 * Sets the cursor shape
703 */
704hterm.Terminal.prototype.setCursorShape = function(shape) {
705 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800706 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700707}
708
709/**
710 * Get the cursor shape
711 */
712hterm.Terminal.prototype.getCursorShape = function() {
713 return this.cursorShape_;
714}
715
716/**
rginda87b86462011-12-14 13:48:03 -0800717 * Set the width of the terminal, resizing the UI to match.
718 */
719hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800720 if (columnCount == null) {
721 this.div_.style.width = '100%';
722 return;
723 }
724
rginda35c456b2012-02-09 17:29:05 -0800725 this.div_.style.width = this.scrollPort_.characterSize.width *
Robert Ginda97769282013-02-01 15:30:30 -0800726 columnCount + this.scrollPort_.currentScrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400727 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800728 this.scheduleSyncCursorPosition_();
729};
rginda87b86462011-12-14 13:48:03 -0800730
rgindac9bc5502012-01-18 11:48:44 -0800731/**
rginda35c456b2012-02-09 17:29:05 -0800732 * Set the height of the terminal, resizing the UI to match.
733 */
734hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800735 if (rowCount == null) {
736 this.div_.style.height = '100%';
737 return;
738 }
739
rginda35c456b2012-02-09 17:29:05 -0800740 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700741 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800742 this.realizeSize_(this.screenSize.width, rowCount);
743 this.scheduleSyncCursorPosition_();
744};
745
746/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400747 * Deal with terminal size changes.
748 *
749 */
750hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
751 if (columnCount != this.screenSize.width)
752 this.realizeWidth_(columnCount);
753
754 if (rowCount != this.screenSize.height)
755 this.realizeHeight_(rowCount);
756
757 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700758 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400759};
760
761/**
rgindac9bc5502012-01-18 11:48:44 -0800762 * Deal with terminal width changes.
763 *
764 * This function does what needs to be done when the terminal width changes
765 * out from under us. It happens here rather than in onResize_() because this
766 * code may need to run synchronously to handle programmatic changes of
767 * terminal width.
768 *
769 * Relying on the browser to send us an async resize event means we may not be
770 * in the correct state yet when the next escape sequence hits.
771 */
772hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700773 if (columnCount <= 0)
774 throw new Error('Attempt to realize bad width: ' + columnCount);
775
rgindac9bc5502012-01-18 11:48:44 -0800776 var deltaColumns = columnCount - this.screen_.getWidth();
777
rginda87b86462011-12-14 13:48:03 -0800778 this.screenSize.width = columnCount;
779 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800780
781 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400782 if (this.defaultTabStops)
783 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800784 } else {
785 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400786 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800787 break;
788
789 this.tabStops_.pop();
790 }
791 }
792
793 this.screen_.setColumnCount(this.screenSize.width);
794};
795
796/**
797 * Deal with terminal height changes.
798 *
799 * This function does what needs to be done when the terminal height changes
800 * out from under us. It happens here rather than in onResize_() because this
801 * code may need to run synchronously to handle programmatic changes of
802 * terminal height.
803 *
804 * Relying on the browser to send us an async resize event means we may not be
805 * in the correct state yet when the next escape sequence hits.
806 */
807hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700808 if (rowCount <= 0)
809 throw new Error('Attempt to realize bad height: ' + rowCount);
810
rgindac9bc5502012-01-18 11:48:44 -0800811 var deltaRows = rowCount - this.screen_.getHeight();
812
813 this.screenSize.height = rowCount;
814
815 var cursor = this.saveCursor();
816
817 if (deltaRows < 0) {
818 // Screen got smaller.
819 deltaRows *= -1;
820 while (deltaRows) {
821 var lastRow = this.getRowCount() - 1;
822 if (lastRow - this.scrollbackRows_.length == cursor.row)
823 break;
824
825 if (this.getRowText(lastRow))
826 break;
827
828 this.screen_.popRow();
829 deltaRows--;
830 }
831
832 var ary = this.screen_.shiftRows(deltaRows);
833 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
834
835 // We just removed rows from the top of the screen, we need to update
836 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800837 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800838 } else if (deltaRows > 0) {
839 // Screen got larger.
840
841 if (deltaRows <= this.scrollbackRows_.length) {
842 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
843 var rows = this.scrollbackRows_.splice(
844 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
845 this.screen_.unshiftRows(rows);
846 deltaRows -= scrollbackCount;
847 cursor.row += scrollbackCount;
848 }
849
850 if (deltaRows)
851 this.appendRows_(deltaRows);
852 }
853
rginda35c456b2012-02-09 17:29:05 -0800854 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800855 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800856};
857
858/**
859 * Scroll the terminal to the top of the scrollback buffer.
860 */
861hterm.Terminal.prototype.scrollHome = function() {
862 this.scrollPort_.scrollRowToTop(0);
863};
864
865/**
866 * Scroll the terminal to the end.
867 */
868hterm.Terminal.prototype.scrollEnd = function() {
869 this.scrollPort_.scrollRowToBottom(this.getRowCount());
870};
871
872/**
873 * Scroll the terminal one page up (minus one line) relative to the current
874 * position.
875 */
876hterm.Terminal.prototype.scrollPageUp = function() {
877 var i = this.scrollPort_.getTopRowIndex();
878 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
879};
880
881/**
882 * Scroll the terminal one page down (minus one line) relative to the current
883 * position.
884 */
885hterm.Terminal.prototype.scrollPageDown = function() {
886 var i = this.scrollPort_.getTopRowIndex();
887 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800888};
889
rgindac9bc5502012-01-18 11:48:44 -0800890/**
Robert Ginda40932892012-12-10 17:26:40 -0800891 * Clear primary screen, secondary screen, and the scrollback buffer.
892 */
893hterm.Terminal.prototype.wipeContents = function() {
894 this.scrollbackRows_.length = 0;
895 this.scrollPort_.resetCache();
896
897 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
898 var bottom = screen.getHeight();
899 if (bottom > 0) {
900 this.renumberRows_(0, bottom);
901 this.clearHome(screen);
902 }
903 }.bind(this));
904
905 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -0700906 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -0800907};
908
909/**
rgindac9bc5502012-01-18 11:48:44 -0800910 * Full terminal reset.
911 */
rginda87b86462011-12-14 13:48:03 -0800912hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800913 this.clearAllTabStops();
914 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700915
916 this.clearHome(this.primaryScreen_);
917 this.primaryScreen_.textAttributes.reset();
918
919 this.clearHome(this.alternateScreen_);
920 this.alternateScreen_.textAttributes.reset();
921
rgindab8bc8932012-04-27 12:45:03 -0700922 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
923
Robert Ginda92e18102013-03-14 13:56:37 -0700924 this.vt.reset();
925
rgindac9bc5502012-01-18 11:48:44 -0800926 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800927};
928
rgindac9bc5502012-01-18 11:48:44 -0800929/**
930 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700931 *
932 * Perform a soft reset to the default values listed in
933 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800934 */
rginda0f5c0292012-01-13 11:00:13 -0800935hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700936 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800937 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700938
rgindab8bc8932012-04-27 12:45:03 -0700939 // Xterm also resets the color palette on soft reset, even though it doesn't
940 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700941 this.primaryScreen_.textAttributes.resetColorPalette();
942 this.alternateScreen_.textAttributes.resetColorPalette();
943
rgindab8bc8932012-04-27 12:45:03 -0700944 // The xterm man page explicitly says this will happen on soft reset.
945 this.setVTScrollRegion(null, null);
946
947 // Xterm also shows the cursor on soft reset, but does not alter the blink
948 // state.
rgindaa19afe22012-01-25 15:40:22 -0800949 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800950};
951
rgindac9bc5502012-01-18 11:48:44 -0800952/**
953 * Move the cursor forward to the next tab stop, or to the last column
954 * if no more tab stops are set.
955 */
956hterm.Terminal.prototype.forwardTabStop = function() {
957 var column = this.screen_.cursorPosition.column;
958
959 for (var i = 0; i < this.tabStops_.length; i++) {
960 if (this.tabStops_[i] > column) {
961 this.setCursorColumn(this.tabStops_[i]);
962 return;
963 }
964 }
965
David Benjamin66e954d2012-05-05 21:08:12 -0400966 // xterm does not clear the overflow flag on HT or CHT.
967 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800968 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400969 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800970};
971
rgindac9bc5502012-01-18 11:48:44 -0800972/**
973 * Move the cursor backward to the previous tab stop, or to the first column
974 * if no previous tab stops are set.
975 */
976hterm.Terminal.prototype.backwardTabStop = function() {
977 var column = this.screen_.cursorPosition.column;
978
979 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
980 if (this.tabStops_[i] < column) {
981 this.setCursorColumn(this.tabStops_[i]);
982 return;
983 }
984 }
985
986 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800987};
988
rgindac9bc5502012-01-18 11:48:44 -0800989/**
990 * Set a tab stop at the given column.
991 *
992 * @param {int} column Zero based column.
993 */
994hterm.Terminal.prototype.setTabStop = function(column) {
995 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
996 if (this.tabStops_[i] == column)
997 return;
998
999 if (this.tabStops_[i] < column) {
1000 this.tabStops_.splice(i + 1, 0, column);
1001 return;
1002 }
1003 }
1004
1005 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001006};
1007
rgindac9bc5502012-01-18 11:48:44 -08001008/**
1009 * Clear the tab stop at the current cursor position.
1010 *
1011 * No effect if there is no tab stop at the current cursor position.
1012 */
1013hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1014 var column = this.screen_.cursorPosition.column;
1015
1016 var i = this.tabStops_.indexOf(column);
1017 if (i == -1)
1018 return;
1019
1020 this.tabStops_.splice(i, 1);
1021};
1022
1023/**
1024 * Clear all tab stops.
1025 */
1026hterm.Terminal.prototype.clearAllTabStops = function() {
1027 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001028 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001029};
1030
1031/**
1032 * Set up the default tab stops, starting from a given column.
1033 *
1034 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001035 * from the specified column, or 0 if no column is provided. It also flags
1036 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001037 *
1038 * This does not clear the existing tab stops first, use clearAllTabStops
1039 * for that.
1040 *
1041 * @param {int} opt_start Optional starting zero based starting column, useful
1042 * for filling out missing tab stops when the terminal is resized.
1043 */
1044hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1045 var start = opt_start || 0;
1046 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001047 // Round start up to a default tab stop.
1048 start = start - 1 - ((start - 1) % w) + w;
1049 for (var i = start; i < this.screenSize.width; i += w) {
1050 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001051 }
David Benjamin66e954d2012-05-05 21:08:12 -04001052
1053 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001054};
1055
rginda6d397402012-01-17 10:58:29 -08001056/**
rginda8ba33642011-12-14 12:31:31 -08001057 * Interpret a sequence of characters.
1058 *
1059 * Incomplete escape sequences are buffered until the next call.
1060 *
1061 * @param {string} str Sequence of characters to interpret or pass through.
1062 */
1063hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001064 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001065 this.scheduleSyncCursorPosition_();
1066};
1067
1068/**
1069 * Take over the given DIV for use as the terminal display.
1070 *
1071 * @param {HTMLDivElement} div The div to use as the terminal display.
1072 */
1073hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001074 this.div_ = div;
1075
rginda8ba33642011-12-14 12:31:31 -08001076 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001077 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001078 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1079 this.scrollPort_.setBackgroundPosition(
1080 this.prefs_.get('background-position'));
Robert Gindae76aa9f2014-03-14 12:29:12 -07001081 this.scrollPort_.setUserCss(this.prefs_.get('user-css'));
rginda30f20f62012-04-05 16:36:19 -07001082
rginda0918b652012-04-04 11:26:24 -07001083 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001084
rginda9f5222b2012-03-05 11:53:28 -08001085 this.setFontSize(this.prefs_.get('font-size'));
1086 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001087
David Reveman8f552492012-03-28 12:18:41 -04001088 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1089
rginda8ba33642011-12-14 12:31:31 -08001090 this.document_ = this.scrollPort_.getDocument();
1091
rginda4bba5e12012-06-20 16:15:30 -07001092 this.document_.body.oncontextmenu = function() { return false };
1093
1094 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001095 var screenNode = this.scrollPort_.getScreenNode();
1096 screenNode.addEventListener('mousedown', onMouse);
1097 screenNode.addEventListener('mouseup', onMouse);
1098 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001099 this.scrollPort_.onScrollWheel = onMouse;
1100
Toni Barzic0bfa8922013-11-22 11:18:35 -08001101 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001102 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001103 // Listen for mousedown events on the screenNode as in FF the focus
1104 // events don't bubble.
1105 screenNode.addEventListener('mousedown', function() {
1106 setTimeout(this.onFocusChange_.bind(this, true));
1107 }.bind(this));
1108
Toni Barzic0bfa8922013-11-22 11:18:35 -08001109 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001110 'blur', this.onFocusChange_.bind(this, false));
1111
1112 var style = this.document_.createElement('style');
1113 style.textContent =
1114 ('.cursor-node[focus="false"] {' +
1115 ' box-sizing: border-box;' +
1116 ' background-color: transparent !important;' +
1117 ' border-width: 2px;' +
1118 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001119 '}' +
1120 '.wc-node {' +
1121 ' display: inline-block;' +
1122 ' text-align: center;' +
1123 ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001124 '}');
1125 this.document_.head.appendChild(style);
1126
Ricky Liang48f05cb2013-12-31 23:35:29 +08001127 var styleSheets = this.document_.styleSheets;
1128 var cssRules = styleSheets[styleSheets.length - 1].cssRules;
1129 this.wcCssRule_ = cssRules[cssRules.length - 1];
1130
rginda8ba33642011-12-14 12:31:31 -08001131 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001132 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001133 this.cursorNode_.style.cssText =
1134 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001135 'top: -99px;' +
1136 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001137 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1138 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
Rob Spies06533ba2014-04-24 11:20:37 -07001139 '-webkit-transition: opacity, background-color 100ms linear;' +
1140 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001141
rginda8e92a692012-05-20 19:37:20 -07001142 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001143 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1144 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001145
rginda8ba33642011-12-14 12:31:31 -08001146 this.document_.body.appendChild(this.cursorNode_);
1147
rgindad5613292012-06-19 15:40:37 -07001148 // When 'enableMouseDragScroll' is off we reposition this element directly
1149 // under the mouse cursor after a click. This makes Chrome associate
1150 // subsequent mousemove events with the scroll-blocker. Since the
1151 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1152 // events do not cause the scrollport to scroll.
1153 //
1154 // It's a hack, but it's the cleanest way I could find.
1155 this.scrollBlockerNode_ = this.document_.createElement('div');
1156 this.scrollBlockerNode_.style.cssText =
1157 ('position: absolute;' +
1158 'top: -99px;' +
1159 'display: block;' +
1160 'width: 10px;' +
1161 'height: 10px;');
1162 this.document_.body.appendChild(this.scrollBlockerNode_);
1163
1164 var onMouse = this.onMouse_.bind(this);
1165 this.scrollPort_.onScrollWheel = onMouse;
1166 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1167 ].forEach(function(event) {
1168 this.scrollBlockerNode_.addEventListener(event, onMouse);
1169 this.cursorNode_.addEventListener(event, onMouse);
1170 this.document_.addEventListener(event, onMouse);
1171 }.bind(this));
1172
1173 this.cursorNode_.addEventListener('mousedown', function() {
1174 setTimeout(this.focus.bind(this));
1175 }.bind(this));
1176
rginda8ba33642011-12-14 12:31:31 -08001177 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001178
rginda87b86462011-12-14 13:48:03 -08001179 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001180 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001181};
1182
rginda0918b652012-04-04 11:26:24 -07001183/**
1184 * Return the HTML document that contains the terminal DOM nodes.
1185 */
rginda87b86462011-12-14 13:48:03 -08001186hterm.Terminal.prototype.getDocument = function() {
1187 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001188};
1189
1190/**
rginda0918b652012-04-04 11:26:24 -07001191 * Focus the terminal.
1192 */
1193hterm.Terminal.prototype.focus = function() {
1194 this.scrollPort_.focus();
1195};
1196
1197/**
rginda8ba33642011-12-14 12:31:31 -08001198 * Return the HTML Element for a given row index.
1199 *
1200 * This is a method from the RowProvider interface. The ScrollPort uses
1201 * it to fetch rows on demand as they are scrolled into view.
1202 *
1203 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1204 * pairs to conserve memory.
1205 *
1206 * @param {integer} index The zero-based row index, measured relative to the
1207 * start of the scrollback buffer. On-screen rows will always have the
1208 * largest indicies.
1209 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1210 */
1211hterm.Terminal.prototype.getRowNode = function(index) {
1212 if (index < this.scrollbackRows_.length)
1213 return this.scrollbackRows_[index];
1214
1215 var screenIndex = index - this.scrollbackRows_.length;
1216 return this.screen_.rowsArray[screenIndex];
1217};
1218
1219/**
1220 * Return the text content for a given range of rows.
1221 *
1222 * This is a method from the RowProvider interface. The ScrollPort uses
1223 * it to fetch text content on demand when the user attempts to copy their
1224 * selection to the clipboard.
1225 *
1226 * @param {integer} start The zero-based row index to start from, measured
1227 * relative to the start of the scrollback buffer. On-screen rows will
1228 * always have the largest indicies.
1229 * @param {integer} end The zero-based row index to end on, measured
1230 * relative to the start of the scrollback buffer.
1231 * @return {string} A single string containing the text value of the range of
1232 * rows. Lines will be newline delimited, with no trailing newline.
1233 */
1234hterm.Terminal.prototype.getRowsText = function(start, end) {
1235 var ary = [];
1236 for (var i = start; i < end; i++) {
1237 var node = this.getRowNode(i);
1238 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001239 if (i < end - 1 && !node.getAttribute('line-overflow'))
1240 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001241 }
1242
rgindaa09e7332012-08-17 12:49:51 -07001243 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001244};
1245
1246/**
1247 * Return the text content for a given row.
1248 *
1249 * This is a method from the RowProvider interface. The ScrollPort uses
1250 * it to fetch text content on demand when the user attempts to copy their
1251 * selection to the clipboard.
1252 *
1253 * @param {integer} index The zero-based row index to return, measured
1254 * relative to the start of the scrollback buffer. On-screen rows will
1255 * always have the largest indicies.
1256 * @return {string} A string containing the text value of the selected row.
1257 */
1258hterm.Terminal.prototype.getRowText = function(index) {
1259 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001260 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001261};
1262
1263/**
1264 * Return the total number of rows in the addressable screen and in the
1265 * scrollback buffer of this terminal.
1266 *
1267 * This is a method from the RowProvider interface. The ScrollPort uses
1268 * it to compute the size of the scrollbar.
1269 *
1270 * @return {integer} The number of rows in this terminal.
1271 */
1272hterm.Terminal.prototype.getRowCount = function() {
1273 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1274};
1275
1276/**
1277 * Create DOM nodes for new rows and append them to the end of the terminal.
1278 *
1279 * This is the only correct way to add a new DOM node for a row. Notice that
1280 * the new row is appended to the bottom of the list of rows, and does not
1281 * require renumbering (of the rowIndex property) of previous rows.
1282 *
1283 * If you think you want a new blank row somewhere in the middle of the
1284 * terminal, look into moveRows_().
1285 *
1286 * This method does not pay attention to vtScrollTop/Bottom, since you should
1287 * be using moveRows() in cases where they would matter.
1288 *
1289 * The cursor will be positioned at column 0 of the first inserted line.
1290 */
1291hterm.Terminal.prototype.appendRows_ = function(count) {
1292 var cursorRow = this.screen_.rowsArray.length;
1293 var offset = this.scrollbackRows_.length + cursorRow;
1294 for (var i = 0; i < count; i++) {
1295 var row = this.document_.createElement('x-row');
1296 row.appendChild(this.document_.createTextNode(''));
1297 row.rowIndex = offset + i;
1298 this.screen_.pushRow(row);
1299 }
1300
1301 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1302 if (extraRows > 0) {
1303 var ary = this.screen_.shiftRows(extraRows);
1304 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001305 if (this.scrollPort_.isScrolledEnd)
1306 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001307 }
1308
1309 if (cursorRow >= this.screen_.rowsArray.length)
1310 cursorRow = this.screen_.rowsArray.length - 1;
1311
rginda87b86462011-12-14 13:48:03 -08001312 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001313};
1314
1315/**
1316 * Relocate rows from one part of the addressable screen to another.
1317 *
1318 * This is used to recycle rows during VT scrolls (those which are driven
1319 * by VT commands, rather than by the user manipulating the scrollbar.)
1320 *
1321 * In this case, the blank lines scrolled into the scroll region are made of
1322 * the nodes we scrolled off. These have their rowIndex properties carefully
1323 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001324 */
1325hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1326 var ary = this.screen_.removeRows(fromIndex, count);
1327 this.screen_.insertRows(toIndex, ary);
1328
1329 var start, end;
1330 if (fromIndex < toIndex) {
1331 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001332 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001333 } else {
1334 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001335 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001336 }
1337
1338 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001339 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001340};
1341
1342/**
1343 * Renumber the rowIndex property of the given range of rows.
1344 *
1345 * The start and end indicies are relative to the screen, not the scrollback.
1346 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001347 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001348 * no need to renumber scrollback rows.
1349 */
Robert Ginda40932892012-12-10 17:26:40 -08001350hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1351 var screen = opt_screen || this.screen_;
1352
rginda8ba33642011-12-14 12:31:31 -08001353 var offset = this.scrollbackRows_.length;
1354 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001355 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001356 }
1357};
1358
1359/**
1360 * Print a string to the terminal.
1361 *
1362 * This respects the current insert and wraparound modes. It will add new lines
1363 * to the end of the terminal, scrolling off the top into the scrollback buffer
1364 * if necessary.
1365 *
1366 * The string is *not* parsed for escape codes. Use the interpret() method if
1367 * that's what you're after.
1368 *
1369 * @param{string} str The string to print.
1370 */
1371hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001372 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001373
Ricky Liang48f05cb2013-12-31 23:35:29 +08001374 var strWidth = lib.wc.strWidth(str);
1375
1376 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001377 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1378 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001379 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001380 }
rgindaa19afe22012-01-25 15:40:22 -08001381
Ricky Liang48f05cb2013-12-31 23:35:29 +08001382 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001383 var didOverflow = false;
1384 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001385
rgindaa9abdd82012-08-06 18:05:09 -07001386 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1387 didOverflow = true;
1388 count = this.screenSize.width - this.screen_.cursorPosition.column;
1389 }
rgindaa19afe22012-01-25 15:40:22 -08001390
rgindaa9abdd82012-08-06 18:05:09 -07001391 if (didOverflow && !this.options_.wraparound) {
1392 // If the string overflowed the line but wraparound is off, then the
1393 // last printed character should be the last of the string.
1394 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001395 substr = lib.wc.substr(str, startOffset, count - 1) +
1396 lib.wc.substr(str, strWidth - 1);
1397 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001398 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001399 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001400 }
rgindaa19afe22012-01-25 15:40:22 -08001401
Ricky Liang48f05cb2013-12-31 23:35:29 +08001402 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1403 for (var i = 0; i < tokens.length; i++) {
1404 if (tokens[i].wcNode)
1405 this.screen_.textAttributes.wcNode = true;
1406
1407 if (this.options_.insertMode) {
1408 this.screen_.insertString(tokens[i].str);
1409 } else {
1410 this.screen_.overwriteString(tokens[i].str);
1411 }
1412 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001413 }
1414
1415 this.screen_.maybeClipCurrentRow();
1416 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001417 }
rginda8ba33642011-12-14 12:31:31 -08001418
1419 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001420
rginda9f5222b2012-03-05 11:53:28 -08001421 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001422 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001423};
1424
1425/**
rginda87b86462011-12-14 13:48:03 -08001426 * Set the VT scroll region.
1427 *
rginda87b86462011-12-14 13:48:03 -08001428 * This also resets the cursor position to the absolute (0, 0) position, since
1429 * that's what xterm appears to do.
1430 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001431 * Setting the scroll region to the full height of the terminal will clear
1432 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1433 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1434 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1435 * continue to work as most users would expect.
1436 *
rginda87b86462011-12-14 13:48:03 -08001437 * @param {integer} scrollTop The zero-based top of the scroll region.
1438 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1439 * inclusive.
1440 */
1441hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001442 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001443 this.vtScrollTop_ = null;
1444 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001445 } else {
1446 this.vtScrollTop_ = scrollTop;
1447 this.vtScrollBottom_ = scrollBottom;
1448 }
rginda87b86462011-12-14 13:48:03 -08001449};
1450
1451/**
rginda8ba33642011-12-14 12:31:31 -08001452 * Return the top row index according to the VT.
1453 *
1454 * This will return 0 unless the terminal has been told to restrict scrolling
1455 * to some lower row. It is used for some VT cursor positioning and scrolling
1456 * commands.
1457 *
1458 * @return {integer} The topmost row in the terminal's scroll region.
1459 */
1460hterm.Terminal.prototype.getVTScrollTop = function() {
1461 if (this.vtScrollTop_ != null)
1462 return this.vtScrollTop_;
1463
1464 return 0;
rginda87b86462011-12-14 13:48:03 -08001465};
rginda8ba33642011-12-14 12:31:31 -08001466
1467/**
1468 * Return the bottom row index according to the VT.
1469 *
1470 * This will return the height of the terminal unless the it has been told to
1471 * restrict scrolling to some higher row. It is used for some VT cursor
1472 * positioning and scrolling commands.
1473 *
1474 * @return {integer} The bottommost row in the terminal's scroll region.
1475 */
1476hterm.Terminal.prototype.getVTScrollBottom = function() {
1477 if (this.vtScrollBottom_ != null)
1478 return this.vtScrollBottom_;
1479
rginda87b86462011-12-14 13:48:03 -08001480 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001481}
1482
1483/**
1484 * Process a '\n' character.
1485 *
1486 * If the cursor is on the final row of the terminal this will append a new
1487 * blank row to the screen and scroll the topmost row into the scrollback
1488 * buffer.
1489 *
1490 * Otherwise, this moves the cursor to column zero of the next row.
1491 */
1492hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001493 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1494 this.screen_.rowsArray.length - 1);
1495
1496 if (this.vtScrollBottom_ != null) {
1497 // A VT Scroll region is active, we never append new rows.
1498 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1499 // We're at the end of the VT Scroll Region, perform a VT scroll.
1500 this.vtScrollUp(1);
1501 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1502 } else if (cursorAtEndOfScreen) {
1503 // We're at the end of the screen, the only thing to do is put the
1504 // cursor to column 0.
1505 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1506 } else {
1507 // Anywhere else, advance the cursor row, and reset the column.
1508 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1509 }
1510 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001511 // We're at the end of the screen. Append a new row to the terminal,
1512 // shifting the top row into the scrollback.
1513 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001514 } else {
rginda87b86462011-12-14 13:48:03 -08001515 // Anywhere else in the screen just moves the cursor.
1516 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001517 }
1518};
1519
1520/**
1521 * Like newLine(), except maintain the cursor column.
1522 */
1523hterm.Terminal.prototype.lineFeed = function() {
1524 var column = this.screen_.cursorPosition.column;
1525 this.newLine();
1526 this.setCursorColumn(column);
1527};
1528
1529/**
rginda87b86462011-12-14 13:48:03 -08001530 * If autoCarriageReturn is set then newLine(), else lineFeed().
1531 */
1532hterm.Terminal.prototype.formFeed = function() {
1533 if (this.options_.autoCarriageReturn) {
1534 this.newLine();
1535 } else {
1536 this.lineFeed();
1537 }
1538};
1539
1540/**
1541 * Move the cursor up one row, possibly inserting a blank line.
1542 *
1543 * The cursor column is not changed.
1544 */
1545hterm.Terminal.prototype.reverseLineFeed = function() {
1546 var scrollTop = this.getVTScrollTop();
1547 var currentRow = this.screen_.cursorPosition.row;
1548
1549 if (currentRow == scrollTop) {
1550 this.insertLines(1);
1551 } else {
1552 this.setAbsoluteCursorRow(currentRow - 1);
1553 }
1554};
1555
1556/**
rginda8ba33642011-12-14 12:31:31 -08001557 * Replace all characters to the left of the current cursor with the space
1558 * character.
1559 *
1560 * TODO(rginda): This should probably *remove* the characters (not just replace
1561 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001562 * position.
rginda8ba33642011-12-14 12:31:31 -08001563 */
1564hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001565 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001566 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001567 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001568 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001569};
1570
1571/**
David Benjamin684a9b72012-05-01 17:19:58 -04001572 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001573 *
1574 * The cursor position is unchanged.
1575 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001576 * If the current background color is not the default background color this
1577 * will insert spaces rather than delete. This is unfortunate because the
1578 * trailing space will affect text selection, but it's difficult to come up
1579 * with a way to style empty space that wouldn't trip up the hterm.Screen
1580 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001581 *
1582 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1583 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1584 * crbug.com/232390 for details.
rginda8ba33642011-12-14 12:31:31 -08001585 */
1586hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001587 if (this.screen_.cursorPosition.overflow)
1588 return;
1589
Robert Ginda7fd57082012-09-25 14:41:47 -07001590 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1591 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001592
1593 if (this.screen_.textAttributes.background ===
1594 this.screen_.textAttributes.DEFAULT_COLOR) {
1595 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001596 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001597 this.screen_.cursorPosition.column + count) {
1598 this.screen_.deleteChars(count);
1599 this.clearCursorOverflow();
1600 return;
1601 }
1602 }
1603
rginda87b86462011-12-14 13:48:03 -08001604 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001605 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001606 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001607 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001608};
1609
1610/**
1611 * Erase the current line.
1612 *
1613 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001614 */
1615hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001616 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001617 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001618 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001619 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001620};
1621
1622/**
David Benjamina08d78f2012-05-05 00:28:49 -04001623 * Erase all characters from the start of the screen to the current cursor
1624 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001625 *
1626 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001627 */
1628hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001629 var cursor = this.saveCursor();
1630
1631 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001632
David Benjamina08d78f2012-05-05 00:28:49 -04001633 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001634 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001635 this.screen_.clearCursorRow();
1636 }
1637
rginda87b86462011-12-14 13:48:03 -08001638 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001639 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001640};
1641
1642/**
1643 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001644 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001645 *
1646 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001647 */
1648hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001649 var cursor = this.saveCursor();
1650
1651 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001652
David Benjamina08d78f2012-05-05 00:28:49 -04001653 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001654 for (var i = cursor.row + 1; i <= bottom; i++) {
1655 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001656 this.screen_.clearCursorRow();
1657 }
1658
rginda87b86462011-12-14 13:48:03 -08001659 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001660 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001661};
1662
1663/**
1664 * Fill the terminal with a given character.
1665 *
1666 * This methods does not respect the VT scroll region.
1667 *
1668 * @param {string} ch The character to use for the fill.
1669 */
1670hterm.Terminal.prototype.fill = function(ch) {
1671 var cursor = this.saveCursor();
1672
1673 this.setAbsoluteCursorPosition(0, 0);
1674 for (var row = 0; row < this.screenSize.height; row++) {
1675 for (var col = 0; col < this.screenSize.width; col++) {
1676 this.setAbsoluteCursorPosition(row, col);
1677 this.screen_.overwriteString(ch);
1678 }
1679 }
1680
1681 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001682};
1683
1684/**
rginda9ea433c2012-03-16 11:57:00 -07001685 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001686 *
rginda9ea433c2012-03-16 11:57:00 -07001687 * This does not respect the scroll region.
1688 *
1689 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1690 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001691 */
rginda9ea433c2012-03-16 11:57:00 -07001692hterm.Terminal.prototype.clearHome = function(opt_screen) {
1693 var screen = opt_screen || this.screen_;
1694 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001695
rginda11057d52012-04-25 12:29:56 -07001696 if (bottom == 0) {
1697 // Empty screen, nothing to do.
1698 return;
1699 }
1700
rgindae4d29232012-01-19 10:47:13 -08001701 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001702 screen.setCursorPosition(i, 0);
1703 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001704 }
1705
rginda9ea433c2012-03-16 11:57:00 -07001706 screen.setCursorPosition(0, 0);
1707};
1708
1709/**
1710 * Erase the entire display without changing the cursor position.
1711 *
1712 * The cursor position is unchanged. This does not respect the scroll
1713 * region.
1714 *
1715 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1716 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001717 */
1718hterm.Terminal.prototype.clear = function(opt_screen) {
1719 var screen = opt_screen || this.screen_;
1720 var cursor = screen.cursorPosition.clone();
1721 this.clearHome(screen);
1722 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001723};
1724
1725/**
1726 * VT command to insert lines at the current cursor row.
1727 *
1728 * This respects the current scroll region. Rows pushed off the bottom are
1729 * lost (they won't show up in the scrollback buffer).
1730 *
rginda8ba33642011-12-14 12:31:31 -08001731 * @param {integer} count The number of lines to insert.
1732 */
1733hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001734 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001735
1736 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001737 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001738
Robert Ginda579186b2012-09-26 11:40:04 -07001739 // The moveCount is the number of rows we need to relocate to make room for
1740 // the new row(s). The count is the distance to move them.
1741 var moveCount = bottom - cursorRow - count + 1;
1742 if (moveCount)
1743 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001744
Robert Ginda579186b2012-09-26 11:40:04 -07001745 for (var i = count - 1; i >= 0; i--) {
1746 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001747 this.screen_.clearCursorRow();
1748 }
rginda8ba33642011-12-14 12:31:31 -08001749};
1750
1751/**
1752 * VT command to delete lines at the current cursor row.
1753 *
1754 * New rows are added to the bottom of scroll region to take their place. New
1755 * rows are strictly there to take up space and have no content or style.
1756 */
1757hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001758 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001759
rginda87b86462011-12-14 13:48:03 -08001760 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001761 var bottom = this.getVTScrollBottom();
1762
rginda87b86462011-12-14 13:48:03 -08001763 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001764 count = Math.min(count, maxCount);
1765
rginda87b86462011-12-14 13:48:03 -08001766 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001767 if (count != maxCount)
1768 this.moveRows_(top, count, moveStart);
1769
1770 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001771 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001772 this.screen_.clearCursorRow();
1773 }
1774
rginda87b86462011-12-14 13:48:03 -08001775 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001776 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001777};
1778
1779/**
1780 * Inserts the given number of spaces at the current cursor position.
1781 *
rginda87b86462011-12-14 13:48:03 -08001782 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001783 */
1784hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001785 var cursor = this.saveCursor();
1786
rgindacbbd7482012-06-13 15:06:16 -07001787 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001788 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001789 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001790
1791 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001792 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001793};
1794
1795/**
1796 * Forward-delete the specified number of characters starting at the cursor
1797 * position.
1798 *
1799 * @param {integer} count The number of characters to delete.
1800 */
1801hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001802 var deleted = this.screen_.deleteChars(count);
1803 if (deleted && !this.screen_.textAttributes.isDefault()) {
1804 var cursor = this.saveCursor();
1805 this.setCursorColumn(this.screenSize.width - deleted);
1806 this.screen_.insertString(lib.f.getWhitespace(deleted));
1807 this.restoreCursor(cursor);
1808 }
1809
David Benjamin54e8bf62012-06-01 22:31:40 -04001810 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001811};
1812
1813/**
1814 * Shift rows in the scroll region upwards by a given number of lines.
1815 *
1816 * New rows are inserted at the bottom of the scroll region to fill the
1817 * vacated rows. The new rows not filled out with the current text attributes.
1818 *
1819 * This function does not affect the scrollback rows at all. Rows shifted
1820 * off the top are lost.
1821 *
rginda87b86462011-12-14 13:48:03 -08001822 * The cursor position is not altered.
1823 *
rginda8ba33642011-12-14 12:31:31 -08001824 * @param {integer} count The number of rows to scroll.
1825 */
1826hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001827 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001828
rginda87b86462011-12-14 13:48:03 -08001829 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001830 this.deleteLines(count);
1831
rginda87b86462011-12-14 13:48:03 -08001832 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001833};
1834
1835/**
1836 * Shift rows below the cursor down by a given number of lines.
1837 *
1838 * This function respects the current scroll region.
1839 *
1840 * New rows are inserted at the top of the scroll region to fill the
1841 * vacated rows. The new rows not filled out with the current text attributes.
1842 *
1843 * This function does not affect the scrollback rows at all. Rows shifted
1844 * off the bottom are lost.
1845 *
1846 * @param {integer} count The number of rows to scroll.
1847 */
1848hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001849 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001850
rginda87b86462011-12-14 13:48:03 -08001851 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001852 this.insertLines(opt_count);
1853
rginda87b86462011-12-14 13:48:03 -08001854 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001855};
1856
rginda87b86462011-12-14 13:48:03 -08001857
rginda8ba33642011-12-14 12:31:31 -08001858/**
1859 * Set the cursor position.
1860 *
1861 * The cursor row is relative to the scroll region if the terminal has
1862 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1863 *
1864 * @param {integer} row The new zero-based cursor row.
1865 * @param {integer} row The new zero-based cursor column.
1866 */
1867hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1868 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001869 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001870 } else {
rginda87b86462011-12-14 13:48:03 -08001871 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001872 }
rginda87b86462011-12-14 13:48:03 -08001873};
rginda8ba33642011-12-14 12:31:31 -08001874
rginda87b86462011-12-14 13:48:03 -08001875hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1876 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001877 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1878 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001879 this.screen_.setCursorPosition(row, column);
1880};
1881
1882hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001883 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1884 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001885 this.screen_.setCursorPosition(row, column);
1886};
1887
1888/**
1889 * Set the cursor column.
1890 *
1891 * @param {integer} column The new zero-based cursor column.
1892 */
1893hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001894 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001895};
1896
1897/**
1898 * Return the cursor column.
1899 *
1900 * @return {integer} The zero-based cursor column.
1901 */
1902hterm.Terminal.prototype.getCursorColumn = function() {
1903 return this.screen_.cursorPosition.column;
1904};
1905
1906/**
1907 * Set the cursor row.
1908 *
1909 * The cursor row is relative to the scroll region if the terminal has
1910 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1911 *
1912 * @param {integer} row The new cursor row.
1913 */
rginda87b86462011-12-14 13:48:03 -08001914hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1915 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001916};
1917
1918/**
1919 * Return the cursor row.
1920 *
1921 * @return {integer} The zero-based cursor row.
1922 */
1923hterm.Terminal.prototype.getCursorRow = function(row) {
1924 return this.screen_.cursorPosition.row;
1925};
1926
1927/**
1928 * Request that the ScrollPort redraw itself soon.
1929 *
1930 * The redraw will happen asynchronously, soon after the call stack winds down.
1931 * Multiple calls will be coalesced into a single redraw.
1932 */
1933hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001934 if (this.timeouts_.redraw)
1935 return;
rginda8ba33642011-12-14 12:31:31 -08001936
1937 var self = this;
rginda87b86462011-12-14 13:48:03 -08001938 this.timeouts_.redraw = setTimeout(function() {
1939 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001940 self.scrollPort_.redraw_();
1941 }, 0);
1942};
1943
1944/**
1945 * Request that the ScrollPort be scrolled to the bottom.
1946 *
1947 * The scroll will happen asynchronously, soon after the call stack winds down.
1948 * Multiple calls will be coalesced into a single scroll.
1949 *
1950 * This affects the scrollbar position of the ScrollPort, and has nothing to
1951 * do with the VT scroll commands.
1952 */
1953hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1954 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001955 return;
rginda8ba33642011-12-14 12:31:31 -08001956
1957 var self = this;
1958 this.timeouts_.scrollDown = setTimeout(function() {
1959 delete self.timeouts_.scrollDown;
1960 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1961 }, 10);
1962};
1963
1964/**
1965 * Move the cursor up a specified number of rows.
1966 *
1967 * @param {integer} count The number of rows to move the cursor.
1968 */
1969hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001970 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001971};
1972
1973/**
1974 * Move the cursor down a specified number of rows.
1975 *
1976 * @param {integer} count The number of rows to move the cursor.
1977 */
1978hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001979 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001980 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1981 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1982 this.screenSize.height - 1);
1983
rgindacbbd7482012-06-13 15:06:16 -07001984 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001985 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001986 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001987};
1988
1989/**
1990 * Move the cursor left a specified number of columns.
1991 *
1992 * @param {integer} count The number of columns to move the cursor.
1993 */
1994hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001995 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001996};
1997
1998/**
1999 * Move the cursor right a specified number of columns.
2000 *
2001 * @param {integer} count The number of columns to move the cursor.
2002 */
2003hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002004 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07002005 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002006 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002007 this.setCursorColumn(column);
2008};
2009
2010/**
2011 * Reverse the foreground and background colors of the terminal.
2012 *
2013 * This only affects text that was drawn with no attributes.
2014 *
2015 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2016 * been drawn with attributes that happen to coincide with the default
2017 * 'no-attribute' colors. My guess is probably not.
2018 */
2019hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002020 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002021 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002022 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2023 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002024 } else {
rginda9f5222b2012-03-05 11:53:28 -08002025 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2026 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002027 }
2028};
2029
2030/**
rginda87b86462011-12-14 13:48:03 -08002031 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002032 *
2033 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002034 */
2035hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002036 this.cursorNode_.style.backgroundColor =
2037 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002038
2039 var self = this;
2040 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002041 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002042 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002043
2044 if (this.bellAudio_.getAttribute('src')) {
Robert Gindaa6331372013-03-19 10:35:39 -07002045 if (this.bellSquelchTimeout_)
Robert Ginda92e18102013-03-14 13:56:37 -07002046 return;
2047
2048 this.bellAudio_.play();
2049
2050 this.bellSequelchTimeout_ = setTimeout(function() {
2051 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002052 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002053 } else {
2054 delete this.bellSquelchTimeout_;
2055 }
rginda87b86462011-12-14 13:48:03 -08002056};
2057
2058/**
rginda8ba33642011-12-14 12:31:31 -08002059 * Set the origin mode bit.
2060 *
2061 * If origin mode is on, certain VT cursor and scrolling commands measure their
2062 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2063 * to the top of the addressable screen.
2064 *
2065 * Defaults to off.
2066 *
2067 * @param {boolean} state True to set origin mode, false to unset.
2068 */
2069hterm.Terminal.prototype.setOriginMode = function(state) {
2070 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002071 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002072};
2073
2074/**
2075 * Set the insert mode bit.
2076 *
2077 * If insert mode is on, existing text beyond the cursor position will be
2078 * shifted right to make room for new text. Otherwise, new text overwrites
2079 * any existing text.
2080 *
2081 * Defaults to off.
2082 *
2083 * @param {boolean} state True to set insert mode, false to unset.
2084 */
2085hterm.Terminal.prototype.setInsertMode = function(state) {
2086 this.options_.insertMode = state;
2087};
2088
2089/**
rginda87b86462011-12-14 13:48:03 -08002090 * Set the auto carriage return bit.
2091 *
2092 * If auto carriage return is on then a formfeed character is interpreted
2093 * as a newline, otherwise it's the same as a linefeed. The difference boils
2094 * down to whether or not the cursor column is reset.
2095 */
2096hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2097 this.options_.autoCarriageReturn = state;
2098};
2099
2100/**
rginda8ba33642011-12-14 12:31:31 -08002101 * Set the wraparound mode bit.
2102 *
2103 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2104 * to the start of the following row. Otherwise, the cursor is clamped to the
2105 * end of the screen and attempts to write past it are ignored.
2106 *
2107 * Defaults to on.
2108 *
2109 * @param {boolean} state True to set wraparound mode, false to unset.
2110 */
2111hterm.Terminal.prototype.setWraparound = function(state) {
2112 this.options_.wraparound = state;
2113};
2114
2115/**
2116 * Set the reverse-wraparound mode bit.
2117 *
2118 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2119 * to the end of the previous row. Otherwise, the cursor is clamped to column
2120 * 0.
2121 *
2122 * Defaults to off.
2123 *
2124 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2125 */
2126hterm.Terminal.prototype.setReverseWraparound = function(state) {
2127 this.options_.reverseWraparound = state;
2128};
2129
2130/**
2131 * Selects between the primary and alternate screens.
2132 *
2133 * If alternate mode is on, the alternate screen is active. Otherwise the
2134 * primary screen is active.
2135 *
2136 * Swapping screens has no effect on the scrollback buffer.
2137 *
2138 * Each screen maintains its own cursor position.
2139 *
2140 * Defaults to off.
2141 *
2142 * @param {boolean} state True to set alternate mode, false to unset.
2143 */
2144hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002145 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002146 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2147
rginda35c456b2012-02-09 17:29:05 -08002148 if (this.screen_.rowsArray.length &&
2149 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2150 // If the screen changed sizes while we were away, our rowIndexes may
2151 // be incorrect.
2152 var offset = this.scrollbackRows_.length;
2153 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002154 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002155 ary[i].rowIndex = offset + i;
2156 }
2157 }
rginda8ba33642011-12-14 12:31:31 -08002158
rginda35c456b2012-02-09 17:29:05 -08002159 this.realizeWidth_(this.screenSize.width);
2160 this.realizeHeight_(this.screenSize.height);
2161 this.scrollPort_.syncScrollHeight();
2162 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002163
rginda6d397402012-01-17 10:58:29 -08002164 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002165 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002166};
2167
2168/**
2169 * Set the cursor-blink mode bit.
2170 *
2171 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2172 * a visible cursor does not blink.
2173 *
2174 * You should make sure to turn blinking off if you're going to dispose of a
2175 * terminal, otherwise you'll leak a timeout.
2176 *
2177 * Defaults to on.
2178 *
2179 * @param {boolean} state True to set cursor-blink mode, false to unset.
2180 */
2181hterm.Terminal.prototype.setCursorBlink = function(state) {
2182 this.options_.cursorBlink = state;
2183
2184 if (!state && this.timeouts_.cursorBlink) {
2185 clearTimeout(this.timeouts_.cursorBlink);
2186 delete this.timeouts_.cursorBlink;
2187 }
2188
2189 if (this.options_.cursorVisible)
2190 this.setCursorVisible(true);
2191};
2192
2193/**
2194 * Set the cursor-visible mode bit.
2195 *
2196 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2197 *
2198 * Defaults to on.
2199 *
2200 * @param {boolean} state True to set cursor-visible mode, false to unset.
2201 */
2202hterm.Terminal.prototype.setCursorVisible = function(state) {
2203 this.options_.cursorVisible = state;
2204
2205 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002206 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002207 return;
2208 }
2209
rginda87b86462011-12-14 13:48:03 -08002210 this.syncCursorPosition_();
2211
2212 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002213
2214 if (this.options_.cursorBlink) {
2215 if (this.timeouts_.cursorBlink)
2216 return;
2217
2218 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2219 500);
2220 } else {
2221 if (this.timeouts_.cursorBlink) {
2222 clearTimeout(this.timeouts_.cursorBlink);
2223 delete this.timeouts_.cursorBlink;
2224 }
2225 }
2226};
2227
2228/**
rginda87b86462011-12-14 13:48:03 -08002229 * Synchronizes the visible cursor and document selection with the current
2230 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002231 */
2232hterm.Terminal.prototype.syncCursorPosition_ = function() {
2233 var topRowIndex = this.scrollPort_.getTopRowIndex();
2234 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2235 var cursorRowIndex = this.scrollbackRows_.length +
2236 this.screen_.cursorPosition.row;
2237
2238 if (cursorRowIndex > bottomRowIndex) {
2239 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002240 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002241 return;
2242 }
2243
2244 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002245 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2246 'px';
2247 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2248 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002249
2250 this.cursorNode_.setAttribute('title',
2251 '(' + this.screen_.cursorPosition.row +
2252 ', ' + this.screen_.cursorPosition.column +
2253 ')');
2254
2255 // Update the caret for a11y purposes.
2256 var selection = this.document_.getSelection();
2257 if (selection && selection.isCollapsed)
2258 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002259};
2260
Robert Gindafb1be6a2013-12-11 11:56:22 -08002261/**
2262 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2263 * and character cell dimensions.
2264 */
Robert Ginda830583c2013-08-07 13:20:46 -07002265hterm.Terminal.prototype.restyleCursor_ = function() {
2266 var shape = this.cursorShape_;
2267
2268 if (this.cursorNode_.getAttribute('focus') == 'false') {
2269 // Always show a block cursor when unfocused.
2270 shape = hterm.Terminal.cursorShape.BLOCK;
2271 }
2272
2273 var style = this.cursorNode_.style;
2274
Robert Gindafb1be6a2013-12-11 11:56:22 -08002275 style.width = this.scrollPort_.characterSize.width + 'px';
2276
Robert Ginda830583c2013-08-07 13:20:46 -07002277 switch (shape) {
2278 case hterm.Terminal.cursorShape.BEAM:
2279 style.height = this.scrollPort_.characterSize.height + 'px';
2280 style.backgroundColor = 'transparent';
2281 style.borderBottomStyle = null;
2282 style.borderLeftStyle = 'solid';
2283 break;
2284
2285 case hterm.Terminal.cursorShape.UNDERLINE:
2286 style.height = this.scrollPort_.characterSize.baseline + 'px';
2287 style.backgroundColor = 'transparent';
2288 style.borderBottomStyle = 'solid';
2289 // correct the size to put it exactly at the baseline
2290 style.borderLeftStyle = null;
2291 break;
2292
2293 default:
2294 style.height = this.scrollPort_.characterSize.height + 'px';
2295 style.backgroundColor = this.cursorColor_;
2296 style.borderBottomStyle = null;
2297 style.borderLeftStyle = null;
2298 break;
2299 }
2300};
2301
rginda8ba33642011-12-14 12:31:31 -08002302/**
2303 * Synchronizes the visible cursor with the current cursor coordinates.
2304 *
2305 * The sync will happen asynchronously, soon after the call stack winds down.
2306 * Multiple calls will be coalesced into a single sync.
2307 */
2308hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2309 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002310 return;
rginda8ba33642011-12-14 12:31:31 -08002311
2312 var self = this;
2313 this.timeouts_.syncCursor = setTimeout(function() {
2314 self.syncCursorPosition_();
2315 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002316 }, 0);
2317};
2318
rgindacc2996c2012-02-24 14:59:31 -08002319/**
rgindaf522ce02012-04-17 17:49:17 -07002320 * Show or hide the zoom warning.
2321 *
2322 * The zoom warning is a message warning the user that their browser zoom must
2323 * be set to 100% in order for hterm to function properly.
2324 *
2325 * @param {boolean} state True to show the message, false to hide it.
2326 */
2327hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2328 if (!this.zoomWarningNode_) {
2329 if (!state)
2330 return;
2331
2332 this.zoomWarningNode_ = this.document_.createElement('div');
2333 this.zoomWarningNode_.style.cssText = (
2334 'color: black;' +
2335 'background-color: #ff2222;' +
2336 'font-size: large;' +
2337 'border-radius: 8px;' +
2338 'opacity: 0.75;' +
2339 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2340 'top: 0.5em;' +
2341 'right: 1.2em;' +
2342 'position: absolute;' +
2343 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002344 '-webkit-user-select: none;' +
2345 '-moz-text-size-adjust: none;' +
2346 '-moz-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002347 }
2348
Robert Gindab4839c22013-02-28 16:52:10 -08002349 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2350 hterm.zoomWarningMessage,
2351 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2352
rgindaf522ce02012-04-17 17:49:17 -07002353 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2354
2355 if (state) {
2356 if (!this.zoomWarningNode_.parentNode)
2357 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2358 } else if (this.zoomWarningNode_.parentNode) {
2359 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2360 }
2361};
2362
2363/**
rgindacc2996c2012-02-24 14:59:31 -08002364 * Show the terminal overlay for a given amount of time.
2365 *
2366 * The terminal overlay appears in inverse video in a large font, centered
2367 * over the terminal. You should probably keep the overlay message brief,
2368 * since it's in a large font and you probably aren't going to check the size
2369 * of the terminal first.
2370 *
2371 * @param {string} msg The text (not HTML) message to display in the overlay.
2372 * @param {number} opt_timeout The amount of time to wait before fading out
2373 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2374 * stay up forever (or until the next overlay).
2375 */
2376hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002377 if (!this.overlayNode_) {
2378 if (!this.div_)
2379 return;
2380
2381 this.overlayNode_ = this.document_.createElement('div');
2382 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002383 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002384 'font-size: xx-large;' +
2385 'opacity: 0.75;' +
2386 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2387 'position: absolute;' +
2388 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002389 '-webkit-transition: opacity 180ms ease-in;' +
2390 '-moz-user-select: none;' +
2391 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002392
2393 this.overlayNode_.addEventListener('mousedown', function(e) {
2394 e.preventDefault();
2395 e.stopPropagation();
2396 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002397 }
2398
rginda9f5222b2012-03-05 11:53:28 -08002399 this.overlayNode_.style.color = this.prefs_.get('background-color');
2400 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2401 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2402
rgindaf0090c92012-02-10 14:58:52 -08002403 this.overlayNode_.textContent = msg;
2404 this.overlayNode_.style.opacity = '0.75';
2405
2406 if (!this.overlayNode_.parentNode)
2407 this.div_.appendChild(this.overlayNode_);
2408
Robert Ginda97769282013-02-01 15:30:30 -08002409 var divSize = hterm.getClientSize(this.div_);
2410 var overlaySize = hterm.getClientSize(this.overlayNode_);
2411
2412 this.overlayNode_.style.top = (divSize.height - overlaySize.height) / 2;
2413 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
2414 this.scrollPort_.currentScrollbarWidthPx) / 2;
rgindaf0090c92012-02-10 14:58:52 -08002415
2416 var self = this;
2417
2418 if (this.overlayTimeout_)
2419 clearTimeout(this.overlayTimeout_);
2420
rgindacc2996c2012-02-24 14:59:31 -08002421 if (opt_timeout === null)
2422 return;
2423
rgindaf0090c92012-02-10 14:58:52 -08002424 this.overlayTimeout_ = setTimeout(function() {
2425 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002426 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002427 if (self.overlayNode_.parentNode)
2428 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002429 self.overlayTimeout_ = null;
2430 self.overlayNode_.style.opacity = '0.75';
2431 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002432 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002433};
2434
rginda4bba5e12012-06-20 16:15:30 -07002435/**
2436 * Paste from the system clipboard to the terminal.
2437 */
2438hterm.Terminal.prototype.paste = function() {
2439 hterm.pasteFromClipboard(this.document_);
2440};
2441
2442/**
2443 * Copy a string to the system clipboard.
2444 *
2445 * Note: If there is a selected range in the terminal, it'll be cleared.
2446 */
2447hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002448 if (this.prefs_.get('enable-clipboard-notice'))
2449 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2450
rgindaa09e7332012-08-17 12:49:51 -07002451 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002452 copySource.textContent = str;
2453 copySource.style.cssText = (
2454 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002455 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002456 'position: absolute;' +
2457 'top: -99px');
2458
2459 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002460
rginda4bba5e12012-06-20 16:15:30 -07002461 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002462 var anchorNode = selection.anchorNode;
2463 var anchorOffset = selection.anchorOffset;
2464 var focusNode = selection.focusNode;
2465 var focusOffset = selection.focusOffset;
2466
rginda4bba5e12012-06-20 16:15:30 -07002467 selection.selectAllChildren(copySource);
2468
rgindaa09e7332012-08-17 12:49:51 -07002469 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002470
Rob Spies56953412014-04-28 14:09:47 -07002471 // IE doesn't support selection.extend. This means that the selection
2472 // won't return on IE.
2473 if (selection.extend) {
2474 selection.collapse(anchorNode, anchorOffset);
2475 selection.extend(focusNode, focusOffset);
2476 }
rgindafaa74742012-08-21 13:34:03 -07002477
rginda4bba5e12012-06-20 16:15:30 -07002478 copySource.parentNode.removeChild(copySource);
2479};
2480
rgindaa09e7332012-08-17 12:49:51 -07002481hterm.Terminal.prototype.getSelectionText = function() {
2482 var selection = this.scrollPort_.selection;
2483 selection.sync();
2484
2485 if (selection.isCollapsed)
2486 return null;
2487
2488
2489 // Start offset measures from the beginning of the line.
2490 var startOffset = selection.startOffset;
2491 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002492
Robert Gindafdbb3f22012-09-06 20:23:06 -07002493 if (node.nodeName != 'X-ROW') {
2494 // If the selection doesn't start on an x-row node, then it must be
2495 // somewhere inside the x-row. Add any characters from previous siblings
2496 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002497
2498 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2499 // If node is the text node in a styled span, move up to the span node.
2500 node = node.parentNode;
2501 }
2502
Robert Gindafdbb3f22012-09-06 20:23:06 -07002503 while (node.previousSibling) {
2504 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002505 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002506 }
rgindaa09e7332012-08-17 12:49:51 -07002507 }
2508
2509 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002510 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2511 selection.endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002512 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002513
Robert Gindafdbb3f22012-09-06 20:23:06 -07002514 if (node.nodeName != 'X-ROW') {
2515 // If the selection doesn't end on an x-row node, then it must be
2516 // somewhere inside the x-row. Add any characters from following siblings
2517 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002518
2519 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2520 // If node is the text node in a styled span, move up to the span node.
2521 node = node.parentNode;
2522 }
2523
Robert Gindafdbb3f22012-09-06 20:23:06 -07002524 while (node.nextSibling) {
2525 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002526 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002527 }
rgindaa09e7332012-08-17 12:49:51 -07002528 }
2529
2530 var rv = this.getRowsText(selection.startRow.rowIndex,
2531 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002532 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002533};
2534
rginda4bba5e12012-06-20 16:15:30 -07002535/**
2536 * Copy the current selection to the system clipboard, then clear it after a
2537 * short delay.
2538 */
2539hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002540 var text = this.getSelectionText();
2541 if (text != null)
2542 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002543};
2544
rgindaf0090c92012-02-10 14:58:52 -08002545hterm.Terminal.prototype.overlaySize = function() {
2546 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2547};
2548
rginda87b86462011-12-14 13:48:03 -08002549/**
2550 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2551 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002552 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002553 */
2554hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002555 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002556 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2557
Robert Ginda8cb7d902013-06-20 14:37:18 -07002558 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002559};
2560
2561/**
rgindad5613292012-06-19 15:40:37 -07002562 * Add the terminalRow and terminalColumn properties to mouse events and
2563 * then forward on to onMouse().
2564 *
2565 * The terminalRow and terminalColumn properties contain the (row, column)
2566 * coordinates for the mouse event.
2567 */
2568hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002569 if (e.processedByTerminalHandler_) {
2570 // We register our event handlers on the document, as well as the cursor
2571 // and the scroll blocker. Mouse events that occur on the cursor or
2572 // scroll blocker will also appear on the document, but we don't want to
2573 // process them twice.
2574 //
2575 // We can't just prevent bubbling because that has other side effects, so
2576 // we decorate the event object with this property instead.
2577 return;
2578 }
2579
2580 e.processedByTerminalHandler_ = true;
2581
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002582 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2583 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002584 return;
2585 }
2586
rgindad5613292012-06-19 15:40:37 -07002587 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2588 this.scrollPort_.characterSize.height) + 1;
2589 e.terminalColumn = parseInt(e.clientX /
2590 this.scrollPort_.characterSize.width) + 1;
2591
Robert Ginda928cf632014-03-05 15:07:41 -08002592 if (e.type == 'mousedown') {
2593 if (e.altKey || this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED) {
2594 // If VT mouse reporting is disabled, or has been defeated with
2595 // alt-mousedown, then the mouse will act on the local selection.
2596 this.reportMouseEvents_ = false;
2597 this.setSelectionEnabled(true);
2598 } else {
2599 // Otherwise we defer ownership of the mouse to the VT.
2600 this.reportMouseEvents_ = true;
2601 this.document_.getSelection().collapse();
2602 this.setSelectionEnabled(false);
2603 e.preventDefault();
2604 }
2605 }
2606
2607 if (!this.reportMouseEvents_) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002608 if (e.type == 'dblclick') {
2609 this.screen_.expandSelection(this.document_.getSelection());
2610 hterm.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07002611 }
2612
Robert Ginda928cf632014-03-05 15:07:41 -08002613 if (e.type == 'mousedown' && e.which == this.mousePasteButton)
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002614 this.paste();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002615
2616 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2617 !this.document_.getSelection().isCollapsed) {
2618 hterm.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002619 }
2620
2621 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
2622 this.scrollBlockerNode_.engaged) {
2623 // Disengage the scroll-blocker after one of these events.
2624 this.scrollBlockerNode_.engaged = false;
2625 this.scrollBlockerNode_.style.top = '-99px';
2626 }
2627
Robert Ginda928cf632014-03-05 15:07:41 -08002628 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002629 if (!this.scrollBlockerNode_.engaged) {
2630 if (e.type == 'mousedown') {
2631 // Move the scroll-blocker into place if we want to keep the scrollport
2632 // from scrolling.
2633 this.scrollBlockerNode_.engaged = true;
2634 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2635 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2636 } else if (e.type == 'mousemove') {
2637 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
2638 // in which case it's too late to engage the scroll-blocker.
2639 this.document_.getSelection().collapse();
2640 e.preventDefault();
2641 }
2642 }
Robert Ginda928cf632014-03-05 15:07:41 -08002643
2644 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002645 }
2646
Robert Ginda928cf632014-03-05 15:07:41 -08002647 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
2648 // Restore this on mouseup in case it was temporarily defeated with a
2649 // alt-mousedown. Only do this when the selection is empty so that
2650 // we don't immediately kill the users selection.
2651 this.reportMouseEvents_ = (this.vt.mouseReport !=
2652 this.vt.MOUSE_REPORT_DISABLED);
2653 }
rgindad5613292012-06-19 15:40:37 -07002654};
2655
2656/**
2657 * Clients should override this if they care to know about mouse events.
2658 *
2659 * The event parameter will be a normal DOM mouse click event with additional
2660 * 'terminalRow' and 'terminalColumn' properties.
2661 */
2662hterm.Terminal.prototype.onMouse = function(e) { };
2663
2664/**
rginda8e92a692012-05-20 19:37:20 -07002665 * React when focus changes.
2666 */
Rob Spies06533ba2014-04-24 11:20:37 -07002667hterm.Terminal.prototype.onFocusChange_ = function(focused) {
2668 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07002669 this.restyleCursor_();
rginda8e92a692012-05-20 19:37:20 -07002670};
2671
2672/**
rginda8ba33642011-12-14 12:31:31 -08002673 * React when the ScrollPort is scrolled.
2674 */
2675hterm.Terminal.prototype.onScroll_ = function() {
2676 this.scheduleSyncCursorPosition_();
2677};
2678
2679/**
rginda9846e2f2012-01-27 13:53:33 -08002680 * React when text is pasted into the scrollPort.
2681 */
2682hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Ginda8cb7d902013-06-20 14:37:18 -07002683 this.onVTKeystroke(e.text.replace(/\n/mg, '\r'));
rginda9846e2f2012-01-27 13:53:33 -08002684};
2685
2686/**
rgindaa09e7332012-08-17 12:49:51 -07002687 * React when the user tries to copy from the scrollPort.
2688 */
2689hterm.Terminal.prototype.onCopy_ = function(e) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002690 e.preventDefault();
2691 setTimeout(this.copySelectionToClipboard.bind(this), 0);
rgindaa09e7332012-08-17 12:49:51 -07002692};
2693
2694/**
rginda8ba33642011-12-14 12:31:31 -08002695 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002696 *
2697 * Note: This function should not directly contain code that alters the internal
2698 * state of the terminal. That kind of code belongs in realizeWidth or
2699 * realizeHeight, so that it can be executed synchronously in the case of a
2700 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002701 */
2702hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08002703 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002704 this.scrollPort_.characterSize.width);
Robert Ginda19f61292014-03-04 14:07:57 -08002705 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
rginda35c456b2012-02-09 17:29:05 -08002706 this.scrollPort_.characterSize.height);
2707
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002708 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002709 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002710 // gets removed from the document or during the initial load, and we can't
2711 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002712 return;
2713 }
2714
rgindaa8ba17d2012-08-15 14:41:10 -07002715 var isNewSize = (columnCount != this.screenSize.width ||
2716 rowCount != this.screenSize.height);
2717
2718 // We do this even if the size didn't change, just to be sure everything is
2719 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002720 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002721 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002722
2723 if (isNewSize)
2724 this.overlaySize();
2725
Robert Gindafb1be6a2013-12-11 11:56:22 -08002726 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07002727 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002728};
2729
2730/**
2731 * Service the cursor blink timeout.
2732 */
2733hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Ginda830583c2013-08-07 13:20:46 -07002734 if (this.cursorNode_.getAttribute('focus') == 'false' ||
2735 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08002736 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002737 } else {
rginda87b86462011-12-14 13:48:03 -08002738 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002739 }
2740};
David Reveman8f552492012-03-28 12:18:41 -04002741
2742/**
2743 * Set the scrollbar-visible mode bit.
2744 *
2745 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2746 * Otherwise it will not.
2747 *
2748 * Defaults to on.
2749 *
2750 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2751 */
2752hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2753 this.scrollPort_.setScrollbarVisible(state);
2754};