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