Debian Codegraph @

shiny/text/caret.go

raw symbols SCIP
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package text
6
7 // TODO: do we care about "\n" vs "\r" vs "\r\n"? We only recognize "\n" for
8 // now.
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 // Caret is a location in a Frame's text, and is the mechanism for adding and
21 // removing bytes of text. Conceptually, a Caret and a Frame's text is like an
22 // int c and a []byte t such that the text before and after that Caret is t[:c]
23 // and t[c:]. That byte-count location remains unchanged even when a Frame is
24 // re-sized and laid out into a new tree of Paragraphs, Lines and Boxes.
25 //
26 // A Frame can have multiple open Carets. For example, the beginning and end of
27 // a text selection can be represented by two Carets. Multiple Carets for the
28 // one Frame are not safe to use concurrently, but it is valid to interleave
29 // such operations sequentially. For example, if two Carets c0 and c1 for the
30 // one Frame are positioned at the 10th and 20th byte, and 4 bytes are written
31 // to c0, inserting what becomes the equivalent of text[10:14], then c0's
32 // position is updated to be 14 but c1's position is also updated to be 24.
33 type Caret struct {
34 f *Frame
35
36 // caretsIndex is the index of this Caret in the f.carets slice.
37 caretsIndex int
38
39 // seqNum is the Frame f's sequence number for which this Caret's cached p,
40 // l, b and k fields are valid. If f has been modified since then, those
41 // fields will have to be re-calculated based on the pos field (which is
42 // always valid).
43 //
44 // TODO: when re-calculating p, l, b and k, be more efficient than a linear
45 // scan from the start or end?
46 seqNum uint64
47
48 // p, l and b cache the index of the Caret's Paragraph, Line and Box. None
49 // of these values can be zero.
50 p, l, b int32
51
52 // k caches the Caret's position in the text, in Frame.text order. It is
53 // valid to index the Frame.text slice with k, analogous to the Box.i and
54 // Box.j fields. For a Caret c, letting bb := c.f.boxes[c.b], an invariant
55 // is that bb.i <= c.k && c.k <= bb.j if the cache is valid (i.e. the
56 // Caret's seqNum equals the Frame's seqNum).
57 k int32
58
59 // pos is the Caret's position in the text, in layout order. It is the "c"
60 // as in "t[:c]" in the doc comment for type Caret above. It is not valid
61 // to index the Frame.text slice with pos, since the Frame.text slice does
62 // not necessarily hold the textual content in layout order.
63 pos int32
64
65 tmp [utf8.UTFMax]byte
66 }
67
68 // Close closes the Caret.
69 func (c *Caret) Close() error {
70 i, j := c.caretsIndex, len(c.f.carets)-1
71
72 // Swap c with the last element of c.f.carets.
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 // leanOK means that the lean changed the Caret's Box.
89 leanOK leanResult = iota
90 // leanFailedEOF means that the lean did not change the Caret's Box,
91 // because the Caret was already at the end / beginning of the Frame (when
92 // leaning forwards / backwards).
93 leanFailedEOF
94 // leanFailedNotEOB means that the lean did not change the Caret's Box,
95 // because the Caret was not placed at the end / beginning of the Box (when
96 // leaning forwards / backwards).
97 leanFailedNotEOB
98 )
99
100 // leanForwards moves the Caret from the right end of one Box to the left end
101 // of the next Box, crossing Lines and Paragraphs to find that next Box. It
102 // returns whether the Caret moved to a different Box.
103 //
104 // Diagramatically, suppose we have two adjacent boxes (shown by square
105 // brackets below), with the Caret (an integer location called Caret.pos in the
106 // Frame's text) in the middle of the "foo2bar3" word:
107 //
108 // [foo0 foo1 foo2]^[bar3 bar4 bar5]
109 //
110 // leanForwards moves Caret.k from fooBox.j to barBox.i, also updating the
111 // Caret's p, l and b. Caret.pos remains unchanged.
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 // leanBackwards is like leanForwards but in the other direction.
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 // calculatePLBK ensures that the Caret's cached p, l, b and k fields are
180 // valid.
181 func (c *Caret) calculatePLBK() {
182 if c.seqNum != c.f.seqNum {
183 c.seek(c.pos)
184 }
185 }
186
187 // Seek satisfies the io.Seeker interface.
188 func (c *Caret) Seek(offset int64, whence int) (int64, error) {
189 switch whence {
190 case SeekSet:
191 // No-op.
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 // If the new offset is closer to the start or the end than to the current
212 // c.pos, or if c's cached {p,l,b,k} values are invalid, move to the start
213 // or end first. In case of a tie, we prefer to seek forwards (i.e. set
214 // delta > 0).
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 // Seek forwards.
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 // Seek backwards.
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 // A Caret can't be placed at the end of a Paragraph, unless it is the
254 // final Paragraph. A simple way to enforce this is to lean forwards.
255 c.leanForwards()
256 }
257
258 c.seqNum = c.f.seqNum
259 }
260
261 // Read satisfies the io.Reader interface by copying those bytes after the
262 // Caret and incrementing the Caret.
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 // A Caret can't be placed at the end of a Paragraph, unless it is the
274 // final Paragraph. A simple way to enforce this is to lean forwards.
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 // ReadByte returns the next byte after the Caret and increments the Caret.
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 // A Caret can't be placed at the end of a Paragraph, unless it is
294 // the final Paragraph. A simple way to enforce this is to lean
295 // forwards.
296 c.leanForwards()
297 return x, nil
298 }
299 if c.leanForwards() == leanFailedEOF {
300 return 0, io.EOF
301 }
302 }
303 }
304
305 // ReadRune returns the next rune after the Caret and increments the Caret.
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 // A Caret can't be placed at the end of a Paragraph, unless it is
313 // the final Paragraph. A simple way to enforce this is to lean
314 // forwards.
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 // WriteByte inserts x into the Frame's text at the Caret and increments the
325 // Caret.
326 func (c *Caret) WriteByte(x byte) error {
327 c.tmp[0] = x
328 return c.write(c.tmp[:1], "")
329 }
330
331 // WriteRune inserts r into the Frame's text at the Caret and increments the
332 // Caret.
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 // WriteString inserts s into the Frame's text at the Caret and increments the
342 // Caret.
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 // Write inserts s into the Frame's text at the Caret and increments the Caret.
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 // write inserts a []byte or string into the Frame's text at the Caret.
375 //
376 // Exactly one of s0 and s1 must be non-empty. That non-empty argument must
377 // contain at most one '\n' and if it does contain one, it must be the final
378 // byte.
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 // Ensure that the Caret is at the end of its Box, and that Box's text is
385 // at the end of the Frame's buffer.
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 // If the Box's text is empty, move its empty i:j range to the
394 // equivalent empty range at the end of c.f.text.
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 // Make the Caret be at the end of its Box.
407 if c.k != bb.j {
408 c.splitBox(true)
409 continue
410 }
411
412 // Make a new empty Box and move the Caret to it.
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 // TODO: re-layout the new c.p paragraph, if we saw '\n'.
450 layout(c.f, oldL)
451
452 c.f.seqNum++
453 return nil
454 }
455
456 // breakParagraph breaks the Paragraph p into two Paragraphs, just after Box b
457 // in Line l in Paragraph p. b's text must end with a '\n'. The new Paragraph
458 // is inserted after p.
459 func breakParagraph(f *Frame, p, l, b int32) {
460 // Assert that the Box b's text ends with a '\n'.
461 if j := f.boxes[b].j; j == 0 || f.text[j-1] != '\n' {
462 panic("text: invalid state")
463 }
464
465 // Make a new, empty Paragraph after this Paragraph p.
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 // Any Lines in this Paragraph after the break point's Line l move to the
476 // newP Paragraph.
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 // Any Boxes in this Line after the break point's Box b move to a new Line
484 // at the start of the newP Paragraph.
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 // Make the newP Paragraph's first Line and first Box explicit, since
498 // Carets require an explicit p, l and b.
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 // TODO: re-layout the newP paragraph.
511 }
512
513 // breakLine breaks the Line l at text index k in Box b. The b-and-k index must
514 // not be at the start or end of the Line. Text to the right of b-and-k in the
515 // Line l will be moved to the start of the next Line in the Paragraph, with
516 // that next Line being created if it didn't already exist.
517 func breakLine(f *Frame, l, b, k int32) {
518 // Split this Box into two if necessary, so that k equals a Box's j end.
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 // Assert that the break point isn't already at the start or end of the Line.
541 if bb.next == 0 || (bb.prev == 0 && k == bb.i) {
542 panic("text: invalid state")
543 }
544
545 // Insert a line after this one, if one doesn't already exist.
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 // Move the remaining boxes to the next line.
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 // If the next Line already contained Boxes, append them to the end of the
564 // nextB chain, and join the two newly linked Boxes if possible.
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 // layout inserts a soft return in the Line l if its text measures longer than
576 // f.maxWidth and a suitable line break point is found. This may spill text
577 // onto the next line, which will also be laid out, and so on recursively.
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 // TODO: match all whitespace, not just ' '?
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 // Delete deletes nBytes bytes in the specified direction from the Caret's
617 // location. It returns the number of bytes deleted, which can be fewer than
618 // that requested if it hits the beginning or end of the Frame.
619 func (c *Caret) Delete(dir Direction, nBytes int) (dBytes int) {
620 if nBytes <= 0 {
621 return 0
622 }
623
624 // Convert a backwards delete of n bytes from position p to a forwards
625 // delete of n bytes from position p-n.
626 //
627 // In general, it's easier to delete forwards than backwards. For example,
628 // when crossing paragraph boundaries, it's easier to find the first line
629 // of the next paragraph than the last line of the previous paragraph.
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 // The mergeIntoOneLine will shake out any empty Boxes.
675 l := c.f.mergeIntoOneLine(c.p)
676 layout(c.f, l)
677 c.f.invalidateCaches()
678
679 // Compact c.f.text if it's large enough and the fraction of deleted text
680 // is above some threshold. The actual threshold value (25%) is arbitrary.
681 // A lower value means more frequent compactions, so less memory on average
682 // but more CPU. A higher value means the opposite.
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 // No-op.
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 // DeleteRunes deletes nRunes runes in the specified direction from the Caret's
705 // location. It returns the number of runes and bytes deleted, which can be
706 // fewer than that requested if it hits the beginning or end of the Frame.
707 func (c *Caret) DeleteRunes(dir Direction, nRunes int) (dRunes, dBytes int) {
708 // Save the current Caret position, move the Caret by nRunes runes to
709 // calculate how many bytes to delete, restore that saved Caret position,
710 // then delete that many bytes.
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 // joinNextParagraph joins c's current and next Paragraph. That next Paragraph
744 // must exist, and c must be at the last Line of its current Paragraph.
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 // splitBox splits the Caret's Box into two, at the Caret's location. Unless
767 // force is set, it does nothing if the Caret is at either edge of its Box. It
768 // returns whether the Box was split. If so, the new Box is created after, not
769 // before, the Caret's current Box.
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 }