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