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 | |
| 5 | /** |
| 6 | * Constructor for the Terminal class. |
| 7 | * |
| 8 | * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100 |
| 9 | * classes to provide the complete terminal functionality. |
| 10 | * |
| 11 | * There are a number of lower-level Terminal methods that can be called |
| 12 | * directly to manipulate the cursor, text, scroll region, and other terminal |
| 13 | * attributes. However, the primary method is interpret(), which parses VT |
| 14 | * escape sequences and invokes the appropriate Terminal methods. |
| 15 | * |
| 16 | * This class was heavily influenced by Cory Maccarrone's Framebuffer class. |
| 17 | * |
| 18 | * TODO(rginda): Eventually we're going to need to support characters which are |
| 19 | * displayed twice as wide as standard latin characters. This is to support |
| 20 | * CJK (and possibly other character sets). |
| 21 | */ |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 22 | hterm.Terminal = function(fontSize, opt_lineHeight) { |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 23 | // Two screen instances. |
| 24 | this.primaryScreen_ = new hterm.Screen(); |
| 25 | this.alternateScreen_ = new hterm.Screen(); |
| 26 | |
| 27 | // The "current" screen. |
| 28 | this.screen_ = this.primaryScreen_; |
| 29 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 30 | // The local notion of the screen size. ScreenBuffers also have a size which |
| 31 | // indicates their present size. During size changes, the two may disagree. |
| 32 | // Also, the inactive screen's size is not altered until it is made the active |
| 33 | // screen. |
| 34 | this.screenSize = new hterm.Size(0, 0); |
| 35 | |
| 36 | // The pixel dimensions of a single character on the screen. |
| 37 | this.characterSize_ = new hterm.Size(0, 0); |
| 38 | |
| 39 | // The scroll port we'll be using to display the visible rows. |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 40 | this.scrollPort_ = new hterm.ScrollPort(this, fontSize, opt_lineHeight); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 41 | this.scrollPort_.subscribe('resize', this.onResize_.bind(this)); |
| 42 | this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this)); |
rginda | 9846e2f | 2012-01-27 13:53:33 -0800 | [diff] [blame^] | 43 | this.scrollPort_.subscribe('paste', this.onPaste_.bind(this)); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 44 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 45 | // The div that contains this terminal. |
| 46 | this.div_ = null; |
| 47 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 48 | // The document that contains the scrollPort. Defaulted to the global |
| 49 | // document here so that the terminal is functional even if it hasn't been |
| 50 | // inserted into a document yet, but re-set in decorate(). |
| 51 | this.document_ = window.document; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 52 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 53 | // The rows that have scrolled off screen and are no longer addressable. |
| 54 | this.scrollbackRows_ = []; |
| 55 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 56 | // Saved tab stops. |
| 57 | this.tabStops_ = []; |
| 58 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 59 | // The VT's notion of the top and bottom rows. Used during some VT |
| 60 | // cursor positioning and scrolling commands. |
| 61 | this.vtScrollTop_ = null; |
| 62 | this.vtScrollBottom_ = null; |
| 63 | |
| 64 | // The DIV element for the visible cursor. |
| 65 | this.cursorNode_ = null; |
| 66 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 67 | // Default font family for the terminal text. |
| 68 | this.defaultFontFamily_ = '"DejaVu Sans Mono", "Everson Mono", FreeMono, ' + |
| 69 | '"Andale Mono", "Lucida Console", monospace' |
| 70 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 71 | // The default colors for text with no other color attributes. |
| 72 | this.backgroundColor = 'black'; |
| 73 | this.foregroundColor = 'white'; |
| 74 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 75 | // Default tab with of 8 to match xterm. |
| 76 | this.tabWidth = 8; |
| 77 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 78 | // The color of the visible cursor. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 79 | this.cursorColor = 'rgba(255,0,0,0.5)'; |
| 80 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 81 | // If true, scroll to the bottom on any keystroke. |
| 82 | this.scrollOnKeystroke = true; |
| 83 | |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 84 | // If true, scroll to the bottom on terminal output. |
| 85 | this.scrollOnOutput = false; |
| 86 | |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 87 | // Cursor position and attributes saved with DECSC. |
| 88 | this.savedOptions_ = {}; |
| 89 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 90 | // The current mode bits for the terminal. |
| 91 | this.options_ = new hterm.Options(); |
| 92 | |
| 93 | // Timeouts we might need to clear. |
| 94 | this.timeouts_ = {}; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 95 | |
| 96 | // The VT escape sequence interpreter. |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 97 | this.vt = new hterm.VT(this); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 98 | |
| 99 | // General IO interface that can be given to third parties without exposing |
| 100 | // the entire terminal object. |
| 101 | this.io = new hterm.Terminal.IO(this); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 102 | |
Dmitry Polukhin | bb2ef71 | 2012-01-19 19:00:37 +0400 | [diff] [blame] | 103 | this.realizeSize_(80, 24); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 104 | this.setDefaultTabStops(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | /** |
| 108 | * Create a new instance of a terminal command and run it with a given |
| 109 | * argument string. |
| 110 | * |
| 111 | * @param {function} commandClass The constructor for a terminal command. |
| 112 | * @param {string} argString The argument string to pass to the command. |
| 113 | */ |
| 114 | hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) { |
| 115 | var self = this; |
| 116 | this.command = new commandClass( |
| 117 | { argString: argString || '', |
| 118 | io: this.io.push(), |
| 119 | onExit: function(code) { |
| 120 | self.io.pop(); |
| 121 | self.io.println(hterm.msg('COMMAND_COMPLETE', |
| 122 | [self.command.commandName, code])); |
rginda | 9846e2f | 2012-01-27 13:53:33 -0800 | [diff] [blame^] | 123 | self.vt.keyboard.installKeyboard(null); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 124 | } |
| 125 | }); |
| 126 | |
rginda | 9846e2f | 2012-01-27 13:53:33 -0800 | [diff] [blame^] | 127 | this.vt.keyboard.installKeyboard(this.document_.body.firstChild); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 128 | this.command.run(); |
| 129 | }; |
| 130 | |
| 131 | /** |
| 132 | * Return a copy of the current cursor position. |
| 133 | * |
| 134 | * @return {hterm.RowCol} The RowCol object representing the current position. |
| 135 | */ |
| 136 | hterm.Terminal.prototype.saveCursor = function() { |
| 137 | return this.screen_.cursorPosition.clone(); |
| 138 | }; |
| 139 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 140 | hterm.Terminal.prototype.getTextAttributes = function() { |
| 141 | return this.screen_.textAttributes; |
| 142 | }; |
| 143 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 144 | /** |
rginda | 9846e2f | 2012-01-27 13:53:33 -0800 | [diff] [blame^] | 145 | * Change the title of this terminal's window. |
| 146 | */ |
| 147 | hterm.Terminal.prototype.setWindowTitle = function(title) { |
| 148 | this.document_.title = title; |
| 149 | }; |
| 150 | |
| 151 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 152 | * Restore a previously saved cursor position. |
| 153 | * |
| 154 | * @param {hterm.RowCol} cursor The position to restore. |
| 155 | */ |
| 156 | hterm.Terminal.prototype.restoreCursor = function(cursor) { |
| 157 | this.screen_.setCursorPosition(cursor.row, cursor.column); |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 158 | this.screen_.cursorPosition.overflow = cursor.overflow; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | /** |
| 162 | * Set the width of the terminal, resizing the UI to match. |
| 163 | */ |
| 164 | hterm.Terminal.prototype.setWidth = function(columnCount) { |
Dmitry Polukhin | bb2ef71 | 2012-01-19 19:00:37 +0400 | [diff] [blame] | 165 | this.div_.style.width = this.characterSize_.width * columnCount + 16 + 'px'; |
| 166 | this.realizeSize_(columnCount, this.screenSize.height); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 167 | this.scheduleSyncCursorPosition_(); |
| 168 | }; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 169 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 170 | /** |
Dmitry Polukhin | bb2ef71 | 2012-01-19 19:00:37 +0400 | [diff] [blame] | 171 | * Deal with terminal size changes. |
| 172 | * |
| 173 | */ |
| 174 | hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) { |
| 175 | if (columnCount != this.screenSize.width) |
| 176 | this.realizeWidth_(columnCount); |
| 177 | |
| 178 | if (rowCount != this.screenSize.height) |
| 179 | this.realizeHeight_(rowCount); |
| 180 | |
| 181 | // Send new terminal size to plugin. |
| 182 | this.io.onTerminalResize(columnCount, rowCount); |
| 183 | }; |
| 184 | |
| 185 | /** |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 186 | * Deal with terminal width changes. |
| 187 | * |
| 188 | * This function does what needs to be done when the terminal width changes |
| 189 | * out from under us. It happens here rather than in onResize_() because this |
| 190 | * code may need to run synchronously to handle programmatic changes of |
| 191 | * terminal width. |
| 192 | * |
| 193 | * Relying on the browser to send us an async resize event means we may not be |
| 194 | * in the correct state yet when the next escape sequence hits. |
| 195 | */ |
| 196 | hterm.Terminal.prototype.realizeWidth_ = function(columnCount) { |
| 197 | var deltaColumns = columnCount - this.screen_.getWidth(); |
| 198 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 199 | this.screenSize.width = columnCount; |
| 200 | this.screen_.setColumnCount(columnCount); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 201 | |
| 202 | if (deltaColumns > 0) { |
| 203 | this.setDefaultTabStops(this.screenSize.width - deltaColumns); |
| 204 | } else { |
| 205 | for (var i = this.tabStops_.length - 1; i >= 0; i--) { |
| 206 | if (this.tabStops_[i] <= columnCount) |
| 207 | break; |
| 208 | |
| 209 | this.tabStops_.pop(); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | this.screen_.setColumnCount(this.screenSize.width); |
| 214 | }; |
| 215 | |
| 216 | /** |
| 217 | * Deal with terminal height changes. |
| 218 | * |
| 219 | * This function does what needs to be done when the terminal height changes |
| 220 | * out from under us. It happens here rather than in onResize_() because this |
| 221 | * code may need to run synchronously to handle programmatic changes of |
| 222 | * terminal height. |
| 223 | * |
| 224 | * Relying on the browser to send us an async resize event means we may not be |
| 225 | * in the correct state yet when the next escape sequence hits. |
| 226 | */ |
| 227 | hterm.Terminal.prototype.realizeHeight_ = function(rowCount) { |
| 228 | var deltaRows = rowCount - this.screen_.getHeight(); |
| 229 | |
| 230 | this.screenSize.height = rowCount; |
| 231 | |
| 232 | var cursor = this.saveCursor(); |
| 233 | |
| 234 | if (deltaRows < 0) { |
| 235 | // Screen got smaller. |
| 236 | deltaRows *= -1; |
| 237 | while (deltaRows) { |
| 238 | var lastRow = this.getRowCount() - 1; |
| 239 | if (lastRow - this.scrollbackRows_.length == cursor.row) |
| 240 | break; |
| 241 | |
| 242 | if (this.getRowText(lastRow)) |
| 243 | break; |
| 244 | |
| 245 | this.screen_.popRow(); |
| 246 | deltaRows--; |
| 247 | } |
| 248 | |
| 249 | var ary = this.screen_.shiftRows(deltaRows); |
| 250 | this.scrollbackRows_.push.apply(this.scrollbackRows_, ary); |
| 251 | |
| 252 | // We just removed rows from the top of the screen, we need to update |
| 253 | // the cursor to match. |
| 254 | cursor.row -= deltaRows; |
| 255 | |
| 256 | } else if (deltaRows > 0) { |
| 257 | // Screen got larger. |
| 258 | |
| 259 | if (deltaRows <= this.scrollbackRows_.length) { |
| 260 | var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length); |
| 261 | var rows = this.scrollbackRows_.splice( |
| 262 | this.scrollbackRows_.length - scrollbackCount, scrollbackCount); |
| 263 | this.screen_.unshiftRows(rows); |
| 264 | deltaRows -= scrollbackCount; |
| 265 | cursor.row += scrollbackCount; |
| 266 | } |
| 267 | |
| 268 | if (deltaRows) |
| 269 | this.appendRows_(deltaRows); |
| 270 | } |
| 271 | |
| 272 | this.restoreCursor(cursor); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | /** |
| 276 | * Scroll the terminal to the top of the scrollback buffer. |
| 277 | */ |
| 278 | hterm.Terminal.prototype.scrollHome = function() { |
| 279 | this.scrollPort_.scrollRowToTop(0); |
| 280 | }; |
| 281 | |
| 282 | /** |
| 283 | * Scroll the terminal to the end. |
| 284 | */ |
| 285 | hterm.Terminal.prototype.scrollEnd = function() { |
| 286 | this.scrollPort_.scrollRowToBottom(this.getRowCount()); |
| 287 | }; |
| 288 | |
| 289 | /** |
| 290 | * Scroll the terminal one page up (minus one line) relative to the current |
| 291 | * position. |
| 292 | */ |
| 293 | hterm.Terminal.prototype.scrollPageUp = function() { |
| 294 | var i = this.scrollPort_.getTopRowIndex(); |
| 295 | this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1); |
| 296 | }; |
| 297 | |
| 298 | /** |
| 299 | * Scroll the terminal one page down (minus one line) relative to the current |
| 300 | * position. |
| 301 | */ |
| 302 | hterm.Terminal.prototype.scrollPageDown = function() { |
| 303 | var i = this.scrollPort_.getTopRowIndex(); |
| 304 | this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 305 | }; |
| 306 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 307 | /** |
| 308 | * Full terminal reset. |
| 309 | */ |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 310 | hterm.Terminal.prototype.reset = function() { |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 311 | this.clearAllTabStops(); |
| 312 | this.setDefaultTabStops(); |
| 313 | this.clearColorAndAttributes(); |
| 314 | this.setVTScrollRegion(null, null); |
| 315 | this.clear(); |
| 316 | this.setAbsoluteCursorPosition(0, 0); |
| 317 | this.softReset(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 318 | }; |
| 319 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 320 | /** |
| 321 | * Soft terminal reset. |
| 322 | */ |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 323 | hterm.Terminal.prototype.softReset = function() { |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 324 | this.options_ = new hterm.Options(); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 325 | this.setCursorVisible(true); |
| 326 | this.setCursorBlink(false); |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 327 | }; |
| 328 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 329 | hterm.Terminal.prototype.clearColorAndAttributes = function() { |
| 330 | //console.log('clearColorAndAttributes'); |
| 331 | }; |
| 332 | |
| 333 | hterm.Terminal.prototype.setForegroundColor256 = function() { |
| 334 | console.log('setForegroundColor256'); |
| 335 | }; |
| 336 | |
| 337 | hterm.Terminal.prototype.setBackgroundColor256 = function() { |
| 338 | console.log('setBackgroundColor256'); |
| 339 | }; |
| 340 | |
| 341 | hterm.Terminal.prototype.setForegroundColor = function() { |
| 342 | //console.log('setForegroundColor'); |
| 343 | }; |
| 344 | |
| 345 | hterm.Terminal.prototype.setBackgroundColor = function() { |
| 346 | //console.log('setBackgroundColor'); |
| 347 | }; |
| 348 | |
| 349 | hterm.Terminal.prototype.setAttributes = function() { |
| 350 | //console.log('setAttributes'); |
| 351 | }; |
| 352 | |
| 353 | hterm.Terminal.prototype.resize = function() { |
| 354 | console.log('resize'); |
| 355 | }; |
| 356 | |
rginda | e4d2923 | 2012-01-19 10:47:13 -0800 | [diff] [blame] | 357 | hterm.Terminal.prototype.setCharacterSet = function() { |
| 358 | //console.log('setCharacterSet'); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 359 | }; |
| 360 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 361 | /** |
| 362 | * Move the cursor forward to the next tab stop, or to the last column |
| 363 | * if no more tab stops are set. |
| 364 | */ |
| 365 | hterm.Terminal.prototype.forwardTabStop = function() { |
| 366 | var column = this.screen_.cursorPosition.column; |
| 367 | |
| 368 | for (var i = 0; i < this.tabStops_.length; i++) { |
| 369 | if (this.tabStops_[i] > column) { |
| 370 | this.setCursorColumn(this.tabStops_[i]); |
| 371 | return; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | this.setCursorColumn(this.screenSize.width - 1); |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 376 | }; |
| 377 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 378 | /** |
| 379 | * Move the cursor backward to the previous tab stop, or to the first column |
| 380 | * if no previous tab stops are set. |
| 381 | */ |
| 382 | hterm.Terminal.prototype.backwardTabStop = function() { |
| 383 | var column = this.screen_.cursorPosition.column; |
| 384 | |
| 385 | for (var i = this.tabStops_.length - 1; i >= 0; i--) { |
| 386 | if (this.tabStops_[i] < column) { |
| 387 | this.setCursorColumn(this.tabStops_[i]); |
| 388 | return; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | this.setCursorColumn(1); |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 393 | }; |
| 394 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 395 | /** |
| 396 | * Set a tab stop at the given column. |
| 397 | * |
| 398 | * @param {int} column Zero based column. |
| 399 | */ |
| 400 | hterm.Terminal.prototype.setTabStop = function(column) { |
| 401 | for (var i = this.tabStops_.length - 1; i >= 0; i--) { |
| 402 | if (this.tabStops_[i] == column) |
| 403 | return; |
| 404 | |
| 405 | if (this.tabStops_[i] < column) { |
| 406 | this.tabStops_.splice(i + 1, 0, column); |
| 407 | return; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | this.tabStops_.splice(0, 0, column); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 412 | }; |
| 413 | |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 414 | /** |
| 415 | * Clear the tab stop at the current cursor position. |
| 416 | * |
| 417 | * No effect if there is no tab stop at the current cursor position. |
| 418 | */ |
| 419 | hterm.Terminal.prototype.clearTabStopAtCursor = function() { |
| 420 | var column = this.screen_.cursorPosition.column; |
| 421 | |
| 422 | var i = this.tabStops_.indexOf(column); |
| 423 | if (i == -1) |
| 424 | return; |
| 425 | |
| 426 | this.tabStops_.splice(i, 1); |
| 427 | }; |
| 428 | |
| 429 | /** |
| 430 | * Clear all tab stops. |
| 431 | */ |
| 432 | hterm.Terminal.prototype.clearAllTabStops = function() { |
| 433 | this.tabStops_.length = 0; |
| 434 | }; |
| 435 | |
| 436 | /** |
| 437 | * Set up the default tab stops, starting from a given column. |
| 438 | * |
| 439 | * This sets a tabstop every (column % this.tabWidth) column, starting |
| 440 | * from the specified column, or 0 if no column is provided. |
| 441 | * |
| 442 | * This does not clear the existing tab stops first, use clearAllTabStops |
| 443 | * for that. |
| 444 | * |
| 445 | * @param {int} opt_start Optional starting zero based starting column, useful |
| 446 | * for filling out missing tab stops when the terminal is resized. |
| 447 | */ |
| 448 | hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) { |
| 449 | var start = opt_start || 0; |
| 450 | var w = this.tabWidth; |
| 451 | var stopCount = Math.floor((this.screenSize.width - start) / this.tabWidth) |
| 452 | for (var i = 0; i < stopCount; i++) { |
| 453 | this.setTabStop(Math.floor((start + i * w) / w) * w + w); |
| 454 | } |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 455 | }; |
| 456 | |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 457 | /** |
| 458 | * Save cursor position and attributes. |
| 459 | * |
| 460 | * TODO(rginda): Save attributes once we support them. |
| 461 | */ |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 462 | hterm.Terminal.prototype.saveOptions = function() { |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 463 | this.savedOptions_.cursor = this.saveCursor(); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 464 | this.savedOptions_.textAttributes = this.screen_.textAttributes.clone(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 465 | }; |
| 466 | |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 467 | /** |
| 468 | * Restore cursor position and attributes. |
| 469 | * |
| 470 | * TODO(rginda): Restore attributes once we support them. |
| 471 | */ |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 472 | hterm.Terminal.prototype.restoreOptions = function() { |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 473 | if (this.savedOptions_.cursor) |
| 474 | this.restoreCursor(this.savedOptions_.cursor); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 475 | if (this.savedOptions_.textAttributes) |
| 476 | this.screen_.textAttributes = this.savedOptions_.textAttributes; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 477 | }; |
| 478 | |
| 479 | /** |
| 480 | * Interpret a sequence of characters. |
| 481 | * |
| 482 | * Incomplete escape sequences are buffered until the next call. |
| 483 | * |
| 484 | * @param {string} str Sequence of characters to interpret or pass through. |
| 485 | */ |
| 486 | hterm.Terminal.prototype.interpret = function(str) { |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 487 | this.vt.interpret(str); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 488 | this.scheduleSyncCursorPosition_(); |
| 489 | }; |
| 490 | |
| 491 | /** |
| 492 | * Take over the given DIV for use as the terminal display. |
| 493 | * |
| 494 | * @param {HTMLDivElement} div The div to use as the terminal display. |
| 495 | */ |
| 496 | hterm.Terminal.prototype.decorate = function(div) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 497 | this.div_ = div; |
| 498 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 499 | this.scrollPort_.decorate(div); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 500 | this.scrollPort_.setFontFamily(this.defaultFontFamily_); |
| 501 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 502 | this.document_ = this.scrollPort_.getDocument(); |
| 503 | |
| 504 | // Get character dimensions from the scrollPort. |
| 505 | this.characterSize_.height = this.scrollPort_.getRowHeight(); |
| 506 | this.characterSize_.width = this.scrollPort_.getCharacterWidth(); |
| 507 | |
| 508 | this.cursorNode_ = this.document_.createElement('div'); |
| 509 | this.cursorNode_.style.cssText = |
| 510 | ('position: absolute;' + |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 511 | 'top: -99px;' + |
| 512 | 'display: block;' + |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 513 | 'width: ' + this.characterSize_.width + 'px;' + |
| 514 | 'height: ' + this.characterSize_.height + 'px;' + |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 515 | '-webkit-transition: opacity, background-color 100ms linear;' + |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 516 | 'background-color: ' + this.cursorColor); |
| 517 | this.document_.body.appendChild(this.cursorNode_); |
| 518 | |
| 519 | this.setReverseVideo(false); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 520 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 521 | var link = this.document_.createElement('link'); |
| 522 | link.setAttribute('href', '../css/dialogs.css'); |
| 523 | link.setAttribute('rel', 'stylesheet'); |
| 524 | this.document_.head.appendChild(link); |
| 525 | |
| 526 | this.alertDialog = new AlertDialog(this.document_.body); |
| 527 | this.promptDialog = new PromptDialog(this.document_.body); |
| 528 | this.confirmDialog = new ConfirmDialog(this.document_.body); |
| 529 | |
| 530 | this.scrollPort_.focus(); |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 531 | this.scrollPort_.scheduleRedraw(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 532 | }; |
| 533 | |
| 534 | hterm.Terminal.prototype.getDocument = function() { |
| 535 | return this.document_; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 536 | }; |
| 537 | |
| 538 | /** |
| 539 | * Return the HTML Element for a given row index. |
| 540 | * |
| 541 | * This is a method from the RowProvider interface. The ScrollPort uses |
| 542 | * it to fetch rows on demand as they are scrolled into view. |
| 543 | * |
| 544 | * TODO(rginda): Consider saving scrollback rows as (HTML source, text content) |
| 545 | * pairs to conserve memory. |
| 546 | * |
| 547 | * @param {integer} index The zero-based row index, measured relative to the |
| 548 | * start of the scrollback buffer. On-screen rows will always have the |
| 549 | * largest indicies. |
| 550 | * @return {HTMLElement} The 'x-row' element containing for the requested row. |
| 551 | */ |
| 552 | hterm.Terminal.prototype.getRowNode = function(index) { |
| 553 | if (index < this.scrollbackRows_.length) |
| 554 | return this.scrollbackRows_[index]; |
| 555 | |
| 556 | var screenIndex = index - this.scrollbackRows_.length; |
| 557 | return this.screen_.rowsArray[screenIndex]; |
| 558 | }; |
| 559 | |
| 560 | /** |
| 561 | * Return the text content for a given range of rows. |
| 562 | * |
| 563 | * This is a method from the RowProvider interface. The ScrollPort uses |
| 564 | * it to fetch text content on demand when the user attempts to copy their |
| 565 | * selection to the clipboard. |
| 566 | * |
| 567 | * @param {integer} start The zero-based row index to start from, measured |
| 568 | * relative to the start of the scrollback buffer. On-screen rows will |
| 569 | * always have the largest indicies. |
| 570 | * @param {integer} end The zero-based row index to end on, measured |
| 571 | * relative to the start of the scrollback buffer. |
| 572 | * @return {string} A single string containing the text value of the range of |
| 573 | * rows. Lines will be newline delimited, with no trailing newline. |
| 574 | */ |
| 575 | hterm.Terminal.prototype.getRowsText = function(start, end) { |
| 576 | var ary = []; |
| 577 | for (var i = start; i < end; i++) { |
| 578 | var node = this.getRowNode(i); |
| 579 | ary.push(node.textContent); |
| 580 | } |
| 581 | |
| 582 | return ary.join('\n'); |
| 583 | }; |
| 584 | |
| 585 | /** |
| 586 | * Return the text content for a given row. |
| 587 | * |
| 588 | * This is a method from the RowProvider interface. The ScrollPort uses |
| 589 | * it to fetch text content on demand when the user attempts to copy their |
| 590 | * selection to the clipboard. |
| 591 | * |
| 592 | * @param {integer} index The zero-based row index to return, measured |
| 593 | * relative to the start of the scrollback buffer. On-screen rows will |
| 594 | * always have the largest indicies. |
| 595 | * @return {string} A string containing the text value of the selected row. |
| 596 | */ |
| 597 | hterm.Terminal.prototype.getRowText = function(index) { |
| 598 | var node = this.getRowNode(index); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 599 | return node.textContent; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 600 | }; |
| 601 | |
| 602 | /** |
| 603 | * Return the total number of rows in the addressable screen and in the |
| 604 | * scrollback buffer of this terminal. |
| 605 | * |
| 606 | * This is a method from the RowProvider interface. The ScrollPort uses |
| 607 | * it to compute the size of the scrollbar. |
| 608 | * |
| 609 | * @return {integer} The number of rows in this terminal. |
| 610 | */ |
| 611 | hterm.Terminal.prototype.getRowCount = function() { |
| 612 | return this.scrollbackRows_.length + this.screen_.rowsArray.length; |
| 613 | }; |
| 614 | |
| 615 | /** |
| 616 | * Create DOM nodes for new rows and append them to the end of the terminal. |
| 617 | * |
| 618 | * This is the only correct way to add a new DOM node for a row. Notice that |
| 619 | * the new row is appended to the bottom of the list of rows, and does not |
| 620 | * require renumbering (of the rowIndex property) of previous rows. |
| 621 | * |
| 622 | * If you think you want a new blank row somewhere in the middle of the |
| 623 | * terminal, look into moveRows_(). |
| 624 | * |
| 625 | * This method does not pay attention to vtScrollTop/Bottom, since you should |
| 626 | * be using moveRows() in cases where they would matter. |
| 627 | * |
| 628 | * The cursor will be positioned at column 0 of the first inserted line. |
| 629 | */ |
| 630 | hterm.Terminal.prototype.appendRows_ = function(count) { |
| 631 | var cursorRow = this.screen_.rowsArray.length; |
| 632 | var offset = this.scrollbackRows_.length + cursorRow; |
| 633 | for (var i = 0; i < count; i++) { |
| 634 | var row = this.document_.createElement('x-row'); |
| 635 | row.appendChild(this.document_.createTextNode('')); |
| 636 | row.rowIndex = offset + i; |
| 637 | this.screen_.pushRow(row); |
| 638 | } |
| 639 | |
| 640 | var extraRows = this.screen_.rowsArray.length - this.screenSize.height; |
| 641 | if (extraRows > 0) { |
| 642 | var ary = this.screen_.shiftRows(extraRows); |
| 643 | Array.prototype.push.apply(this.scrollbackRows_, ary); |
| 644 | this.scheduleScrollDown_(); |
| 645 | } |
| 646 | |
| 647 | if (cursorRow >= this.screen_.rowsArray.length) |
| 648 | cursorRow = this.screen_.rowsArray.length - 1; |
| 649 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 650 | this.setAbsoluteCursorPosition(cursorRow, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 651 | }; |
| 652 | |
| 653 | /** |
| 654 | * Relocate rows from one part of the addressable screen to another. |
| 655 | * |
| 656 | * This is used to recycle rows during VT scrolls (those which are driven |
| 657 | * by VT commands, rather than by the user manipulating the scrollbar.) |
| 658 | * |
| 659 | * In this case, the blank lines scrolled into the scroll region are made of |
| 660 | * the nodes we scrolled off. These have their rowIndex properties carefully |
| 661 | * renumbered so as not to confuse the ScrollPort. |
| 662 | * |
| 663 | * TODO(rginda): I'm not sure why this doesn't require a scrollport repaint. |
| 664 | * It may just be luck. I wouldn't be surprised if we actually needed to call |
| 665 | * scrollPort_.invalidateRowRange, but I'm going to wait for evidence before |
| 666 | * adding it. |
| 667 | */ |
| 668 | hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) { |
| 669 | var ary = this.screen_.removeRows(fromIndex, count); |
| 670 | this.screen_.insertRows(toIndex, ary); |
| 671 | |
| 672 | var start, end; |
| 673 | if (fromIndex < toIndex) { |
| 674 | start = fromIndex; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 675 | end = toIndex + count; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 676 | } else { |
| 677 | start = toIndex; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 678 | end = fromIndex + count; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | this.renumberRows_(start, end); |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 682 | this.scrollPort_.scheduleInvalidate(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 683 | }; |
| 684 | |
| 685 | /** |
| 686 | * Renumber the rowIndex property of the given range of rows. |
| 687 | * |
| 688 | * The start and end indicies are relative to the screen, not the scrollback. |
| 689 | * Rows in the scrollback buffer cannot be renumbered. Since they are not |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 690 | * addressable (you can't delete them, scroll them, etc), you should have |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 691 | * no need to renumber scrollback rows. |
| 692 | */ |
| 693 | hterm.Terminal.prototype.renumberRows_ = function(start, end) { |
| 694 | var offset = this.scrollbackRows_.length; |
| 695 | for (var i = start; i < end; i++) { |
| 696 | this.screen_.rowsArray[i].rowIndex = offset + i; |
| 697 | } |
| 698 | }; |
| 699 | |
| 700 | /** |
| 701 | * Print a string to the terminal. |
| 702 | * |
| 703 | * This respects the current insert and wraparound modes. It will add new lines |
| 704 | * to the end of the terminal, scrolling off the top into the scrollback buffer |
| 705 | * if necessary. |
| 706 | * |
| 707 | * The string is *not* parsed for escape codes. Use the interpret() method if |
| 708 | * that's what you're after. |
| 709 | * |
| 710 | * @param{string} str The string to print. |
| 711 | */ |
| 712 | hterm.Terminal.prototype.print = function(str) { |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 713 | if (this.options_.wraparound && this.screen_.cursorPosition.overflow) |
| 714 | this.newLine(); |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 715 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 716 | if (this.options_.insertMode) { |
| 717 | this.screen_.insertString(str); |
| 718 | } else { |
| 719 | this.screen_.overwriteString(str); |
| 720 | } |
| 721 | |
| 722 | var overflow = this.screen_.maybeClipCurrentRow(); |
| 723 | |
| 724 | if (this.options_.wraparound && overflow) { |
| 725 | var lastColumn; |
| 726 | |
| 727 | do { |
| 728 | if (this.screen_.cursorPosition.overflow) |
| 729 | this.newLine(); |
| 730 | |
| 731 | if (!this.options_.insertMode) |
| 732 | this.screen_.deleteChars(overflow.characterLength); |
| 733 | |
| 734 | this.screen_.prependNodes(overflow); |
| 735 | lastColumn = overflow.characterCount; |
| 736 | |
| 737 | overflow = this.screen_.maybeClipCurrentRow(); |
| 738 | } while (overflow); |
| 739 | |
| 740 | this.setCursorColumn(lastColumn); |
| 741 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 742 | |
| 743 | this.scheduleSyncCursorPosition_(); |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 744 | |
| 745 | if (this.scrollOnOutput) |
| 746 | this.scrollPort_.scrollRowToBottom(this.getRowCount()); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 747 | }; |
| 748 | |
| 749 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 750 | * Set the VT scroll region. |
| 751 | * |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 752 | * This also resets the cursor position to the absolute (0, 0) position, since |
| 753 | * that's what xterm appears to do. |
| 754 | * |
| 755 | * @param {integer} scrollTop The zero-based top of the scroll region. |
| 756 | * @param {integer} scrollBottom The zero-based bottom of the scroll region, |
| 757 | * inclusive. |
| 758 | */ |
| 759 | hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) { |
| 760 | this.vtScrollTop_ = scrollTop; |
| 761 | this.vtScrollBottom_ = scrollBottom; |
| 762 | this.setAbsoluteCursorPosition(0, 0); |
| 763 | }; |
| 764 | |
| 765 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 766 | * Return the top row index according to the VT. |
| 767 | * |
| 768 | * This will return 0 unless the terminal has been told to restrict scrolling |
| 769 | * to some lower row. It is used for some VT cursor positioning and scrolling |
| 770 | * commands. |
| 771 | * |
| 772 | * @return {integer} The topmost row in the terminal's scroll region. |
| 773 | */ |
| 774 | hterm.Terminal.prototype.getVTScrollTop = function() { |
| 775 | if (this.vtScrollTop_ != null) |
| 776 | return this.vtScrollTop_; |
| 777 | |
| 778 | return 0; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 779 | }; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 780 | |
| 781 | /** |
| 782 | * Return the bottom row index according to the VT. |
| 783 | * |
| 784 | * This will return the height of the terminal unless the it has been told to |
| 785 | * restrict scrolling to some higher row. It is used for some VT cursor |
| 786 | * positioning and scrolling commands. |
| 787 | * |
| 788 | * @return {integer} The bottommost row in the terminal's scroll region. |
| 789 | */ |
| 790 | hterm.Terminal.prototype.getVTScrollBottom = function() { |
| 791 | if (this.vtScrollBottom_ != null) |
| 792 | return this.vtScrollBottom_; |
| 793 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 794 | return this.screenSize.height - 1; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | /** |
| 798 | * Process a '\n' character. |
| 799 | * |
| 800 | * If the cursor is on the final row of the terminal this will append a new |
| 801 | * blank row to the screen and scroll the topmost row into the scrollback |
| 802 | * buffer. |
| 803 | * |
| 804 | * Otherwise, this moves the cursor to column zero of the next row. |
| 805 | */ |
| 806 | hterm.Terminal.prototype.newLine = function() { |
| 807 | if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 808 | // If we're at the end of the screen we need to append a new line and |
| 809 | // scroll the top line into the scrollback buffer. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 810 | this.appendRows_(1); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 811 | } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) { |
| 812 | // End of the scroll region does not affect the scrollback buffer. |
| 813 | this.vtScrollUp(1); |
| 814 | this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 815 | } else { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 816 | // Anywhere else in the screen just moves the cursor. |
| 817 | this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 818 | } |
| 819 | }; |
| 820 | |
| 821 | /** |
| 822 | * Like newLine(), except maintain the cursor column. |
| 823 | */ |
| 824 | hterm.Terminal.prototype.lineFeed = function() { |
| 825 | var column = this.screen_.cursorPosition.column; |
| 826 | this.newLine(); |
| 827 | this.setCursorColumn(column); |
| 828 | }; |
| 829 | |
| 830 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 831 | * If autoCarriageReturn is set then newLine(), else lineFeed(). |
| 832 | */ |
| 833 | hterm.Terminal.prototype.formFeed = function() { |
| 834 | if (this.options_.autoCarriageReturn) { |
| 835 | this.newLine(); |
| 836 | } else { |
| 837 | this.lineFeed(); |
| 838 | } |
| 839 | }; |
| 840 | |
| 841 | /** |
| 842 | * Move the cursor up one row, possibly inserting a blank line. |
| 843 | * |
| 844 | * The cursor column is not changed. |
| 845 | */ |
| 846 | hterm.Terminal.prototype.reverseLineFeed = function() { |
| 847 | var scrollTop = this.getVTScrollTop(); |
| 848 | var currentRow = this.screen_.cursorPosition.row; |
| 849 | |
| 850 | if (currentRow == scrollTop) { |
| 851 | this.insertLines(1); |
| 852 | } else { |
| 853 | this.setAbsoluteCursorRow(currentRow - 1); |
| 854 | } |
| 855 | }; |
| 856 | |
| 857 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 858 | * Replace all characters to the left of the current cursor with the space |
| 859 | * character. |
| 860 | * |
| 861 | * TODO(rginda): This should probably *remove* the characters (not just replace |
| 862 | * with a space) if there are no characters at or beyond the current cursor |
| 863 | * position. Once it does that, it'll have the same text-attribute related |
| 864 | * issues as hterm.Screen.prototype.clearCursorRow :/ |
| 865 | */ |
| 866 | hterm.Terminal.prototype.eraseToLeft = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 867 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 868 | this.setCursorColumn(0); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 869 | this.screen_.overwriteString(hterm.getWhitespace(cursor.column + 1)); |
| 870 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 871 | }; |
| 872 | |
| 873 | /** |
| 874 | * Erase a given number of characters to the right of the cursor, shifting |
| 875 | * remaining characters to the left. |
| 876 | * |
| 877 | * The cursor position is unchanged. |
| 878 | * |
| 879 | * TODO(rginda): Test that this works even when the cursor is positioned beyond |
| 880 | * the end of the text. |
| 881 | * |
| 882 | * TODO(rginda): This likely has text-attribute related troubles similar to the |
| 883 | * todo on hterm.Screen.prototype.clearCursorRow. |
| 884 | */ |
| 885 | hterm.Terminal.prototype.eraseToRight = function(opt_count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 886 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 887 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 888 | var maxCount = this.screenSize.width - cursor.column; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 889 | var count = (opt_count && opt_count < maxCount) ? opt_count : maxCount; |
| 890 | this.screen_.deleteChars(count); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 891 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 892 | }; |
| 893 | |
| 894 | /** |
| 895 | * Erase the current line. |
| 896 | * |
| 897 | * The cursor position is unchanged. |
| 898 | * |
| 899 | * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which |
| 900 | * has a text-attribute related TODO. |
| 901 | */ |
| 902 | hterm.Terminal.prototype.eraseLine = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 903 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 904 | this.screen_.clearCursorRow(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 905 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 906 | }; |
| 907 | |
| 908 | /** |
| 909 | * Erase all characters from the start of the scroll region to the current |
| 910 | * cursor position. |
| 911 | * |
| 912 | * The cursor position is unchanged. |
| 913 | * |
| 914 | * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which |
| 915 | * has a text-attribute related TODO. |
| 916 | */ |
| 917 | hterm.Terminal.prototype.eraseAbove = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 918 | var cursor = this.saveCursor(); |
| 919 | |
| 920 | this.eraseToLeft(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 921 | |
| 922 | var top = this.getVTScrollTop(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 923 | for (var i = top; i < cursor.row; i++) { |
| 924 | this.setAbsoluteCursorPosition(i, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 925 | this.screen_.clearCursorRow(); |
| 926 | } |
| 927 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 928 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 929 | }; |
| 930 | |
| 931 | /** |
| 932 | * Erase all characters from the current cursor position to the end of the |
| 933 | * scroll region. |
| 934 | * |
| 935 | * The cursor position is unchanged. |
| 936 | * |
| 937 | * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which |
| 938 | * has a text-attribute related TODO. |
| 939 | */ |
| 940 | hterm.Terminal.prototype.eraseBelow = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 941 | var cursor = this.saveCursor(); |
| 942 | |
| 943 | this.eraseToRight(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 944 | |
| 945 | var bottom = this.getVTScrollBottom(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 946 | for (var i = cursor.row + 1; i <= bottom; i++) { |
| 947 | this.setAbsoluteCursorPosition(i, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 948 | this.screen_.clearCursorRow(); |
| 949 | } |
| 950 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 951 | this.restoreCursor(cursor); |
| 952 | }; |
| 953 | |
| 954 | /** |
| 955 | * Fill the terminal with a given character. |
| 956 | * |
| 957 | * This methods does not respect the VT scroll region. |
| 958 | * |
| 959 | * @param {string} ch The character to use for the fill. |
| 960 | */ |
| 961 | hterm.Terminal.prototype.fill = function(ch) { |
| 962 | var cursor = this.saveCursor(); |
| 963 | |
| 964 | this.setAbsoluteCursorPosition(0, 0); |
| 965 | for (var row = 0; row < this.screenSize.height; row++) { |
| 966 | for (var col = 0; col < this.screenSize.width; col++) { |
| 967 | this.setAbsoluteCursorPosition(row, col); |
| 968 | this.screen_.overwriteString(ch); |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 973 | }; |
| 974 | |
| 975 | /** |
rginda | e4d2923 | 2012-01-19 10:47:13 -0800 | [diff] [blame] | 976 | * Erase the entire display. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 977 | * |
rginda | e4d2923 | 2012-01-19 10:47:13 -0800 | [diff] [blame] | 978 | * The cursor position is unchanged. This does not respect the scroll |
| 979 | * region. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 980 | * |
| 981 | * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which |
| 982 | * has a text-attribute related TODO. |
| 983 | */ |
| 984 | hterm.Terminal.prototype.clear = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 985 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 986 | |
rginda | e4d2923 | 2012-01-19 10:47:13 -0800 | [diff] [blame] | 987 | var bottom = this.screenSize.height; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 988 | |
rginda | e4d2923 | 2012-01-19 10:47:13 -0800 | [diff] [blame] | 989 | for (var i = 0; i < bottom; i++) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 990 | this.setAbsoluteCursorPosition(i, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 991 | this.screen_.clearCursorRow(); |
| 992 | } |
| 993 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 994 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 995 | }; |
| 996 | |
| 997 | /** |
| 998 | * VT command to insert lines at the current cursor row. |
| 999 | * |
| 1000 | * This respects the current scroll region. Rows pushed off the bottom are |
| 1001 | * lost (they won't show up in the scrollback buffer). |
| 1002 | * |
| 1003 | * TODO(rginda): This relies on hterm.Screen.prototype.clearCursorRow, which |
| 1004 | * has a text-attribute related TODO. |
| 1005 | * |
| 1006 | * @param {integer} count The number of lines to insert. |
| 1007 | */ |
| 1008 | hterm.Terminal.prototype.insertLines = function(count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1009 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1010 | |
| 1011 | var bottom = this.getVTScrollBottom(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1012 | count = Math.min(count, bottom - cursor.row); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1013 | |
rginda | e4d2923 | 2012-01-19 10:47:13 -0800 | [diff] [blame] | 1014 | var start = bottom - count + 1; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1015 | if (start != cursor.row) |
| 1016 | this.moveRows_(start, count, cursor.row); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1017 | |
| 1018 | for (var i = 0; i < count; i++) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1019 | this.setAbsoluteCursorPosition(cursor.row + i, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1020 | this.screen_.clearCursorRow(); |
| 1021 | } |
| 1022 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1023 | cursor.column = 0; |
| 1024 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1025 | }; |
| 1026 | |
| 1027 | /** |
| 1028 | * VT command to delete lines at the current cursor row. |
| 1029 | * |
| 1030 | * New rows are added to the bottom of scroll region to take their place. New |
| 1031 | * rows are strictly there to take up space and have no content or style. |
| 1032 | */ |
| 1033 | hterm.Terminal.prototype.deleteLines = function(count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1034 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1035 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1036 | var top = cursor.row; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1037 | var bottom = this.getVTScrollBottom(); |
| 1038 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1039 | var maxCount = bottom - top + 1; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1040 | count = Math.min(count, maxCount); |
| 1041 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1042 | var moveStart = bottom - count + 1; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1043 | if (count != maxCount) |
| 1044 | this.moveRows_(top, count, moveStart); |
| 1045 | |
| 1046 | for (var i = 0; i < count; i++) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1047 | this.setAbsoluteCursorPosition(moveStart + i, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1048 | this.screen_.clearCursorRow(); |
| 1049 | } |
| 1050 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1051 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1052 | }; |
| 1053 | |
| 1054 | /** |
| 1055 | * Inserts the given number of spaces at the current cursor position. |
| 1056 | * |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1057 | * The cursor position is not changed. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1058 | */ |
| 1059 | hterm.Terminal.prototype.insertSpace = function(count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1060 | var cursor = this.saveCursor(); |
| 1061 | |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1062 | var ws = hterm.getWhitespace(count || 1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1063 | this.screen_.insertString(ws); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 1064 | this.screen_.maybeClipCurrentRow(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1065 | |
| 1066 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1067 | }; |
| 1068 | |
| 1069 | /** |
| 1070 | * Forward-delete the specified number of characters starting at the cursor |
| 1071 | * position. |
| 1072 | * |
| 1073 | * @param {integer} count The number of characters to delete. |
| 1074 | */ |
| 1075 | hterm.Terminal.prototype.deleteChars = function(count) { |
| 1076 | this.screen_.deleteChars(count); |
| 1077 | }; |
| 1078 | |
| 1079 | /** |
| 1080 | * Shift rows in the scroll region upwards by a given number of lines. |
| 1081 | * |
| 1082 | * New rows are inserted at the bottom of the scroll region to fill the |
| 1083 | * vacated rows. The new rows not filled out with the current text attributes. |
| 1084 | * |
| 1085 | * This function does not affect the scrollback rows at all. Rows shifted |
| 1086 | * off the top are lost. |
| 1087 | * |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1088 | * The cursor position is not altered. |
| 1089 | * |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1090 | * @param {integer} count The number of rows to scroll. |
| 1091 | */ |
| 1092 | hterm.Terminal.prototype.vtScrollUp = function(count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1093 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1094 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1095 | this.setAbsoluteCursorRow(this.getVTScrollTop()); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1096 | this.deleteLines(count); |
| 1097 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1098 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1099 | }; |
| 1100 | |
| 1101 | /** |
| 1102 | * Shift rows below the cursor down by a given number of lines. |
| 1103 | * |
| 1104 | * This function respects the current scroll region. |
| 1105 | * |
| 1106 | * New rows are inserted at the top of the scroll region to fill the |
| 1107 | * vacated rows. The new rows not filled out with the current text attributes. |
| 1108 | * |
| 1109 | * This function does not affect the scrollback rows at all. Rows shifted |
| 1110 | * off the bottom are lost. |
| 1111 | * |
| 1112 | * @param {integer} count The number of rows to scroll. |
| 1113 | */ |
| 1114 | hterm.Terminal.prototype.vtScrollDown = function(opt_count) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1115 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1116 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1117 | this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1118 | this.insertLines(opt_count); |
| 1119 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1120 | this.restoreCursor(cursor); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1121 | }; |
| 1122 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1123 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1124 | /** |
| 1125 | * Set the cursor position. |
| 1126 | * |
| 1127 | * The cursor row is relative to the scroll region if the terminal has |
| 1128 | * 'origin mode' enabled, or relative to the addressable screen otherwise. |
| 1129 | * |
| 1130 | * @param {integer} row The new zero-based cursor row. |
| 1131 | * @param {integer} row The new zero-based cursor column. |
| 1132 | */ |
| 1133 | hterm.Terminal.prototype.setCursorPosition = function(row, column) { |
| 1134 | if (this.options_.originMode) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1135 | this.setRelativeCursorPosition(row, column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1136 | } else { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1137 | this.setAbsoluteCursorPosition(row, column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1138 | } |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1139 | }; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1140 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1141 | hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) { |
| 1142 | var scrollTop = this.getVTScrollTop(); |
| 1143 | row = hterm.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom()); |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 1144 | column = hterm.clamp(column, 0, this.screenSize.width - 1); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1145 | this.screen_.setCursorPosition(row, column); |
| 1146 | }; |
| 1147 | |
| 1148 | hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) { |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 1149 | row = hterm.clamp(row, 0, this.screenSize.height - 1); |
| 1150 | column = hterm.clamp(column, 0, this.screenSize.width - 1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1151 | this.screen_.setCursorPosition(row, column); |
| 1152 | }; |
| 1153 | |
| 1154 | /** |
| 1155 | * Set the cursor column. |
| 1156 | * |
| 1157 | * @param {integer} column The new zero-based cursor column. |
| 1158 | */ |
| 1159 | hterm.Terminal.prototype.setCursorColumn = function(column) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1160 | this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1161 | }; |
| 1162 | |
| 1163 | /** |
| 1164 | * Return the cursor column. |
| 1165 | * |
| 1166 | * @return {integer} The zero-based cursor column. |
| 1167 | */ |
| 1168 | hterm.Terminal.prototype.getCursorColumn = function() { |
| 1169 | return this.screen_.cursorPosition.column; |
| 1170 | }; |
| 1171 | |
| 1172 | /** |
| 1173 | * Set the cursor row. |
| 1174 | * |
| 1175 | * The cursor row is relative to the scroll region if the terminal has |
| 1176 | * 'origin mode' enabled, or relative to the addressable screen otherwise. |
| 1177 | * |
| 1178 | * @param {integer} row The new cursor row. |
| 1179 | */ |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1180 | hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) { |
| 1181 | this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1182 | }; |
| 1183 | |
| 1184 | /** |
| 1185 | * Return the cursor row. |
| 1186 | * |
| 1187 | * @return {integer} The zero-based cursor row. |
| 1188 | */ |
| 1189 | hterm.Terminal.prototype.getCursorRow = function(row) { |
| 1190 | return this.screen_.cursorPosition.row; |
| 1191 | }; |
| 1192 | |
| 1193 | /** |
| 1194 | * Request that the ScrollPort redraw itself soon. |
| 1195 | * |
| 1196 | * The redraw will happen asynchronously, soon after the call stack winds down. |
| 1197 | * Multiple calls will be coalesced into a single redraw. |
| 1198 | */ |
| 1199 | hterm.Terminal.prototype.scheduleRedraw_ = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1200 | if (this.timeouts_.redraw) |
| 1201 | return; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1202 | |
| 1203 | var self = this; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1204 | this.timeouts_.redraw = setTimeout(function() { |
| 1205 | delete self.timeouts_.redraw; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1206 | self.scrollPort_.redraw_(); |
| 1207 | }, 0); |
| 1208 | }; |
| 1209 | |
| 1210 | /** |
| 1211 | * Request that the ScrollPort be scrolled to the bottom. |
| 1212 | * |
| 1213 | * The scroll will happen asynchronously, soon after the call stack winds down. |
| 1214 | * Multiple calls will be coalesced into a single scroll. |
| 1215 | * |
| 1216 | * This affects the scrollbar position of the ScrollPort, and has nothing to |
| 1217 | * do with the VT scroll commands. |
| 1218 | */ |
| 1219 | hterm.Terminal.prototype.scheduleScrollDown_ = function() { |
| 1220 | if (this.timeouts_.scrollDown) |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1221 | return; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1222 | |
| 1223 | var self = this; |
| 1224 | this.timeouts_.scrollDown = setTimeout(function() { |
| 1225 | delete self.timeouts_.scrollDown; |
| 1226 | self.scrollPort_.scrollRowToBottom(self.getRowCount()); |
| 1227 | }, 10); |
| 1228 | }; |
| 1229 | |
| 1230 | /** |
| 1231 | * Move the cursor up a specified number of rows. |
| 1232 | * |
| 1233 | * @param {integer} count The number of rows to move the cursor. |
| 1234 | */ |
| 1235 | hterm.Terminal.prototype.cursorUp = function(count) { |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1236 | return this.cursorDown(-(count || 1)); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1237 | }; |
| 1238 | |
| 1239 | /** |
| 1240 | * Move the cursor down a specified number of rows. |
| 1241 | * |
| 1242 | * @param {integer} count The number of rows to move the cursor. |
| 1243 | */ |
| 1244 | hterm.Terminal.prototype.cursorDown = function(count) { |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1245 | count = count || 1; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1246 | var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0); |
| 1247 | var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() : |
| 1248 | this.screenSize.height - 1); |
| 1249 | |
| 1250 | var row = hterm.clamp(this.screen_.cursorPosition.row + count, |
| 1251 | minHeight, maxHeight); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1252 | this.setAbsoluteCursorRow(row); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1253 | }; |
| 1254 | |
| 1255 | /** |
| 1256 | * Move the cursor left a specified number of columns. |
| 1257 | * |
| 1258 | * @param {integer} count The number of columns to move the cursor. |
| 1259 | */ |
| 1260 | hterm.Terminal.prototype.cursorLeft = function(count) { |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1261 | return this.cursorRight(-(count || 1)); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1262 | }; |
| 1263 | |
| 1264 | /** |
| 1265 | * Move the cursor right a specified number of columns. |
| 1266 | * |
| 1267 | * @param {integer} count The number of columns to move the cursor. |
| 1268 | */ |
| 1269 | hterm.Terminal.prototype.cursorRight = function(count) { |
rginda | 0f5c029 | 2012-01-13 11:00:13 -0800 | [diff] [blame] | 1270 | count = count || 1; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1271 | var column = hterm.clamp(this.screen_.cursorPosition.column + count, |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1272 | 0, this.screenSize.width - 1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1273 | this.setCursorColumn(column); |
| 1274 | }; |
| 1275 | |
| 1276 | /** |
| 1277 | * Reverse the foreground and background colors of the terminal. |
| 1278 | * |
| 1279 | * This only affects text that was drawn with no attributes. |
| 1280 | * |
| 1281 | * TODO(rginda): Test xterm to see if reverse is respected for text that has |
| 1282 | * been drawn with attributes that happen to coincide with the default |
| 1283 | * 'no-attribute' colors. My guess is probably not. |
| 1284 | */ |
| 1285 | hterm.Terminal.prototype.setReverseVideo = function(state) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1286 | this.options_.reverseVideo = state; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1287 | if (state) { |
| 1288 | this.scrollPort_.setForegroundColor(this.backgroundColor); |
| 1289 | this.scrollPort_.setBackgroundColor(this.foregroundColor); |
| 1290 | } else { |
| 1291 | this.scrollPort_.setForegroundColor(this.foregroundColor); |
| 1292 | this.scrollPort_.setBackgroundColor(this.backgroundColor); |
| 1293 | } |
| 1294 | }; |
| 1295 | |
| 1296 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1297 | * Ring the terminal bell. |
| 1298 | * |
| 1299 | * We only have a visual bell, which quickly toggles inverse video in the |
| 1300 | * terminal. |
| 1301 | */ |
| 1302 | hterm.Terminal.prototype.ringBell = function() { |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 1303 | this.cursorNode_.style.backgroundColor = |
| 1304 | this.scrollPort_.getForegroundColor(); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1305 | |
| 1306 | var self = this; |
| 1307 | setTimeout(function() { |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 1308 | self.cursorNode_.style.backgroundColor = self.cursorColor; |
| 1309 | }, 200); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1310 | }; |
| 1311 | |
| 1312 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1313 | * Set the origin mode bit. |
| 1314 | * |
| 1315 | * If origin mode is on, certain VT cursor and scrolling commands measure their |
| 1316 | * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds |
| 1317 | * to the top of the addressable screen. |
| 1318 | * |
| 1319 | * Defaults to off. |
| 1320 | * |
| 1321 | * @param {boolean} state True to set origin mode, false to unset. |
| 1322 | */ |
| 1323 | hterm.Terminal.prototype.setOriginMode = function(state) { |
| 1324 | this.options_.originMode = state; |
rginda | e4d2923 | 2012-01-19 10:47:13 -0800 | [diff] [blame] | 1325 | this.setCursorPosition(0, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1326 | }; |
| 1327 | |
| 1328 | /** |
| 1329 | * Set the insert mode bit. |
| 1330 | * |
| 1331 | * If insert mode is on, existing text beyond the cursor position will be |
| 1332 | * shifted right to make room for new text. Otherwise, new text overwrites |
| 1333 | * any existing text. |
| 1334 | * |
| 1335 | * Defaults to off. |
| 1336 | * |
| 1337 | * @param {boolean} state True to set insert mode, false to unset. |
| 1338 | */ |
| 1339 | hterm.Terminal.prototype.setInsertMode = function(state) { |
| 1340 | this.options_.insertMode = state; |
| 1341 | }; |
| 1342 | |
| 1343 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1344 | * Set the auto carriage return bit. |
| 1345 | * |
| 1346 | * If auto carriage return is on then a formfeed character is interpreted |
| 1347 | * as a newline, otherwise it's the same as a linefeed. The difference boils |
| 1348 | * down to whether or not the cursor column is reset. |
| 1349 | */ |
| 1350 | hterm.Terminal.prototype.setAutoCarriageReturn = function(state) { |
| 1351 | this.options_.autoCarriageReturn = state; |
| 1352 | }; |
| 1353 | |
| 1354 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1355 | * Set the wraparound mode bit. |
| 1356 | * |
| 1357 | * If wraparound mode is on, certain VT commands will allow the cursor to wrap |
| 1358 | * to the start of the following row. Otherwise, the cursor is clamped to the |
| 1359 | * end of the screen and attempts to write past it are ignored. |
| 1360 | * |
| 1361 | * Defaults to on. |
| 1362 | * |
| 1363 | * @param {boolean} state True to set wraparound mode, false to unset. |
| 1364 | */ |
| 1365 | hterm.Terminal.prototype.setWraparound = function(state) { |
| 1366 | this.options_.wraparound = state; |
| 1367 | }; |
| 1368 | |
| 1369 | /** |
| 1370 | * Set the reverse-wraparound mode bit. |
| 1371 | * |
| 1372 | * If wraparound mode is off, certain VT commands will allow the cursor to wrap |
| 1373 | * to the end of the previous row. Otherwise, the cursor is clamped to column |
| 1374 | * 0. |
| 1375 | * |
| 1376 | * Defaults to off. |
| 1377 | * |
| 1378 | * @param {boolean} state True to set reverse-wraparound mode, false to unset. |
| 1379 | */ |
| 1380 | hterm.Terminal.prototype.setReverseWraparound = function(state) { |
| 1381 | this.options_.reverseWraparound = state; |
| 1382 | }; |
| 1383 | |
| 1384 | /** |
| 1385 | * Selects between the primary and alternate screens. |
| 1386 | * |
| 1387 | * If alternate mode is on, the alternate screen is active. Otherwise the |
| 1388 | * primary screen is active. |
| 1389 | * |
| 1390 | * Swapping screens has no effect on the scrollback buffer. |
| 1391 | * |
| 1392 | * Each screen maintains its own cursor position. |
| 1393 | * |
| 1394 | * Defaults to off. |
| 1395 | * |
| 1396 | * @param {boolean} state True to set alternate mode, false to unset. |
| 1397 | */ |
| 1398 | hterm.Terminal.prototype.setAlternateMode = function(state) { |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 1399 | var cursor = this.saveCursor(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1400 | this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_; |
| 1401 | |
| 1402 | this.screen_.setColumnCount(this.screenSize.width); |
| 1403 | |
| 1404 | var rowDelta = this.screenSize.height - this.screen_.getHeight(); |
| 1405 | if (rowDelta > 0) |
| 1406 | this.appendRows_(rowDelta); |
| 1407 | |
rginda | 6d39740 | 2012-01-17 10:58:29 -0800 | [diff] [blame] | 1408 | this.restoreCursor(cursor); |
| 1409 | |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 1410 | this.scrollPort_.invalidate(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1411 | this.syncCursorPosition_(); |
| 1412 | }; |
| 1413 | |
| 1414 | /** |
| 1415 | * Set the cursor-blink mode bit. |
| 1416 | * |
| 1417 | * If cursor-blink is on, the cursor will blink when it is visible. Otherwise |
| 1418 | * a visible cursor does not blink. |
| 1419 | * |
| 1420 | * You should make sure to turn blinking off if you're going to dispose of a |
| 1421 | * terminal, otherwise you'll leak a timeout. |
| 1422 | * |
| 1423 | * Defaults to on. |
| 1424 | * |
| 1425 | * @param {boolean} state True to set cursor-blink mode, false to unset. |
| 1426 | */ |
| 1427 | hterm.Terminal.prototype.setCursorBlink = function(state) { |
| 1428 | this.options_.cursorBlink = state; |
| 1429 | |
| 1430 | if (!state && this.timeouts_.cursorBlink) { |
| 1431 | clearTimeout(this.timeouts_.cursorBlink); |
| 1432 | delete this.timeouts_.cursorBlink; |
| 1433 | } |
| 1434 | |
| 1435 | if (this.options_.cursorVisible) |
| 1436 | this.setCursorVisible(true); |
| 1437 | }; |
| 1438 | |
| 1439 | /** |
| 1440 | * Set the cursor-visible mode bit. |
| 1441 | * |
| 1442 | * If cursor-visible is on, the cursor will be visible. Otherwise it will not. |
| 1443 | * |
| 1444 | * Defaults to on. |
| 1445 | * |
| 1446 | * @param {boolean} state True to set cursor-visible mode, false to unset. |
| 1447 | */ |
| 1448 | hterm.Terminal.prototype.setCursorVisible = function(state) { |
| 1449 | this.options_.cursorVisible = state; |
| 1450 | |
| 1451 | if (!state) { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1452 | this.cursorNode_.style.opacity = '0'; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1453 | return; |
| 1454 | } |
| 1455 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1456 | this.syncCursorPosition_(); |
| 1457 | |
| 1458 | this.cursorNode_.style.opacity = '1'; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1459 | |
| 1460 | if (this.options_.cursorBlink) { |
| 1461 | if (this.timeouts_.cursorBlink) |
| 1462 | return; |
| 1463 | |
| 1464 | this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this), |
| 1465 | 500); |
| 1466 | } else { |
| 1467 | if (this.timeouts_.cursorBlink) { |
| 1468 | clearTimeout(this.timeouts_.cursorBlink); |
| 1469 | delete this.timeouts_.cursorBlink; |
| 1470 | } |
| 1471 | } |
| 1472 | }; |
| 1473 | |
| 1474 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1475 | * Synchronizes the visible cursor and document selection with the current |
| 1476 | * cursor coordinates. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1477 | */ |
| 1478 | hterm.Terminal.prototype.syncCursorPosition_ = function() { |
| 1479 | var topRowIndex = this.scrollPort_.getTopRowIndex(); |
| 1480 | var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex); |
| 1481 | var cursorRowIndex = this.scrollbackRows_.length + |
| 1482 | this.screen_.cursorPosition.row; |
| 1483 | |
| 1484 | if (cursorRowIndex > bottomRowIndex) { |
| 1485 | // Cursor is scrolled off screen, move it outside of the visible area. |
| 1486 | this.cursorNode_.style.top = -this.characterSize_.height; |
| 1487 | return; |
| 1488 | } |
| 1489 | |
| 1490 | this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin + |
| 1491 | this.characterSize_.height * (cursorRowIndex - topRowIndex); |
| 1492 | this.cursorNode_.style.left = this.characterSize_.width * |
| 1493 | this.screen_.cursorPosition.column; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1494 | |
| 1495 | this.cursorNode_.setAttribute('title', |
| 1496 | '(' + this.screen_.cursorPosition.row + |
| 1497 | ', ' + this.screen_.cursorPosition.column + |
| 1498 | ')'); |
| 1499 | |
| 1500 | // Update the caret for a11y purposes. |
| 1501 | var selection = this.document_.getSelection(); |
| 1502 | if (selection && selection.isCollapsed) |
| 1503 | this.screen_.syncSelectionCaret(selection); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1504 | }; |
| 1505 | |
| 1506 | /** |
| 1507 | * Synchronizes the visible cursor with the current cursor coordinates. |
| 1508 | * |
| 1509 | * The sync will happen asynchronously, soon after the call stack winds down. |
| 1510 | * Multiple calls will be coalesced into a single sync. |
| 1511 | */ |
| 1512 | hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() { |
| 1513 | if (this.timeouts_.syncCursor) |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1514 | return; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1515 | |
| 1516 | var self = this; |
| 1517 | this.timeouts_.syncCursor = setTimeout(function() { |
| 1518 | self.syncCursorPosition_(); |
| 1519 | delete self.timeouts_.syncCursor; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1520 | }, 0); |
| 1521 | }; |
| 1522 | |
| 1523 | /** |
| 1524 | * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected. |
| 1525 | * |
| 1526 | * @param {string} string The VT string representing the keystroke. |
| 1527 | */ |
| 1528 | hterm.Terminal.prototype.onVTKeystroke = function(string) { |
| 1529 | if (this.scrollOnKeystroke) |
| 1530 | this.scrollPort_.scrollRowToBottom(this.getRowCount()); |
| 1531 | |
| 1532 | this.io.onVTKeystroke(string); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1533 | }; |
| 1534 | |
| 1535 | /** |
| 1536 | * React when the ScrollPort is scrolled. |
| 1537 | */ |
| 1538 | hterm.Terminal.prototype.onScroll_ = function() { |
| 1539 | this.scheduleSyncCursorPosition_(); |
| 1540 | }; |
| 1541 | |
| 1542 | /** |
rginda | 9846e2f | 2012-01-27 13:53:33 -0800 | [diff] [blame^] | 1543 | * React when text is pasted into the scrollPort. |
| 1544 | */ |
| 1545 | hterm.Terminal.prototype.onPaste_ = function(e) { |
| 1546 | this.io.onVTKeystroke(e.text); |
| 1547 | }; |
| 1548 | |
| 1549 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1550 | * React when the ScrollPort is resized. |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1551 | * |
| 1552 | * Note: This function should not directly contain code that alters the internal |
| 1553 | * state of the terminal. That kind of code belongs in realizeWidth or |
| 1554 | * realizeHeight, so that it can be executed synchronously in the case of a |
| 1555 | * programmatic width change. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1556 | */ |
| 1557 | hterm.Terminal.prototype.onResize_ = function() { |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1558 | var columnCount = Math.floor(this.scrollPort_.getScreenWidth() / |
| 1559 | this.characterSize_.width); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1560 | var rowCount = this.scrollPort_.visibleRowCount; |
Dmitry Polukhin | bb2ef71 | 2012-01-19 19:00:37 +0400 | [diff] [blame] | 1561 | this.realizeSize_(columnCount, rowCount); |
rginda | c9bc550 | 2012-01-18 11:48:44 -0800 | [diff] [blame] | 1562 | this.scheduleSyncCursorPosition_(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1563 | }; |
| 1564 | |
| 1565 | /** |
| 1566 | * Service the cursor blink timeout. |
| 1567 | */ |
| 1568 | hterm.Terminal.prototype.onCursorBlink_ = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1569 | if (this.cursorNode_.style.opacity == '0') { |
| 1570 | this.cursorNode_.style.opacity = '1'; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1571 | } else { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 1572 | this.cursorNode_.style.opacity = '0'; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 1573 | } |
| 1574 | }; |