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