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