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