| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package text |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
import ( |
| 11 |
"bytes" |
| 12 |
"errors" |
| 13 |
"io" |
| 14 |
"strings" |
| 15 |
"unicode/utf8" |
| 16 |
|
| 17 |
"golang.org/x/image/math/fixed" |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
type Caret struct { |
| 34 |
f *Frame |
| 35 |
|
| 36 |
|
| 37 |
caretsIndex int |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
seqNum uint64 |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
p, l, b int32 |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
k int32 |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
pos int32 |
| 64 |
|
| 65 |
tmp [utf8.UTFMax]byte |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
func (c *Caret) Close() error { |
| 70 |
i, j := c.caretsIndex, len(c.f.carets)-1 |
| 71 |
|
| 72 |
|
| 73 |
if i != j { |
| 74 |
other := c.f.carets[j] |
| 75 |
other.caretsIndex = i |
| 76 |
c.f.carets[i] = other |
| 77 |
} |
| 78 |
|
| 79 |
c.f.carets[j] = nil |
| 80 |
c.f.carets = c.f.carets[:j] |
| 81 |
*c = Caret{} |
| 82 |
return nil |
| 83 |
} |
| 84 |
|
| 85 |
type leanResult int |
| 86 |
|
| 87 |
const ( |
| 88 |
|
| 89 |
leanOK leanResult = iota |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
leanFailedEOF |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
leanFailedNotEOB |
| 98 |
) |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
func (c *Caret) leanForwards() leanResult { |
| 113 |
if c.k != c.f.boxes[c.b].j { |
| 114 |
return leanFailedNotEOB |
| 115 |
} |
| 116 |
if nextB := c.f.boxes[c.b].next; nextB != 0 { |
| 117 |
c.b = nextB |
| 118 |
c.k = c.f.boxes[c.b].i |
| 119 |
return leanOK |
| 120 |
} |
| 121 |
if nextL := c.f.lines[c.l].next; nextL != 0 { |
| 122 |
c.l = nextL |
| 123 |
c.b = c.f.lines[c.l].firstB |
| 124 |
c.k = c.f.boxes[c.b].i |
| 125 |
return leanOK |
| 126 |
} |
| 127 |
if nextP := c.f.paragraphs[c.p].next; nextP != 0 { |
| 128 |
c.p = nextP |
| 129 |
c.l = c.f.paragraphs[c.p].firstL |
| 130 |
c.b = c.f.lines[c.l].firstB |
| 131 |
c.k = c.f.boxes[c.b].i |
| 132 |
return leanOK |
| 133 |
} |
| 134 |
return leanFailedEOF |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
func (c *Caret) leanBackwards() leanResult { |
| 139 |
if c.k != c.f.boxes[c.b].i { |
| 140 |
return leanFailedNotEOB |
| 141 |
} |
| 142 |
if prevB := c.f.boxes[c.b].prev; prevB != 0 { |
| 143 |
c.b = prevB |
| 144 |
c.k = c.f.boxes[c.b].j |
| 145 |
return leanOK |
| 146 |
} |
| 147 |
if prevL := c.f.lines[c.l].prev; prevL != 0 { |
| 148 |
c.l = prevL |
| 149 |
c.b = c.f.lines[c.l].lastBox(c.f) |
| 150 |
c.k = c.f.boxes[c.b].j |
| 151 |
return leanOK |
| 152 |
} |
| 153 |
if prevP := c.f.paragraphs[c.p].prev; prevP != 0 { |
| 154 |
c.p = prevP |
| 155 |
c.l = c.f.paragraphs[c.p].lastLine(c.f) |
| 156 |
c.b = c.f.lines[c.l].lastBox(c.f) |
| 157 |
c.k = c.f.boxes[c.b].j |
| 158 |
return leanOK |
| 159 |
} |
| 160 |
return leanFailedEOF |
| 161 |
} |
| 162 |
|
| 163 |
func (c *Caret) seekStart() { |
| 164 |
c.p = c.f.firstP |
| 165 |
c.l = c.f.paragraphs[c.p].firstL |
| 166 |
c.b = c.f.lines[c.l].firstB |
| 167 |
c.k = c.f.boxes[c.b].i |
| 168 |
c.pos = 0 |
| 169 |
} |
| 170 |
|
| 171 |
func (c *Caret) seekEnd() { |
| 172 |
c.p = c.f.lastParagraph() |
| 173 |
c.l = c.f.paragraphs[c.p].lastLine(c.f) |
| 174 |
c.b = c.f.lines[c.l].lastBox(c.f) |
| 175 |
c.k = c.f.boxes[c.b].j |
| 176 |
c.pos = int32(c.f.len) |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
func (c *Caret) calculatePLBK() { |
| 182 |
if c.seqNum != c.f.seqNum { |
| 183 |
c.seek(c.pos) |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
func (c *Caret) Seek(offset int64, whence int) (int64, error) { |
| 189 |
switch whence { |
| 190 |
case SeekSet: |
| 191 |
|
| 192 |
case SeekCur: |
| 193 |
offset += int64(c.pos) |
| 194 |
case SeekEnd: |
| 195 |
offset += int64(c.f.len) |
| 196 |
default: |
| 197 |
return 0, errors.New("text: invalid seek whence") |
| 198 |
} |
| 199 |
if offset < 0 { |
| 200 |
return 0, errors.New("text: negative seek position") |
| 201 |
} |
| 202 |
if offset > int64(c.f.len) { |
| 203 |
offset = int64(c.f.len) |
| 204 |
} |
| 205 |
c.seek(int32(offset)) |
| 206 |
return offset, nil |
| 207 |
} |
| 208 |
|
| 209 |
func (c *Caret) seek(off int32) { |
| 210 |
delta := off - c.pos |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
if (delta < 0 && -delta >= off) || (c.seqNum != c.f.seqNum) { |
| 216 |
c.seekStart() |
| 217 |
delta = off - c.pos |
| 218 |
} |
| 219 |
if delta > 0 && delta > int32(c.f.len)-off { |
| 220 |
c.seekEnd() |
| 221 |
delta = off - c.pos |
| 222 |
} |
| 223 |
|
| 224 |
if delta != 0 { |
| 225 |
|
| 226 |
for delta > 0 { |
| 227 |
if n := c.f.boxes[c.b].j - c.k; n > 0 { |
| 228 |
if n > delta { |
| 229 |
n = delta |
| 230 |
} |
| 231 |
c.pos += n |
| 232 |
c.k += n |
| 233 |
delta -= n |
| 234 |
} else if c.leanForwards() != leanOK { |
| 235 |
panic("text: invalid state") |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
for delta < 0 { |
| 241 |
if n := c.f.boxes[c.b].i - c.k; n < 0 { |
| 242 |
if n < delta { |
| 243 |
n = delta |
| 244 |
} |
| 245 |
c.pos += n |
| 246 |
c.k += n |
| 247 |
delta -= n |
| 248 |
} else if c.leanBackwards() != leanOK { |
| 249 |
panic("text: invalid state") |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
c.leanForwards() |
| 256 |
} |
| 257 |
|
| 258 |
c.seqNum = c.f.seqNum |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
func (c *Caret) Read(buf []byte) (n int, err error) { |
| 264 |
c.calculatePLBK() |
| 265 |
for len(buf) > 0 { |
| 266 |
if j := c.f.boxes[c.b].j; c.k < j { |
| 267 |
nn := copy(buf, c.f.text[c.k:j]) |
| 268 |
buf = buf[nn:] |
| 269 |
n += nn |
| 270 |
c.pos += int32(nn) |
| 271 |
c.k += int32(nn) |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
if c.leanForwards() == leanFailedEOF { |
| 276 |
break |
| 277 |
} |
| 278 |
} |
| 279 |
if int(c.pos) == c.f.len { |
| 280 |
err = io.EOF |
| 281 |
} |
| 282 |
return n, err |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
func (c *Caret) ReadByte() (x byte, err error) { |
| 287 |
c.calculatePLBK() |
| 288 |
for { |
| 289 |
if j := c.f.boxes[c.b].j; c.k < j { |
| 290 |
x = c.f.text[c.k] |
| 291 |
c.pos++ |
| 292 |
c.k++ |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
c.leanForwards() |
| 297 |
return x, nil |
| 298 |
} |
| 299 |
if c.leanForwards() == leanFailedEOF { |
| 300 |
return 0, io.EOF |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
func (c *Caret) ReadRune() (r rune, size int, err error) { |
| 307 |
c.calculatePLBK() |
| 308 |
for { |
| 309 |
if c.k < c.f.boxes[c.b].j { |
| 310 |
r, size, c.b, c.k = c.f.readRune(c.b, c.k) |
| 311 |
c.pos += int32(size) |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
c.leanForwards() |
| 316 |
return r, size, nil |
| 317 |
} |
| 318 |
if c.leanForwards() == leanFailedEOF { |
| 319 |
return 0, 0, io.EOF |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
func (c *Caret) WriteByte(x byte) error { |
| 327 |
c.tmp[0] = x |
| 328 |
return c.write(c.tmp[:1], "") |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
func (c *Caret) WriteRune(r rune) (size int, err error) { |
| 334 |
size = utf8.EncodeRune(c.tmp[:], r) |
| 335 |
if err = c.write(c.tmp[:size], ""); err != nil { |
| 336 |
return 0, err |
| 337 |
} |
| 338 |
return size, nil |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
func (c *Caret) WriteString(s string) (n int, err error) { |
| 344 |
for len(s) > 0 { |
| 345 |
i := 1 + strings.IndexByte(s, '\n') |
| 346 |
if i == 0 { |
| 347 |
i = len(s) |
| 348 |
} |
| 349 |
if err = c.write(nil, s[:i]); err != nil { |
| 350 |
break |
| 351 |
} |
| 352 |
n += i |
| 353 |
s = s[i:] |
| 354 |
} |
| 355 |
return n, err |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
func (c *Caret) Write(s []byte) (n int, err error) { |
| 360 |
for len(s) > 0 { |
| 361 |
i := 1 + bytes.IndexByte(s, '\n') |
| 362 |
if i == 0 { |
| 363 |
i = len(s) |
| 364 |
} |
| 365 |
if err = c.write(s[:i], ""); err != nil { |
| 366 |
break |
| 367 |
} |
| 368 |
n += i |
| 369 |
s = s[i:] |
| 370 |
} |
| 371 |
return n, err |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
func (c *Caret) write(s0 []byte, s1 string) error { |
| 380 |
if m := maxLen - len(c.f.text); len(s0) > m || len(s1) > m { |
| 381 |
return errors.New("text: insufficient space for writing") |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
c.calculatePLBK() |
| 387 |
for { |
| 388 |
bb, n := &c.f.boxes[c.b], int32(len(c.f.text)) |
| 389 |
if c.k == bb.j && c.k == n { |
| 390 |
break |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
if bb.i == bb.j { |
| 396 |
bb.i = n |
| 397 |
bb.j = n |
| 398 |
for _, cc := range c.f.carets { |
| 399 |
if cc.b == c.b { |
| 400 |
cc.k = n |
| 401 |
} |
| 402 |
} |
| 403 |
continue |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
if c.k != bb.j { |
| 408 |
c.splitBox(true) |
| 409 |
continue |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
c.splitBox(true) |
| 414 |
c.leanForwards() |
| 415 |
} |
| 416 |
|
| 417 |
c.f.invalidateCaches() |
| 418 |
c.f.paragraphs[c.p].invalidateCaches() |
| 419 |
c.f.lines[c.l].invalidateCaches() |
| 420 |
|
| 421 |
length, nl := len(s0), false |
| 422 |
if length > 0 { |
| 423 |
nl = s0[length-1] == '\n' |
| 424 |
c.f.text = append(c.f.text, s0...) |
| 425 |
} else { |
| 426 |
length = len(s1) |
| 427 |
nl = s1[length-1] == '\n' |
| 428 |
c.f.text = append(c.f.text, s1...) |
| 429 |
} |
| 430 |
c.f.len += length |
| 431 |
c.f.boxes[c.b].j += int32(length) |
| 432 |
c.k += int32(length) |
| 433 |
for _, cc := range c.f.carets { |
| 434 |
if cc.pos > c.pos { |
| 435 |
cc.pos += int32(length) |
| 436 |
} |
| 437 |
} |
| 438 |
c.pos += int32(length) |
| 439 |
oldL := c.l |
| 440 |
|
| 441 |
if nl { |
| 442 |
breakParagraph(c.f, c.p, c.l, c.b) |
| 443 |
c.p = c.f.paragraphs[c.p].next |
| 444 |
c.l = c.f.paragraphs[c.p].firstL |
| 445 |
c.b = c.f.lines[c.l].firstB |
| 446 |
c.k = c.f.boxes[c.b].i |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
layout(c.f, oldL) |
| 451 |
|
| 452 |
c.f.seqNum++ |
| 453 |
return nil |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
func breakParagraph(f *Frame, p, l, b int32) { |
| 460 |
|
| 461 |
if j := f.boxes[b].j; j == 0 || f.text[j-1] != '\n' { |
| 462 |
panic("text: invalid state") |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
newP, _ := f.newParagraph() |
| 467 |
nextP := f.paragraphs[p].next |
| 468 |
if nextP != 0 { |
| 469 |
f.paragraphs[nextP].prev = newP |
| 470 |
} |
| 471 |
f.paragraphs[newP].next = nextP |
| 472 |
f.paragraphs[newP].prev = p |
| 473 |
f.paragraphs[p].next = newP |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
if nextL := f.lines[l].next; nextL != 0 { |
| 478 |
f.lines[l].next = 0 |
| 479 |
f.lines[nextL].prev = 0 |
| 480 |
f.paragraphs[newP].firstL = nextL |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
if nextB := f.boxes[b].next; nextB != 0 { |
| 486 |
f.boxes[b].next = 0 |
| 487 |
f.boxes[nextB].prev = 0 |
| 488 |
newL, _ := f.newLine() |
| 489 |
f.lines[newL].firstB = nextB |
| 490 |
if newPFirstL := f.paragraphs[newP].firstL; newPFirstL != 0 { |
| 491 |
f.lines[newL].next = newPFirstL |
| 492 |
f.lines[newPFirstL].prev = newL |
| 493 |
} |
| 494 |
f.paragraphs[newP].firstL = newL |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
{ |
| 500 |
pp := &f.paragraphs[newP] |
| 501 |
if pp.firstL == 0 { |
| 502 |
pp.firstL, _ = f.newLine() |
| 503 |
} |
| 504 |
ll := &f.lines[pp.firstL] |
| 505 |
if ll.firstB == 0 { |
| 506 |
ll.firstB, _ = f.newBox() |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
func breakLine(f *Frame, l, b, k int32) { |
| 518 |
|
| 519 |
bb := &f.boxes[b] |
| 520 |
if k != bb.j { |
| 521 |
if k == bb.i { |
| 522 |
panic("TODO: degenerate split left, possibly adjusting the Line's firstB??") |
| 523 |
} |
| 524 |
newB, realloc := f.newBox() |
| 525 |
if realloc { |
| 526 |
bb = &f.boxes[b] |
| 527 |
} |
| 528 |
nextB := bb.next |
| 529 |
if nextB != 0 { |
| 530 |
f.boxes[nextB].prev = newB |
| 531 |
} |
| 532 |
f.boxes[newB].next = nextB |
| 533 |
f.boxes[newB].prev = b |
| 534 |
f.boxes[newB].i = k |
| 535 |
f.boxes[newB].j = bb.j |
| 536 |
bb.next = newB |
| 537 |
bb.j = k |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
if bb.next == 0 || (bb.prev == 0 && k == bb.i) { |
| 542 |
panic("text: invalid state") |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
ll := &f.lines[l] |
| 547 |
if ll.next == 0 { |
| 548 |
newL, realloc := f.newLine() |
| 549 |
if realloc { |
| 550 |
ll = &f.lines[l] |
| 551 |
} |
| 552 |
f.lines[newL].prev = l |
| 553 |
ll.next = newL |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
nextB, nextL := bb.next, ll.next |
| 558 |
bb.next = 0 |
| 559 |
f.boxes[nextB].prev = 0 |
| 560 |
fb := f.lines[nextL].firstB |
| 561 |
f.lines[nextL].firstB = nextB |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
if fb != 0 { |
| 566 |
lb := f.lines[nextL].lastBox(f) |
| 567 |
lbb := &f.boxes[lb] |
| 568 |
fbb := &f.boxes[fb] |
| 569 |
lbb.next = fb |
| 570 |
fbb.prev = lb |
| 571 |
f.joinBoxes(lb, fb, lbb, fbb) |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
func layout(f *Frame, l int32) { |
| 579 |
if f.maxWidth <= 0 || f.face == nil { |
| 580 |
return |
| 581 |
} |
| 582 |
f.seqNum++ |
| 583 |
|
| 584 |
for ; l != 0; l = f.lines[l].next { |
| 585 |
var ( |
| 586 |
firstB = f.lines[l].firstB |
| 587 |
reader = f.lineReader(firstB, f.boxes[firstB].i) |
| 588 |
breakPoint bAndK |
| 589 |
prevR rune |
| 590 |
prevRValid bool |
| 591 |
advance fixed.Int26_6 |
| 592 |
) |
| 593 |
for { |
| 594 |
r, _, err := reader.ReadRune() |
| 595 |
if err != nil || r == '\n' { |
| 596 |
return |
| 597 |
} |
| 598 |
if prevRValid { |
| 599 |
advance += f.face.Kern(prevR, r) |
| 600 |
} |
| 601 |
|
| 602 |
if r == ' ' { |
| 603 |
breakPoint = reader.bAndK() |
| 604 |
} |
| 605 |
a, _ := f.face.GlyphAdvance(r) |
| 606 |
advance += a |
| 607 |
if r != ' ' && advance > f.maxWidth && breakPoint.b != 0 { |
| 608 |
breakLine(f, l, breakPoint.b, breakPoint.k) |
| 609 |
break |
| 610 |
} |
| 611 |
prevR, prevRValid = r, true |
| 612 |
} |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
func (c *Caret) Delete(dir Direction, nBytes int) (dBytes int) { |
| 620 |
if nBytes <= 0 { |
| 621 |
return 0 |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
if dir == Backwards { |
| 631 |
newPos := int(c.pos) - nBytes |
| 632 |
if newPos < 0 { |
| 633 |
newPos = 0 |
| 634 |
nBytes = int(c.pos) |
| 635 |
if nBytes == 0 { |
| 636 |
return 0 |
| 637 |
} |
| 638 |
} |
| 639 |
c.seek(int32(newPos)) |
| 640 |
} |
| 641 |
|
| 642 |
if int(c.pos) == c.f.len { |
| 643 |
return 0 |
| 644 |
} |
| 645 |
|
| 646 |
c.calculatePLBK() |
| 647 |
c.leanForwards() |
| 648 |
if c.f.boxes[c.b].i != c.k && c.splitBox(false) { |
| 649 |
c.leanForwards() |
| 650 |
} |
| 651 |
for nBytes > 0 && int(c.pos) != c.f.len { |
| 652 |
bb := &c.f.boxes[c.b] |
| 653 |
n := bb.j - bb.i |
| 654 |
newLine := n != 0 && c.f.text[bb.j-1] == '\n' |
| 655 |
if int(n) > nBytes { |
| 656 |
n = int32(nBytes) |
| 657 |
} |
| 658 |
bb.i += n |
| 659 |
c.k += n |
| 660 |
dBytes += int(n) |
| 661 |
nBytes -= int(n) |
| 662 |
c.f.len -= int(n) |
| 663 |
|
| 664 |
if bb.i != bb.j { |
| 665 |
break |
| 666 |
} |
| 667 |
|
| 668 |
if newLine { |
| 669 |
c.joinNextParagraph() |
| 670 |
} |
| 671 |
c.leanForwards() |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
l := c.f.mergeIntoOneLine(c.p) |
| 676 |
layout(c.f, l) |
| 677 |
c.f.invalidateCaches() |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
if len(c.f.text) > 4096 && len(c.f.text)/4 < c.f.deletedLen() { |
| 684 |
c.f.compactText() |
| 685 |
} |
| 686 |
|
| 687 |
c.f.seqNum++ |
| 688 |
for _, cc := range c.f.carets { |
| 689 |
if cc == c { |
| 690 |
continue |
| 691 |
} |
| 692 |
switch relPos := cc.pos - c.pos; { |
| 693 |
case relPos <= 0: |
| 694 |
|
| 695 |
case relPos <= int32(dBytes): |
| 696 |
cc.pos = c.pos |
| 697 |
default: |
| 698 |
cc.pos -= int32(dBytes) |
| 699 |
} |
| 700 |
} |
| 701 |
return dBytes |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
func (c *Caret) DeleteRunes(dir Direction, nRunes int) (dRunes, dBytes int) { |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
c.calculatePLBK() |
| 712 |
savedC := *c |
| 713 |
if dir == Forwards { |
| 714 |
for dRunes < nRunes { |
| 715 |
var size int |
| 716 |
_, size, c.b, c.k = c.f.readRune(c.b, c.k) |
| 717 |
if size != 0 { |
| 718 |
dRunes++ |
| 719 |
dBytes += size |
| 720 |
} else if c.leanForwards() != leanOK { |
| 721 |
break |
| 722 |
} |
| 723 |
} |
| 724 |
} else { |
| 725 |
for dRunes < nRunes { |
| 726 |
var size int |
| 727 |
_, size, c.b, c.k = c.f.readLastRune(c.b, c.k) |
| 728 |
if size != 0 { |
| 729 |
dRunes++ |
| 730 |
dBytes += size |
| 731 |
} else if c.leanBackwards() != leanOK { |
| 732 |
break |
| 733 |
} |
| 734 |
} |
| 735 |
} |
| 736 |
*c = savedC |
| 737 |
if dBytes != c.Delete(dir, dBytes) { |
| 738 |
panic("text: invalid state") |
| 739 |
} |
| 740 |
return dRunes, dBytes |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
func (c *Caret) joinNextParagraph() { |
| 746 |
pp0 := &c.f.paragraphs[c.p] |
| 747 |
ll0 := &c.f.lines[c.l] |
| 748 |
if pp0.next == 0 || ll0.next != 0 { |
| 749 |
panic("text: invalid state") |
| 750 |
} |
| 751 |
pp1 := &c.f.paragraphs[pp0.next] |
| 752 |
l1 := pp1.firstL |
| 753 |
|
| 754 |
ll0.next = l1 |
| 755 |
c.f.lines[l1].prev = c.l |
| 756 |
|
| 757 |
toFree := pp0.next |
| 758 |
pp0.next = pp1.next |
| 759 |
if pp0.next != 0 { |
| 760 |
c.f.paragraphs[pp0.next].prev = c.p |
| 761 |
} |
| 762 |
|
| 763 |
c.f.freeParagraph(toFree) |
| 764 |
} |
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
func (c *Caret) splitBox(force bool) bool { |
| 771 |
bb := &c.f.boxes[c.b] |
| 772 |
if !force && (c.k == bb.i || c.k == bb.j) { |
| 773 |
return false |
| 774 |
} |
| 775 |
newB, realloc := c.f.newBox() |
| 776 |
if realloc { |
| 777 |
bb = &c.f.boxes[c.b] |
| 778 |
} |
| 779 |
nextB := bb.next |
| 780 |
if nextB != 0 { |
| 781 |
c.f.boxes[nextB].prev = newB |
| 782 |
} |
| 783 |
c.f.boxes[newB] = Box{ |
| 784 |
next: nextB, |
| 785 |
prev: c.b, |
| 786 |
i: c.k, |
| 787 |
j: bb.j, |
| 788 |
} |
| 789 |
bb.next = newB |
| 790 |
bb.j = c.k |
| 791 |
return true |
| 792 |
} |