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