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