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