rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 5 | 'use strict'; |
| 6 | |
Masaya Suzuki | 273aa98 | 2014-05-31 07:25:55 +0900 | [diff] [blame] | 7 | lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource', 'lib.wc', |
Rob Spies | f4e90e8 | 2015-01-28 12:10:13 -0800 | [diff] [blame] | 8 | 'lib.f', 'hterm.Keyboard', 'hterm.Options', 'hterm.PreferenceManager', |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 9 | 'hterm.Screen', 'hterm.ScrollPort', 'hterm.Size', |
| 10 | 'hterm.TextAttributes', 'hterm.VT'); |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 11 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 12 | /** |
| 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). |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 28 | * |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 29 | * @param {string} opt_profileId Optional preference profile name. If not |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 30 | * provided, defaults to 'default'. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 31 | */ |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 32 | hterm.Terminal = function(opt_profileId) { |
| 33 | this.profileId_ = null; |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 34 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 35 | // 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 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 42 | // 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 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 48 | // The scroll port we'll be using to display the visible rows. |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 49 | this.scrollPort_ = new hterm.ScrollPort(this); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 50 | this.scrollPort_.subscribe('resize', this.onResize_.bind(this)); |
| 51 | this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this)); |
rginda | 9846e2f | 2012-01-27 13:53:33 -0800 | [diff] [blame] | 52 | this.scrollPort_.subscribe('paste', this.onPaste_.bind(this)); |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 53 | this.scrollPort_.onCopy = this.onCopy_.bind(this); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 54 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 55 | // The div that contains this terminal. |
| 56 | this.div_ = null; |
| 57 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 58 | // 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; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 62 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 63 | // The rows that have scrolled off screen and are no longer addressable. |
| 64 | this.scrollbackRows_ = []; |
| 65 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 66 | // Saved tab stops. |
| 67 | this.tabStops_ = []; |
| 68 | |
David Benjamin | 66e954d | 2012-05-05 21:08:12 -0400 | [diff] [blame] | 69 | // 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 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 73 | // 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 Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 81 | // 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 | |
Robert Ginda | ea2183e | 2014-07-17 09:51:51 -0700 | [diff] [blame] | 87 | // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded. |
| 88 | this.cursorBlinkCycle_ = [100, 100]; |
| 89 | |
| 90 | // Pre-bound onCursorBlink_ handler, so we don't have to do this for each |
| 91 | // cursor on/off servicing. |
| 92 | this.myOnCursorBlink_ = this.onCursorBlink_.bind(this); |
| 93 | |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 94 | // These prefs are cached so we don't have to read from local storage with |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 95 | // each output and keystroke. They are initialized by the preference manager. |
Robert Ginda | 8cb7d90 | 2013-06-20 14:37:18 -0700 | [diff] [blame] | 96 | this.backgroundColor_ = null; |
| 97 | this.foregroundColor_ = null; |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 98 | this.scrollOnOutput_ = null; |
| 99 | this.scrollOnKeystroke_ = null; |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 100 | |
Robert Ginda | 6aec7eb | 2015-06-16 10:31:30 -0700 | [diff] [blame] | 101 | // True if we should override mouse event reporting to allow local selection. |
| 102 | this.defeatMouseReports_ = false; |
Robert Ginda | 928cf63 | 2014-03-05 15:07:41 -0800 | [diff] [blame] | 103 | |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 104 | // Terminal bell sound. |
| 105 | this.bellAudio_ = this.document_.createElement('audio'); |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 106 | this.bellAudio_.setAttribute('preload', 'auto'); |
| 107 | |
Michael Kelly | 485ecd1 | 2014-06-09 11:41:56 -0400 | [diff] [blame] | 108 | // All terminal bell notifications that have been generated (not necessarily |
| 109 | // shown). |
| 110 | this.bellNotificationList_ = []; |
| 111 | |
| 112 | // Whether we have permission to display notifications. |
| 113 | this.desktopNotificationBell_ = false; |
Michael Kelly | 485ecd1 | 2014-06-09 11:41:56 -0400 | [diff] [blame] | 114 | |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 115 | // Cursor position and attributes saved with DECSC. |
| 116 | this.savedOptions_ = {}; |
| 117 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 118 | // The current mode bits for the terminal. |
| 119 | this.options_ = new hterm.Options(); |
| 120 | |
| 121 | // Timeouts we might need to clear. |
| 122 | this.timeouts_ = {}; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 123 | |
| 124 | // The VT escape sequence interpreter. |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 125 | this.vt = new hterm.VT(this); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 126 | |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame] | 127 | // The keyboard handler. |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 128 | this.keyboard = new hterm.Keyboard(this); |
| 129 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 130 | // General IO interface that can be given to third parties without exposing |
| 131 | // the entire terminal object. |
| 132 | this.io = new hterm.Terminal.IO(this); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 133 | |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 134 | // True if mouse-click-drag should scroll the terminal. |
| 135 | this.enableMouseDragScroll = true; |
| 136 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 137 | this.copyOnSelect = null; |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 138 | this.mousePasteButton = null; |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 139 | |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame] | 140 | // Whether to use the default window copy behavior. |
Rob Spies | 0bec09b | 2014-06-06 15:58:09 -0700 | [diff] [blame] | 141 | this.useDefaultWindowCopy = false; |
| 142 | |
| 143 | this.clearSelectionAfterCopy = true; |
| 144 | |
Dmitry Polukhin | bb2ef71 | 2012-01-19 19:00:37 +0400 | [diff] [blame] | 145 | this.realizeSize_(80, 24); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 146 | this.setDefaultTabStops(); |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 147 | |
| 148 | this.setProfile(opt_profileId || 'default', |
Evan Jones | 5f9df81 | 2016-12-06 09:38:58 -0500 | [diff] [blame] | 149 | function() { this.onTerminalReady(); }.bind(this)); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | /** |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 153 | * Possible cursor shapes. |
| 154 | */ |
| 155 | hterm.Terminal.cursorShape = { |
| 156 | BLOCK: 'BLOCK', |
| 157 | BEAM: 'BEAM', |
| 158 | UNDERLINE: 'UNDERLINE' |
| 159 | }; |
| 160 | |
| 161 | /** |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 162 | * Clients should override this to be notified when the terminal is ready |
| 163 | * for use. |
| 164 | * |
| 165 | * The terminal initialization is asynchronous, and shouldn't be used before |
| 166 | * this method is called. |
| 167 | */ |
| 168 | hterm.Terminal.prototype.onTerminalReady = function() { }; |
| 169 | |
| 170 | /** |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 171 | * Default tab with of 8 to match xterm. |
| 172 | */ |
| 173 | hterm.Terminal.prototype.tabWidth = 8; |
| 174 | |
| 175 | /** |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 176 | * Select a preference profile. |
| 177 | * |
| 178 | * This will load the terminal preferences for the given profile name and |
| 179 | * associate subsequent preference changes with the new preference profile. |
| 180 | * |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 181 | * @param {string} profileId The name of the preference profile. Forward slash |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 182 | * characters will be removed from the name. |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 183 | * @param {function} opt_callback Optional callback to invoke when the profile |
| 184 | * transition is complete. |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 185 | */ |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 186 | hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) { |
| 187 | this.profileId_ = profileId.replace(/\//g, ''); |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 188 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 189 | var terminal = this; |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 190 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 191 | if (this.prefs_) |
| 192 | this.prefs_.deactivate(); |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 193 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 194 | this.prefs_ = new hterm.PreferenceManager(this.profileId_); |
| 195 | this.prefs_.addObservers(null, { |
Robert Ginda | 034ffa7 | 2015-02-26 14:02:37 -0800 | [diff] [blame] | 196 | 'alt-gr-mode': function(v) { |
| 197 | if (v == null) { |
| 198 | if (navigator.language.toLowerCase() == 'en-us') { |
| 199 | v = 'none'; |
| 200 | } else { |
| 201 | v = 'right-alt'; |
| 202 | } |
| 203 | } else if (typeof v == 'string') { |
| 204 | v = v.toLowerCase(); |
| 205 | } else { |
| 206 | v = 'none'; |
| 207 | } |
| 208 | |
| 209 | if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v)) |
| 210 | v = 'none'; |
| 211 | |
| 212 | terminal.keyboard.altGrMode = v; |
| 213 | }, |
| 214 | |
Andrew de los Reyes | 574e10e | 2013-04-04 09:31:57 -0700 | [diff] [blame] | 215 | 'alt-backspace-is-meta-backspace': function(v) { |
| 216 | terminal.keyboard.altBackspaceIsMetaBackspace = v; |
| 217 | }, |
| 218 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 219 | 'alt-is-meta': function(v) { |
| 220 | terminal.keyboard.altIsMeta = v; |
| 221 | }, |
| 222 | |
| 223 | 'alt-sends-what': function(v) { |
| 224 | if (!/^(escape|8-bit|browser-key)$/.test(v)) |
| 225 | v = 'escape'; |
| 226 | |
| 227 | terminal.keyboard.altSendsWhat = v; |
| 228 | }, |
| 229 | |
| 230 | 'audible-bell-sound': function(v) { |
Robert Ginda | b4839c2 | 2013-02-28 16:52:10 -0800 | [diff] [blame] | 231 | var ary = v.match(/^lib-resource:(\S+)/); |
| 232 | if (ary) { |
| 233 | terminal.bellAudio_.setAttribute('src', |
| 234 | lib.resource.getDataUrl(ary[1])); |
| 235 | } else { |
| 236 | terminal.bellAudio_.setAttribute('src', v); |
| 237 | } |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 238 | }, |
| 239 | |
Michael Kelly | 485ecd1 | 2014-06-09 11:41:56 -0400 | [diff] [blame] | 240 | 'desktop-notification-bell': function(v) { |
| 241 | if (v && Notification) { |
Robert Ginda | 348dc2b | 2014-06-24 14:42:23 -0700 | [diff] [blame] | 242 | terminal.desktopNotificationBell_ = |
Michael Kelly | b806786 | 2014-06-26 12:59:47 -0400 | [diff] [blame] | 243 | Notification.permission === 'granted'; |
| 244 | if (!terminal.desktopNotificationBell_) { |
| 245 | // Note: We don't call Notification.requestPermission here because |
| 246 | // Chrome requires the call be the result of a user action (such as an |
| 247 | // onclick handler), and pref listeners are run asynchronously. |
| 248 | // |
| 249 | // A way of working around this would be to display a dialog in the |
| 250 | // terminal with a "click-to-request-permission" button. |
| 251 | console.warn('desktop-notification-bell is true but we do not have ' + |
| 252 | 'permission to display notifications.'); |
Michael Kelly | 485ecd1 | 2014-06-09 11:41:56 -0400 | [diff] [blame] | 253 | } |
| 254 | } else { |
| 255 | terminal.desktopNotificationBell_ = false; |
| 256 | } |
| 257 | }, |
| 258 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 259 | 'background-color': function(v) { |
| 260 | terminal.setBackgroundColor(v); |
| 261 | }, |
| 262 | |
| 263 | 'background-image': function(v) { |
| 264 | terminal.scrollPort_.setBackgroundImage(v); |
| 265 | }, |
| 266 | |
| 267 | 'background-size': function(v) { |
| 268 | terminal.scrollPort_.setBackgroundSize(v); |
| 269 | }, |
| 270 | |
| 271 | 'background-position': function(v) { |
| 272 | terminal.scrollPort_.setBackgroundPosition(v); |
| 273 | }, |
| 274 | |
| 275 | 'backspace-sends-backspace': function(v) { |
| 276 | terminal.keyboard.backspaceSendsBackspace = v; |
| 277 | }, |
| 278 | |
Brad Town | 18654b6 | 2015-03-12 00:27:45 -0700 | [diff] [blame] | 279 | 'character-map-overrides': function(v) { |
| 280 | if (!(v == null || v instanceof Object)) { |
| 281 | console.warn('Preference character-map-modifications is not an ' + |
| 282 | 'object: ' + v); |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | for (var code in v) { |
| 287 | var glmap = hterm.VT.CharacterMap.maps[code].glmap; |
| 288 | for (var received in v[code]) { |
| 289 | glmap[received] = v[code][received]; |
| 290 | } |
| 291 | hterm.VT.CharacterMap.maps[code].reset(glmap); |
| 292 | } |
| 293 | }, |
| 294 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 295 | 'cursor-blink': function(v) { |
| 296 | terminal.setCursorBlink(!!v); |
| 297 | }, |
| 298 | |
Robert Ginda | ea2183e | 2014-07-17 09:51:51 -0700 | [diff] [blame] | 299 | 'cursor-blink-cycle': function(v) { |
| 300 | if (v instanceof Array && |
| 301 | typeof v[0] == 'number' && |
| 302 | typeof v[1] == 'number') { |
| 303 | terminal.cursorBlinkCycle_ = v; |
| 304 | } else if (typeof v == 'number') { |
| 305 | terminal.cursorBlinkCycle_ = [v, v]; |
| 306 | } else { |
| 307 | // Fast blink indicates an error. |
| 308 | terminal.cursorBlinkCycle_ = [100, 100]; |
| 309 | } |
| 310 | }, |
| 311 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 312 | 'cursor-color': function(v) { |
| 313 | terminal.setCursorColor(v); |
| 314 | }, |
| 315 | |
| 316 | 'color-palette-overrides': function(v) { |
| 317 | if (!(v == null || v instanceof Object || v instanceof Array)) { |
| 318 | console.warn('Preference color-palette-overrides is not an array or ' + |
| 319 | 'object: ' + v); |
| 320 | return; |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 321 | } |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 322 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 323 | lib.colors.colorPalette = lib.colors.stockColorPalette.concat(); |
rginda | 39bdf6f | 2012-04-10 16:50:55 -0700 | [diff] [blame] | 324 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 325 | if (v) { |
| 326 | for (var key in v) { |
| 327 | var i = parseInt(key); |
| 328 | if (isNaN(i) || i < 0 || i > 255) { |
| 329 | console.log('Invalid value in palette: ' + key + ': ' + v[key]); |
| 330 | continue; |
| 331 | } |
| 332 | |
| 333 | if (v[i]) { |
| 334 | var rgb = lib.colors.normalizeCSS(v[i]); |
| 335 | if (rgb) |
| 336 | lib.colors.colorPalette[i] = rgb; |
| 337 | } |
| 338 | } |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 339 | } |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 340 | |
Evan Jones | 5f9df81 | 2016-12-06 09:38:58 -0500 | [diff] [blame] | 341 | terminal.primaryScreen_.textAttributes.resetColorPalette(); |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 342 | terminal.alternateScreen_.textAttributes.resetColorPalette(); |
| 343 | }, |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 344 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 345 | 'copy-on-select': function(v) { |
| 346 | terminal.copyOnSelect = !!v; |
| 347 | }, |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 348 | |
Rob Spies | 0bec09b | 2014-06-06 15:58:09 -0700 | [diff] [blame] | 349 | 'use-default-window-copy': function(v) { |
| 350 | terminal.useDefaultWindowCopy = !!v; |
| 351 | }, |
| 352 | |
| 353 | 'clear-selection-after-copy': function(v) { |
| 354 | terminal.clearSelectionAfterCopy = !!v; |
| 355 | }, |
| 356 | |
Robert Ginda | 7e5e952 | 2014-03-14 12:23:58 -0700 | [diff] [blame] | 357 | 'ctrl-plus-minus-zero-zoom': function(v) { |
| 358 | terminal.keyboard.ctrlPlusMinusZeroZoom = v; |
| 359 | }, |
| 360 | |
Robert Ginda | fb5a3f9 | 2014-05-13 14:12:00 -0700 | [diff] [blame] | 361 | 'ctrl-c-copy': function(v) { |
| 362 | terminal.keyboard.ctrlCCopy = v; |
| 363 | }, |
| 364 | |
Leonardo Mesquita | 61e7c31 | 2014-01-04 12:53:12 +0100 | [diff] [blame] | 365 | 'ctrl-v-paste': function(v) { |
| 366 | terminal.keyboard.ctrlVPaste = v; |
Rob Spies | e52e184 | 2014-07-10 15:32:51 -0700 | [diff] [blame] | 367 | terminal.scrollPort_.setCtrlVPaste(v); |
Leonardo Mesquita | 61e7c31 | 2014-01-04 12:53:12 +0100 | [diff] [blame] | 368 | }, |
| 369 | |
Masaya Suzuki | 273aa98 | 2014-05-31 07:25:55 +0900 | [diff] [blame] | 370 | 'east-asian-ambiguous-as-two-column': function(v) { |
| 371 | lib.wc.regardCjkAmbiguous = v; |
| 372 | }, |
| 373 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 374 | 'enable-8-bit-control': function(v) { |
| 375 | terminal.vt.enable8BitControl = !!v; |
| 376 | }, |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 377 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 378 | 'enable-bold': function(v) { |
| 379 | terminal.syncBoldSafeState(); |
| 380 | }, |
Philip Douglass | 959b49d | 2012-05-30 13:29:29 -0400 | [diff] [blame] | 381 | |
Robert Ginda | 3e278d7 | 2014-03-25 13:18:51 -0700 | [diff] [blame] | 382 | 'enable-bold-as-bright': function(v) { |
| 383 | terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v; |
| 384 | terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v; |
| 385 | }, |
| 386 | |
Mike Frysinger | 93b75ba | 2017-04-05 19:43:18 -0400 | [diff] [blame] | 387 | 'enable-blink': function(v) { |
| 388 | terminal.syncBlinkState(); |
| 389 | }, |
| 390 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 391 | 'enable-clipboard-write': function(v) { |
| 392 | terminal.vt.enableClipboardWrite = !!v; |
| 393 | }, |
Philip Douglass | 959b49d | 2012-05-30 13:29:29 -0400 | [diff] [blame] | 394 | |
Robert Ginda | 3755e75 | 2013-05-31 13:34:09 -0700 | [diff] [blame] | 395 | 'enable-dec12': function(v) { |
| 396 | terminal.vt.enableDec12 = !!v; |
| 397 | }, |
| 398 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 399 | 'font-family': function(v) { |
| 400 | terminal.syncFontFamily(); |
| 401 | }, |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 402 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 403 | 'font-size': function(v) { |
| 404 | terminal.setFontSize(v); |
| 405 | }, |
rginda | 9875d90 | 2012-08-20 16:21:57 -0700 | [diff] [blame] | 406 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 407 | 'font-smoothing': function(v) { |
| 408 | terminal.syncFontFamily(); |
| 409 | }, |
rginda | de84e38 | 2012-04-20 15:39:31 -0700 | [diff] [blame] | 410 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 411 | 'foreground-color': function(v) { |
| 412 | terminal.setForegroundColor(v); |
| 413 | }, |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 414 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 415 | 'home-keys-scroll': function(v) { |
| 416 | terminal.keyboard.homeKeysScroll = v; |
| 417 | }, |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 418 | |
Robert Ginda | a816569 | 2015-06-15 14:46:31 -0700 | [diff] [blame] | 419 | 'keybindings': function(v) { |
| 420 | terminal.keyboard.bindings.clear(); |
| 421 | |
| 422 | if (!v) |
| 423 | return; |
| 424 | |
| 425 | if (!(v instanceof Object)) { |
| 426 | console.error('Error in keybindings preference: Expected object'); |
| 427 | return; |
| 428 | } |
| 429 | |
| 430 | try { |
| 431 | terminal.keyboard.bindings.addBindings(v); |
| 432 | } catch (ex) { |
| 433 | console.error('Error in keybindings preference: ' + ex); |
| 434 | } |
| 435 | }, |
| 436 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 437 | 'max-string-sequence': function(v) { |
| 438 | terminal.vt.maxStringSequence = v; |
| 439 | }, |
rginda | 11057d5 | 2012-04-25 12:29:56 -0700 | [diff] [blame] | 440 | |
Andrew de los Reyes | 6af23ae | 2013-04-04 14:17:50 -0700 | [diff] [blame] | 441 | 'media-keys-are-fkeys': function(v) { |
| 442 | terminal.keyboard.mediaKeysAreFKeys = v; |
| 443 | }, |
| 444 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 445 | 'meta-sends-escape': function(v) { |
| 446 | terminal.keyboard.metaSendsEscape = v; |
| 447 | }, |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 448 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 449 | 'mouse-paste-button': function(v) { |
| 450 | terminal.syncMousePasteButton(); |
| 451 | }, |
rginda | a8ba17d | 2012-08-15 14:41:10 -0700 | [diff] [blame] | 452 | |
Robert Ginda | e76aa9f | 2014-03-14 12:29:12 -0700 | [diff] [blame] | 453 | 'page-keys-scroll': function(v) { |
| 454 | terminal.keyboard.pageKeysScroll = v; |
| 455 | }, |
| 456 | |
Robert Ginda | 4093289 | 2012-12-10 17:26:40 -0800 | [diff] [blame] | 457 | 'pass-alt-number': function(v) { |
| 458 | if (v == null) { |
| 459 | var osx = window.navigator.userAgent.match(/Mac OS X/); |
| 460 | |
| 461 | // Let Alt-1..9 pass to the browser (to control tab switching) on |
| 462 | // non-OS X systems, or if hterm is not opened in an app window. |
| 463 | v = (!osx && hterm.windowType != 'popup'); |
| 464 | } |
| 465 | |
| 466 | terminal.passAltNumber = v; |
| 467 | }, |
| 468 | |
| 469 | 'pass-ctrl-number': function(v) { |
| 470 | if (v == null) { |
| 471 | var osx = window.navigator.userAgent.match(/Mac OS X/); |
| 472 | |
| 473 | // Let Ctrl-1..9 pass to the browser (to control tab switching) on |
| 474 | // non-OS X systems, or if hterm is not opened in an app window. |
| 475 | v = (!osx && hterm.windowType != 'popup'); |
| 476 | } |
| 477 | |
| 478 | terminal.passCtrlNumber = v; |
| 479 | }, |
| 480 | |
| 481 | 'pass-meta-number': function(v) { |
| 482 | if (v == null) { |
| 483 | var osx = window.navigator.userAgent.match(/Mac OS X/); |
| 484 | |
| 485 | // Let Meta-1..9 pass to the browser (to control tab switching) on |
| 486 | // OS X systems, or if hterm is not opened in an app window. |
| 487 | v = (osx && hterm.windowType != 'popup'); |
| 488 | } |
| 489 | |
| 490 | terminal.passMetaNumber = v; |
| 491 | }, |
| 492 | |
Marius Schilder | 77857b3 | 2014-05-14 16:21:26 -0700 | [diff] [blame] | 493 | 'pass-meta-v': function(v) { |
Marius Schilder | 1a56781 | 2014-05-15 20:30:02 -0700 | [diff] [blame] | 494 | terminal.keyboard.passMetaV = v; |
Marius Schilder | 77857b3 | 2014-05-14 16:21:26 -0700 | [diff] [blame] | 495 | }, |
| 496 | |
Robert Ginda | 8cb7d90 | 2013-06-20 14:37:18 -0700 | [diff] [blame] | 497 | 'receive-encoding': function(v) { |
| 498 | if (!(/^(utf-8|raw)$/).test(v)) { |
| 499 | console.warn('Invalid value for "receive-encoding": ' + v); |
| 500 | v = 'utf-8'; |
| 501 | } |
| 502 | |
| 503 | terminal.vt.characterEncoding = v; |
| 504 | }, |
| 505 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 506 | 'scroll-on-keystroke': function(v) { |
| 507 | terminal.scrollOnKeystroke_ = v; |
| 508 | }, |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 509 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 510 | 'scroll-on-output': function(v) { |
| 511 | terminal.scrollOnOutput_ = v; |
| 512 | }, |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 513 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 514 | 'scrollbar-visible': function(v) { |
| 515 | terminal.setScrollbarVisible(v); |
| 516 | }, |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 517 | |
Rob Spies | 49039e5 | 2014-12-17 13:40:04 -0800 | [diff] [blame] | 518 | 'scroll-wheel-move-multiplier': function(v) { |
| 519 | terminal.setScrollWheelMoveMultipler(v); |
| 520 | }, |
| 521 | |
Robert Ginda | 8cb7d90 | 2013-06-20 14:37:18 -0700 | [diff] [blame] | 522 | 'send-encoding': function(v) { |
| 523 | if (!(/^(utf-8|raw)$/).test(v)) { |
| 524 | console.warn('Invalid value for "send-encoding": ' + v); |
| 525 | v = 'utf-8'; |
| 526 | } |
| 527 | |
| 528 | terminal.keyboard.characterEncoding = v; |
| 529 | }, |
| 530 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 531 | 'shift-insert-paste': function(v) { |
| 532 | terminal.keyboard.shiftInsertPaste = v; |
| 533 | }, |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 534 | |
Robert Ginda | e76aa9f | 2014-03-14 12:29:12 -0700 | [diff] [blame] | 535 | 'user-css': function(v) { |
Mike Frysinger | 08bad43 | 2017-04-24 00:50:54 -0400 | [diff] [blame] | 536 | terminal.scrollPort_.setUserCssUrl(v); |
| 537 | }, |
| 538 | |
| 539 | 'user-css-text': function(v) { |
| 540 | terminal.scrollPort_.setUserCssText(v); |
| 541 | }, |
Mike Frysinger | 664e999 | 2017-05-19 01:24:24 -0400 | [diff] [blame^] | 542 | |
| 543 | 'word-break-match-left': function(v) { |
| 544 | terminal.primaryScreen_.wordBreakMatchLeft = v; |
| 545 | terminal.alternateScreen_.wordBreakMatchLeft = v; |
| 546 | }, |
| 547 | |
| 548 | 'word-break-match-right': function(v) { |
| 549 | terminal.primaryScreen_.wordBreakMatchRight = v; |
| 550 | terminal.alternateScreen_.wordBreakMatchRight = v; |
| 551 | }, |
| 552 | |
| 553 | 'word-break-match-middle': function(v) { |
| 554 | terminal.primaryScreen_.wordBreakMatchMiddle = v; |
| 555 | terminal.alternateScreen_.wordBreakMatchMiddle = v; |
| 556 | }, |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 557 | }); |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 558 | |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 559 | this.prefs_.readStorage(function() { |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 560 | this.prefs_.notifyAll(); |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 561 | |
| 562 | if (opt_callback) |
| 563 | opt_callback(); |
| 564 | }.bind(this)); |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 565 | }; |
| 566 | |
Rob Spies | 5695341 | 2014-04-28 14:09:47 -0700 | [diff] [blame] | 567 | |
| 568 | /** |
| 569 | * Returns the preferences manager used for configuring this terminal. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 570 | * |
| 571 | * @return {hterm.PreferenceManager} |
Rob Spies | 5695341 | 2014-04-28 14:09:47 -0700 | [diff] [blame] | 572 | */ |
| 573 | hterm.Terminal.prototype.getPrefs = function() { |
| 574 | return this.prefs_; |
| 575 | }; |
| 576 | |
Robert Ginda | a063b20 | 2014-07-21 11:08:25 -0700 | [diff] [blame] | 577 | /** |
| 578 | * Enable or disable bracketed paste mode. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 579 | * |
| 580 | * @param {boolean} state The value to set. |
Robert Ginda | a063b20 | 2014-07-21 11:08:25 -0700 | [diff] [blame] | 581 | */ |
| 582 | hterm.Terminal.prototype.setBracketedPaste = function(state) { |
| 583 | this.options_.bracketedPaste = state; |
| 584 | }; |
Rob Spies | 5695341 | 2014-04-28 14:09:47 -0700 | [diff] [blame] | 585 | |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 586 | /** |
| 587 | * Set the color for the cursor. |
| 588 | * |
| 589 | * If you want this setting to persist, set it through prefs_, rather than |
| 590 | * with this method. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 591 | * |
| 592 | * @param {string} color The color to set. |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 593 | */ |
| 594 | hterm.Terminal.prototype.setCursorColor = function(color) { |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 595 | this.cursorColor_ = color; |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 596 | this.cursorNode_.style.backgroundColor = color; |
| 597 | this.cursorNode_.style.borderColor = color; |
| 598 | }; |
| 599 | |
| 600 | /** |
| 601 | * Return the current cursor color as a string. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 602 | * @return {string} |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 603 | */ |
| 604 | hterm.Terminal.prototype.getCursorColor = function() { |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 605 | return this.cursorColor_; |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 606 | }; |
| 607 | |
| 608 | /** |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 609 | * Enable or disable mouse based text selection in the terminal. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 610 | * |
| 611 | * @param {boolean} state The value to set. |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 612 | */ |
| 613 | hterm.Terminal.prototype.setSelectionEnabled = function(state) { |
| 614 | this.enableMouseDragScroll = state; |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 615 | }; |
| 616 | |
| 617 | /** |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 618 | * Set the background color. |
| 619 | * |
| 620 | * If you want this setting to persist, set it through prefs_, rather than |
| 621 | * with this method. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 622 | * |
| 623 | * @param {string} color The color to set. |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 624 | */ |
| 625 | hterm.Terminal.prototype.setBackgroundColor = function(color) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 626 | this.backgroundColor_ = lib.colors.normalizeCSS(color); |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 627 | this.primaryScreen_.textAttributes.setDefaults( |
| 628 | this.foregroundColor_, this.backgroundColor_); |
| 629 | this.alternateScreen_.textAttributes.setDefaults( |
| 630 | this.foregroundColor_, this.backgroundColor_); |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 631 | this.scrollPort_.setBackgroundColor(color); |
| 632 | }; |
| 633 | |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 634 | /** |
| 635 | * Return the current terminal background color. |
| 636 | * |
| 637 | * Intended for use by other classes, so we don't have to expose the entire |
| 638 | * prefs_ object. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 639 | * |
| 640 | * @return {string} |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 641 | */ |
| 642 | hterm.Terminal.prototype.getBackgroundColor = function() { |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 643 | return this.backgroundColor_; |
| 644 | }; |
| 645 | |
| 646 | /** |
| 647 | * Set the foreground color. |
| 648 | * |
| 649 | * If you want this setting to persist, set it through prefs_, rather than |
| 650 | * with this method. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 651 | * |
| 652 | * @param {string} color The color to set. |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 653 | */ |
| 654 | hterm.Terminal.prototype.setForegroundColor = function(color) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 655 | this.foregroundColor_ = lib.colors.normalizeCSS(color); |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 656 | this.primaryScreen_.textAttributes.setDefaults( |
| 657 | this.foregroundColor_, this.backgroundColor_); |
| 658 | this.alternateScreen_.textAttributes.setDefaults( |
| 659 | this.foregroundColor_, this.backgroundColor_); |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 660 | this.scrollPort_.setForegroundColor(color); |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 661 | }; |
| 662 | |
| 663 | /** |
| 664 | * Return the current terminal foreground color. |
| 665 | * |
| 666 | * Intended for use by other classes, so we don't have to expose the entire |
| 667 | * prefs_ object. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 668 | * |
| 669 | * @return {string} |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 670 | */ |
| 671 | hterm.Terminal.prototype.getForegroundColor = function() { |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 672 | return this.foregroundColor_; |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 673 | }; |
| 674 | |
| 675 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 676 | * Create a new instance of a terminal command and run it with a given |
| 677 | * argument string. |
| 678 | * |
| 679 | * @param {function} commandClass The constructor for a terminal command. |
| 680 | * @param {string} argString The argument string to pass to the command. |
| 681 | */ |
| 682 | hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) { |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 683 | var environment = this.prefs_.get('environment'); |
| 684 | if (typeof environment != 'object' || environment == null) |
| 685 | environment = {}; |
| 686 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 687 | var self = this; |
| 688 | this.command = new commandClass( |
| 689 | { argString: argString || '', |
| 690 | io: this.io.push(), |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 691 | environment: environment, |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 692 | onExit: function(code) { |
| 693 | self.io.pop(); |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 694 | self.uninstallKeyboard(); |
rginda | 9875d90 | 2012-08-20 16:21:57 -0700 | [diff] [blame] | 695 | if (self.prefs_.get('close-on-exit')) |
| 696 | window.close(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 697 | } |
| 698 | }); |
| 699 | |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 700 | this.installKeyboard(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 701 | this.command.run(); |
| 702 | }; |
| 703 | |
| 704 | /** |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 705 | * Returns true if the current screen is the primary screen, false otherwise. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 706 | * |
| 707 | * @return {boolean} |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 708 | */ |
| 709 | hterm.Terminal.prototype.isPrimaryScreen = function() { |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 710 | return this.screen_ == this.primaryScreen_; |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 711 | }; |
| 712 | |
| 713 | /** |
| 714 | * Install the keyboard handler for this terminal. |
| 715 | * |
| 716 | * This will prevent the browser from seeing any keystrokes sent to the |
| 717 | * terminal. |
| 718 | */ |
| 719 | hterm.Terminal.prototype.installKeyboard = function() { |
Rob Spies | 06533ba | 2014-04-24 11:20:37 -0700 | [diff] [blame] | 720 | this.keyboard.installKeyboard(this.scrollPort_.getDocument().body); |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | /** |
| 724 | * Uninstall the keyboard handler for this terminal. |
| 725 | */ |
| 726 | hterm.Terminal.prototype.uninstallKeyboard = function() { |
| 727 | this.keyboard.installKeyboard(null); |
| 728 | } |
| 729 | |
| 730 | /** |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 731 | * Set the font size for this terminal. |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 732 | * |
| 733 | * Call setFontSize(0) to reset to the default font size. |
| 734 | * |
| 735 | * This function does not modify the font-size preference. |
| 736 | * |
| 737 | * @param {number} px The desired font size, in pixels. |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 738 | */ |
| 739 | hterm.Terminal.prototype.setFontSize = function(px) { |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 740 | if (px === 0) |
| 741 | px = this.prefs_.get('font-size'); |
| 742 | |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 743 | this.scrollPort_.setFontSize(px); |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 744 | if (this.wcCssRule_) { |
| 745 | this.wcCssRule_.style.width = this.scrollPort_.characterSize.width * 2 + |
| 746 | 'px'; |
| 747 | } |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 748 | }; |
| 749 | |
| 750 | /** |
| 751 | * Get the current font size. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 752 | * |
| 753 | * @return {number} |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 754 | */ |
| 755 | hterm.Terminal.prototype.getFontSize = function() { |
| 756 | return this.scrollPort_.getFontSize(); |
| 757 | }; |
| 758 | |
| 759 | /** |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 760 | * Get the current font family. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 761 | * |
| 762 | * @return {string} |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 763 | */ |
| 764 | hterm.Terminal.prototype.getFontFamily = function() { |
| 765 | return this.scrollPort_.getFontFamily(); |
| 766 | }; |
| 767 | |
| 768 | /** |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 769 | * Set the CSS "font-family" for this terminal. |
| 770 | */ |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 771 | hterm.Terminal.prototype.syncFontFamily = function() { |
| 772 | this.scrollPort_.setFontFamily(this.prefs_.get('font-family'), |
| 773 | this.prefs_.get('font-smoothing')); |
| 774 | this.syncBoldSafeState(); |
| 775 | }; |
| 776 | |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 777 | /** |
| 778 | * Set this.mousePasteButton based on the mouse-paste-button pref, |
| 779 | * autodetecting if necessary. |
| 780 | */ |
| 781 | hterm.Terminal.prototype.syncMousePasteButton = function() { |
| 782 | var button = this.prefs_.get('mouse-paste-button'); |
| 783 | if (typeof button == 'number') { |
| 784 | this.mousePasteButton = button; |
| 785 | return; |
| 786 | } |
| 787 | |
| 788 | var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/); |
Mike Frysinger | 98dd15b | 2017-05-18 22:52:23 -0400 | [diff] [blame] | 789 | if (!ary || ary[1] == 'CrOS') { |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 790 | this.mousePasteButton = 2; |
| 791 | } else { |
| 792 | this.mousePasteButton = 3; |
| 793 | } |
| 794 | }; |
| 795 | |
| 796 | /** |
| 797 | * Enable or disable bold based on the enable-bold pref, autodetecting if |
| 798 | * necessary. |
| 799 | */ |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 800 | hterm.Terminal.prototype.syncBoldSafeState = function() { |
| 801 | var enableBold = this.prefs_.get('enable-bold'); |
| 802 | if (enableBold !== null) { |
Robert Ginda | ed01626 | 2012-10-26 16:27:09 -0700 | [diff] [blame] | 803 | this.primaryScreen_.textAttributes.enableBold = enableBold; |
| 804 | this.alternateScreen_.textAttributes.enableBold = enableBold; |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 805 | return; |
| 806 | } |
| 807 | |
rginda | f752139 | 2012-02-28 17:20:34 -0800 | [diff] [blame] | 808 | var normalSize = this.scrollPort_.measureCharacterSize(); |
| 809 | var boldSize = this.scrollPort_.measureCharacterSize('bold'); |
| 810 | |
| 811 | var isBoldSafe = normalSize.equals(boldSize); |
rginda | f752139 | 2012-02-28 17:20:34 -0800 | [diff] [blame] | 812 | if (!isBoldSafe) { |
| 813 | console.warn('Bold characters disabled: Size of bold weight differs ' + |
rginda | c9759de | 2012-03-19 13:21:41 -0700 | [diff] [blame] | 814 | 'from normal. Font family is: ' + |
| 815 | this.scrollPort_.getFontFamily()); |
rginda | f752139 | 2012-02-28 17:20:34 -0800 | [diff] [blame] | 816 | } |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 817 | |
Robert Ginda | ed01626 | 2012-10-26 16:27:09 -0700 | [diff] [blame] | 818 | this.primaryScreen_.textAttributes.enableBold = isBoldSafe; |
| 819 | this.alternateScreen_.textAttributes.enableBold = isBoldSafe; |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 820 | }; |
| 821 | |
| 822 | /** |
Mike Frysinger | 93b75ba | 2017-04-05 19:43:18 -0400 | [diff] [blame] | 823 | * Enable or disable blink based on the enable-blink pref. |
| 824 | */ |
| 825 | hterm.Terminal.prototype.syncBlinkState = function() { |
| 826 | this.document_.documentElement.style.setProperty( |
| 827 | '--hterm-blink-node-duration', |
| 828 | this.prefs_.get('enable-blink') ? '0.7s' : '0'); |
| 829 | }; |
| 830 | |
| 831 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 832 | * Return a copy of the current cursor position. |
| 833 | * |
| 834 | * @return {hterm.RowCol} The RowCol object representing the current position. |
| 835 | */ |
| 836 | hterm.Terminal.prototype.saveCursor = function() { |
| 837 | return this.screen_.cursorPosition.clone(); |
| 838 | }; |
| 839 | |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 840 | /** |
| 841 | * Return the current text attributes. |
| 842 | * |
| 843 | * @return {string} |
| 844 | */ |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 845 | hterm.Terminal.prototype.getTextAttributes = function() { |
| 846 | return this.screen_.textAttributes; |
| 847 | }; |
| 848 | |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 849 | /** |
| 850 | * Set the text attributes. |
| 851 | * |
| 852 | * @param {string} textAttributes The attributes to set. |
| 853 | */ |
rginda | 1a09aa0 | 2012-06-18 21:11:25 -0700 | [diff] [blame] | 854 | hterm.Terminal.prototype.setTextAttributes = function(textAttributes) { |
| 855 | this.screen_.textAttributes = textAttributes; |
| 856 | }; |
| 857 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 858 | /** |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 859 | * Return the current browser zoom factor applied to the terminal. |
| 860 | * |
| 861 | * @return {number} The current browser zoom factor. |
| 862 | */ |
| 863 | hterm.Terminal.prototype.getZoomFactor = function() { |
| 864 | return this.scrollPort_.characterSize.zoomFactor; |
| 865 | }; |
| 866 | |
| 867 | /** |
rginda | 9846e2f | 2012-01-27 13:53:33 -0800 | [diff] [blame] | 868 | * Change the title of this terminal's window. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 869 | * |
| 870 | * @param {string} title The title to set. |
rginda | 9846e2f | 2012-01-27 13:53:33 -0800 | [diff] [blame] | 871 | */ |
| 872 | hterm.Terminal.prototype.setWindowTitle = function(title) { |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 873 | window.document.title = title; |
rginda | 9846e2f | 2012-01-27 13:53:33 -0800 | [diff] [blame] | 874 | }; |
| 875 | |
| 876 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 877 | * Restore a previously saved cursor position. |
| 878 | * |
| 879 | * @param {hterm.RowCol} cursor The position to restore. |
| 880 | */ |
| 881 | hterm.Terminal.prototype.restoreCursor = function(cursor) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 882 | var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1); |
| 883 | var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1); |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 884 | this.screen_.setCursorPosition(row, column); |
| 885 | if (cursor.column > column || |
| 886 | cursor.column == column && cursor.overflow) { |
| 887 | this.screen_.cursorPosition.overflow = true; |
| 888 | } |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 889 | }; |
| 890 | |
| 891 | /** |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 892 | * Clear the cursor's overflow flag. |
| 893 | */ |
| 894 | hterm.Terminal.prototype.clearCursorOverflow = function() { |
| 895 | this.screen_.cursorPosition.overflow = false; |
| 896 | }; |
| 897 | |
| 898 | /** |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 899 | * Sets the cursor shape |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 900 | * |
| 901 | * @param {string} shape The shape to set. |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 902 | */ |
| 903 | hterm.Terminal.prototype.setCursorShape = function(shape) { |
| 904 | this.cursorShape_ = shape; |
Robert Ginda | fb1be6a | 2013-12-11 11:56:22 -0800 | [diff] [blame] | 905 | this.restyleCursor_(); |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | /** |
| 909 | * Get the cursor shape |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 910 | * |
| 911 | * @return {string} |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 912 | */ |
| 913 | hterm.Terminal.prototype.getCursorShape = function() { |
| 914 | return this.cursorShape_; |
| 915 | } |
| 916 | |
| 917 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 918 | * Set the width of the terminal, resizing the UI to match. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 919 | * |
| 920 | * @param {number} columnCount |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 921 | */ |
| 922 | hterm.Terminal.prototype.setWidth = function(columnCount) { |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 923 | if (columnCount == null) { |
| 924 | this.div_.style.width = '100%'; |
| 925 | return; |
| 926 | } |
| 927 | |
Robert Ginda | 26806d1 | 2014-07-24 13:44:07 -0700 | [diff] [blame] | 928 | this.div_.style.width = Math.ceil( |
| 929 | this.scrollPort_.characterSize.width * |
| 930 | columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px'; |
Dmitry Polukhin | bb2ef71 | 2012-01-19 19:00:37 +0400 | [diff] [blame] | 931 | this.realizeSize_(columnCount, this.screenSize.height); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 932 | this.scheduleSyncCursorPosition_(); |
| 933 | }; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 934 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 935 | /** |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 936 | * Set the height of the terminal, resizing the UI to match. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 937 | * |
| 938 | * @param {number} rowCount The height in rows. |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 939 | */ |
| 940 | hterm.Terminal.prototype.setHeight = function(rowCount) { |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 941 | if (rowCount == null) { |
| 942 | this.div_.style.height = '100%'; |
| 943 | return; |
| 944 | } |
| 945 | |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 946 | this.div_.style.height = |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 947 | this.scrollPort_.characterSize.height * rowCount + 'px'; |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 948 | this.realizeSize_(this.screenSize.width, rowCount); |
| 949 | this.scheduleSyncCursorPosition_(); |
| 950 | }; |
| 951 | |
| 952 | /** |
Dmitry Polukhin | bb2ef71 | 2012-01-19 19:00:37 +0400 | [diff] [blame] | 953 | * Deal with terminal size changes. |
| 954 | * |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 955 | * @param {number} columnCount The number of columns. |
| 956 | * @param {number} rowCount The number of rows. |
Dmitry Polukhin | bb2ef71 | 2012-01-19 19:00:37 +0400 | [diff] [blame] | 957 | */ |
| 958 | hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) { |
| 959 | if (columnCount != this.screenSize.width) |
| 960 | this.realizeWidth_(columnCount); |
| 961 | |
| 962 | if (rowCount != this.screenSize.height) |
| 963 | this.realizeHeight_(rowCount); |
| 964 | |
| 965 | // Send new terminal size to plugin. |
Robert Ginda | e81427f | 2013-05-24 10:34:46 -0700 | [diff] [blame] | 966 | this.io.onTerminalResize_(columnCount, rowCount); |
Dmitry Polukhin | bb2ef71 | 2012-01-19 19:00:37 +0400 | [diff] [blame] | 967 | }; |
| 968 | |
| 969 | /** |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 970 | * Deal with terminal width changes. |
| 971 | * |
| 972 | * This function does what needs to be done when the terminal width changes |
| 973 | * out from under us. It happens here rather than in onResize_() because this |
| 974 | * code may need to run synchronously to handle programmatic changes of |
| 975 | * terminal width. |
| 976 | * |
| 977 | * Relying on the browser to send us an async resize event means we may not be |
| 978 | * in the correct state yet when the next escape sequence hits. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 979 | * |
| 980 | * @param {number} columnCount The number of columns. |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 981 | */ |
| 982 | hterm.Terminal.prototype.realizeWidth_ = function(columnCount) { |
Robert Ginda | 4e83f3a | 2012-09-04 15:25:25 -0700 | [diff] [blame] | 983 | if (columnCount <= 0) |
| 984 | throw new Error('Attempt to realize bad width: ' + columnCount); |
| 985 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 986 | var deltaColumns = columnCount - this.screen_.getWidth(); |
| 987 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 988 | this.screenSize.width = columnCount; |
| 989 | this.screen_.setColumnCount(columnCount); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 990 | |
| 991 | if (deltaColumns > 0) { |
David Benjamin | 66e954d | 2012-05-05 21:08:12 -0400 | [diff] [blame] | 992 | if (this.defaultTabStops) |
| 993 | this.setDefaultTabStops(this.screenSize.width - deltaColumns); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 994 | } else { |
| 995 | for (var i = this.tabStops_.length - 1; i >= 0; i--) { |
David Benjamin | 66e954d | 2012-05-05 21:08:12 -0400 | [diff] [blame] | 996 | if (this.tabStops_[i] < columnCount) |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 997 | break; |
| 998 | |
| 999 | this.tabStops_.pop(); |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | this.screen_.setColumnCount(this.screenSize.width); |
| 1004 | }; |
| 1005 | |
| 1006 | /** |
| 1007 | * Deal with terminal height changes. |
| 1008 | * |
| 1009 | * This function does what needs to be done when the terminal height changes |
| 1010 | * out from under us. It happens here rather than in onResize_() because this |
| 1011 | * code may need to run synchronously to handle programmatic changes of |
| 1012 | * terminal height. |
| 1013 | * |
| 1014 | * Relying on the browser to send us an async resize event means we may not be |
| 1015 | * in the correct state yet when the next escape sequence hits. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 1016 | * |
| 1017 | * @param {number} rowCount The number of rows. |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1018 | */ |
| 1019 | hterm.Terminal.prototype.realizeHeight_ = function(rowCount) { |
Robert Ginda | 4e83f3a | 2012-09-04 15:25:25 -0700 | [diff] [blame] | 1020 | if (rowCount <= 0) |
| 1021 | throw new Error('Attempt to realize bad height: ' + rowCount); |
| 1022 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1023 | var deltaRows = rowCount - this.screen_.getHeight(); |
| 1024 | |
| 1025 | this.screenSize.height = rowCount; |
| 1026 | |
| 1027 | var cursor = this.saveCursor(); |
| 1028 | |
| 1029 | if (deltaRows < 0) { |
| 1030 | // Screen got smaller. |
| 1031 | deltaRows *= -1; |
| 1032 | while (deltaRows) { |
| 1033 | var lastRow = this.getRowCount() - 1; |
| 1034 | if (lastRow - this.scrollbackRows_.length == cursor.row) |
| 1035 | break; |
| 1036 | |
| 1037 | if (this.getRowText(lastRow)) |
| 1038 | break; |
| 1039 | |
| 1040 | this.screen_.popRow(); |
| 1041 | deltaRows--; |
| 1042 | } |
| 1043 | |
| 1044 | var ary = this.screen_.shiftRows(deltaRows); |
| 1045 | this.scrollbackRows_.push.apply(this.scrollbackRows_, ary); |
| 1046 | |
| 1047 | // We just removed rows from the top of the screen, we need to update |
| 1048 | // the cursor to match. |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 1049 | cursor.row = Math.max(cursor.row - deltaRows, 0); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1050 | } else if (deltaRows > 0) { |
| 1051 | // Screen got larger. |
| 1052 | |
| 1053 | if (deltaRows <= this.scrollbackRows_.length) { |
| 1054 | var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length); |
| 1055 | var rows = this.scrollbackRows_.splice( |
| 1056 | this.scrollbackRows_.length - scrollbackCount, scrollbackCount); |
| 1057 | this.screen_.unshiftRows(rows); |
| 1058 | deltaRows -= scrollbackCount; |
| 1059 | cursor.row += scrollbackCount; |
| 1060 | } |
| 1061 | |
| 1062 | if (deltaRows) |
| 1063 | this.appendRows_(deltaRows); |
| 1064 | } |
| 1065 | |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 1066 | this.setVTScrollRegion(null, null); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1067 | this.restoreCursor(cursor); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1068 | }; |
| 1069 | |
| 1070 | /** |
| 1071 | * Scroll the terminal to the top of the scrollback buffer. |
| 1072 | */ |
| 1073 | hterm.Terminal.prototype.scrollHome = function() { |
| 1074 | this.scrollPort_.scrollRowToTop(0); |
| 1075 | }; |
| 1076 | |
| 1077 | /** |
| 1078 | * Scroll the terminal to the end. |
| 1079 | */ |
| 1080 | hterm.Terminal.prototype.scrollEnd = function() { |
| 1081 | this.scrollPort_.scrollRowToBottom(this.getRowCount()); |
| 1082 | }; |
| 1083 | |
| 1084 | /** |
| 1085 | * Scroll the terminal one page up (minus one line) relative to the current |
| 1086 | * position. |
| 1087 | */ |
| 1088 | hterm.Terminal.prototype.scrollPageUp = function() { |
| 1089 | var i = this.scrollPort_.getTopRowIndex(); |
| 1090 | this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1); |
| 1091 | }; |
| 1092 | |
| 1093 | /** |
| 1094 | * Scroll the terminal one page down (minus one line) relative to the current |
| 1095 | * position. |
| 1096 | */ |
| 1097 | hterm.Terminal.prototype.scrollPageDown = function() { |
| 1098 | var i = this.scrollPort_.getTopRowIndex(); |
| 1099 | this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1100 | }; |
| 1101 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1102 | /** |
Mike Frysinger | cd56a63 | 2017-05-10 14:45:28 -0400 | [diff] [blame] | 1103 | * Scroll the terminal one line up relative to the current position. |
| 1104 | */ |
| 1105 | hterm.Terminal.prototype.scrollLineUp = function() { |
| 1106 | var i = this.scrollPort_.getTopRowIndex(); |
| 1107 | this.scrollPort_.scrollRowToTop(i - 1); |
| 1108 | }; |
| 1109 | |
| 1110 | /** |
| 1111 | * Scroll the terminal one line down relative to the current position. |
| 1112 | */ |
| 1113 | hterm.Terminal.prototype.scrollLineDown = function() { |
| 1114 | var i = this.scrollPort_.getTopRowIndex(); |
| 1115 | this.scrollPort_.scrollRowToTop(i + 1); |
| 1116 | }; |
| 1117 | |
| 1118 | /** |
Robert Ginda | 4093289 | 2012-12-10 17:26:40 -0800 | [diff] [blame] | 1119 | * Clear primary screen, secondary screen, and the scrollback buffer. |
| 1120 | */ |
| 1121 | hterm.Terminal.prototype.wipeContents = function() { |
| 1122 | this.scrollbackRows_.length = 0; |
| 1123 | this.scrollPort_.resetCache(); |
| 1124 | |
| 1125 | [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) { |
| 1126 | var bottom = screen.getHeight(); |
| 1127 | if (bottom > 0) { |
| 1128 | this.renumberRows_(0, bottom); |
| 1129 | this.clearHome(screen); |
| 1130 | } |
| 1131 | }.bind(this)); |
| 1132 | |
| 1133 | this.syncCursorPosition_(); |
Andrew de los Reyes | 68e0780 | 2013-04-04 15:38:55 -0700 | [diff] [blame] | 1134 | this.scrollPort_.invalidate(); |
Robert Ginda | 4093289 | 2012-12-10 17:26:40 -0800 | [diff] [blame] | 1135 | }; |
| 1136 | |
| 1137 | /** |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1138 | * Full terminal reset. |
| 1139 | */ |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1140 | hterm.Terminal.prototype.reset = function() { |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1141 | this.clearAllTabStops(); |
| 1142 | this.setDefaultTabStops(); |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 1143 | |
| 1144 | this.clearHome(this.primaryScreen_); |
| 1145 | this.primaryScreen_.textAttributes.reset(); |
| 1146 | |
| 1147 | this.clearHome(this.alternateScreen_); |
| 1148 | this.alternateScreen_.textAttributes.reset(); |
| 1149 | |
rginda | b8bc893 | 2012-04-27 12:45:03 -0700 | [diff] [blame] | 1150 | this.setCursorBlink(!!this.prefs_.get('cursor-blink')); |
| 1151 | |
Robert Ginda | 92e1810 | 2013-03-14 13:56:37 -0700 | [diff] [blame] | 1152 | this.vt.reset(); |
| 1153 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1154 | this.softReset(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1155 | }; |
| 1156 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1157 | /** |
| 1158 | * Soft terminal reset. |
rginda | b8bc893 | 2012-04-27 12:45:03 -0700 | [diff] [blame] | 1159 | * |
| 1160 | * Perform a soft reset to the default values listed in |
| 1161 | * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9 |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1162 | */ |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1163 | hterm.Terminal.prototype.softReset = function() { |
rginda | b8bc893 | 2012-04-27 12:45:03 -0700 | [diff] [blame] | 1164 | // Reset terminal options to their default values. |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1165 | this.options_ = new hterm.Options(); |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 1166 | |
Brad Town | b62dfdc | 2015-03-16 19:07:15 -0700 | [diff] [blame] | 1167 | // We show the cursor on soft reset but do not alter the blink state. |
| 1168 | this.options_.cursorBlink = !!this.timeouts_.cursorBlink; |
| 1169 | |
rginda | b8bc893 | 2012-04-27 12:45:03 -0700 | [diff] [blame] | 1170 | // Xterm also resets the color palette on soft reset, even though it doesn't |
| 1171 | // seem to be documented anywhere. |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 1172 | this.primaryScreen_.textAttributes.resetColorPalette(); |
| 1173 | this.alternateScreen_.textAttributes.resetColorPalette(); |
| 1174 | |
rginda | b8bc893 | 2012-04-27 12:45:03 -0700 | [diff] [blame] | 1175 | // The xterm man page explicitly says this will happen on soft reset. |
| 1176 | this.setVTScrollRegion(null, null); |
| 1177 | |
| 1178 | // Xterm also shows the cursor on soft reset, but does not alter the blink |
| 1179 | // state. |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 1180 | this.setCursorVisible(true); |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1181 | }; |
| 1182 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1183 | /** |
| 1184 | * Move the cursor forward to the next tab stop, or to the last column |
| 1185 | * if no more tab stops are set. |
| 1186 | */ |
| 1187 | hterm.Terminal.prototype.forwardTabStop = function() { |
| 1188 | var column = this.screen_.cursorPosition.column; |
| 1189 | |
| 1190 | for (var i = 0; i < this.tabStops_.length; i++) { |
| 1191 | if (this.tabStops_[i] > column) { |
| 1192 | this.setCursorColumn(this.tabStops_[i]); |
| 1193 | return; |
| 1194 | } |
| 1195 | } |
| 1196 | |
David Benjamin | 66e954d | 2012-05-05 21:08:12 -0400 | [diff] [blame] | 1197 | // xterm does not clear the overflow flag on HT or CHT. |
| 1198 | var overflow = this.screen_.cursorPosition.overflow; |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1199 | this.setCursorColumn(this.screenSize.width - 1); |
David Benjamin | 66e954d | 2012-05-05 21:08:12 -0400 | [diff] [blame] | 1200 | this.screen_.cursorPosition.overflow = overflow; |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1201 | }; |
| 1202 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1203 | /** |
| 1204 | * Move the cursor backward to the previous tab stop, or to the first column |
| 1205 | * if no previous tab stops are set. |
| 1206 | */ |
| 1207 | hterm.Terminal.prototype.backwardTabStop = function() { |
| 1208 | var column = this.screen_.cursorPosition.column; |
| 1209 | |
| 1210 | for (var i = this.tabStops_.length - 1; i >= 0; i--) { |
| 1211 | if (this.tabStops_[i] < column) { |
| 1212 | this.setCursorColumn(this.tabStops_[i]); |
| 1213 | return; |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | this.setCursorColumn(1); |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1218 | }; |
| 1219 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1220 | /** |
| 1221 | * Set a tab stop at the given column. |
| 1222 | * |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 1223 | * @param {integer} column Zero based column. |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1224 | */ |
| 1225 | hterm.Terminal.prototype.setTabStop = function(column) { |
| 1226 | for (var i = this.tabStops_.length - 1; i >= 0; i--) { |
| 1227 | if (this.tabStops_[i] == column) |
| 1228 | return; |
| 1229 | |
| 1230 | if (this.tabStops_[i] < column) { |
| 1231 | this.tabStops_.splice(i + 1, 0, column); |
| 1232 | return; |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | this.tabStops_.splice(0, 0, column); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1237 | }; |
| 1238 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1239 | /** |
| 1240 | * Clear the tab stop at the current cursor position. |
| 1241 | * |
| 1242 | * No effect if there is no tab stop at the current cursor position. |
| 1243 | */ |
| 1244 | hterm.Terminal.prototype.clearTabStopAtCursor = function() { |
| 1245 | var column = this.screen_.cursorPosition.column; |
| 1246 | |
| 1247 | var i = this.tabStops_.indexOf(column); |
| 1248 | if (i == -1) |
| 1249 | return; |
| 1250 | |
| 1251 | this.tabStops_.splice(i, 1); |
| 1252 | }; |
| 1253 | |
| 1254 | /** |
| 1255 | * Clear all tab stops. |
| 1256 | */ |
| 1257 | hterm.Terminal.prototype.clearAllTabStops = function() { |
| 1258 | this.tabStops_.length = 0; |
David Benjamin | 66e954d | 2012-05-05 21:08:12 -0400 | [diff] [blame] | 1259 | this.defaultTabStops = false; |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1260 | }; |
| 1261 | |
| 1262 | /** |
| 1263 | * Set up the default tab stops, starting from a given column. |
| 1264 | * |
| 1265 | * This sets a tabstop every (column % this.tabWidth) column, starting |
David Benjamin | 66e954d | 2012-05-05 21:08:12 -0400 | [diff] [blame] | 1266 | * from the specified column, or 0 if no column is provided. It also flags |
| 1267 | * future resizes to set them up. |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1268 | * |
| 1269 | * This does not clear the existing tab stops first, use clearAllTabStops |
| 1270 | * for that. |
| 1271 | * |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 1272 | * @param {integer} opt_start Optional starting zero based starting column, useful |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1273 | * for filling out missing tab stops when the terminal is resized. |
| 1274 | */ |
| 1275 | hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) { |
| 1276 | var start = opt_start || 0; |
| 1277 | var w = this.tabWidth; |
David Benjamin | 66e954d | 2012-05-05 21:08:12 -0400 | [diff] [blame] | 1278 | // Round start up to a default tab stop. |
| 1279 | start = start - 1 - ((start - 1) % w) + w; |
| 1280 | for (var i = start; i < this.screenSize.width; i += w) { |
| 1281 | this.setTabStop(i); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1282 | } |
David Benjamin | 66e954d | 2012-05-05 21:08:12 -0400 | [diff] [blame] | 1283 | |
| 1284 | this.defaultTabStops = true; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1285 | }; |
| 1286 | |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 1287 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1288 | * Interpret a sequence of characters. |
| 1289 | * |
| 1290 | * Incomplete escape sequences are buffered until the next call. |
| 1291 | * |
| 1292 | * @param {string} str Sequence of characters to interpret or pass through. |
| 1293 | */ |
| 1294 | hterm.Terminal.prototype.interpret = function(str) { |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1295 | this.vt.interpret(str); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1296 | this.scheduleSyncCursorPosition_(); |
| 1297 | }; |
| 1298 | |
| 1299 | /** |
| 1300 | * Take over the given DIV for use as the terminal display. |
| 1301 | * |
| 1302 | * @param {HTMLDivElement} div The div to use as the terminal display. |
| 1303 | */ |
| 1304 | hterm.Terminal.prototype.decorate = function(div) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1305 | this.div_ = div; |
| 1306 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1307 | this.scrollPort_.decorate(div); |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 1308 | this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image')); |
Philip Douglass | 959b49d | 2012-05-30 13:29:29 -0400 | [diff] [blame] | 1309 | this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size')); |
| 1310 | this.scrollPort_.setBackgroundPosition( |
| 1311 | this.prefs_.get('background-position')); |
Mike Frysinger | 08bad43 | 2017-04-24 00:50:54 -0400 | [diff] [blame] | 1312 | this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css')); |
| 1313 | this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text')); |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 1314 | |
rginda | 0918b65 | 2012-04-04 11:26:24 -0700 | [diff] [blame] | 1315 | this.div_.focus = this.focus.bind(this); |
rginda | f752139 | 2012-02-28 17:20:34 -0800 | [diff] [blame] | 1316 | |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 1317 | this.setFontSize(this.prefs_.get('font-size')); |
| 1318 | this.syncFontFamily(); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 1319 | |
David Reveman | 8f55249 | 2012-03-28 12:18:41 -0400 | [diff] [blame] | 1320 | this.setScrollbarVisible(this.prefs_.get('scrollbar-visible')); |
Rob Spies | 49039e5 | 2014-12-17 13:40:04 -0800 | [diff] [blame] | 1321 | this.setScrollWheelMoveMultipler( |
| 1322 | this.prefs_.get('scroll-wheel-move-multiplier')); |
David Reveman | 8f55249 | 2012-03-28 12:18:41 -0400 | [diff] [blame] | 1323 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1324 | this.document_ = this.scrollPort_.getDocument(); |
| 1325 | |
Evan Jones | 5f9df81 | 2016-12-06 09:38:58 -0500 | [diff] [blame] | 1326 | this.document_.body.oncontextmenu = function() { return false; }; |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 1327 | |
| 1328 | var onMouse = this.onMouse_.bind(this); |
Toni Barzic | 0bfa892 | 2013-11-22 11:18:35 -0800 | [diff] [blame] | 1329 | var screenNode = this.scrollPort_.getScreenNode(); |
| 1330 | screenNode.addEventListener('mousedown', onMouse); |
| 1331 | screenNode.addEventListener('mouseup', onMouse); |
| 1332 | screenNode.addEventListener('mousemove', onMouse); |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 1333 | this.scrollPort_.onScrollWheel = onMouse; |
| 1334 | |
Toni Barzic | 0bfa892 | 2013-11-22 11:18:35 -0800 | [diff] [blame] | 1335 | screenNode.addEventListener( |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 1336 | 'focus', this.onFocusChange_.bind(this, true)); |
Rob Spies | 06533ba | 2014-04-24 11:20:37 -0700 | [diff] [blame] | 1337 | // Listen for mousedown events on the screenNode as in FF the focus |
| 1338 | // events don't bubble. |
| 1339 | screenNode.addEventListener('mousedown', function() { |
| 1340 | setTimeout(this.onFocusChange_.bind(this, true)); |
| 1341 | }.bind(this)); |
| 1342 | |
Toni Barzic | 0bfa892 | 2013-11-22 11:18:35 -0800 | [diff] [blame] | 1343 | screenNode.addEventListener( |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 1344 | 'blur', this.onFocusChange_.bind(this, false)); |
| 1345 | |
| 1346 | var style = this.document_.createElement('style'); |
| 1347 | style.textContent = |
| 1348 | ('.cursor-node[focus="false"] {' + |
| 1349 | ' box-sizing: border-box;' + |
| 1350 | ' background-color: transparent !important;' + |
| 1351 | ' border-width: 2px;' + |
| 1352 | ' border-style: solid;' + |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 1353 | '}' + |
| 1354 | '.wc-node {' + |
| 1355 | ' display: inline-block;' + |
| 1356 | ' text-align: center;' + |
| 1357 | ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' + |
Mike Frysinger | 93b75ba | 2017-04-05 19:43:18 -0400 | [diff] [blame] | 1358 | '}' + |
| 1359 | ':root {' + |
| 1360 | ' --hterm-blink-node-duration: 0.7s;' + |
| 1361 | '}' + |
| 1362 | '@keyframes blink {' + |
| 1363 | ' from { opacity: 1.0; }' + |
| 1364 | ' to { opacity: 0.0; }' + |
| 1365 | '}' + |
| 1366 | '.blink-node {' + |
| 1367 | ' animation-name: blink;' + |
| 1368 | ' animation-duration: var(--hterm-blink-node-duration);' + |
| 1369 | ' animation-iteration-count: infinite;' + |
| 1370 | ' animation-timing-function: ease-in-out;' + |
| 1371 | ' animation-direction: alternate;' + |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 1372 | '}'); |
| 1373 | this.document_.head.appendChild(style); |
| 1374 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 1375 | var styleSheets = this.document_.styleSheets; |
| 1376 | var cssRules = styleSheets[styleSheets.length - 1].cssRules; |
| 1377 | this.wcCssRule_ = cssRules[cssRules.length - 1]; |
| 1378 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1379 | this.cursorNode_ = this.document_.createElement('div'); |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 1380 | this.cursorNode_.className = 'cursor-node'; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1381 | this.cursorNode_.style.cssText = |
| 1382 | ('position: absolute;' + |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1383 | 'top: -99px;' + |
| 1384 | 'display: block;' + |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 1385 | 'width: ' + this.scrollPort_.characterSize.width + 'px;' + |
| 1386 | 'height: ' + this.scrollPort_.characterSize.height + 'px;' + |
Rob Spies | 06533ba | 2014-04-24 11:20:37 -0700 | [diff] [blame] | 1387 | '-webkit-transition: opacity, background-color 100ms linear;' + |
| 1388 | '-moz-transition: opacity, background-color 100ms linear;'); |
Robert Ginda | fb1be6a | 2013-12-11 11:56:22 -0800 | [diff] [blame] | 1389 | |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 1390 | this.setCursorColor(this.prefs_.get('cursor-color')); |
Robert Ginda | fb1be6a | 2013-12-11 11:56:22 -0800 | [diff] [blame] | 1391 | this.setCursorBlink(!!this.prefs_.get('cursor-blink')); |
| 1392 | this.restyleCursor_(); |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 1393 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1394 | this.document_.body.appendChild(this.cursorNode_); |
| 1395 | |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 1396 | // When 'enableMouseDragScroll' is off we reposition this element directly |
| 1397 | // under the mouse cursor after a click. This makes Chrome associate |
| 1398 | // subsequent mousemove events with the scroll-blocker. Since the |
| 1399 | // scroll-blocker is a peer (not a child) of the scrollport, the mousemove |
| 1400 | // events do not cause the scrollport to scroll. |
| 1401 | // |
| 1402 | // It's a hack, but it's the cleanest way I could find. |
| 1403 | this.scrollBlockerNode_ = this.document_.createElement('div'); |
| 1404 | this.scrollBlockerNode_.style.cssText = |
| 1405 | ('position: absolute;' + |
| 1406 | 'top: -99px;' + |
| 1407 | 'display: block;' + |
| 1408 | 'width: 10px;' + |
| 1409 | 'height: 10px;'); |
| 1410 | this.document_.body.appendChild(this.scrollBlockerNode_); |
| 1411 | |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 1412 | this.scrollPort_.onScrollWheel = onMouse; |
| 1413 | ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick', |
| 1414 | ].forEach(function(event) { |
| 1415 | this.scrollBlockerNode_.addEventListener(event, onMouse); |
| 1416 | this.cursorNode_.addEventListener(event, onMouse); |
| 1417 | this.document_.addEventListener(event, onMouse); |
| 1418 | }.bind(this)); |
| 1419 | |
| 1420 | this.cursorNode_.addEventListener('mousedown', function() { |
| 1421 | setTimeout(this.focus.bind(this)); |
| 1422 | }.bind(this)); |
| 1423 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1424 | this.setReverseVideo(false); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1425 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1426 | this.scrollPort_.focus(); |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 1427 | this.scrollPort_.scheduleRedraw(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1428 | }; |
| 1429 | |
rginda | 0918b65 | 2012-04-04 11:26:24 -0700 | [diff] [blame] | 1430 | /** |
| 1431 | * Return the HTML document that contains the terminal DOM nodes. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 1432 | * |
| 1433 | * @return {HTMLDocument} |
rginda | 0918b65 | 2012-04-04 11:26:24 -0700 | [diff] [blame] | 1434 | */ |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1435 | hterm.Terminal.prototype.getDocument = function() { |
| 1436 | return this.document_; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1437 | }; |
| 1438 | |
| 1439 | /** |
rginda | 0918b65 | 2012-04-04 11:26:24 -0700 | [diff] [blame] | 1440 | * Focus the terminal. |
| 1441 | */ |
| 1442 | hterm.Terminal.prototype.focus = function() { |
| 1443 | this.scrollPort_.focus(); |
| 1444 | }; |
| 1445 | |
| 1446 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1447 | * Return the HTML Element for a given row index. |
| 1448 | * |
| 1449 | * This is a method from the RowProvider interface. The ScrollPort uses |
| 1450 | * it to fetch rows on demand as they are scrolled into view. |
| 1451 | * |
| 1452 | * TODO(rginda): Consider saving scrollback rows as (HTML source, text content) |
| 1453 | * pairs to conserve memory. |
| 1454 | * |
| 1455 | * @param {integer} index The zero-based row index, measured relative to the |
| 1456 | * start of the scrollback buffer. On-screen rows will always have the |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame] | 1457 | * largest indices. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1458 | * @return {HTMLElement} The 'x-row' element containing for the requested row. |
| 1459 | */ |
| 1460 | hterm.Terminal.prototype.getRowNode = function(index) { |
| 1461 | if (index < this.scrollbackRows_.length) |
| 1462 | return this.scrollbackRows_[index]; |
| 1463 | |
| 1464 | var screenIndex = index - this.scrollbackRows_.length; |
| 1465 | return this.screen_.rowsArray[screenIndex]; |
| 1466 | }; |
| 1467 | |
| 1468 | /** |
| 1469 | * Return the text content for a given range of rows. |
| 1470 | * |
| 1471 | * This is a method from the RowProvider interface. The ScrollPort uses |
| 1472 | * it to fetch text content on demand when the user attempts to copy their |
| 1473 | * selection to the clipboard. |
| 1474 | * |
| 1475 | * @param {integer} start The zero-based row index to start from, measured |
| 1476 | * relative to the start of the scrollback buffer. On-screen rows will |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame] | 1477 | * always have the largest indices. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1478 | * @param {integer} end The zero-based row index to end on, measured |
| 1479 | * relative to the start of the scrollback buffer. |
| 1480 | * @return {string} A single string containing the text value of the range of |
| 1481 | * rows. Lines will be newline delimited, with no trailing newline. |
| 1482 | */ |
| 1483 | hterm.Terminal.prototype.getRowsText = function(start, end) { |
| 1484 | var ary = []; |
| 1485 | for (var i = start; i < end; i++) { |
| 1486 | var node = this.getRowNode(i); |
| 1487 | ary.push(node.textContent); |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 1488 | if (i < end - 1 && !node.getAttribute('line-overflow')) |
| 1489 | ary.push('\n'); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1490 | } |
| 1491 | |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 1492 | return ary.join(''); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1493 | }; |
| 1494 | |
| 1495 | /** |
| 1496 | * Return the text content for a given row. |
| 1497 | * |
| 1498 | * This is a method from the RowProvider interface. The ScrollPort uses |
| 1499 | * it to fetch text content on demand when the user attempts to copy their |
| 1500 | * selection to the clipboard. |
| 1501 | * |
| 1502 | * @param {integer} index The zero-based row index to return, measured |
| 1503 | * relative to the start of the scrollback buffer. On-screen rows will |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame] | 1504 | * always have the largest indices. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1505 | * @return {string} A string containing the text value of the selected row. |
| 1506 | */ |
| 1507 | hterm.Terminal.prototype.getRowText = function(index) { |
| 1508 | var node = this.getRowNode(index); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1509 | return node.textContent; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1510 | }; |
| 1511 | |
| 1512 | /** |
| 1513 | * Return the total number of rows in the addressable screen and in the |
| 1514 | * scrollback buffer of this terminal. |
| 1515 | * |
| 1516 | * This is a method from the RowProvider interface. The ScrollPort uses |
| 1517 | * it to compute the size of the scrollbar. |
| 1518 | * |
| 1519 | * @return {integer} The number of rows in this terminal. |
| 1520 | */ |
| 1521 | hterm.Terminal.prototype.getRowCount = function() { |
| 1522 | return this.scrollbackRows_.length + this.screen_.rowsArray.length; |
| 1523 | }; |
| 1524 | |
| 1525 | /** |
| 1526 | * Create DOM nodes for new rows and append them to the end of the terminal. |
| 1527 | * |
| 1528 | * This is the only correct way to add a new DOM node for a row. Notice that |
| 1529 | * the new row is appended to the bottom of the list of rows, and does not |
| 1530 | * require renumbering (of the rowIndex property) of previous rows. |
| 1531 | * |
| 1532 | * If you think you want a new blank row somewhere in the middle of the |
| 1533 | * terminal, look into moveRows_(). |
| 1534 | * |
| 1535 | * This method does not pay attention to vtScrollTop/Bottom, since you should |
| 1536 | * be using moveRows() in cases where they would matter. |
| 1537 | * |
| 1538 | * The cursor will be positioned at column 0 of the first inserted line. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 1539 | * |
| 1540 | * @param {number} count The number of rows to created. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1541 | */ |
| 1542 | hterm.Terminal.prototype.appendRows_ = function(count) { |
| 1543 | var cursorRow = this.screen_.rowsArray.length; |
| 1544 | var offset = this.scrollbackRows_.length + cursorRow; |
| 1545 | for (var i = 0; i < count; i++) { |
| 1546 | var row = this.document_.createElement('x-row'); |
| 1547 | row.appendChild(this.document_.createTextNode('')); |
| 1548 | row.rowIndex = offset + i; |
| 1549 | this.screen_.pushRow(row); |
| 1550 | } |
| 1551 | |
| 1552 | var extraRows = this.screen_.rowsArray.length - this.screenSize.height; |
| 1553 | if (extraRows > 0) { |
| 1554 | var ary = this.screen_.shiftRows(extraRows); |
| 1555 | Array.prototype.push.apply(this.scrollbackRows_, ary); |
Robert Ginda | 36c5aa6 | 2012-10-15 11:17:47 -0700 | [diff] [blame] | 1556 | if (this.scrollPort_.isScrolledEnd) |
| 1557 | this.scheduleScrollDown_(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | if (cursorRow >= this.screen_.rowsArray.length) |
| 1561 | cursorRow = this.screen_.rowsArray.length - 1; |
| 1562 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1563 | this.setAbsoluteCursorPosition(cursorRow, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1564 | }; |
| 1565 | |
| 1566 | /** |
| 1567 | * Relocate rows from one part of the addressable screen to another. |
| 1568 | * |
| 1569 | * This is used to recycle rows during VT scrolls (those which are driven |
| 1570 | * by VT commands, rather than by the user manipulating the scrollbar.) |
| 1571 | * |
| 1572 | * In this case, the blank lines scrolled into the scroll region are made of |
| 1573 | * the nodes we scrolled off. These have their rowIndex properties carefully |
| 1574 | * renumbered so as not to confuse the ScrollPort. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 1575 | * |
| 1576 | * @param {number} fromIndex The start index. |
| 1577 | * @param {number} count The number of rows to move. |
| 1578 | * @param {number} toIndex The destination index. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1579 | */ |
| 1580 | hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) { |
| 1581 | var ary = this.screen_.removeRows(fromIndex, count); |
| 1582 | this.screen_.insertRows(toIndex, ary); |
| 1583 | |
| 1584 | var start, end; |
| 1585 | if (fromIndex < toIndex) { |
| 1586 | start = fromIndex; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1587 | end = toIndex + count; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1588 | } else { |
| 1589 | start = toIndex; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1590 | end = fromIndex + count; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | this.renumberRows_(start, end); |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 1594 | this.scrollPort_.scheduleInvalidate(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1595 | }; |
| 1596 | |
| 1597 | /** |
| 1598 | * Renumber the rowIndex property of the given range of rows. |
| 1599 | * |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame] | 1600 | * The start and end indices are relative to the screen, not the scrollback. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1601 | * Rows in the scrollback buffer cannot be renumbered. Since they are not |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 1602 | * addressable (you can't delete them, scroll them, etc), you should have |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1603 | * no need to renumber scrollback rows. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 1604 | * |
| 1605 | * @param {number} start The start index. |
| 1606 | * @param {number} end The end index. |
| 1607 | * @param {hterm.Screen} opt_screen The screen to renumber. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1608 | */ |
Robert Ginda | 4093289 | 2012-12-10 17:26:40 -0800 | [diff] [blame] | 1609 | hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) { |
| 1610 | var screen = opt_screen || this.screen_; |
| 1611 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1612 | var offset = this.scrollbackRows_.length; |
| 1613 | for (var i = start; i < end; i++) { |
Robert Ginda | 4093289 | 2012-12-10 17:26:40 -0800 | [diff] [blame] | 1614 | screen.rowsArray[i].rowIndex = offset + i; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1615 | } |
| 1616 | }; |
| 1617 | |
| 1618 | /** |
| 1619 | * Print a string to the terminal. |
| 1620 | * |
| 1621 | * This respects the current insert and wraparound modes. It will add new lines |
| 1622 | * to the end of the terminal, scrolling off the top into the scrollback buffer |
| 1623 | * if necessary. |
| 1624 | * |
| 1625 | * The string is *not* parsed for escape codes. Use the interpret() method if |
| 1626 | * that's what you're after. |
| 1627 | * |
| 1628 | * @param{string} str The string to print. |
| 1629 | */ |
| 1630 | hterm.Terminal.prototype.print = function(str) { |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 1631 | var startOffset = 0; |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 1632 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 1633 | var strWidth = lib.wc.strWidth(str); |
| 1634 | |
| 1635 | while (startOffset < strWidth) { |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 1636 | if (this.options_.wraparound && this.screen_.cursorPosition.overflow) { |
| 1637 | this.screen_.commitLineOverflow(); |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 1638 | this.newLine(); |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 1639 | } |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 1640 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 1641 | var count = strWidth - startOffset; |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 1642 | var didOverflow = false; |
| 1643 | var substr; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 1644 | |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 1645 | if (this.screen_.cursorPosition.column + count >= this.screenSize.width) { |
| 1646 | didOverflow = true; |
| 1647 | count = this.screenSize.width - this.screen_.cursorPosition.column; |
| 1648 | } |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 1649 | |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 1650 | if (didOverflow && !this.options_.wraparound) { |
| 1651 | // If the string overflowed the line but wraparound is off, then the |
| 1652 | // last printed character should be the last of the string. |
| 1653 | // TODO: This will add to our problems with multibyte UTF-16 characters. |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 1654 | substr = lib.wc.substr(str, startOffset, count - 1) + |
| 1655 | lib.wc.substr(str, strWidth - 1); |
| 1656 | count = strWidth; |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 1657 | } else { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 1658 | substr = lib.wc.substr(str, startOffset, count); |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 1659 | } |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 1660 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 1661 | var tokens = hterm.TextAttributes.splitWidecharString(substr); |
| 1662 | for (var i = 0; i < tokens.length; i++) { |
| 1663 | if (tokens[i].wcNode) |
| 1664 | this.screen_.textAttributes.wcNode = true; |
| 1665 | |
| 1666 | if (this.options_.insertMode) { |
| 1667 | this.screen_.insertString(tokens[i].str); |
| 1668 | } else { |
| 1669 | this.screen_.overwriteString(tokens[i].str); |
| 1670 | } |
| 1671 | this.screen_.textAttributes.wcNode = false; |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | this.screen_.maybeClipCurrentRow(); |
| 1675 | startOffset += count; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 1676 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1677 | |
| 1678 | this.scheduleSyncCursorPosition_(); |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1679 | |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 1680 | if (this.scrollOnOutput_) |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1681 | this.scrollPort_.scrollRowToBottom(this.getRowCount()); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1682 | }; |
| 1683 | |
| 1684 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1685 | * Set the VT scroll region. |
| 1686 | * |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1687 | * This also resets the cursor position to the absolute (0, 0) position, since |
| 1688 | * that's what xterm appears to do. |
| 1689 | * |
Robert Ginda | 5b9fbe6 | 2013-10-30 14:05:53 -0700 | [diff] [blame] | 1690 | * Setting the scroll region to the full height of the terminal will clear |
| 1691 | * the scroll region. This is *NOT* what most terminals do. We're explicitly |
| 1692 | * going "off-spec" here because it makes `screen` and `tmux` overflow into the |
| 1693 | * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn |
| 1694 | * continue to work as most users would expect. |
| 1695 | * |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1696 | * @param {integer} scrollTop The zero-based top of the scroll region. |
| 1697 | * @param {integer} scrollBottom The zero-based bottom of the scroll region, |
| 1698 | * inclusive. |
| 1699 | */ |
| 1700 | hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) { |
Robert Ginda | 5b9fbe6 | 2013-10-30 14:05:53 -0700 | [diff] [blame] | 1701 | if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) { |
Robert Ginda | 43684e2 | 2013-11-25 14:18:52 -0800 | [diff] [blame] | 1702 | this.vtScrollTop_ = null; |
| 1703 | this.vtScrollBottom_ = null; |
Robert Ginda | 5b9fbe6 | 2013-10-30 14:05:53 -0700 | [diff] [blame] | 1704 | } else { |
| 1705 | this.vtScrollTop_ = scrollTop; |
| 1706 | this.vtScrollBottom_ = scrollBottom; |
| 1707 | } |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1708 | }; |
| 1709 | |
| 1710 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1711 | * Return the top row index according to the VT. |
| 1712 | * |
| 1713 | * This will return 0 unless the terminal has been told to restrict scrolling |
| 1714 | * to some lower row. It is used for some VT cursor positioning and scrolling |
| 1715 | * commands. |
| 1716 | * |
| 1717 | * @return {integer} The topmost row in the terminal's scroll region. |
| 1718 | */ |
| 1719 | hterm.Terminal.prototype.getVTScrollTop = function() { |
| 1720 | if (this.vtScrollTop_ != null) |
| 1721 | return this.vtScrollTop_; |
| 1722 | |
| 1723 | return 0; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1724 | }; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1725 | |
| 1726 | /** |
| 1727 | * Return the bottom row index according to the VT. |
| 1728 | * |
| 1729 | * This will return the height of the terminal unless the it has been told to |
| 1730 | * restrict scrolling to some higher row. It is used for some VT cursor |
| 1731 | * positioning and scrolling commands. |
| 1732 | * |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame] | 1733 | * @return {integer} The bottom most row in the terminal's scroll region. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1734 | */ |
| 1735 | hterm.Terminal.prototype.getVTScrollBottom = function() { |
| 1736 | if (this.vtScrollBottom_ != null) |
| 1737 | return this.vtScrollBottom_; |
| 1738 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1739 | return this.screenSize.height - 1; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | /** |
| 1743 | * Process a '\n' character. |
| 1744 | * |
| 1745 | * If the cursor is on the final row of the terminal this will append a new |
| 1746 | * blank row to the screen and scroll the topmost row into the scrollback |
| 1747 | * buffer. |
| 1748 | * |
| 1749 | * Otherwise, this moves the cursor to column zero of the next row. |
| 1750 | */ |
| 1751 | hterm.Terminal.prototype.newLine = function() { |
Robert Ginda | 9937abc | 2013-07-25 16:09:23 -0700 | [diff] [blame] | 1752 | var cursorAtEndOfScreen = (this.screen_.cursorPosition.row == |
| 1753 | this.screen_.rowsArray.length - 1); |
| 1754 | |
| 1755 | if (this.vtScrollBottom_ != null) { |
| 1756 | // A VT Scroll region is active, we never append new rows. |
| 1757 | if (this.screen_.cursorPosition.row == this.vtScrollBottom_) { |
| 1758 | // We're at the end of the VT Scroll Region, perform a VT scroll. |
| 1759 | this.vtScrollUp(1); |
| 1760 | this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0); |
| 1761 | } else if (cursorAtEndOfScreen) { |
| 1762 | // We're at the end of the screen, the only thing to do is put the |
| 1763 | // cursor to column 0. |
| 1764 | this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0); |
| 1765 | } else { |
| 1766 | // Anywhere else, advance the cursor row, and reset the column. |
| 1767 | this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0); |
| 1768 | } |
| 1769 | } else if (cursorAtEndOfScreen) { |
Robert Ginda | 1b06b37 | 2013-07-19 15:22:51 -0700 | [diff] [blame] | 1770 | // We're at the end of the screen. Append a new row to the terminal, |
| 1771 | // shifting the top row into the scrollback. |
| 1772 | this.appendRows_(1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1773 | } else { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1774 | // Anywhere else in the screen just moves the cursor. |
| 1775 | this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1776 | } |
| 1777 | }; |
| 1778 | |
| 1779 | /** |
| 1780 | * Like newLine(), except maintain the cursor column. |
| 1781 | */ |
| 1782 | hterm.Terminal.prototype.lineFeed = function() { |
| 1783 | var column = this.screen_.cursorPosition.column; |
| 1784 | this.newLine(); |
| 1785 | this.setCursorColumn(column); |
| 1786 | }; |
| 1787 | |
| 1788 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1789 | * If autoCarriageReturn is set then newLine(), else lineFeed(). |
| 1790 | */ |
| 1791 | hterm.Terminal.prototype.formFeed = function() { |
| 1792 | if (this.options_.autoCarriageReturn) { |
| 1793 | this.newLine(); |
| 1794 | } else { |
| 1795 | this.lineFeed(); |
| 1796 | } |
| 1797 | }; |
| 1798 | |
| 1799 | /** |
| 1800 | * Move the cursor up one row, possibly inserting a blank line. |
| 1801 | * |
| 1802 | * The cursor column is not changed. |
| 1803 | */ |
| 1804 | hterm.Terminal.prototype.reverseLineFeed = function() { |
| 1805 | var scrollTop = this.getVTScrollTop(); |
| 1806 | var currentRow = this.screen_.cursorPosition.row; |
| 1807 | |
| 1808 | if (currentRow == scrollTop) { |
| 1809 | this.insertLines(1); |
| 1810 | } else { |
| 1811 | this.setAbsoluteCursorRow(currentRow - 1); |
| 1812 | } |
| 1813 | }; |
| 1814 | |
| 1815 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1816 | * Replace all characters to the left of the current cursor with the space |
| 1817 | * character. |
| 1818 | * |
| 1819 | * TODO(rginda): This should probably *remove* the characters (not just replace |
| 1820 | * with a space) if there are no characters at or beyond the current cursor |
Robert Ginda | f2547f1 | 2012-10-25 20:36:21 -0700 | [diff] [blame] | 1821 | * position. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1822 | */ |
| 1823 | hterm.Terminal.prototype.eraseToLeft = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1824 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1825 | this.setCursorColumn(0); |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 1826 | this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1)); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1827 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1828 | }; |
| 1829 | |
| 1830 | /** |
David Benjamin | 684a9b7 | 2012-05-01 17:19:58 -0400 | [diff] [blame] | 1831 | * Erase a given number of characters to the right of the cursor. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1832 | * |
| 1833 | * The cursor position is unchanged. |
| 1834 | * |
Robert Ginda | f2547f1 | 2012-10-25 20:36:21 -0700 | [diff] [blame] | 1835 | * If the current background color is not the default background color this |
| 1836 | * will insert spaces rather than delete. This is unfortunate because the |
| 1837 | * trailing space will affect text selection, but it's difficult to come up |
| 1838 | * with a way to style empty space that wouldn't trip up the hterm.Screen |
| 1839 | * code. |
Robert Ginda | cd5637d | 2013-10-30 14:59:10 -0700 | [diff] [blame] | 1840 | * |
| 1841 | * eraseToRight is ignored in the presence of a cursor overflow. This deviates |
| 1842 | * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See |
| 1843 | * crbug.com/232390 for details. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 1844 | * |
| 1845 | * @param {number} opt_count The number of characters to erase. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1846 | */ |
| 1847 | hterm.Terminal.prototype.eraseToRight = function(opt_count) { |
Robert Ginda | cd5637d | 2013-10-30 14:59:10 -0700 | [diff] [blame] | 1848 | if (this.screen_.cursorPosition.overflow) |
| 1849 | return; |
| 1850 | |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 1851 | var maxCount = this.screenSize.width - this.screen_.cursorPosition.column; |
| 1852 | var count = opt_count ? Math.min(opt_count, maxCount) : maxCount; |
Robert Ginda | f2547f1 | 2012-10-25 20:36:21 -0700 | [diff] [blame] | 1853 | |
| 1854 | if (this.screen_.textAttributes.background === |
| 1855 | this.screen_.textAttributes.DEFAULT_COLOR) { |
| 1856 | var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row]; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 1857 | if (hterm.TextAttributes.nodeWidth(cursorRow) <= |
Robert Ginda | f2547f1 | 2012-10-25 20:36:21 -0700 | [diff] [blame] | 1858 | this.screen_.cursorPosition.column + count) { |
| 1859 | this.screen_.deleteChars(count); |
| 1860 | this.clearCursorOverflow(); |
| 1861 | return; |
| 1862 | } |
| 1863 | } |
| 1864 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1865 | var cursor = this.saveCursor(); |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 1866 | this.screen_.overwriteString(lib.f.getWhitespace(count)); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1867 | this.restoreCursor(cursor); |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 1868 | this.clearCursorOverflow(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1869 | }; |
| 1870 | |
| 1871 | /** |
| 1872 | * Erase the current line. |
| 1873 | * |
| 1874 | * The cursor position is unchanged. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1875 | */ |
| 1876 | hterm.Terminal.prototype.eraseLine = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1877 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1878 | this.screen_.clearCursorRow(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1879 | this.restoreCursor(cursor); |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 1880 | this.clearCursorOverflow(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1881 | }; |
| 1882 | |
| 1883 | /** |
David Benjamin | a08d78f | 2012-05-05 00:28:49 -0400 | [diff] [blame] | 1884 | * Erase all characters from the start of the screen to the current cursor |
| 1885 | * position, regardless of scroll region. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1886 | * |
| 1887 | * The cursor position is unchanged. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1888 | */ |
| 1889 | hterm.Terminal.prototype.eraseAbove = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1890 | var cursor = this.saveCursor(); |
| 1891 | |
| 1892 | this.eraseToLeft(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1893 | |
David Benjamin | a08d78f | 2012-05-05 00:28:49 -0400 | [diff] [blame] | 1894 | for (var i = 0; i < cursor.row; i++) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1895 | this.setAbsoluteCursorPosition(i, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1896 | this.screen_.clearCursorRow(); |
| 1897 | } |
| 1898 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1899 | this.restoreCursor(cursor); |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 1900 | this.clearCursorOverflow(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1901 | }; |
| 1902 | |
| 1903 | /** |
| 1904 | * Erase all characters from the current cursor position to the end of the |
David Benjamin | a08d78f | 2012-05-05 00:28:49 -0400 | [diff] [blame] | 1905 | * screen, regardless of scroll region. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1906 | * |
| 1907 | * The cursor position is unchanged. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1908 | */ |
| 1909 | hterm.Terminal.prototype.eraseBelow = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1910 | var cursor = this.saveCursor(); |
| 1911 | |
| 1912 | this.eraseToRight(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1913 | |
David Benjamin | a08d78f | 2012-05-05 00:28:49 -0400 | [diff] [blame] | 1914 | var bottom = this.screenSize.height - 1; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1915 | for (var i = cursor.row + 1; i <= bottom; i++) { |
| 1916 | this.setAbsoluteCursorPosition(i, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1917 | this.screen_.clearCursorRow(); |
| 1918 | } |
| 1919 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1920 | this.restoreCursor(cursor); |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 1921 | this.clearCursorOverflow(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1922 | }; |
| 1923 | |
| 1924 | /** |
| 1925 | * Fill the terminal with a given character. |
| 1926 | * |
| 1927 | * This methods does not respect the VT scroll region. |
| 1928 | * |
| 1929 | * @param {string} ch The character to use for the fill. |
| 1930 | */ |
| 1931 | hterm.Terminal.prototype.fill = function(ch) { |
| 1932 | var cursor = this.saveCursor(); |
| 1933 | |
| 1934 | this.setAbsoluteCursorPosition(0, 0); |
| 1935 | for (var row = 0; row < this.screenSize.height; row++) { |
| 1936 | for (var col = 0; col < this.screenSize.width; col++) { |
| 1937 | this.setAbsoluteCursorPosition(row, col); |
| 1938 | this.screen_.overwriteString(ch); |
| 1939 | } |
| 1940 | } |
| 1941 | |
| 1942 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1943 | }; |
| 1944 | |
| 1945 | /** |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 1946 | * Erase the entire display and leave the cursor at (0, 0). |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1947 | * |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 1948 | * This does not respect the scroll region. |
| 1949 | * |
| 1950 | * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults |
| 1951 | * to the current screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1952 | */ |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 1953 | hterm.Terminal.prototype.clearHome = function(opt_screen) { |
| 1954 | var screen = opt_screen || this.screen_; |
| 1955 | var bottom = screen.getHeight(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1956 | |
rginda | 11057d5 | 2012-04-25 12:29:56 -0700 | [diff] [blame] | 1957 | if (bottom == 0) { |
| 1958 | // Empty screen, nothing to do. |
| 1959 | return; |
| 1960 | } |
| 1961 | |
rginda | e4d2923 | 2012-01-19 10:47:13 -0800 | [diff] [blame] | 1962 | for (var i = 0; i < bottom; i++) { |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 1963 | screen.setCursorPosition(i, 0); |
| 1964 | screen.clearCursorRow(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1965 | } |
| 1966 | |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 1967 | screen.setCursorPosition(0, 0); |
| 1968 | }; |
| 1969 | |
| 1970 | /** |
| 1971 | * Erase the entire display without changing the cursor position. |
| 1972 | * |
| 1973 | * The cursor position is unchanged. This does not respect the scroll |
| 1974 | * region. |
| 1975 | * |
| 1976 | * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults |
| 1977 | * to the current screen. |
rginda | 9ea433c | 2012-03-16 11:57:00 -0700 | [diff] [blame] | 1978 | */ |
| 1979 | hterm.Terminal.prototype.clear = function(opt_screen) { |
| 1980 | var screen = opt_screen || this.screen_; |
| 1981 | var cursor = screen.cursorPosition.clone(); |
| 1982 | this.clearHome(screen); |
| 1983 | screen.setCursorPosition(cursor.row, cursor.column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1984 | }; |
| 1985 | |
| 1986 | /** |
| 1987 | * VT command to insert lines at the current cursor row. |
| 1988 | * |
| 1989 | * This respects the current scroll region. Rows pushed off the bottom are |
| 1990 | * lost (they won't show up in the scrollback buffer). |
| 1991 | * |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1992 | * @param {integer} count The number of lines to insert. |
| 1993 | */ |
| 1994 | hterm.Terminal.prototype.insertLines = function(count) { |
Robert Ginda | 579186b | 2012-09-26 11:40:04 -0700 | [diff] [blame] | 1995 | var cursorRow = this.screen_.cursorPosition.row; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1996 | |
| 1997 | var bottom = this.getVTScrollBottom(); |
Robert Ginda | 579186b | 2012-09-26 11:40:04 -0700 | [diff] [blame] | 1998 | count = Math.min(count, bottom - cursorRow); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1999 | |
Robert Ginda | 579186b | 2012-09-26 11:40:04 -0700 | [diff] [blame] | 2000 | // The moveCount is the number of rows we need to relocate to make room for |
| 2001 | // the new row(s). The count is the distance to move them. |
| 2002 | var moveCount = bottom - cursorRow - count + 1; |
| 2003 | if (moveCount) |
| 2004 | this.moveRows_(cursorRow, moveCount, cursorRow + count); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2005 | |
Robert Ginda | 579186b | 2012-09-26 11:40:04 -0700 | [diff] [blame] | 2006 | for (var i = count - 1; i >= 0; i--) { |
| 2007 | this.setAbsoluteCursorPosition(cursorRow + i, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2008 | this.screen_.clearCursorRow(); |
| 2009 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2010 | }; |
| 2011 | |
| 2012 | /** |
| 2013 | * VT command to delete lines at the current cursor row. |
| 2014 | * |
| 2015 | * New rows are added to the bottom of scroll region to take their place. New |
| 2016 | * rows are strictly there to take up space and have no content or style. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 2017 | * |
| 2018 | * @param {number} count The number of lines to delete. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2019 | */ |
| 2020 | hterm.Terminal.prototype.deleteLines = function(count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2021 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2022 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2023 | var top = cursor.row; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2024 | var bottom = this.getVTScrollBottom(); |
| 2025 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2026 | var maxCount = bottom - top + 1; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2027 | count = Math.min(count, maxCount); |
| 2028 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2029 | var moveStart = bottom - count + 1; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2030 | if (count != maxCount) |
| 2031 | this.moveRows_(top, count, moveStart); |
| 2032 | |
| 2033 | for (var i = 0; i < count; i++) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2034 | this.setAbsoluteCursorPosition(moveStart + i, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2035 | this.screen_.clearCursorRow(); |
| 2036 | } |
| 2037 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2038 | this.restoreCursor(cursor); |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 2039 | this.clearCursorOverflow(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2040 | }; |
| 2041 | |
| 2042 | /** |
| 2043 | * Inserts the given number of spaces at the current cursor position. |
| 2044 | * |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2045 | * The cursor position is not changed. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 2046 | * |
| 2047 | * @param {number} count The number of spaces to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2048 | */ |
| 2049 | hterm.Terminal.prototype.insertSpace = function(count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2050 | var cursor = this.saveCursor(); |
| 2051 | |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 2052 | var ws = lib.f.getWhitespace(count || 1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2053 | this.screen_.insertString(ws); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 2054 | this.screen_.maybeClipCurrentRow(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2055 | |
| 2056 | this.restoreCursor(cursor); |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 2057 | this.clearCursorOverflow(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2058 | }; |
| 2059 | |
| 2060 | /** |
| 2061 | * Forward-delete the specified number of characters starting at the cursor |
| 2062 | * position. |
| 2063 | * |
| 2064 | * @param {integer} count The number of characters to delete. |
| 2065 | */ |
| 2066 | hterm.Terminal.prototype.deleteChars = function(count) { |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 2067 | var deleted = this.screen_.deleteChars(count); |
| 2068 | if (deleted && !this.screen_.textAttributes.isDefault()) { |
| 2069 | var cursor = this.saveCursor(); |
| 2070 | this.setCursorColumn(this.screenSize.width - deleted); |
| 2071 | this.screen_.insertString(lib.f.getWhitespace(deleted)); |
| 2072 | this.restoreCursor(cursor); |
| 2073 | } |
| 2074 | |
David Benjamin | 54e8bf6 | 2012-06-01 22:31:40 -0400 | [diff] [blame] | 2075 | this.clearCursorOverflow(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2076 | }; |
| 2077 | |
| 2078 | /** |
| 2079 | * Shift rows in the scroll region upwards by a given number of lines. |
| 2080 | * |
| 2081 | * New rows are inserted at the bottom of the scroll region to fill the |
| 2082 | * vacated rows. The new rows not filled out with the current text attributes. |
| 2083 | * |
| 2084 | * This function does not affect the scrollback rows at all. Rows shifted |
| 2085 | * off the top are lost. |
| 2086 | * |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2087 | * The cursor position is not altered. |
| 2088 | * |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2089 | * @param {integer} count The number of rows to scroll. |
| 2090 | */ |
| 2091 | hterm.Terminal.prototype.vtScrollUp = function(count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2092 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2093 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2094 | this.setAbsoluteCursorRow(this.getVTScrollTop()); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2095 | this.deleteLines(count); |
| 2096 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2097 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2098 | }; |
| 2099 | |
| 2100 | /** |
| 2101 | * Shift rows below the cursor down by a given number of lines. |
| 2102 | * |
| 2103 | * This function respects the current scroll region. |
| 2104 | * |
| 2105 | * New rows are inserted at the top of the scroll region to fill the |
| 2106 | * vacated rows. The new rows not filled out with the current text attributes. |
| 2107 | * |
| 2108 | * This function does not affect the scrollback rows at all. Rows shifted |
| 2109 | * off the bottom are lost. |
| 2110 | * |
| 2111 | * @param {integer} count The number of rows to scroll. |
| 2112 | */ |
| 2113 | hterm.Terminal.prototype.vtScrollDown = function(opt_count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2114 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2115 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2116 | this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2117 | this.insertLines(opt_count); |
| 2118 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2119 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2120 | }; |
| 2121 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2122 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2123 | /** |
| 2124 | * Set the cursor position. |
| 2125 | * |
| 2126 | * The cursor row is relative to the scroll region if the terminal has |
| 2127 | * 'origin mode' enabled, or relative to the addressable screen otherwise. |
| 2128 | * |
| 2129 | * @param {integer} row The new zero-based cursor row. |
| 2130 | * @param {integer} row The new zero-based cursor column. |
| 2131 | */ |
| 2132 | hterm.Terminal.prototype.setCursorPosition = function(row, column) { |
| 2133 | if (this.options_.originMode) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2134 | this.setRelativeCursorPosition(row, column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2135 | } else { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2136 | this.setAbsoluteCursorPosition(row, column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2137 | } |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2138 | }; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2139 | |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 2140 | /** |
| 2141 | * Move the cursor relative to its current position. |
| 2142 | * |
| 2143 | * @param {number} row |
| 2144 | * @param {number} column |
| 2145 | */ |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2146 | hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) { |
| 2147 | var scrollTop = this.getVTScrollTop(); |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 2148 | row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom()); |
| 2149 | column = lib.f.clamp(column, 0, this.screenSize.width - 1); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2150 | this.screen_.setCursorPosition(row, column); |
| 2151 | }; |
| 2152 | |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 2153 | /** |
| 2154 | * Move the cursor to the specified position. |
| 2155 | * |
| 2156 | * @param {number} row |
| 2157 | * @param {number} column |
| 2158 | */ |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2159 | hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 2160 | row = lib.f.clamp(row, 0, this.screenSize.height - 1); |
| 2161 | column = lib.f.clamp(column, 0, this.screenSize.width - 1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2162 | this.screen_.setCursorPosition(row, column); |
| 2163 | }; |
| 2164 | |
| 2165 | /** |
| 2166 | * Set the cursor column. |
| 2167 | * |
| 2168 | * @param {integer} column The new zero-based cursor column. |
| 2169 | */ |
| 2170 | hterm.Terminal.prototype.setCursorColumn = function(column) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2171 | this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2172 | }; |
| 2173 | |
| 2174 | /** |
| 2175 | * Return the cursor column. |
| 2176 | * |
| 2177 | * @return {integer} The zero-based cursor column. |
| 2178 | */ |
| 2179 | hterm.Terminal.prototype.getCursorColumn = function() { |
| 2180 | return this.screen_.cursorPosition.column; |
| 2181 | }; |
| 2182 | |
| 2183 | /** |
| 2184 | * Set the cursor row. |
| 2185 | * |
| 2186 | * The cursor row is relative to the scroll region if the terminal has |
| 2187 | * 'origin mode' enabled, or relative to the addressable screen otherwise. |
| 2188 | * |
| 2189 | * @param {integer} row The new cursor row. |
| 2190 | */ |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2191 | hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) { |
| 2192 | this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2193 | }; |
| 2194 | |
| 2195 | /** |
| 2196 | * Return the cursor row. |
| 2197 | * |
| 2198 | * @return {integer} The zero-based cursor row. |
| 2199 | */ |
Mike Frysinger | cf3c762 | 2017-04-21 11:37:33 -0400 | [diff] [blame] | 2200 | hterm.Terminal.prototype.getCursorRow = function() { |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2201 | return this.screen_.cursorPosition.row; |
| 2202 | }; |
| 2203 | |
| 2204 | /** |
| 2205 | * Request that the ScrollPort redraw itself soon. |
| 2206 | * |
| 2207 | * The redraw will happen asynchronously, soon after the call stack winds down. |
| 2208 | * Multiple calls will be coalesced into a single redraw. |
| 2209 | */ |
| 2210 | hterm.Terminal.prototype.scheduleRedraw_ = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2211 | if (this.timeouts_.redraw) |
| 2212 | return; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2213 | |
| 2214 | var self = this; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2215 | this.timeouts_.redraw = setTimeout(function() { |
| 2216 | delete self.timeouts_.redraw; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2217 | self.scrollPort_.redraw_(); |
| 2218 | }, 0); |
| 2219 | }; |
| 2220 | |
| 2221 | /** |
| 2222 | * Request that the ScrollPort be scrolled to the bottom. |
| 2223 | * |
| 2224 | * The scroll will happen asynchronously, soon after the call stack winds down. |
| 2225 | * Multiple calls will be coalesced into a single scroll. |
| 2226 | * |
| 2227 | * This affects the scrollbar position of the ScrollPort, and has nothing to |
| 2228 | * do with the VT scroll commands. |
| 2229 | */ |
| 2230 | hterm.Terminal.prototype.scheduleScrollDown_ = function() { |
| 2231 | if (this.timeouts_.scrollDown) |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2232 | return; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2233 | |
| 2234 | var self = this; |
| 2235 | this.timeouts_.scrollDown = setTimeout(function() { |
| 2236 | delete self.timeouts_.scrollDown; |
| 2237 | self.scrollPort_.scrollRowToBottom(self.getRowCount()); |
| 2238 | }, 10); |
| 2239 | }; |
| 2240 | |
| 2241 | /** |
| 2242 | * Move the cursor up a specified number of rows. |
| 2243 | * |
| 2244 | * @param {integer} count The number of rows to move the cursor. |
| 2245 | */ |
| 2246 | hterm.Terminal.prototype.cursorUp = function(count) { |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 2247 | return this.cursorDown(-(count || 1)); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2248 | }; |
| 2249 | |
| 2250 | /** |
| 2251 | * Move the cursor down a specified number of rows. |
| 2252 | * |
| 2253 | * @param {integer} count The number of rows to move the cursor. |
| 2254 | */ |
| 2255 | hterm.Terminal.prototype.cursorDown = function(count) { |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 2256 | count = count || 1; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2257 | var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0); |
| 2258 | var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() : |
| 2259 | this.screenSize.height - 1); |
| 2260 | |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 2261 | var row = lib.f.clamp(this.screen_.cursorPosition.row + count, |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2262 | minHeight, maxHeight); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2263 | this.setAbsoluteCursorRow(row); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2264 | }; |
| 2265 | |
| 2266 | /** |
| 2267 | * Move the cursor left a specified number of columns. |
| 2268 | * |
Robert Ginda | aaba613 | 2014-07-16 16:33:07 -0700 | [diff] [blame] | 2269 | * If reverse wraparound mode is enabled and the previous row wrapped into |
| 2270 | * the current row then we back up through the wraparound as well. |
| 2271 | * |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2272 | * @param {integer} count The number of columns to move the cursor. |
| 2273 | */ |
| 2274 | hterm.Terminal.prototype.cursorLeft = function(count) { |
Robert Ginda | aaba613 | 2014-07-16 16:33:07 -0700 | [diff] [blame] | 2275 | count = count || 1; |
| 2276 | |
| 2277 | if (count < 1) |
| 2278 | return; |
| 2279 | |
| 2280 | var currentColumn = this.screen_.cursorPosition.column; |
Robert Ginda | bfb3262 | 2014-07-17 13:20:27 -0700 | [diff] [blame] | 2281 | if (this.options_.reverseWraparound) { |
| 2282 | if (this.screen_.cursorPosition.overflow) { |
| 2283 | // If this cursor is in the right margin, consume one count to get it |
| 2284 | // back to the last column. This only applies when we're in reverse |
| 2285 | // wraparound mode. |
| 2286 | count--; |
| 2287 | this.clearCursorOverflow(); |
| 2288 | |
| 2289 | if (!count) |
Robert Ginda | aaba613 | 2014-07-16 16:33:07 -0700 | [diff] [blame] | 2290 | return; |
Robert Ginda | aaba613 | 2014-07-16 16:33:07 -0700 | [diff] [blame] | 2291 | } |
| 2292 | |
Robert Ginda | bfb3262 | 2014-07-17 13:20:27 -0700 | [diff] [blame] | 2293 | var newRow = this.screen_.cursorPosition.row; |
| 2294 | var newColumn = currentColumn - count; |
| 2295 | if (newColumn < 0) { |
| 2296 | newRow = newRow - Math.floor(count / this.screenSize.width) - 1; |
| 2297 | if (newRow < 0) { |
| 2298 | // xterm also wraps from row 0 to the last row. |
| 2299 | newRow = this.screenSize.height + newRow % this.screenSize.height; |
| 2300 | } |
| 2301 | newColumn = this.screenSize.width + newColumn % this.screenSize.width; |
| 2302 | } |
Robert Ginda | aaba613 | 2014-07-16 16:33:07 -0700 | [diff] [blame] | 2303 | |
Robert Ginda | bfb3262 | 2014-07-17 13:20:27 -0700 | [diff] [blame] | 2304 | this.setCursorPosition(Math.max(newRow, 0), newColumn); |
| 2305 | |
| 2306 | } else { |
| 2307 | var newColumn = Math.max(currentColumn - count, 0); |
| 2308 | this.setCursorColumn(newColumn); |
| 2309 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2310 | }; |
| 2311 | |
| 2312 | /** |
| 2313 | * Move the cursor right a specified number of columns. |
| 2314 | * |
| 2315 | * @param {integer} count The number of columns to move the cursor. |
| 2316 | */ |
| 2317 | hterm.Terminal.prototype.cursorRight = function(count) { |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 2318 | count = count || 1; |
Robert Ginda | aaba613 | 2014-07-16 16:33:07 -0700 | [diff] [blame] | 2319 | |
| 2320 | if (count < 1) |
| 2321 | return; |
| 2322 | |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 2323 | var column = lib.f.clamp(this.screen_.cursorPosition.column + count, |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2324 | 0, this.screenSize.width - 1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2325 | this.setCursorColumn(column); |
| 2326 | }; |
| 2327 | |
| 2328 | /** |
| 2329 | * Reverse the foreground and background colors of the terminal. |
| 2330 | * |
| 2331 | * This only affects text that was drawn with no attributes. |
| 2332 | * |
| 2333 | * TODO(rginda): Test xterm to see if reverse is respected for text that has |
| 2334 | * been drawn with attributes that happen to coincide with the default |
| 2335 | * 'no-attribute' colors. My guess is probably not. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 2336 | * |
| 2337 | * @param {boolean} state The state to set. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2338 | */ |
| 2339 | hterm.Terminal.prototype.setReverseVideo = function(state) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2340 | this.options_.reverseVideo = state; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2341 | if (state) { |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 2342 | this.scrollPort_.setForegroundColor(this.prefs_.get('background-color')); |
| 2343 | this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color')); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2344 | } else { |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 2345 | this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color')); |
| 2346 | this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color')); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2347 | } |
| 2348 | }; |
| 2349 | |
| 2350 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2351 | * Ring the terminal bell. |
Robert Ginda | 92e1810 | 2013-03-14 13:56:37 -0700 | [diff] [blame] | 2352 | * |
| 2353 | * This will not play the bell audio more than once per second. |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2354 | */ |
| 2355 | hterm.Terminal.prototype.ringBell = function() { |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 2356 | this.cursorNode_.style.backgroundColor = |
| 2357 | this.scrollPort_.getForegroundColor(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2358 | |
| 2359 | var self = this; |
| 2360 | setTimeout(function() { |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 2361 | self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color'); |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 2362 | }, 200); |
Robert Ginda | 92e1810 | 2013-03-14 13:56:37 -0700 | [diff] [blame] | 2363 | |
Michael Kelly | 485ecd1 | 2014-06-09 11:41:56 -0400 | [diff] [blame] | 2364 | // bellSquelchTimeout_ affects both audio and notification bells. |
| 2365 | if (this.bellSquelchTimeout_) |
| 2366 | return; |
| 2367 | |
Robert Ginda | 92e1810 | 2013-03-14 13:56:37 -0700 | [diff] [blame] | 2368 | if (this.bellAudio_.getAttribute('src')) { |
Robert Ginda | 92e1810 | 2013-03-14 13:56:37 -0700 | [diff] [blame] | 2369 | this.bellAudio_.play(); |
Robert Ginda | 92e1810 | 2013-03-14 13:56:37 -0700 | [diff] [blame] | 2370 | this.bellSequelchTimeout_ = setTimeout(function() { |
| 2371 | delete this.bellSquelchTimeout_; |
Robert Ginda | a633137 | 2013-03-19 10:35:39 -0700 | [diff] [blame] | 2372 | }.bind(this), 500); |
Robert Ginda | 92e1810 | 2013-03-14 13:56:37 -0700 | [diff] [blame] | 2373 | } else { |
| 2374 | delete this.bellSquelchTimeout_; |
| 2375 | } |
Michael Kelly | 485ecd1 | 2014-06-09 11:41:56 -0400 | [diff] [blame] | 2376 | |
| 2377 | if (this.desktopNotificationBell_ && !this.document_.hasFocus()) { |
| 2378 | var n = new Notification( |
| 2379 | lib.f.replaceVars(hterm.desktopNotificationTitle, |
Robert Ginda | 348dc2b | 2014-06-24 14:42:23 -0700 | [diff] [blame] | 2380 | {'title': window.document.title || 'hterm'})); |
Michael Kelly | 485ecd1 | 2014-06-09 11:41:56 -0400 | [diff] [blame] | 2381 | this.bellNotificationList_.push(n); |
| 2382 | // TODO: Should we try to raise the window here? |
| 2383 | n.onclick = function() { self.closeBellNotifications_(); }; |
| 2384 | } |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2385 | }; |
| 2386 | |
| 2387 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2388 | * Set the origin mode bit. |
| 2389 | * |
| 2390 | * If origin mode is on, certain VT cursor and scrolling commands measure their |
| 2391 | * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds |
| 2392 | * to the top of the addressable screen. |
| 2393 | * |
| 2394 | * Defaults to off. |
| 2395 | * |
| 2396 | * @param {boolean} state True to set origin mode, false to unset. |
| 2397 | */ |
| 2398 | hterm.Terminal.prototype.setOriginMode = function(state) { |
| 2399 | this.options_.originMode = state; |
rginda | e4d2923 | 2012-01-19 10:47:13 -0800 | [diff] [blame] | 2400 | this.setCursorPosition(0, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2401 | }; |
| 2402 | |
| 2403 | /** |
| 2404 | * Set the insert mode bit. |
| 2405 | * |
| 2406 | * If insert mode is on, existing text beyond the cursor position will be |
| 2407 | * shifted right to make room for new text. Otherwise, new text overwrites |
| 2408 | * any existing text. |
| 2409 | * |
| 2410 | * Defaults to off. |
| 2411 | * |
| 2412 | * @param {boolean} state True to set insert mode, false to unset. |
| 2413 | */ |
| 2414 | hterm.Terminal.prototype.setInsertMode = function(state) { |
| 2415 | this.options_.insertMode = state; |
| 2416 | }; |
| 2417 | |
| 2418 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2419 | * Set the auto carriage return bit. |
| 2420 | * |
| 2421 | * If auto carriage return is on then a formfeed character is interpreted |
| 2422 | * as a newline, otherwise it's the same as a linefeed. The difference boils |
| 2423 | * down to whether or not the cursor column is reset. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 2424 | * |
| 2425 | * @param {boolean} state The state to set. |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2426 | */ |
| 2427 | hterm.Terminal.prototype.setAutoCarriageReturn = function(state) { |
| 2428 | this.options_.autoCarriageReturn = state; |
| 2429 | }; |
| 2430 | |
| 2431 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2432 | * Set the wraparound mode bit. |
| 2433 | * |
| 2434 | * If wraparound mode is on, certain VT commands will allow the cursor to wrap |
| 2435 | * to the start of the following row. Otherwise, the cursor is clamped to the |
| 2436 | * end of the screen and attempts to write past it are ignored. |
| 2437 | * |
| 2438 | * Defaults to on. |
| 2439 | * |
| 2440 | * @param {boolean} state True to set wraparound mode, false to unset. |
| 2441 | */ |
| 2442 | hterm.Terminal.prototype.setWraparound = function(state) { |
| 2443 | this.options_.wraparound = state; |
| 2444 | }; |
| 2445 | |
| 2446 | /** |
| 2447 | * Set the reverse-wraparound mode bit. |
| 2448 | * |
| 2449 | * If wraparound mode is off, certain VT commands will allow the cursor to wrap |
| 2450 | * to the end of the previous row. Otherwise, the cursor is clamped to column |
| 2451 | * 0. |
| 2452 | * |
| 2453 | * Defaults to off. |
| 2454 | * |
| 2455 | * @param {boolean} state True to set reverse-wraparound mode, false to unset. |
| 2456 | */ |
| 2457 | hterm.Terminal.prototype.setReverseWraparound = function(state) { |
| 2458 | this.options_.reverseWraparound = state; |
| 2459 | }; |
| 2460 | |
| 2461 | /** |
| 2462 | * Selects between the primary and alternate screens. |
| 2463 | * |
| 2464 | * If alternate mode is on, the alternate screen is active. Otherwise the |
| 2465 | * primary screen is active. |
| 2466 | * |
| 2467 | * Swapping screens has no effect on the scrollback buffer. |
| 2468 | * |
| 2469 | * Each screen maintains its own cursor position. |
| 2470 | * |
| 2471 | * Defaults to off. |
| 2472 | * |
| 2473 | * @param {boolean} state True to set alternate mode, false to unset. |
| 2474 | */ |
| 2475 | hterm.Terminal.prototype.setAlternateMode = function(state) { |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 2476 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2477 | this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_; |
| 2478 | |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 2479 | if (this.screen_.rowsArray.length && |
| 2480 | this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) { |
| 2481 | // If the screen changed sizes while we were away, our rowIndexes may |
| 2482 | // be incorrect. |
| 2483 | var offset = this.scrollbackRows_.length; |
| 2484 | var ary = this.screen_.rowsArray; |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 2485 | for (var i = 0; i < ary.length; i++) { |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 2486 | ary[i].rowIndex = offset + i; |
| 2487 | } |
| 2488 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2489 | |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 2490 | this.realizeWidth_(this.screenSize.width); |
| 2491 | this.realizeHeight_(this.screenSize.height); |
| 2492 | this.scrollPort_.syncScrollHeight(); |
| 2493 | this.scrollPort_.invalidate(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2494 | |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 2495 | this.restoreCursor(cursor); |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 2496 | this.scrollPort_.resize(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2497 | }; |
| 2498 | |
| 2499 | /** |
| 2500 | * Set the cursor-blink mode bit. |
| 2501 | * |
| 2502 | * If cursor-blink is on, the cursor will blink when it is visible. Otherwise |
| 2503 | * a visible cursor does not blink. |
| 2504 | * |
| 2505 | * You should make sure to turn blinking off if you're going to dispose of a |
| 2506 | * terminal, otherwise you'll leak a timeout. |
| 2507 | * |
| 2508 | * Defaults to on. |
| 2509 | * |
| 2510 | * @param {boolean} state True to set cursor-blink mode, false to unset. |
| 2511 | */ |
| 2512 | hterm.Terminal.prototype.setCursorBlink = function(state) { |
| 2513 | this.options_.cursorBlink = state; |
| 2514 | |
| 2515 | if (!state && this.timeouts_.cursorBlink) { |
| 2516 | clearTimeout(this.timeouts_.cursorBlink); |
| 2517 | delete this.timeouts_.cursorBlink; |
| 2518 | } |
| 2519 | |
| 2520 | if (this.options_.cursorVisible) |
| 2521 | this.setCursorVisible(true); |
| 2522 | }; |
| 2523 | |
| 2524 | /** |
| 2525 | * Set the cursor-visible mode bit. |
| 2526 | * |
| 2527 | * If cursor-visible is on, the cursor will be visible. Otherwise it will not. |
| 2528 | * |
| 2529 | * Defaults to on. |
| 2530 | * |
| 2531 | * @param {boolean} state True to set cursor-visible mode, false to unset. |
| 2532 | */ |
| 2533 | hterm.Terminal.prototype.setCursorVisible = function(state) { |
| 2534 | this.options_.cursorVisible = state; |
| 2535 | |
| 2536 | if (!state) { |
Brad Town | 1c2afa8 | 2015-03-11 21:36:58 -0700 | [diff] [blame] | 2537 | if (this.timeouts_.cursorBlink) { |
| 2538 | clearTimeout(this.timeouts_.cursorBlink); |
| 2539 | delete this.timeouts_.cursorBlink; |
| 2540 | } |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2541 | this.cursorNode_.style.opacity = '0'; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2542 | return; |
| 2543 | } |
| 2544 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2545 | this.syncCursorPosition_(); |
| 2546 | |
| 2547 | this.cursorNode_.style.opacity = '1'; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2548 | |
| 2549 | if (this.options_.cursorBlink) { |
| 2550 | if (this.timeouts_.cursorBlink) |
| 2551 | return; |
| 2552 | |
Robert Ginda | ea2183e | 2014-07-17 09:51:51 -0700 | [diff] [blame] | 2553 | this.onCursorBlink_(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2554 | } else { |
| 2555 | if (this.timeouts_.cursorBlink) { |
| 2556 | clearTimeout(this.timeouts_.cursorBlink); |
| 2557 | delete this.timeouts_.cursorBlink; |
| 2558 | } |
| 2559 | } |
| 2560 | }; |
| 2561 | |
| 2562 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2563 | * Synchronizes the visible cursor and document selection with the current |
| 2564 | * cursor coordinates. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2565 | */ |
| 2566 | hterm.Terminal.prototype.syncCursorPosition_ = function() { |
| 2567 | var topRowIndex = this.scrollPort_.getTopRowIndex(); |
| 2568 | var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex); |
| 2569 | var cursorRowIndex = this.scrollbackRows_.length + |
| 2570 | this.screen_.cursorPosition.row; |
| 2571 | |
| 2572 | if (cursorRowIndex > bottomRowIndex) { |
| 2573 | // Cursor is scrolled off screen, move it outside of the visible area. |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 2574 | this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px'; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2575 | return; |
| 2576 | } |
| 2577 | |
Robert Ginda | b837c05 | 2014-08-11 11:17:51 -0700 | [diff] [blame] | 2578 | if (this.options_.cursorVisible && |
| 2579 | this.cursorNode_.style.display == 'none') { |
| 2580 | // Re-display the terminal cursor if it was hidden by the mouse cursor. |
| 2581 | this.cursorNode_.style.display = ''; |
| 2582 | } |
| 2583 | |
| 2584 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2585 | this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin + |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 2586 | this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) + |
| 2587 | 'px'; |
| 2588 | this.cursorNode_.style.left = this.scrollPort_.characterSize.width * |
| 2589 | this.screen_.cursorPosition.column + 'px'; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2590 | |
| 2591 | this.cursorNode_.setAttribute('title', |
| 2592 | '(' + this.screen_.cursorPosition.row + |
| 2593 | ', ' + this.screen_.cursorPosition.column + |
| 2594 | ')'); |
| 2595 | |
| 2596 | // Update the caret for a11y purposes. |
| 2597 | var selection = this.document_.getSelection(); |
| 2598 | if (selection && selection.isCollapsed) |
| 2599 | this.screen_.syncSelectionCaret(selection); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2600 | }; |
| 2601 | |
Robert Ginda | fb1be6a | 2013-12-11 11:56:22 -0800 | [diff] [blame] | 2602 | /** |
| 2603 | * Adjusts the style of this.cursorNode_ according to the current cursor shape |
| 2604 | * and character cell dimensions. |
| 2605 | */ |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 2606 | hterm.Terminal.prototype.restyleCursor_ = function() { |
| 2607 | var shape = this.cursorShape_; |
| 2608 | |
| 2609 | if (this.cursorNode_.getAttribute('focus') == 'false') { |
| 2610 | // Always show a block cursor when unfocused. |
| 2611 | shape = hterm.Terminal.cursorShape.BLOCK; |
| 2612 | } |
| 2613 | |
| 2614 | var style = this.cursorNode_.style; |
| 2615 | |
Robert Ginda | fb1be6a | 2013-12-11 11:56:22 -0800 | [diff] [blame] | 2616 | style.width = this.scrollPort_.characterSize.width + 'px'; |
| 2617 | |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 2618 | switch (shape) { |
| 2619 | case hterm.Terminal.cursorShape.BEAM: |
| 2620 | style.height = this.scrollPort_.characterSize.height + 'px'; |
| 2621 | style.backgroundColor = 'transparent'; |
| 2622 | style.borderBottomStyle = null; |
| 2623 | style.borderLeftStyle = 'solid'; |
| 2624 | break; |
| 2625 | |
| 2626 | case hterm.Terminal.cursorShape.UNDERLINE: |
| 2627 | style.height = this.scrollPort_.characterSize.baseline + 'px'; |
| 2628 | style.backgroundColor = 'transparent'; |
| 2629 | style.borderBottomStyle = 'solid'; |
| 2630 | // correct the size to put it exactly at the baseline |
| 2631 | style.borderLeftStyle = null; |
| 2632 | break; |
| 2633 | |
| 2634 | default: |
| 2635 | style.height = this.scrollPort_.characterSize.height + 'px'; |
| 2636 | style.backgroundColor = this.cursorColor_; |
| 2637 | style.borderBottomStyle = null; |
| 2638 | style.borderLeftStyle = null; |
| 2639 | break; |
| 2640 | } |
| 2641 | }; |
| 2642 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2643 | /** |
| 2644 | * Synchronizes the visible cursor with the current cursor coordinates. |
| 2645 | * |
| 2646 | * The sync will happen asynchronously, soon after the call stack winds down. |
| 2647 | * Multiple calls will be coalesced into a single sync. |
| 2648 | */ |
| 2649 | hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() { |
| 2650 | if (this.timeouts_.syncCursor) |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2651 | return; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2652 | |
| 2653 | var self = this; |
| 2654 | this.timeouts_.syncCursor = setTimeout(function() { |
| 2655 | self.syncCursorPosition_(); |
| 2656 | delete self.timeouts_.syncCursor; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2657 | }, 0); |
| 2658 | }; |
| 2659 | |
rginda | cc2996c | 2012-02-24 14:59:31 -0800 | [diff] [blame] | 2660 | /** |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 2661 | * Show or hide the zoom warning. |
| 2662 | * |
| 2663 | * The zoom warning is a message warning the user that their browser zoom must |
| 2664 | * be set to 100% in order for hterm to function properly. |
| 2665 | * |
| 2666 | * @param {boolean} state True to show the message, false to hide it. |
| 2667 | */ |
| 2668 | hterm.Terminal.prototype.showZoomWarning_ = function(state) { |
| 2669 | if (!this.zoomWarningNode_) { |
| 2670 | if (!state) |
| 2671 | return; |
| 2672 | |
| 2673 | this.zoomWarningNode_ = this.document_.createElement('div'); |
| 2674 | this.zoomWarningNode_.style.cssText = ( |
| 2675 | 'color: black;' + |
| 2676 | 'background-color: #ff2222;' + |
| 2677 | 'font-size: large;' + |
| 2678 | 'border-radius: 8px;' + |
| 2679 | 'opacity: 0.75;' + |
| 2680 | 'padding: 0.2em 0.5em 0.2em 0.5em;' + |
| 2681 | 'top: 0.5em;' + |
| 2682 | 'right: 1.2em;' + |
| 2683 | 'position: absolute;' + |
| 2684 | '-webkit-text-size-adjust: none;' + |
Rob Spies | 06533ba | 2014-04-24 11:20:37 -0700 | [diff] [blame] | 2685 | '-webkit-user-select: none;' + |
| 2686 | '-moz-text-size-adjust: none;' + |
| 2687 | '-moz-user-select: none;'); |
Mike Frysinger | 4c0c5e0 | 2016-03-12 23:11:25 -0500 | [diff] [blame] | 2688 | |
| 2689 | this.zoomWarningNode_.addEventListener('click', function(e) { |
| 2690 | this.parentNode.removeChild(this); |
| 2691 | }); |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 2692 | } |
| 2693 | |
Robert Ginda | b4839c2 | 2013-02-28 16:52:10 -0800 | [diff] [blame] | 2694 | this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences( |
| 2695 | hterm.zoomWarningMessage, |
| 2696 | [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]); |
| 2697 | |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 2698 | this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family'); |
| 2699 | |
| 2700 | if (state) { |
| 2701 | if (!this.zoomWarningNode_.parentNode) |
| 2702 | this.div_.parentNode.appendChild(this.zoomWarningNode_); |
| 2703 | } else if (this.zoomWarningNode_.parentNode) { |
| 2704 | this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_); |
| 2705 | } |
| 2706 | }; |
| 2707 | |
| 2708 | /** |
rginda | cc2996c | 2012-02-24 14:59:31 -0800 | [diff] [blame] | 2709 | * Show the terminal overlay for a given amount of time. |
| 2710 | * |
| 2711 | * The terminal overlay appears in inverse video in a large font, centered |
| 2712 | * over the terminal. You should probably keep the overlay message brief, |
| 2713 | * since it's in a large font and you probably aren't going to check the size |
| 2714 | * of the terminal first. |
| 2715 | * |
| 2716 | * @param {string} msg The text (not HTML) message to display in the overlay. |
| 2717 | * @param {number} opt_timeout The amount of time to wait before fading out |
| 2718 | * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay |
| 2719 | * stay up forever (or until the next overlay). |
| 2720 | */ |
| 2721 | hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) { |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2722 | if (!this.overlayNode_) { |
| 2723 | if (!this.div_) |
| 2724 | return; |
| 2725 | |
| 2726 | this.overlayNode_ = this.document_.createElement('div'); |
| 2727 | this.overlayNode_.style.cssText = ( |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2728 | 'border-radius: 15px;' + |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2729 | 'font-size: xx-large;' + |
| 2730 | 'opacity: 0.75;' + |
| 2731 | 'padding: 0.2em 0.5em 0.2em 0.5em;' + |
| 2732 | 'position: absolute;' + |
| 2733 | '-webkit-user-select: none;' + |
Rob Spies | 06533ba | 2014-04-24 11:20:37 -0700 | [diff] [blame] | 2734 | '-webkit-transition: opacity 180ms ease-in;' + |
| 2735 | '-moz-user-select: none;' + |
| 2736 | '-moz-transition: opacity 180ms ease-in;'); |
Robert Ginda | 70926e4 | 2013-11-25 14:56:36 -0800 | [diff] [blame] | 2737 | |
| 2738 | this.overlayNode_.addEventListener('mousedown', function(e) { |
| 2739 | e.preventDefault(); |
| 2740 | e.stopPropagation(); |
| 2741 | }, true); |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2742 | } |
| 2743 | |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 2744 | this.overlayNode_.style.color = this.prefs_.get('background-color'); |
| 2745 | this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color'); |
| 2746 | this.overlayNode_.style.fontFamily = this.prefs_.get('font-family'); |
| 2747 | |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2748 | this.overlayNode_.textContent = msg; |
| 2749 | this.overlayNode_.style.opacity = '0.75'; |
| 2750 | |
| 2751 | if (!this.overlayNode_.parentNode) |
| 2752 | this.div_.appendChild(this.overlayNode_); |
| 2753 | |
Robert Ginda | 9776928 | 2013-02-01 15:30:30 -0800 | [diff] [blame] | 2754 | var divSize = hterm.getClientSize(this.div_); |
| 2755 | var overlaySize = hterm.getClientSize(this.overlayNode_); |
| 2756 | |
Robert Ginda | 8a59f76 | 2014-07-23 11:29:55 -0700 | [diff] [blame] | 2757 | this.overlayNode_.style.top = |
| 2758 | (divSize.height - overlaySize.height) / 2 + 'px'; |
Robert Ginda | 9776928 | 2013-02-01 15:30:30 -0800 | [diff] [blame] | 2759 | this.overlayNode_.style.left = (divSize.width - overlaySize.width - |
Robert Ginda | 8a59f76 | 2014-07-23 11:29:55 -0700 | [diff] [blame] | 2760 | this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px'; |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2761 | |
| 2762 | var self = this; |
| 2763 | |
| 2764 | if (this.overlayTimeout_) |
| 2765 | clearTimeout(this.overlayTimeout_); |
| 2766 | |
rginda | cc2996c | 2012-02-24 14:59:31 -0800 | [diff] [blame] | 2767 | if (opt_timeout === null) |
| 2768 | return; |
| 2769 | |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2770 | this.overlayTimeout_ = setTimeout(function() { |
| 2771 | self.overlayNode_.style.opacity = '0'; |
Robert Ginda | 70926e4 | 2013-11-25 14:56:36 -0800 | [diff] [blame] | 2772 | self.overlayTimeout_ = setTimeout(function() { |
rginda | 259dcca | 2012-03-14 16:37:11 -0700 | [diff] [blame] | 2773 | if (self.overlayNode_.parentNode) |
| 2774 | self.overlayNode_.parentNode.removeChild(self.overlayNode_); |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2775 | self.overlayTimeout_ = null; |
| 2776 | self.overlayNode_.style.opacity = '0.75'; |
| 2777 | }, 200); |
rginda | cc2996c | 2012-02-24 14:59:31 -0800 | [diff] [blame] | 2778 | }, opt_timeout || 1500); |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2779 | }; |
| 2780 | |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2781 | /** |
| 2782 | * Paste from the system clipboard to the terminal. |
| 2783 | */ |
| 2784 | hterm.Terminal.prototype.paste = function() { |
| 2785 | hterm.pasteFromClipboard(this.document_); |
| 2786 | }; |
| 2787 | |
| 2788 | /** |
| 2789 | * Copy a string to the system clipboard. |
| 2790 | * |
| 2791 | * Note: If there is a selected range in the terminal, it'll be cleared. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 2792 | * |
| 2793 | * @param {string} str The string to copy. |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2794 | */ |
| 2795 | hterm.Terminal.prototype.copyStringToClipboard = function(str) { |
Robert Ginda | 4e4d42c | 2014-03-04 14:07:23 -0800 | [diff] [blame] | 2796 | if (this.prefs_.get('enable-clipboard-notice')) |
| 2797 | setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200); |
| 2798 | |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 2799 | var copySource = this.document_.createElement('pre'); |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2800 | copySource.textContent = str; |
| 2801 | copySource.style.cssText = ( |
| 2802 | '-webkit-user-select: text;' + |
Rob Spies | 06533ba | 2014-04-24 11:20:37 -0700 | [diff] [blame] | 2803 | '-moz-user-select: text;' + |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2804 | 'position: absolute;' + |
| 2805 | 'top: -99px'); |
| 2806 | |
| 2807 | this.document_.body.appendChild(copySource); |
rginda | faa7474 | 2012-08-21 13:34:03 -0700 | [diff] [blame] | 2808 | |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2809 | var selection = this.document_.getSelection(); |
rginda | faa7474 | 2012-08-21 13:34:03 -0700 | [diff] [blame] | 2810 | var anchorNode = selection.anchorNode; |
| 2811 | var anchorOffset = selection.anchorOffset; |
| 2812 | var focusNode = selection.focusNode; |
| 2813 | var focusOffset = selection.focusOffset; |
| 2814 | |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2815 | selection.selectAllChildren(copySource); |
| 2816 | |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 2817 | hterm.copySelectionToClipboard(this.document_); |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2818 | |
Rob Spies | 5695341 | 2014-04-28 14:09:47 -0700 | [diff] [blame] | 2819 | // IE doesn't support selection.extend. This means that the selection |
| 2820 | // won't return on IE. |
Robert Ginda | 6f4f0aa | 2014-07-28 10:21:39 -0700 | [diff] [blame] | 2821 | if (selection.extend) { |
Rob Spies | 5695341 | 2014-04-28 14:09:47 -0700 | [diff] [blame] | 2822 | selection.collapse(anchorNode, anchorOffset); |
| 2823 | selection.extend(focusNode, focusOffset); |
| 2824 | } |
rginda | faa7474 | 2012-08-21 13:34:03 -0700 | [diff] [blame] | 2825 | |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2826 | copySource.parentNode.removeChild(copySource); |
| 2827 | }; |
| 2828 | |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 2829 | /** |
| 2830 | * Returns the selected text, or null if no text is selected. |
| 2831 | * |
| 2832 | * @return {string|null} |
| 2833 | */ |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 2834 | hterm.Terminal.prototype.getSelectionText = function() { |
| 2835 | var selection = this.scrollPort_.selection; |
| 2836 | selection.sync(); |
| 2837 | |
| 2838 | if (selection.isCollapsed) |
| 2839 | return null; |
| 2840 | |
| 2841 | |
| 2842 | // Start offset measures from the beginning of the line. |
| 2843 | var startOffset = selection.startOffset; |
| 2844 | var node = selection.startNode; |
Robert Ginda | 0d19050 | 2012-10-02 10:59:00 -0700 | [diff] [blame] | 2845 | |
Robert Ginda | fdbb3f2 | 2012-09-06 20:23:06 -0700 | [diff] [blame] | 2846 | if (node.nodeName != 'X-ROW') { |
| 2847 | // If the selection doesn't start on an x-row node, then it must be |
| 2848 | // somewhere inside the x-row. Add any characters from previous siblings |
| 2849 | // into the start offset. |
Robert Ginda | 0d19050 | 2012-10-02 10:59:00 -0700 | [diff] [blame] | 2850 | |
| 2851 | if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') { |
| 2852 | // If node is the text node in a styled span, move up to the span node. |
| 2853 | node = node.parentNode; |
| 2854 | } |
| 2855 | |
Robert Ginda | fdbb3f2 | 2012-09-06 20:23:06 -0700 | [diff] [blame] | 2856 | while (node.previousSibling) { |
| 2857 | node = node.previousSibling; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 2858 | startOffset += hterm.TextAttributes.nodeWidth(node); |
Robert Ginda | fdbb3f2 | 2012-09-06 20:23:06 -0700 | [diff] [blame] | 2859 | } |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 2860 | } |
| 2861 | |
| 2862 | // End offset measures from the end of the line. |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 2863 | var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) - |
| 2864 | selection.endOffset); |
Evan Jones | 5f9df81 | 2016-12-06 09:38:58 -0500 | [diff] [blame] | 2865 | node = selection.endNode; |
Robert Ginda | 0d19050 | 2012-10-02 10:59:00 -0700 | [diff] [blame] | 2866 | |
Robert Ginda | fdbb3f2 | 2012-09-06 20:23:06 -0700 | [diff] [blame] | 2867 | if (node.nodeName != 'X-ROW') { |
| 2868 | // If the selection doesn't end on an x-row node, then it must be |
| 2869 | // somewhere inside the x-row. Add any characters from following siblings |
| 2870 | // into the end offset. |
Robert Ginda | 0d19050 | 2012-10-02 10:59:00 -0700 | [diff] [blame] | 2871 | |
| 2872 | if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') { |
| 2873 | // If node is the text node in a styled span, move up to the span node. |
| 2874 | node = node.parentNode; |
| 2875 | } |
| 2876 | |
Robert Ginda | fdbb3f2 | 2012-09-06 20:23:06 -0700 | [diff] [blame] | 2877 | while (node.nextSibling) { |
| 2878 | node = node.nextSibling; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 2879 | endOffset += hterm.TextAttributes.nodeWidth(node); |
Robert Ginda | fdbb3f2 | 2012-09-06 20:23:06 -0700 | [diff] [blame] | 2880 | } |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 2881 | } |
| 2882 | |
| 2883 | var rv = this.getRowsText(selection.startRow.rowIndex, |
| 2884 | selection.endRow.rowIndex + 1); |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 2885 | return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset); |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 2886 | }; |
| 2887 | |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2888 | /** |
| 2889 | * Copy the current selection to the system clipboard, then clear it after a |
| 2890 | * short delay. |
| 2891 | */ |
| 2892 | hterm.Terminal.prototype.copySelectionToClipboard = function() { |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 2893 | var text = this.getSelectionText(); |
| 2894 | if (text != null) |
| 2895 | this.copyStringToClipboard(text); |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2896 | }; |
| 2897 | |
rginda | f0090c9 | 2012-02-10 14:58:52 -0800 | [diff] [blame] | 2898 | hterm.Terminal.prototype.overlaySize = function() { |
| 2899 | this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height); |
| 2900 | }; |
| 2901 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2902 | /** |
| 2903 | * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected. |
| 2904 | * |
Robert Ginda | 8cb7d90 | 2013-06-20 14:37:18 -0700 | [diff] [blame] | 2905 | * @param {string} string The VT string representing the keystroke, in UTF-16. |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2906 | */ |
| 2907 | hterm.Terminal.prototype.onVTKeystroke = function(string) { |
rginda | 9f5222b | 2012-03-05 11:53:28 -0800 | [diff] [blame] | 2908 | if (this.scrollOnKeystroke_) |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 2909 | this.scrollPort_.scrollRowToBottom(this.getRowCount()); |
| 2910 | |
Robert Ginda | 8cb7d90 | 2013-06-20 14:37:18 -0700 | [diff] [blame] | 2911 | this.io.onVTKeystroke(this.keyboard.encode(string)); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 2912 | }; |
| 2913 | |
| 2914 | /** |
Mike Frysinger | 70b9469 | 2017-01-26 18:57:50 -1000 | [diff] [blame] | 2915 | * Launches url in a new tab. |
| 2916 | * |
| 2917 | * @param {string} url URL to launch in a new tab. |
| 2918 | */ |
| 2919 | hterm.Terminal.prototype.openUrl = function(url) { |
| 2920 | var win = window.open(url, '_blank'); |
| 2921 | win.focus(); |
| 2922 | } |
| 2923 | |
| 2924 | /** |
| 2925 | * Open the selected url. |
| 2926 | */ |
| 2927 | hterm.Terminal.prototype.openSelectedUrl_ = function() { |
| 2928 | var str = this.getSelectionText(); |
| 2929 | |
| 2930 | // If there is no selection, try and expand wherever they clicked. |
| 2931 | if (str == null) { |
| 2932 | this.screen_.expandSelection(this.document_.getSelection()); |
| 2933 | str = this.getSelectionText(); |
| 2934 | } |
| 2935 | |
| 2936 | // Make sure URL is valid before opening. |
| 2937 | if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0) |
| 2938 | return; |
| 2939 | // If the URL isn't anchored, it'll open relative to the extension. |
| 2940 | // We have no way of knowing the correct schema, so assume http. |
| 2941 | if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) |
| 2942 | str = 'http://' + str; |
| 2943 | |
| 2944 | this.openUrl(str); |
| 2945 | } |
| 2946 | |
| 2947 | |
| 2948 | /** |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 2949 | * Add the terminalRow and terminalColumn properties to mouse events and |
| 2950 | * then forward on to onMouse(). |
| 2951 | * |
| 2952 | * The terminalRow and terminalColumn properties contain the (row, column) |
| 2953 | * coordinates for the mouse event. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 2954 | * |
| 2955 | * @param {Event} e The mouse event to handle. |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 2956 | */ |
| 2957 | hterm.Terminal.prototype.onMouse_ = function(e) { |
rginda | faa7474 | 2012-08-21 13:34:03 -0700 | [diff] [blame] | 2958 | if (e.processedByTerminalHandler_) { |
| 2959 | // We register our event handlers on the document, as well as the cursor |
| 2960 | // and the scroll blocker. Mouse events that occur on the cursor or |
| 2961 | // scroll blocker will also appear on the document, but we don't want to |
| 2962 | // process them twice. |
| 2963 | // |
| 2964 | // We can't just prevent bubbling because that has other side effects, so |
| 2965 | // we decorate the event object with this property instead. |
| 2966 | return; |
| 2967 | } |
| 2968 | |
Robert Ginda | 6aec7eb | 2015-06-16 10:31:30 -0700 | [diff] [blame] | 2969 | var reportMouseEvents = (!this.defeatMouseReports_ && |
| 2970 | this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED); |
| 2971 | |
rginda | faa7474 | 2012-08-21 13:34:03 -0700 | [diff] [blame] | 2972 | e.processedByTerminalHandler_ = true; |
| 2973 | |
Robert Ginda | eda48db | 2014-07-17 09:25:30 -0700 | [diff] [blame] | 2974 | // One based row/column stored on the mouse event. |
| 2975 | e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) / |
| 2976 | this.scrollPort_.characterSize.height) + 1; |
| 2977 | e.terminalColumn = parseInt(e.clientX / |
| 2978 | this.scrollPort_.characterSize.width) + 1; |
| 2979 | |
Robert Ginda | 4e24f8f | 2014-01-08 14:45:06 -0800 | [diff] [blame] | 2980 | if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) { |
| 2981 | // Mousedown in the scrollbar area. |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 2982 | return; |
| 2983 | } |
| 2984 | |
Robert Ginda | 6aec7eb | 2015-06-16 10:31:30 -0700 | [diff] [blame] | 2985 | if (this.options_.cursorVisible && !reportMouseEvents) { |
Robert Ginda | b837c05 | 2014-08-11 11:17:51 -0700 | [diff] [blame] | 2986 | // If the cursor is visible and we're not sending mouse events to the |
| 2987 | // host app, then we want to hide the terminal cursor when the mouse |
| 2988 | // cursor is over top. This keeps the terminal cursor from interfering |
| 2989 | // with local text selection. |
Robert Ginda | eda48db | 2014-07-17 09:25:30 -0700 | [diff] [blame] | 2990 | if (e.terminalRow - 1 == this.screen_.cursorPosition.row && |
| 2991 | e.terminalColumn - 1 == this.screen_.cursorPosition.column) { |
| 2992 | this.cursorNode_.style.display = 'none'; |
| 2993 | } else if (this.cursorNode_.style.display == 'none') { |
| 2994 | this.cursorNode_.style.display = ''; |
| 2995 | } |
| 2996 | } |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 2997 | |
Robert Ginda | 928cf63 | 2014-03-05 15:07:41 -0800 | [diff] [blame] | 2998 | if (e.type == 'mousedown') { |
Robert Ginda | 6aec7eb | 2015-06-16 10:31:30 -0700 | [diff] [blame] | 2999 | if (e.altKey || !reportMouseEvents) { |
Robert Ginda | 928cf63 | 2014-03-05 15:07:41 -0800 | [diff] [blame] | 3000 | // If VT mouse reporting is disabled, or has been defeated with |
| 3001 | // alt-mousedown, then the mouse will act on the local selection. |
Robert Ginda | 6aec7eb | 2015-06-16 10:31:30 -0700 | [diff] [blame] | 3002 | this.defeatMouseReports_ = true; |
Robert Ginda | 928cf63 | 2014-03-05 15:07:41 -0800 | [diff] [blame] | 3003 | this.setSelectionEnabled(true); |
| 3004 | } else { |
| 3005 | // Otherwise we defer ownership of the mouse to the VT. |
Robert Ginda | 6aec7eb | 2015-06-16 10:31:30 -0700 | [diff] [blame] | 3006 | this.defeatMouseReports_ = false; |
Robert Ginda | 3ae3782 | 2014-05-15 13:05:35 -0700 | [diff] [blame] | 3007 | this.document_.getSelection().collapseToEnd(); |
Robert Ginda | 928cf63 | 2014-03-05 15:07:41 -0800 | [diff] [blame] | 3008 | this.setSelectionEnabled(false); |
| 3009 | e.preventDefault(); |
| 3010 | } |
| 3011 | } |
| 3012 | |
Robert Ginda | 6aec7eb | 2015-06-16 10:31:30 -0700 | [diff] [blame] | 3013 | if (!reportMouseEvents) { |
Robert Ginda | 15ed490 | 2016-07-12 10:43:22 -0700 | [diff] [blame] | 3014 | if (e.type == 'dblclick' && this.copyOnSelect) { |
Robert Ginda | 4e24f8f | 2014-01-08 14:45:06 -0800 | [diff] [blame] | 3015 | this.screen_.expandSelection(this.document_.getSelection()); |
Robert Ginda | 15ed490 | 2016-07-12 10:43:22 -0700 | [diff] [blame] | 3016 | this.copySelectionToClipboard(this.document_); |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 3017 | } |
| 3018 | |
Mike Frysinger | 70b9469 | 2017-01-26 18:57:50 -1000 | [diff] [blame] | 3019 | if (e.type == 'click' && !e.shiftKey && e.ctrlKey) { |
| 3020 | // Debounce this event with the dblclick event. If you try to doubleclick |
| 3021 | // a URL to open it, Chrome will fire click then dblclick, but we won't |
| 3022 | // have expanded the selection text at the first click event. |
| 3023 | clearTimeout(this.timeouts_.openUrl); |
| 3024 | this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this), |
| 3025 | 500); |
| 3026 | return; |
| 3027 | } |
| 3028 | |
Robert Ginda | 928cf63 | 2014-03-05 15:07:41 -0800 | [diff] [blame] | 3029 | if (e.type == 'mousedown' && e.which == this.mousePasteButton) |
Robert Ginda | 4e24f8f | 2014-01-08 14:45:06 -0800 | [diff] [blame] | 3030 | this.paste(); |
Robert Ginda | 4e24f8f | 2014-01-08 14:45:06 -0800 | [diff] [blame] | 3031 | |
| 3032 | if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect && |
| 3033 | !this.document_.getSelection().isCollapsed) { |
Robert Ginda | 15ed490 | 2016-07-12 10:43:22 -0700 | [diff] [blame] | 3034 | this.copySelectionToClipboard(this.document_); |
Robert Ginda | 4e24f8f | 2014-01-08 14:45:06 -0800 | [diff] [blame] | 3035 | } |
| 3036 | |
| 3037 | if ((e.type == 'mousemove' || e.type == 'mouseup') && |
| 3038 | this.scrollBlockerNode_.engaged) { |
| 3039 | // Disengage the scroll-blocker after one of these events. |
| 3040 | this.scrollBlockerNode_.engaged = false; |
| 3041 | this.scrollBlockerNode_.style.top = '-99px'; |
| 3042 | } |
| 3043 | |
Robert Ginda | 928cf63 | 2014-03-05 15:07:41 -0800 | [diff] [blame] | 3044 | } else /* if (this.reportMouseEvents) */ { |
Robert Ginda | 4e24f8f | 2014-01-08 14:45:06 -0800 | [diff] [blame] | 3045 | if (!this.scrollBlockerNode_.engaged) { |
| 3046 | if (e.type == 'mousedown') { |
| 3047 | // Move the scroll-blocker into place if we want to keep the scrollport |
| 3048 | // from scrolling. |
| 3049 | this.scrollBlockerNode_.engaged = true; |
| 3050 | this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px'; |
| 3051 | this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px'; |
| 3052 | } else if (e.type == 'mousemove') { |
| 3053 | // Oh. This means that drag-scroll was disabled AFTER the mouse down, |
| 3054 | // in which case it's too late to engage the scroll-blocker. |
Robert Ginda | 3ae3782 | 2014-05-15 13:05:35 -0700 | [diff] [blame] | 3055 | this.document_.getSelection().collapseToEnd(); |
Robert Ginda | 4e24f8f | 2014-01-08 14:45:06 -0800 | [diff] [blame] | 3056 | e.preventDefault(); |
| 3057 | } |
| 3058 | } |
Robert Ginda | 928cf63 | 2014-03-05 15:07:41 -0800 | [diff] [blame] | 3059 | |
| 3060 | this.onMouse(e); |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 3061 | } |
| 3062 | |
Robert Ginda | 928cf63 | 2014-03-05 15:07:41 -0800 | [diff] [blame] | 3063 | if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) { |
| 3064 | // Restore this on mouseup in case it was temporarily defeated with a |
| 3065 | // alt-mousedown. Only do this when the selection is empty so that |
| 3066 | // we don't immediately kill the users selection. |
Robert Ginda | 6aec7eb | 2015-06-16 10:31:30 -0700 | [diff] [blame] | 3067 | this.defeatMouseReports_ = false; |
Robert Ginda | 928cf63 | 2014-03-05 15:07:41 -0800 | [diff] [blame] | 3068 | } |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 3069 | }; |
| 3070 | |
| 3071 | /** |
| 3072 | * Clients should override this if they care to know about mouse events. |
| 3073 | * |
| 3074 | * The event parameter will be a normal DOM mouse click event with additional |
| 3075 | * 'terminalRow' and 'terminalColumn' properties. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 3076 | * |
| 3077 | * @param {Event} e The mouse event to handle. |
rginda | d561329 | 2012-06-19 15:40:37 -0700 | [diff] [blame] | 3078 | */ |
| 3079 | hterm.Terminal.prototype.onMouse = function(e) { }; |
| 3080 | |
| 3081 | /** |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 3082 | * React when focus changes. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 3083 | * |
| 3084 | * @param {boolean} focused True if focused, false otherwise. |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 3085 | */ |
Rob Spies | 06533ba | 2014-04-24 11:20:37 -0700 | [diff] [blame] | 3086 | hterm.Terminal.prototype.onFocusChange_ = function(focused) { |
| 3087 | this.cursorNode_.setAttribute('focus', focused); |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 3088 | this.restyleCursor_(); |
Michael Kelly | 485ecd1 | 2014-06-09 11:41:56 -0400 | [diff] [blame] | 3089 | if (focused === true) |
| 3090 | this.closeBellNotifications_(); |
rginda | 8e92a69 | 2012-05-20 19:37:20 -0700 | [diff] [blame] | 3091 | }; |
| 3092 | |
| 3093 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 3094 | * React when the ScrollPort is scrolled. |
| 3095 | */ |
| 3096 | hterm.Terminal.prototype.onScroll_ = function() { |
| 3097 | this.scheduleSyncCursorPosition_(); |
| 3098 | }; |
| 3099 | |
| 3100 | /** |
rginda | 9846e2f | 2012-01-27 13:53:33 -0800 | [diff] [blame] | 3101 | * React when text is pasted into the scrollPort. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 3102 | * |
| 3103 | * @param {Event} e The DOM paste event to handle. |
rginda | 9846e2f | 2012-01-27 13:53:33 -0800 | [diff] [blame] | 3104 | */ |
| 3105 | hterm.Terminal.prototype.onPaste_ = function(e) { |
Robert Ginda | a063b20 | 2014-07-21 11:08:25 -0700 | [diff] [blame] | 3106 | var data = e.text.replace(/\n/mg, '\r'); |
Connor Hegarty | f525ceb | 2014-09-03 13:36:03 -0700 | [diff] [blame] | 3107 | data = this.keyboard.encode(data); |
Robert Ginda | a063b20 | 2014-07-21 11:08:25 -0700 | [diff] [blame] | 3108 | if (this.options_.bracketedPaste) |
| 3109 | data = '\x1b[200~' + data + '\x1b[201~'; |
| 3110 | |
| 3111 | this.io.sendString(data); |
rginda | 9846e2f | 2012-01-27 13:53:33 -0800 | [diff] [blame] | 3112 | }; |
| 3113 | |
| 3114 | /** |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 3115 | * React when the user tries to copy from the scrollPort. |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 3116 | * |
| 3117 | * @param {Event} e The DOM copy event. |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 3118 | */ |
| 3119 | hterm.Terminal.prototype.onCopy_ = function(e) { |
Rob Spies | 0bec09b | 2014-06-06 15:58:09 -0700 | [diff] [blame] | 3120 | if (!this.useDefaultWindowCopy) { |
| 3121 | e.preventDefault(); |
| 3122 | setTimeout(this.copySelectionToClipboard.bind(this), 0); |
| 3123 | } |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 3124 | }; |
| 3125 | |
| 3126 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 3127 | * React when the ScrollPort is resized. |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 3128 | * |
| 3129 | * Note: This function should not directly contain code that alters the internal |
| 3130 | * state of the terminal. That kind of code belongs in realizeWidth or |
| 3131 | * realizeHeight, so that it can be executed synchronously in the case of a |
| 3132 | * programmatic width change. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 3133 | */ |
| 3134 | hterm.Terminal.prototype.onResize_ = function() { |
Robert Ginda | 19f6129 | 2014-03-04 14:07:57 -0800 | [diff] [blame] | 3135 | var columnCount = Math.floor(this.scrollPort_.getScreenWidth() / |
Rob Spies | 0be2025 | 2015-07-16 14:36:47 -0700 | [diff] [blame] | 3136 | this.scrollPort_.characterSize.width) || 0; |
Rob Spies | f4e90e8 | 2015-01-28 12:10:13 -0800 | [diff] [blame] | 3137 | var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(), |
Rob Spies | 0be2025 | 2015-07-16 14:36:47 -0700 | [diff] [blame] | 3138 | this.scrollPort_.characterSize.height) || 0; |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 3139 | |
Robert Ginda | 4e83f3a | 2012-09-04 15:25:25 -0700 | [diff] [blame] | 3140 | if (columnCount <= 0 || rowCount <= 0) { |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 3141 | // We avoid these situations since they happen sometimes when the terminal |
Robert Ginda | 4e83f3a | 2012-09-04 15:25:25 -0700 | [diff] [blame] | 3142 | // gets removed from the document or during the initial load, and we can't |
| 3143 | // deal with that. |
Rob Spies | 0be2025 | 2015-07-16 14:36:47 -0700 | [diff] [blame] | 3144 | // This can also happen if called before the scrollPort calculates the |
| 3145 | // character size, meaning we dived by 0 above and default to 0 values. |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 3146 | return; |
| 3147 | } |
| 3148 | |
rginda | a8ba17d | 2012-08-15 14:41:10 -0700 | [diff] [blame] | 3149 | var isNewSize = (columnCount != this.screenSize.width || |
| 3150 | rowCount != this.screenSize.height); |
| 3151 | |
| 3152 | // We do this even if the size didn't change, just to be sure everything is |
| 3153 | // in sync. |
Dmitry Polukhin | bb2ef71 | 2012-01-19 19:00:37 +0400 | [diff] [blame] | 3154 | this.realizeSize_(columnCount, rowCount); |
rginda | f522ce0 | 2012-04-17 17:49:17 -0700 | [diff] [blame] | 3155 | this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1); |
rginda | a8ba17d | 2012-08-15 14:41:10 -0700 | [diff] [blame] | 3156 | |
| 3157 | if (isNewSize) |
| 3158 | this.overlaySize(); |
| 3159 | |
Robert Ginda | fb1be6a | 2013-12-11 11:56:22 -0800 | [diff] [blame] | 3160 | this.restyleCursor_(); |
rginda | a8ba17d | 2012-08-15 14:41:10 -0700 | [diff] [blame] | 3161 | this.scheduleSyncCursorPosition_(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 3162 | }; |
| 3163 | |
| 3164 | /** |
| 3165 | * Service the cursor blink timeout. |
| 3166 | */ |
| 3167 | hterm.Terminal.prototype.onCursorBlink_ = function() { |
Robert Ginda | ea2183e | 2014-07-17 09:51:51 -0700 | [diff] [blame] | 3168 | if (!this.options_.cursorBlink) { |
| 3169 | delete this.timeouts_.cursorBlink; |
| 3170 | return; |
| 3171 | } |
| 3172 | |
Robert Ginda | 830583c | 2013-08-07 13:20:46 -0700 | [diff] [blame] | 3173 | if (this.cursorNode_.getAttribute('focus') == 'false' || |
| 3174 | this.cursorNode_.style.opacity == '0') { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 3175 | this.cursorNode_.style.opacity = '1'; |
Robert Ginda | ea2183e | 2014-07-17 09:51:51 -0700 | [diff] [blame] | 3176 | this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_, |
| 3177 | this.cursorBlinkCycle_[0]); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 3178 | } else { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 3179 | this.cursorNode_.style.opacity = '0'; |
Robert Ginda | ea2183e | 2014-07-17 09:51:51 -0700 | [diff] [blame] | 3180 | this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_, |
| 3181 | this.cursorBlinkCycle_[1]); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 3182 | } |
| 3183 | }; |
David Reveman | 8f55249 | 2012-03-28 12:18:41 -0400 | [diff] [blame] | 3184 | |
| 3185 | /** |
| 3186 | * Set the scrollbar-visible mode bit. |
| 3187 | * |
| 3188 | * If scrollbar-visible is on, the vertical scrollbar will be visible. |
| 3189 | * Otherwise it will not. |
| 3190 | * |
| 3191 | * Defaults to on. |
| 3192 | * |
| 3193 | * @param {boolean} state True to set scrollbar-visible mode, false to unset. |
| 3194 | */ |
| 3195 | hterm.Terminal.prototype.setScrollbarVisible = function(state) { |
| 3196 | this.scrollPort_.setScrollbarVisible(state); |
| 3197 | }; |
Michael Kelly | 485ecd1 | 2014-06-09 11:41:56 -0400 | [diff] [blame] | 3198 | |
| 3199 | /** |
Rob Spies | 49039e5 | 2014-12-17 13:40:04 -0800 | [diff] [blame] | 3200 | * Set the scroll wheel move multiplier. This will affect how fast the page |
Mike Frysinger | 9975de6 | 2017-04-28 00:01:14 -0400 | [diff] [blame] | 3201 | * scrolls on wheel events. |
Rob Spies | 49039e5 | 2014-12-17 13:40:04 -0800 | [diff] [blame] | 3202 | * |
| 3203 | * Defaults to 1. |
| 3204 | * |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 3205 | * @param {number} multiplier The multiplier to set. |
Rob Spies | 49039e5 | 2014-12-17 13:40:04 -0800 | [diff] [blame] | 3206 | */ |
| 3207 | hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) { |
| 3208 | this.scrollPort_.setScrollWheelMoveMultipler(multiplier); |
| 3209 | }; |
| 3210 | |
| 3211 | /** |
Michael Kelly | 485ecd1 | 2014-06-09 11:41:56 -0400 | [diff] [blame] | 3212 | * Close all web notifications created by terminal bells. |
| 3213 | */ |
| 3214 | hterm.Terminal.prototype.closeBellNotifications_ = function() { |
| 3215 | this.bellNotificationList_.forEach(function(n) { |
| 3216 | n.close(); |
| 3217 | }); |
| 3218 | this.bellNotificationList_.length = 0; |
| 3219 | }; |