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