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