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