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