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