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