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