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