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