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 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 7 | /** |
| 8 | * @fileoverview This class represents a single terminal screen full of text. |
| 9 | * |
| 10 | * It maintains the current cursor position and has basic methods for text |
| 11 | * insert and overwrite, and adding or removing rows from the screen. |
| 12 | * |
| 13 | * This class has no knowledge of the scrollback buffer. |
| 14 | * |
| 15 | * The number of rows on the screen is determined only by the number of rows |
| 16 | * that the caller inserts into the screen. If a caller wants to ensure a |
| 17 | * constant number of rows on the screen, it's their responsibility to remove a |
| 18 | * row for each row inserted. |
| 19 | * |
| 20 | * The screen width, in contrast, is enforced locally. |
| 21 | * |
| 22 | * |
| 23 | * In practice... |
| 24 | * - The hterm.Terminal class holds two hterm.Screen instances. One for the |
| 25 | * primary screen and one for the alternate screen. |
| 26 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 27 | * - The html.Screen class only cares that rows are HTML Elements. In the |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 28 | * larger context of hterm, however, the rows happen to be displayed by an |
| 29 | * hterm.ScrollPort and have to follow a few rules as a result. Each |
| 30 | * row must be rooted by the custom HTML tag 'x-row', and each must have a |
| 31 | * rowIndex property that corresponds to the index of the row in the context |
| 32 | * of the scrollback buffer. These invariants are enforced by hterm.Terminal |
| 33 | * because that is the class using the hterm.Screen in the context of an |
| 34 | * hterm.ScrollPort. |
| 35 | */ |
| 36 | |
| 37 | /** |
| 38 | * Create a new screen instance. |
| 39 | * |
| 40 | * The screen initially has no rows and a maximum column count of 0. |
| 41 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 42 | * @param {number=} columnCount The maximum number of columns for this |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 43 | * screen. See insertString() and overwriteString() for information about |
| 44 | * what happens when too many characters are added too a row. Defaults to |
| 45 | * 0 if not provided. |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 46 | * @constructor |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 47 | */ |
Mike Frysinger | 60a156d | 2019-06-13 10:15:45 -0400 | [diff] [blame] | 48 | hterm.Screen = function(columnCount=0) { |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 49 | /** |
| 50 | * Public, read-only access to the rows in this screen. |
Mike Frysinger | 23b5b83 | 2019-10-01 17:05:29 -0400 | [diff] [blame] | 51 | * |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 52 | * @type {!Array<!Element>} |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 53 | */ |
| 54 | this.rowsArray = []; |
| 55 | |
| 56 | // The max column width for this screen. |
Mike Frysinger | 60a156d | 2019-06-13 10:15:45 -0400 | [diff] [blame] | 57 | this.columnCount_ = columnCount; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 58 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 59 | // The current color, bold, underline and blink attributes. |
| 60 | this.textAttributes = new hterm.TextAttributes(window.document); |
| 61 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 62 | // Current zero-based cursor coordinates. |
| 63 | this.cursorPosition = new hterm.RowCol(0, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 64 | |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 65 | // Saved state used by DECSC and related settings. This is only for saving |
| 66 | // and restoring specific state, not for the current/active state. |
| 67 | this.cursorState_ = new hterm.Screen.CursorState(this); |
| 68 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 69 | // The node containing the row that the cursor is positioned on. |
| 70 | this.cursorRowNode_ = null; |
| 71 | |
| 72 | // The node containing the span of text that the cursor is positioned on. |
| 73 | this.cursorNode_ = null; |
| 74 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 75 | // The offset in column width into cursorNode_ where the cursor is positioned. |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 76 | this.cursorOffset_ = 0; |
Mike Frysinger | 664e999 | 2017-05-19 01:24:24 -0400 | [diff] [blame] | 77 | |
| 78 | // Regexes for expanding word selections. |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 79 | /** @type {?string} */ |
Mike Frysinger | 664e999 | 2017-05-19 01:24:24 -0400 | [diff] [blame] | 80 | this.wordBreakMatchLeft = null; |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 81 | /** @type {?string} */ |
Mike Frysinger | 664e999 | 2017-05-19 01:24:24 -0400 | [diff] [blame] | 82 | this.wordBreakMatchRight = null; |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 83 | /** @type {?string} */ |
Mike Frysinger | 664e999 | 2017-05-19 01:24:24 -0400 | [diff] [blame] | 84 | this.wordBreakMatchMiddle = null; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | /** |
| 88 | * Return the screen size as an hterm.Size object. |
| 89 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 90 | * @return {!hterm.Size} hterm.Size object representing the current number |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 91 | * of rows and columns in this screen. |
| 92 | */ |
| 93 | hterm.Screen.prototype.getSize = function() { |
| 94 | return new hterm.Size(this.columnCount_, this.rowsArray.length); |
| 95 | }; |
| 96 | |
| 97 | /** |
| 98 | * Return the current number of rows in this screen. |
| 99 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 100 | * @return {number} The number of rows in this screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 101 | */ |
| 102 | hterm.Screen.prototype.getHeight = function() { |
| 103 | return this.rowsArray.length; |
| 104 | }; |
| 105 | |
| 106 | /** |
| 107 | * Return the current number of columns in this screen. |
| 108 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 109 | * @return {number} The number of columns in this screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 110 | */ |
| 111 | hterm.Screen.prototype.getWidth = function() { |
| 112 | return this.columnCount_; |
| 113 | }; |
| 114 | |
| 115 | /** |
| 116 | * Set the maximum number of columns per row. |
| 117 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 118 | * @param {number} count The maximum number of columns per row. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 119 | */ |
| 120 | hterm.Screen.prototype.setColumnCount = function(count) { |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 121 | this.columnCount_ = count; |
| 122 | |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 123 | if (this.cursorPosition.column >= count) |
| 124 | this.setCursorPosition(this.cursorPosition.row, count - 1); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | /** |
| 128 | * Remove the first row from the screen and return it. |
| 129 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 130 | * @return {!Element} The first row in this screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 131 | */ |
| 132 | hterm.Screen.prototype.shiftRow = function() { |
| 133 | return this.shiftRows(1)[0]; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 134 | }; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 135 | |
| 136 | /** |
| 137 | * Remove rows from the top of the screen and return them as an array. |
| 138 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 139 | * @param {number} count The number of rows to remove. |
| 140 | * @return {!Array<!Element>} The selected rows. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 141 | */ |
| 142 | hterm.Screen.prototype.shiftRows = function(count) { |
| 143 | return this.rowsArray.splice(0, count); |
| 144 | }; |
| 145 | |
| 146 | /** |
| 147 | * Insert a row at the top of the screen. |
| 148 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 149 | * @param {!Element} row The row to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 150 | */ |
| 151 | hterm.Screen.prototype.unshiftRow = function(row) { |
| 152 | this.rowsArray.splice(0, 0, row); |
| 153 | }; |
| 154 | |
| 155 | /** |
| 156 | * Insert rows at the top of the screen. |
| 157 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 158 | * @param {!Array<!Element>} rows The rows to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 159 | */ |
| 160 | hterm.Screen.prototype.unshiftRows = function(rows) { |
| 161 | this.rowsArray.unshift.apply(this.rowsArray, rows); |
| 162 | }; |
| 163 | |
| 164 | /** |
| 165 | * Remove the last row from the screen and return it. |
| 166 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 167 | * @return {!Element} The last row in this screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 168 | */ |
| 169 | hterm.Screen.prototype.popRow = function() { |
| 170 | return this.popRows(1)[0]; |
| 171 | }; |
| 172 | |
| 173 | /** |
| 174 | * Remove rows from the bottom of the screen and return them as an array. |
| 175 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 176 | * @param {number} count The number of rows to remove. |
| 177 | * @return {!Array<!Element>} The selected rows. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 178 | */ |
| 179 | hterm.Screen.prototype.popRows = function(count) { |
| 180 | return this.rowsArray.splice(this.rowsArray.length - count, count); |
| 181 | }; |
| 182 | |
| 183 | /** |
| 184 | * Insert a row at the bottom of the screen. |
| 185 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 186 | * @param {!Element} row The row to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 187 | */ |
| 188 | hterm.Screen.prototype.pushRow = function(row) { |
| 189 | this.rowsArray.push(row); |
| 190 | }; |
| 191 | |
| 192 | /** |
| 193 | * Insert rows at the bottom of the screen. |
| 194 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 195 | * @param {!Array<!Element>} rows The rows to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 196 | */ |
| 197 | hterm.Screen.prototype.pushRows = function(rows) { |
| 198 | rows.push.apply(this.rowsArray, rows); |
| 199 | }; |
| 200 | |
| 201 | /** |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 202 | * Insert a row at the specified row of the screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 203 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 204 | * @param {number} index The index to insert the row. |
| 205 | * @param {!Element} row The row to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 206 | */ |
| 207 | hterm.Screen.prototype.insertRow = function(index, row) { |
| 208 | this.rowsArray.splice(index, 0, row); |
| 209 | }; |
| 210 | |
| 211 | /** |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 212 | * Insert rows at the specified row of the screen. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 213 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 214 | * @param {number} index The index to insert the rows. |
| 215 | * @param {!Array<!Element>} rows The rows to insert. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 216 | */ |
| 217 | hterm.Screen.prototype.insertRows = function(index, rows) { |
| 218 | for (var i = 0; i < rows.length; i++) { |
| 219 | this.rowsArray.splice(index + i, 0, rows[i]); |
| 220 | } |
| 221 | }; |
| 222 | |
| 223 | /** |
Evan Jones | 2600d4f | 2016-12-06 09:29:36 -0500 | [diff] [blame] | 224 | * Remove a row from the screen and return it. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 225 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 226 | * @param {number} index The index of the row to remove. |
| 227 | * @return {!Element} The selected row. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 228 | */ |
| 229 | hterm.Screen.prototype.removeRow = function(index) { |
| 230 | return this.rowsArray.splice(index, 1)[0]; |
| 231 | }; |
| 232 | |
| 233 | /** |
| 234 | * Remove rows from the bottom of the screen and return them as an array. |
| 235 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 236 | * @param {number} index The index to start removing rows. |
| 237 | * @param {number} count The number of rows to remove. |
| 238 | * @return {!Array<!Element>} The selected rows. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 239 | */ |
| 240 | hterm.Screen.prototype.removeRows = function(index, count) { |
| 241 | return this.rowsArray.splice(index, count); |
| 242 | }; |
| 243 | |
| 244 | /** |
| 245 | * Invalidate the current cursor position. |
| 246 | * |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 247 | * This sets this.cursorPosition to (0, 0) and clears out some internal |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 248 | * data. |
| 249 | * |
| 250 | * Attempting to insert or overwrite text while the cursor position is invalid |
| 251 | * will raise an obscure exception. |
| 252 | */ |
| 253 | hterm.Screen.prototype.invalidateCursorPosition = function() { |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 254 | this.cursorPosition.move(0, 0); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 255 | this.cursorRowNode_ = null; |
| 256 | this.cursorNode_ = null; |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 257 | this.cursorOffset_ = 0; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 261 | * Clear the contents of the cursor row. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 262 | */ |
| 263 | hterm.Screen.prototype.clearCursorRow = function() { |
| 264 | this.cursorRowNode_.innerHTML = ''; |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 265 | this.cursorRowNode_.removeAttribute('line-overflow'); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 266 | this.cursorOffset_ = 0; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 267 | this.cursorPosition.column = 0; |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 268 | this.cursorPosition.overflow = false; |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 269 | |
| 270 | var text; |
| 271 | if (this.textAttributes.isDefault()) { |
| 272 | text = ''; |
| 273 | } else { |
Mike Frysinger | 73e5646 | 2019-07-17 00:23:46 -0500 | [diff] [blame] | 274 | text = ' '.repeat(this.columnCount_); |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame] | 277 | // We shouldn't honor inverse colors when clearing an area, to match |
| 278 | // xterm's back color erase behavior. |
Edoardo Spadolini | 2fd4364 | 2014-08-23 22:59:57 +0200 | [diff] [blame] | 279 | var inverse = this.textAttributes.inverse; |
| 280 | this.textAttributes.inverse = false; |
| 281 | this.textAttributes.syncColors(); |
| 282 | |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 283 | var node = this.textAttributes.createContainer(text); |
| 284 | this.cursorRowNode_.appendChild(node); |
| 285 | this.cursorNode_ = node; |
Edoardo Spadolini | 2fd4364 | 2014-08-23 22:59:57 +0200 | [diff] [blame] | 286 | |
| 287 | this.textAttributes.inverse = inverse; |
| 288 | this.textAttributes.syncColors(); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 289 | }; |
| 290 | |
| 291 | /** |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 292 | * Mark the current row as having overflowed to the next line. |
| 293 | * |
| 294 | * The line overflow state is used when converting a range of rows into text. |
| 295 | * It makes it possible to recombine two or more overflow terminal rows into |
| 296 | * a single line. |
| 297 | * |
| 298 | * This is distinct from the cursor being in the overflow state. Cursor |
| 299 | * overflow indicates that printing at the cursor position will commit a |
| 300 | * line overflow, unless it is preceded by a repositioning of the cursor |
| 301 | * to a non-overflow state. |
| 302 | */ |
| 303 | hterm.Screen.prototype.commitLineOverflow = function() { |
| 304 | this.cursorRowNode_.setAttribute('line-overflow', true); |
| 305 | }; |
| 306 | |
| 307 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 308 | * Relocate the cursor to a give row and column. |
| 309 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 310 | * @param {number} row The zero based row. |
| 311 | * @param {number} column The zero based column. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 312 | */ |
| 313 | hterm.Screen.prototype.setCursorPosition = function(row, column) { |
rginda | 11057d5 | 2012-04-25 12:29:56 -0700 | [diff] [blame] | 314 | if (!this.rowsArray.length) { |
| 315 | console.warn('Attempt to set cursor position on empty screen.'); |
| 316 | return; |
| 317 | } |
| 318 | |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 319 | if (row >= this.rowsArray.length) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 320 | console.error('Row out of bounds: ' + row); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 321 | row = this.rowsArray.length - 1; |
| 322 | } else if (row < 0) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 323 | console.error('Row out of bounds: ' + row); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 324 | row = 0; |
| 325 | } |
| 326 | |
| 327 | if (column >= this.columnCount_) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 328 | console.error('Column out of bounds: ' + column); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 329 | column = this.columnCount_ - 1; |
| 330 | } else if (column < 0) { |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 331 | console.error('Column out of bounds: ' + column); |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 332 | column = 0; |
| 333 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 334 | |
rginda | 2312fff | 2012-01-05 16:20:52 -0800 | [diff] [blame] | 335 | this.cursorPosition.overflow = false; |
| 336 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 337 | var rowNode = this.rowsArray[row]; |
| 338 | var node = rowNode.firstChild; |
| 339 | |
| 340 | if (!node) { |
| 341 | node = rowNode.ownerDocument.createTextNode(''); |
| 342 | rowNode.appendChild(node); |
| 343 | } |
| 344 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 345 | var currentColumn = 0; |
| 346 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 347 | if (rowNode == this.cursorRowNode_) { |
| 348 | if (column >= this.cursorPosition.column - this.cursorOffset_) { |
| 349 | node = this.cursorNode_; |
| 350 | currentColumn = this.cursorPosition.column - this.cursorOffset_; |
| 351 | } |
| 352 | } else { |
| 353 | this.cursorRowNode_ = rowNode; |
| 354 | } |
| 355 | |
| 356 | this.cursorPosition.move(row, column); |
| 357 | |
| 358 | while (node) { |
| 359 | var offset = column - currentColumn; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 360 | var width = hterm.TextAttributes.nodeWidth(node); |
| 361 | if (!node.nextSibling || width > offset) { |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 362 | this.cursorNode_ = node; |
| 363 | this.cursorOffset_ = offset; |
| 364 | return; |
| 365 | } |
| 366 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 367 | currentColumn += width; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 368 | node = node.nextSibling; |
| 369 | } |
| 370 | }; |
| 371 | |
| 372 | /** |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 373 | * Set the provided selection object to be a caret selection at the current |
| 374 | * cursor position. |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 375 | * |
| 376 | * @param {!Selection} selection |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 377 | */ |
| 378 | hterm.Screen.prototype.syncSelectionCaret = function(selection) { |
Rob Spies | 06533ba | 2014-04-24 11:20:37 -0700 | [diff] [blame] | 379 | try { |
| 380 | selection.collapse(this.cursorNode_, this.cursorOffset_); |
| 381 | } catch (firefoxIgnoredException) { |
| 382 | // FF can throw an exception if the range is off, rather than just not |
| 383 | // performing the collapse. |
| 384 | } |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 385 | }; |
| 386 | |
| 387 | /** |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 388 | * Split a single node into two nodes at the given offset. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 389 | * |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 390 | * For example: |
| 391 | * Given the DOM fragment '<div><span>Hello World</span></div>', call splitNode_ |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame] | 392 | * passing the span and an offset of 6. This would modify the fragment to |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 393 | * become: '<div><span>Hello </span><span>World</span></div>'. If the span |
| 394 | * had any attributes they would have been copied to the new span as well. |
| 395 | * |
| 396 | * The to-be-split node must have a container, so that the new node can be |
| 397 | * placed next to it. |
| 398 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 399 | * @param {!Node} node The node to split. |
| 400 | * @param {number} offset The offset into the node where the split should |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 401 | * occur. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 402 | */ |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 403 | hterm.Screen.prototype.splitNode_ = function(node, offset) { |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 404 | var afterNode = node.cloneNode(false); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 405 | |
rginda | 35c456b | 2012-02-09 17:29:05 -0800 | [diff] [blame] | 406 | var textContent = node.textContent; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 407 | node.textContent = hterm.TextAttributes.nodeSubstr(node, 0, offset); |
| 408 | afterNode.textContent = lib.wc.substr(textContent, offset); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 409 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 410 | if (afterNode.textContent) |
| 411 | node.parentNode.insertBefore(afterNode, node.nextSibling); |
| 412 | if (!node.textContent) |
| 413 | node.parentNode.removeChild(node); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 414 | }; |
| 415 | |
| 416 | /** |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 417 | * Ensure that text is clipped and the cursor is clamped to the column count. |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 418 | */ |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 419 | hterm.Screen.prototype.maybeClipCurrentRow = function() { |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 420 | var width = hterm.TextAttributes.nodeWidth(lib.notNull(this.cursorRowNode_)); |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 421 | |
| 422 | if (width <= this.columnCount_) { |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 423 | // Current row does not need clipping, but may need clamping. |
| 424 | if (this.cursorPosition.column >= this.columnCount_) { |
| 425 | this.setCursorPosition(this.cursorPosition.row, this.columnCount_ - 1); |
| 426 | this.cursorPosition.overflow = true; |
| 427 | } |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 428 | |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 429 | return; |
| 430 | } |
| 431 | |
| 432 | // Save off the current column so we can maybe restore it later. |
| 433 | var currentColumn = this.cursorPosition.column; |
| 434 | |
| 435 | // Move the cursor to the final column. |
| 436 | this.setCursorPosition(this.cursorPosition.row, this.columnCount_ - 1); |
| 437 | |
| 438 | // Remove any text that partially overflows. |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 439 | width = hterm.TextAttributes.nodeWidth(lib.notNull(this.cursorNode_)); |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 440 | |
| 441 | if (this.cursorOffset_ < width - 1) { |
| 442 | this.cursorNode_.textContent = hterm.TextAttributes.nodeSubstr( |
| 443 | this.cursorNode_, 0, this.cursorOffset_ + 1); |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | // Remove all nodes after the cursor. |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 447 | var rowNode = this.cursorRowNode_; |
| 448 | var node = this.cursorNode_.nextSibling; |
| 449 | |
| 450 | while (node) { |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 451 | rowNode.removeChild(node); |
| 452 | node = this.cursorNode_.nextSibling; |
| 453 | } |
| 454 | |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 455 | if (currentColumn < this.columnCount_) { |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 456 | // If the cursor was within the screen before we started then restore its |
| 457 | // position. |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 458 | this.setCursorPosition(this.cursorPosition.row, currentColumn); |
rginda | a9abdd8 | 2012-08-06 18:05:09 -0700 | [diff] [blame] | 459 | } else { |
| 460 | // Otherwise leave it at the the last column in the overflow state. |
| 461 | this.cursorPosition.overflow = true; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 462 | } |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 463 | }; |
| 464 | |
| 465 | /** |
| 466 | * Insert a string at the current character position using the current |
| 467 | * text attributes. |
| 468 | * |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 469 | * You must call maybeClipCurrentRow() after in order to clip overflowed |
| 470 | * text and clamp the cursor. |
| 471 | * |
| 472 | * It is also up to the caller to properly maintain the line overflow state |
| 473 | * using hterm.Screen..commitLineOverflow(). |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 474 | * |
| 475 | * @param {string} str The string to insert. |
| 476 | * @param {number=} wcwidth The cached lib.wc.strWidth value for |str|. Will be |
| 477 | * calculated on demand if need be. Passing in a cached value helps speed |
| 478 | * up processing as this is a hot codepath. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 479 | */ |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 480 | hterm.Screen.prototype.insertString = function(str, wcwidth=undefined) { |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 481 | var cursorNode = this.cursorNode_; |
| 482 | var cursorNodeText = cursorNode.textContent; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 483 | |
Robert Ginda | a21dfb3 | 2013-10-31 14:17:45 -0700 | [diff] [blame] | 484 | this.cursorRowNode_.removeAttribute('line-overflow'); |
| 485 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 486 | // We may alter the width of the string by prepending some missing |
| 487 | // whitespaces, so we need to record the string width ahead of time. |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 488 | if (wcwidth === undefined) |
| 489 | wcwidth = lib.wc.strWidth(str); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 490 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 491 | // No matter what, before this function exits the cursor column will have |
| 492 | // moved this much. |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 493 | this.cursorPosition.column += wcwidth; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 494 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 495 | // Local cache of the cursor offset. |
| 496 | var offset = this.cursorOffset_; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 497 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 498 | // Reverse offset is the offset measured from the end of the string. |
| 499 | // Zero implies that the cursor is at the end of the cursor node. |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 500 | var reverseOffset = hterm.TextAttributes.nodeWidth(cursorNode) - offset; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 501 | |
| 502 | if (reverseOffset < 0) { |
| 503 | // A negative reverse offset means the cursor is positioned past the end |
| 504 | // of the characters on this line. We'll need to insert the missing |
| 505 | // whitespace. |
Mike Frysinger | 73e5646 | 2019-07-17 00:23:46 -0500 | [diff] [blame] | 506 | const ws = ' '.repeat(-reverseOffset); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 507 | |
Brad Town | 7de8330 | 2015-03-12 02:10:32 -0700 | [diff] [blame] | 508 | // This whitespace should be completely unstyled. Underline, background |
| 509 | // color, and strikethrough would be visible on whitespace, so we can't use |
| 510 | // one of those spans to hold the text. |
Edoardo Spadolini | 198828a | 2014-08-08 00:22:51 +0200 | [diff] [blame] | 511 | if (!(this.textAttributes.underline || |
Brad Town | 7de8330 | 2015-03-12 02:10:32 -0700 | [diff] [blame] | 512 | this.textAttributes.strikethrough || |
Edoardo Spadolini | 198828a | 2014-08-08 00:22:51 +0200 | [diff] [blame] | 513 | this.textAttributes.background || |
| 514 | this.textAttributes.wcNode || |
Mike Frysinger | 1e98c0f | 2017-08-15 01:21:31 -0400 | [diff] [blame] | 515 | !this.textAttributes.asciiNode || |
Edoardo Spadolini | 198828a | 2014-08-08 00:22:51 +0200 | [diff] [blame] | 516 | this.textAttributes.tileData != null)) { |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 517 | // Best case scenario, we can just pretend the spaces were part of the |
| 518 | // original string. |
| 519 | str = ws + str; |
Mike Frysinger | 6a4f241 | 2017-08-31 01:11:25 -0400 | [diff] [blame] | 520 | } else if (cursorNode.nodeType == Node.TEXT_NODE || |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 521 | !(cursorNode.wcNode || |
Mike Frysinger | 1e98c0f | 2017-08-15 01:21:31 -0400 | [diff] [blame] | 522 | !cursorNode.asciiNode || |
Edoardo Spadolini | 198828a | 2014-08-08 00:22:51 +0200 | [diff] [blame] | 523 | cursorNode.tileNode || |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 524 | cursorNode.style.textDecoration || |
Mike Frysinger | 09c54f4 | 2017-12-15 01:12:30 -0500 | [diff] [blame] | 525 | cursorNode.style.textDecorationStyle || |
| 526 | cursorNode.style.textDecorationLine || |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 527 | cursorNode.style.backgroundColor)) { |
| 528 | // Second best case, the current node is able to hold the whitespace. |
| 529 | cursorNode.textContent = (cursorNodeText += ws); |
| 530 | } else { |
| 531 | // Worst case, we have to create a new node to hold the whitespace. |
| 532 | var wsNode = cursorNode.ownerDocument.createTextNode(ws); |
| 533 | this.cursorRowNode_.insertBefore(wsNode, cursorNode.nextSibling); |
| 534 | this.cursorNode_ = cursorNode = wsNode; |
| 535 | this.cursorOffset_ = offset = -reverseOffset; |
| 536 | cursorNodeText = ws; |
| 537 | } |
| 538 | |
| 539 | // We now know for sure that we're at the last character of the cursor node. |
| 540 | reverseOffset = 0; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 541 | } |
| 542 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 543 | if (this.textAttributes.matchesContainer(cursorNode)) { |
| 544 | // The new text can be placed directly in the cursor node. |
| 545 | if (reverseOffset == 0) { |
| 546 | cursorNode.textContent = cursorNodeText + str; |
| 547 | } else if (offset == 0) { |
| 548 | cursorNode.textContent = str + cursorNodeText; |
| 549 | } else { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 550 | cursorNode.textContent = |
| 551 | hterm.TextAttributes.nodeSubstr(cursorNode, 0, offset) + |
| 552 | str + hterm.TextAttributes.nodeSubstr(cursorNode, offset); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 553 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 554 | |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 555 | this.cursorOffset_ += wcwidth; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 556 | return; |
rginda | 87b8646 | 2011-12-14 13:48:03 -0800 | [diff] [blame] | 557 | } |
| 558 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 559 | // The cursor node is the wrong style for the new text. If we're at the |
| 560 | // beginning or end of the cursor node, then the adjacent node is also a |
| 561 | // potential candidate. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 562 | |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 563 | if (offset == 0) { |
| 564 | // At the beginning of the cursor node, the check the previous sibling. |
| 565 | var previousSibling = cursorNode.previousSibling; |
| 566 | if (previousSibling && |
| 567 | this.textAttributes.matchesContainer(previousSibling)) { |
| 568 | previousSibling.textContent += str; |
| 569 | this.cursorNode_ = previousSibling; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 570 | this.cursorOffset_ = lib.wc.strWidth(previousSibling.textContent); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 571 | return; |
| 572 | } |
| 573 | |
| 574 | var newNode = this.textAttributes.createContainer(str); |
| 575 | this.cursorRowNode_.insertBefore(newNode, cursorNode); |
| 576 | this.cursorNode_ = newNode; |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 577 | this.cursorOffset_ = wcwidth; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 578 | return; |
| 579 | } |
| 580 | |
| 581 | if (reverseOffset == 0) { |
| 582 | // At the end of the cursor node, the check the next sibling. |
| 583 | var nextSibling = cursorNode.nextSibling; |
| 584 | if (nextSibling && |
| 585 | this.textAttributes.matchesContainer(nextSibling)) { |
| 586 | nextSibling.textContent = str + nextSibling.textContent; |
| 587 | this.cursorNode_ = nextSibling; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 588 | this.cursorOffset_ = lib.wc.strWidth(str); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 589 | return; |
| 590 | } |
| 591 | |
| 592 | var newNode = this.textAttributes.createContainer(str); |
| 593 | this.cursorRowNode_.insertBefore(newNode, nextSibling); |
| 594 | this.cursorNode_ = newNode; |
| 595 | // We specifically need to include any missing whitespace here, since it's |
| 596 | // going in a new node. |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 597 | this.cursorOffset_ = hterm.TextAttributes.nodeWidth(newNode); |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 598 | return; |
| 599 | } |
| 600 | |
| 601 | // Worst case, we're somewhere in the middle of the cursor node. We'll |
| 602 | // have to split it into two nodes and insert our new container in between. |
| 603 | this.splitNode_(cursorNode, offset); |
| 604 | var newNode = this.textAttributes.createContainer(str); |
| 605 | this.cursorRowNode_.insertBefore(newNode, cursorNode.nextSibling); |
| 606 | this.cursorNode_ = newNode; |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 607 | this.cursorOffset_ = wcwidth; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 608 | }; |
| 609 | |
| 610 | /** |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 611 | * Overwrite the text at the current cursor position. |
| 612 | * |
rginda | a09e733 | 2012-08-17 12:49:51 -0700 | [diff] [blame] | 613 | * You must call maybeClipCurrentRow() after in order to clip overflowed |
| 614 | * text and clamp the cursor. |
| 615 | * |
| 616 | * It is also up to the caller to properly maintain the line overflow state |
| 617 | * using hterm.Screen..commitLineOverflow(). |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 618 | * |
| 619 | * @param {string} str The source string for overwriting existing content. |
| 620 | * @param {number=} wcwidth The cached lib.wc.strWidth value for |str|. Will be |
| 621 | * calculated on demand if need be. Passing in a cached value helps speed |
| 622 | * up processing as this is a hot codepath. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 623 | */ |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 624 | hterm.Screen.prototype.overwriteString = function(str, wcwidth=undefined) { |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 625 | var maxLength = this.columnCount_ - this.cursorPosition.column; |
| 626 | if (!maxLength) |
Mike Frysinger | 159b739 | 2019-03-26 11:08:32 -0700 | [diff] [blame] | 627 | return; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 628 | |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 629 | if (wcwidth === undefined) |
| 630 | wcwidth = lib.wc.strWidth(str); |
| 631 | |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 632 | if (this.textAttributes.matchesContainer(lib.notNull(this.cursorNode_)) && |
| 633 | this.cursorNode_.textContent.substr(this.cursorOffset_) == |
| 634 | str) { |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 635 | // This overwrite would be a no-op, just move the cursor and return. |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 636 | this.cursorOffset_ += wcwidth; |
| 637 | this.cursorPosition.column += wcwidth; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 638 | return; |
| 639 | } |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 640 | |
Mike Frysinger | 6380bed | 2017-08-24 18:46:39 -0400 | [diff] [blame] | 641 | this.deleteChars(Math.min(wcwidth, maxLength)); |
| 642 | this.insertString(str, wcwidth); |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 643 | }; |
| 644 | |
| 645 | /** |
| 646 | * Forward-delete one or more characters at the current cursor position. |
| 647 | * |
| 648 | * Text to the right of the deleted characters is shifted left. Only affects |
| 649 | * characters on the same row as the cursor. |
| 650 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 651 | * @param {number} count The column width of characters to delete. This is |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 652 | * clamped to the column width minus the cursor column. |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 653 | * @return {number} The column width of the characters actually deleted. |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 654 | */ |
| 655 | hterm.Screen.prototype.deleteChars = function(count) { |
| 656 | var node = this.cursorNode_; |
| 657 | var offset = this.cursorOffset_; |
| 658 | |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 659 | var currentCursorColumn = this.cursorPosition.column; |
| 660 | count = Math.min(count, this.columnCount_ - currentCursorColumn); |
| 661 | if (!count) |
| 662 | return 0; |
| 663 | |
| 664 | var rv = count; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 665 | var startLength, endLength; |
rginda | a19afe2 | 2012-01-25 15:40:22 -0800 | [diff] [blame] | 666 | |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 667 | while (node && count) { |
Mike Frysinger | 859bcbd | 2017-08-28 23:48:43 -0400 | [diff] [blame] | 668 | // Sanity check so we don't loop forever, but we don't also go quietly. |
| 669 | if (count < 0) { |
| 670 | console.error(`Deleting ${rv} chars went negative: ${count}`); |
| 671 | break; |
| 672 | } |
| 673 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 674 | startLength = hterm.TextAttributes.nodeWidth(node); |
| 675 | node.textContent = hterm.TextAttributes.nodeSubstr(node, 0, offset) + |
| 676 | hterm.TextAttributes.nodeSubstr(node, offset + count); |
| 677 | endLength = hterm.TextAttributes.nodeWidth(node); |
Mike Frysinger | 859bcbd | 2017-08-28 23:48:43 -0400 | [diff] [blame] | 678 | |
| 679 | // Deal with splitting wide characters. There are two ways: we could delete |
| 680 | // the first column or the second column. In both cases, we delete the wide |
| 681 | // character and replace one of the columns with a space (since the other |
| 682 | // was deleted). If there are more chars to delete, the next loop will pick |
| 683 | // up the slack. |
| 684 | if (node.wcNode && offset < startLength && |
Joel Hockey | d36efd6 | 2019-09-30 14:16:20 -0700 | [diff] [blame] | 685 | ((endLength && startLength == endLength) || |
| 686 | (!endLength && offset == 1))) { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 687 | // No characters were deleted when there should be. We're probably trying |
| 688 | // to delete one column width from a wide character node. We remove the |
| 689 | // wide character node here and replace it with a single space. |
| 690 | var spaceNode = this.textAttributes.createContainer(' '); |
Mike Frysinger | 859bcbd | 2017-08-28 23:48:43 -0400 | [diff] [blame] | 691 | node.parentNode.insertBefore(spaceNode, offset ? node : node.nextSibling); |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 692 | node.textContent = ''; |
| 693 | endLength = 0; |
| 694 | count -= 1; |
Mike Frysinger | 859bcbd | 2017-08-28 23:48:43 -0400 | [diff] [blame] | 695 | } else |
| 696 | count -= startLength - endLength; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 697 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 698 | var nextNode = node.nextSibling; |
| 699 | if (endLength == 0 && node != this.cursorNode_) { |
| 700 | node.parentNode.removeChild(node); |
| 701 | } |
| 702 | node = nextNode; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 703 | offset = 0; |
| 704 | } |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 705 | |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 706 | // Remove this.cursorNode_ if it is an empty non-text node. |
Mike Frysinger | 6a4f241 | 2017-08-31 01:11:25 -0400 | [diff] [blame] | 707 | if (this.cursorNode_.nodeType != Node.TEXT_NODE && |
| 708 | !this.cursorNode_.textContent) { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 709 | var cursorNode = this.cursorNode_; |
| 710 | if (cursorNode.previousSibling) { |
| 711 | this.cursorNode_ = cursorNode.previousSibling; |
| 712 | this.cursorOffset_ = hterm.TextAttributes.nodeWidth( |
| 713 | cursorNode.previousSibling); |
| 714 | } else if (cursorNode.nextSibling) { |
| 715 | this.cursorNode_ = cursorNode.nextSibling; |
| 716 | this.cursorOffset_ = 0; |
| 717 | } else { |
| 718 | var emptyNode = this.cursorRowNode_.ownerDocument.createTextNode(''); |
| 719 | this.cursorRowNode_.appendChild(emptyNode); |
| 720 | this.cursorNode_ = emptyNode; |
| 721 | this.cursorOffset_ = 0; |
| 722 | } |
| 723 | this.cursorRowNode_.removeChild(cursorNode); |
| 724 | } |
| 725 | |
Robert Ginda | 7fd5708 | 2012-09-25 14:41:47 -0700 | [diff] [blame] | 726 | return rv; |
rginda | 8ba3364 | 2011-12-14 12:31:31 -0800 | [diff] [blame] | 727 | }; |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 728 | |
| 729 | /** |
| 730 | * Finds first X-ROW of a line containing specified X-ROW. |
| 731 | * Used to support line overflow. |
| 732 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 733 | * @param {!Node} row X-ROW to begin search for first row of line. |
| 734 | * @return {!Node} The X-ROW that is at the beginning of the line. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 735 | **/ |
| 736 | hterm.Screen.prototype.getLineStartRow_ = function(row) { |
| 737 | while (row.previousSibling && |
| 738 | row.previousSibling.hasAttribute('line-overflow')) { |
| 739 | row = row.previousSibling; |
| 740 | } |
| 741 | return row; |
| 742 | }; |
| 743 | |
| 744 | /** |
| 745 | * Gets text of a line beginning with row. |
| 746 | * Supports line overflow. |
| 747 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 748 | * @param {!Node} row First X-ROW of line. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 749 | * @return {string} Text content of line. |
| 750 | **/ |
| 751 | hterm.Screen.prototype.getLineText_ = function(row) { |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 752 | let rowText = ''; |
| 753 | let rowOrNull = row; |
| 754 | while (rowOrNull) { |
| 755 | rowText += rowOrNull.textContent; |
| 756 | if (rowOrNull.hasAttribute('line-overflow')) { |
| 757 | rowOrNull = rowOrNull.nextSibling; |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 758 | } else { |
| 759 | break; |
| 760 | } |
| 761 | } |
| 762 | return rowText; |
| 763 | }; |
| 764 | |
| 765 | /** |
| 766 | * Returns X-ROW that is ancestor of the node. |
| 767 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 768 | * @param {!Node} node Node to get X-ROW ancestor for. |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 769 | * @return {?Node} X-ROW ancestor of node, or null if not found. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 770 | **/ |
| 771 | hterm.Screen.prototype.getXRowAncestor_ = function(node) { |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 772 | let nodeOrNull = node; |
| 773 | while (nodeOrNull) { |
| 774 | if (nodeOrNull.nodeName === 'X-ROW') |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 775 | break; |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 776 | nodeOrNull = nodeOrNull.parentNode; |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 777 | } |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 778 | return nodeOrNull; |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 779 | }; |
| 780 | |
| 781 | /** |
| 782 | * Returns position within line of character at offset within node. |
| 783 | * Supports line overflow. |
| 784 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 785 | * @param {!Node} row X-ROW at beginning of line. |
| 786 | * @param {!Node} node Node to get position of. |
| 787 | * @param {number} offset Offset into node. |
| 788 | * @return {number} Position within line of character at offset within node. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 789 | **/ |
| 790 | hterm.Screen.prototype.getPositionWithOverflow_ = function(row, node, offset) { |
| 791 | if (!node) |
| 792 | return -1; |
| 793 | var ancestorRow = this.getXRowAncestor_(node); |
| 794 | if (!ancestorRow) |
| 795 | return -1; |
| 796 | var position = 0; |
| 797 | while (ancestorRow != row) { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 798 | position += hterm.TextAttributes.nodeWidth(row); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 799 | if (row.hasAttribute('line-overflow') && row.nextSibling) { |
| 800 | row = row.nextSibling; |
| 801 | } else { |
| 802 | return -1; |
| 803 | } |
| 804 | } |
| 805 | return position + this.getPositionWithinRow_(row, node, offset); |
| 806 | }; |
| 807 | |
| 808 | /** |
| 809 | * Returns position within row of character at offset within node. |
| 810 | * Does not support line overflow. |
| 811 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 812 | * @param {!Node} row X-ROW to get position within. |
| 813 | * @param {!Node} node Node to get position for. |
| 814 | * @param {number} offset Offset within node to get position for. |
| 815 | * @return {number} Position within row of character at offset within node. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 816 | **/ |
| 817 | hterm.Screen.prototype.getPositionWithinRow_ = function(row, node, offset) { |
| 818 | if (node.parentNode != row) { |
Mike Frysinger | 498192d | 2017-06-26 18:23:31 -0400 | [diff] [blame] | 819 | // If we traversed to the top node, then there's nothing to find here. |
| 820 | if (node.parentNode == null) |
| 821 | return -1; |
| 822 | |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 823 | return this.getPositionWithinRow_(node.parentNode, node, offset) + |
| 824 | this.getPositionWithinRow_(row, node.parentNode, 0); |
| 825 | } |
| 826 | var position = 0; |
| 827 | for (var i = 0; i < row.childNodes.length; i++) { |
| 828 | var currentNode = row.childNodes[i]; |
| 829 | if (currentNode == node) |
| 830 | return position + offset; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 831 | position += hterm.TextAttributes.nodeWidth(currentNode); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 832 | } |
| 833 | return -1; |
| 834 | }; |
| 835 | |
| 836 | /** |
| 837 | * Returns the node and offset corresponding to position within line. |
| 838 | * Supports line overflow. |
| 839 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 840 | * @param {!Node} row X-ROW at beginning of line. |
| 841 | * @param {number} position Position within line to retrieve node and offset. |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 842 | * @return {?Array} Two element array containing node and offset respectively. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 843 | **/ |
| 844 | hterm.Screen.prototype.getNodeAndOffsetWithOverflow_ = function(row, position) { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 845 | while (row && position > hterm.TextAttributes.nodeWidth(row)) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 846 | if (row.hasAttribute('line-overflow') && row.nextSibling) { |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 847 | position -= hterm.TextAttributes.nodeWidth(row); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 848 | row = row.nextSibling; |
| 849 | } else { |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 850 | return [null, -1]; |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | return this.getNodeAndOffsetWithinRow_(row, position); |
| 854 | }; |
| 855 | |
| 856 | /** |
| 857 | * Returns the node and offset corresponding to position within row. |
| 858 | * Does not support line overflow. |
| 859 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 860 | * @param {!Node} row X-ROW to get position within. |
| 861 | * @param {number} position Position within row to retrieve node and offset. |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 862 | * @return {?Array} Two element array containing node and offset respectively. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 863 | **/ |
| 864 | hterm.Screen.prototype.getNodeAndOffsetWithinRow_ = function(row, position) { |
| 865 | for (var i = 0; i < row.childNodes.length; i++) { |
| 866 | var node = row.childNodes[i]; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 867 | var nodeTextWidth = hterm.TextAttributes.nodeWidth(node); |
| 868 | if (position <= nodeTextWidth) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 869 | if (node.nodeName === 'SPAN') { |
| 870 | /** Drill down to node contained by SPAN. **/ |
| 871 | return this.getNodeAndOffsetWithinRow_(node, position); |
| 872 | } else { |
| 873 | return [node, position]; |
| 874 | } |
| 875 | } |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 876 | position -= nodeTextWidth; |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 877 | } |
| 878 | return null; |
| 879 | }; |
| 880 | |
| 881 | /** |
| 882 | * Returns the node and offset corresponding to position within line. |
| 883 | * Supports line overflow. |
| 884 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 885 | * @param {!Node} row X-ROW at beginning of line. |
| 886 | * @param {number} start Start position of range within line. |
| 887 | * @param {number} end End position of range within line. |
| 888 | * @param {!Range} range Range to modify. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 889 | **/ |
| 890 | hterm.Screen.prototype.setRange_ = function(row, start, end, range) { |
| 891 | var startNodeAndOffset = this.getNodeAndOffsetWithOverflow_(row, start); |
| 892 | if (startNodeAndOffset == null) |
| 893 | return; |
| 894 | var endNodeAndOffset = this.getNodeAndOffsetWithOverflow_(row, end); |
| 895 | if (endNodeAndOffset == null) |
| 896 | return; |
| 897 | range.setStart(startNodeAndOffset[0], startNodeAndOffset[1]); |
| 898 | range.setEnd(endNodeAndOffset[0], endNodeAndOffset[1]); |
| 899 | }; |
| 900 | |
| 901 | /** |
John Lin | cae9b73 | 2018-03-08 13:56:35 +0800 | [diff] [blame] | 902 | * Expands selection to surrounding string with word break matches. |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 903 | * |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 904 | * @param {?Selection} selection Selection to expand. |
John Lin | cae9b73 | 2018-03-08 13:56:35 +0800 | [diff] [blame] | 905 | * @param {string} leftMatch left word break match. |
| 906 | * @param {string} rightMatch right word break match. |
| 907 | * @param {string} insideMatch inside word break match. |
| 908 | */ |
| 909 | hterm.Screen.prototype.expandSelectionWithWordBreakMatches_ = |
| 910 | function(selection, leftMatch, rightMatch, insideMatch) { |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 911 | if (!selection) |
| 912 | return; |
| 913 | |
| 914 | var range = selection.getRangeAt(0); |
| 915 | if (!range || range.toString().match(/\s/)) |
| 916 | return; |
| 917 | |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 918 | const rowElement = this.getXRowAncestor_(lib.notNull(range.startContainer)); |
Raymes Khoury | 334625a | 2018-06-25 10:29:40 +1000 | [diff] [blame] | 919 | if (!rowElement) |
| 920 | return; |
| 921 | const row = this.getLineStartRow_(rowElement); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 922 | if (!row) |
| 923 | return; |
| 924 | |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 925 | var startPosition = this.getPositionWithOverflow_( |
| 926 | row, lib.notNull(range.startContainer), range.startOffset); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 927 | if (startPosition == -1) |
| 928 | return; |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 929 | var endPosition = this.getPositionWithOverflow_( |
| 930 | row, lib.notNull(range.endContainer), range.endOffset); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 931 | if (endPosition == -1) |
| 932 | return; |
| 933 | |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 934 | //Move start to the left. |
| 935 | var rowText = this.getLineText_(row); |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 936 | var lineUpToRange = lib.wc.substring(rowText, 0, endPosition); |
Mike Frysinger | d6e0d43 | 2019-12-02 04:54:41 -0500 | [diff] [blame] | 937 | var leftRegularExpression = new RegExp(leftMatch + insideMatch + '$'); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 938 | var expandedStart = lineUpToRange.search(leftRegularExpression); |
| 939 | if (expandedStart == -1 || expandedStart > startPosition) |
| 940 | return; |
| 941 | |
| 942 | //Move end to the right. |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 943 | var lineFromRange = lib.wc.substring(rowText, startPosition, |
| 944 | lib.wc.strWidth(rowText)); |
Mike Frysinger | d6e0d43 | 2019-12-02 04:54:41 -0500 | [diff] [blame] | 945 | var rightRegularExpression = new RegExp('^' + insideMatch + rightMatch); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 946 | var found = lineFromRange.match(rightRegularExpression); |
| 947 | if (!found) |
| 948 | return; |
Ricky Liang | 48f05cb | 2013-12-31 23:35:29 +0800 | [diff] [blame] | 949 | var expandedEnd = startPosition + lib.wc.strWidth(found[0]); |
John Macinnes | fb68383 | 2013-07-22 14:46:30 -0400 | [diff] [blame] | 950 | if (expandedEnd == -1 || expandedEnd < endPosition) |
| 951 | return; |
| 952 | |
| 953 | this.setRange_(row, expandedStart, expandedEnd, range); |
| 954 | selection.addRange(range); |
| 955 | }; |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 956 | |
| 957 | /** |
John Lin | cae9b73 | 2018-03-08 13:56:35 +0800 | [diff] [blame] | 958 | * Expands selection to surrounding string using the user's settings. |
| 959 | * |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 960 | * @param {?Selection} selection Selection to expand. |
John Lin | cae9b73 | 2018-03-08 13:56:35 +0800 | [diff] [blame] | 961 | */ |
| 962 | hterm.Screen.prototype.expandSelection = function(selection) { |
| 963 | this.expandSelectionWithWordBreakMatches_( |
| 964 | selection, |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 965 | lib.notNull(this.wordBreakMatchLeft), |
| 966 | lib.notNull(this.wordBreakMatchRight), |
| 967 | lib.notNull(this.wordBreakMatchMiddle)); |
Mike Frysinger | 8416e0a | 2017-05-17 09:09:46 -0400 | [diff] [blame] | 968 | }; |
John Lin | cae9b73 | 2018-03-08 13:56:35 +0800 | [diff] [blame] | 969 | |
| 970 | /** |
| 971 | * Expands selection to surrounding URL using a set of fixed match settings. |
| 972 | * |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 973 | * @param {?Selection} selection Selection to expand. |
John Lin | cae9b73 | 2018-03-08 13:56:35 +0800 | [diff] [blame] | 974 | */ |
| 975 | hterm.Screen.prototype.expandSelectionForUrl = function(selection) { |
| 976 | this.expandSelectionWithWordBreakMatches_( |
| 977 | selection, |
Mike Frysinger | 1a1a180 | 2020-01-29 21:38:55 -0500 | [diff] [blame^] | 978 | '[^\\s[\\](){}<>"\'^!@#$%&*,;:`\u{2018}\u{201c}\u{2039}\u{ab}]', |
| 979 | '[^\\s[\\](){}<>"\'^!@#$%&*,;:~.`\u{2019}\u{201d}\u{203a}\u{bb}]', |
Mike Frysinger | 9e11e49 | 2020-01-06 14:29:57 +0545 | [diff] [blame] | 980 | '[^\\s[\\](){}<>"\'^]*'); |
John Lin | cae9b73 | 2018-03-08 13:56:35 +0800 | [diff] [blame] | 981 | }; |
| 982 | |
| 983 | /** |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 984 | * Save the current cursor state to the corresponding screens. |
| 985 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 986 | * @param {!hterm.VT} vt The VT object to read graphic codeset details from. |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 987 | */ |
| 988 | hterm.Screen.prototype.saveCursorAndState = function(vt) { |
| 989 | this.cursorState_.save(vt); |
| 990 | }; |
| 991 | |
| 992 | /** |
| 993 | * Restore the saved cursor state in the corresponding screens. |
| 994 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 995 | * @param {!hterm.VT} vt The VT object to write graphic codeset details to. |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 996 | */ |
| 997 | hterm.Screen.prototype.restoreCursorAndState = function(vt) { |
| 998 | this.cursorState_.restore(vt); |
| 999 | }; |
| 1000 | |
| 1001 | /** |
| 1002 | * Track all the things related to the current "cursor". |
| 1003 | * |
| 1004 | * The set of things saved & restored here is defined by DEC: |
| 1005 | * https://vt100.net/docs/vt510-rm/DECSC.html |
| 1006 | * - Cursor position |
| 1007 | * - Character attributes set by the SGR command |
| 1008 | * - Character sets (G0, G1, G2, or G3) currently in GL and GR |
| 1009 | * - Wrap flag (autowrap or no autowrap) |
| 1010 | * - State of origin mode (DECOM) |
| 1011 | * - Selective erase attribute |
| 1012 | * - Any single shift 2 (SS2) or single shift 3 (SS3) functions sent |
| 1013 | * |
| 1014 | * These are done on a per-screen basis. |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 1015 | * |
| 1016 | * @param {!hterm.Screen} screen The screen this cursor is tied to. |
Joel Hockey | add2f7e | 2019-09-20 16:37:35 -0700 | [diff] [blame] | 1017 | * @constructor |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 1018 | */ |
| 1019 | hterm.Screen.CursorState = function(screen) { |
| 1020 | this.screen_ = screen; |
| 1021 | this.cursor = null; |
| 1022 | this.textAttributes = null; |
| 1023 | this.GL = this.GR = this.G0 = this.G1 = this.G2 = this.G3 = null; |
| 1024 | }; |
| 1025 | |
| 1026 | /** |
| 1027 | * Save all the cursor state. |
| 1028 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 1029 | * @param {!hterm.VT} vt The VT object to read graphic codeset details from. |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 1030 | */ |
| 1031 | hterm.Screen.CursorState.prototype.save = function(vt) { |
| 1032 | this.cursor = vt.terminal.saveCursor(); |
| 1033 | |
| 1034 | this.textAttributes = this.screen_.textAttributes.clone(); |
| 1035 | |
| 1036 | this.GL = vt.GL; |
| 1037 | this.GR = vt.GR; |
| 1038 | |
| 1039 | this.G0 = vt.G0; |
| 1040 | this.G1 = vt.G1; |
| 1041 | this.G2 = vt.G2; |
| 1042 | this.G3 = vt.G3; |
| 1043 | }; |
| 1044 | |
| 1045 | /** |
| 1046 | * Restore the previously saved cursor state. |
| 1047 | * |
Joel Hockey | 0f93358 | 2019-08-27 18:01:51 -0700 | [diff] [blame] | 1048 | * @param {!hterm.VT} vt The VT object to write graphic codeset details to. |
Mike Frysinger | a2cacaa | 2017-11-29 13:51:09 -0800 | [diff] [blame] | 1049 | */ |
| 1050 | hterm.Screen.CursorState.prototype.restore = function(vt) { |
| 1051 | vt.terminal.restoreCursor(this.cursor); |
| 1052 | |
| 1053 | // Cursor restore includes char attributes (bold/etc...), but does not change |
| 1054 | // the color palette (which are a terminal setting). |
| 1055 | const tattrs = this.textAttributes.clone(); |
| 1056 | tattrs.colorPalette = this.screen_.textAttributes.colorPalette; |
| 1057 | tattrs.syncColors(); |
| 1058 | |
| 1059 | this.screen_.textAttributes = tattrs; |
| 1060 | |
| 1061 | vt.GL = this.GL; |
| 1062 | vt.GR = this.GR; |
| 1063 | |
| 1064 | vt.G0 = this.G0; |
| 1065 | vt.G1 = this.G1; |
| 1066 | vt.G2 = this.G2; |
| 1067 | vt.G3 = this.G3; |
| 1068 | }; |