3 // FromCharPos converts from a character position to an x, y position
4 func FromCharPos(loc int, buf *Buffer) Loc {
8 lineLen := Count(buf.Line(y)) + 1
9 for charNum+lineLen <= loc {
12 lineLen = Count(buf.Line(y)) + 1
19 // ToCharPos converts from an x, y position to a character position
20 func ToCharPos(start Loc, buf *Buffer) int {
21 x, y := start.X, start.Y
23 for i := 0; i < y; i++ {
24 // + 1 for the newline
25 loc += Count(buf.Line(i)) + 1
31 // InBounds returns whether the given location is a valid character position in the given buffer
32 func InBounds(pos Loc, buf *Buffer) bool {
33 if pos.Y < 0 || pos.Y >= buf.NumLines || pos.X < 0 || pos.X > Count(buf.Line(pos.Y)) {
40 // ByteOffset is just like ToCharPos except it counts bytes instead of runes
41 func ByteOffset(pos Loc, buf *Buffer) int {
44 for i := 0; i < y; i++ {
45 // + 1 for the newline
46 loc += len(buf.Line(i)) + 1
48 loc += len(buf.Line(y)[:x])
52 // Loc stores a location
57 // LessThan returns true if b is smaller
58 func (l Loc) LessThan(b Loc) bool {
62 if l.Y == b.Y && l.X < b.X {
68 // GreaterThan returns true if b is bigger
69 func (l Loc) GreaterThan(b Loc) bool {
73 if l.Y == b.Y && l.X > b.X {
79 // GreaterEqual returns true if b is greater than or equal to b
80 func (l Loc) GreaterEqual(b Loc) bool {
84 if l.Y == b.Y && l.X > b.X {
93 // LessEqual returns true if b is less than or equal to b
94 func (l Loc) LessEqual(b Loc) bool {
98 if l.Y == b.Y && l.X < b.X {
107 // This moves the location one character to the right
108 func (l Loc) right(buf *Buffer) Loc {
110 return Loc{l.X + 1, l.Y}
113 if l.X < Count(buf.Line(l.Y)) {
114 res = Loc{l.X + 1, l.Y}
116 res = Loc{0, l.Y + 1}
121 // This moves the given location one character to the left
122 func (l Loc) left(buf *Buffer) Loc {
123 if l == buf.Start() {
124 return Loc{l.X - 1, l.Y}
128 res = Loc{l.X - 1, l.Y}
130 res = Loc{Count(buf.Line(l.Y - 1)), l.Y - 1}
135 // Move moves the cursor n characters to the left or right
136 // It moves the cursor left if n is negative
137 func (l Loc) Move(n int, buf *Buffer) Loc {
139 for i := 0; i < n; i++ {
144 for i := 0; i < Abs(n); i++ {