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