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