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