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