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 // Loc stores a location
36 // LessThan returns true if b is smaller
37 func (l Loc) LessThan(b Loc) bool {
41 if l.Y == b.Y && l.X < b.X {
47 // GreaterThan returns true if b is bigger
48 func (l Loc) GreaterThan(b Loc) bool {
52 if l.Y == b.Y && l.X > b.X {
58 // GreaterEqual returns true if b is greater than or equal to b
59 func (l Loc) GreaterEqual(b Loc) bool {
63 if l.Y == b.Y && l.X > b.X {
72 // LessEqual returns true if b is less than or equal to b
73 func (l Loc) LessEqual(b Loc) bool {
77 if l.Y == b.Y && l.X < b.X {
86 // This moves the location one character to the right
87 func (l Loc) right(buf *Buffer) Loc {
89 return Loc{l.X + 1, l.Y}
92 if l.X < Count(buf.Line(l.Y)) {
93 res = Loc{l.X + 1, l.Y}
100 // This moves the given location one character to the left
101 func (l Loc) left(buf *Buffer) Loc {
102 if l == buf.Start() {
103 return Loc{l.X - 1, l.Y}
107 res = Loc{l.X - 1, l.Y}
109 res = Loc{Count(buf.Line(l.Y - 1)), l.Y - 1}
114 // Move moves the cursor n characters to the left or right
115 // It moves the cursor left if n is negative
116 func (l Loc) Move(n int, buf *Buffer) Loc {
118 for i := 0; i < n; i++ {
123 for i := 0; i < Abs(n); i++ {