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