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