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