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