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