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