]> git.lizzy.rs Git - micro.git/blob - cmd/micro/loc.go
Fix draw ordering
[micro.git] / cmd / micro / loc.go
1 package main
2
3 // FromCharPos converts from a character position to an x, y position
4 func FromCharPos(loc int, buf *Buffer) Loc {
5         charNum := 0
6         x, y := 0, 0
7
8         lineLen := Count(buf.Line(y)) + 1
9         for charNum+lineLen <= loc {
10                 charNum += lineLen
11                 y++
12                 lineLen = Count(buf.Line(y)) + 1
13         }
14         x = loc - charNum
15
16         return Loc{x, y}
17 }
18
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
22         loc := 0
23         for i := 0; i < y; i++ {
24                 // + 1 for the newline
25                 loc += Count(buf.Line(i)) + 1
26         }
27         loc += x
28         return loc
29 }
30
31 // Loc stores a location
32 type Loc struct {
33         X, Y int
34 }
35
36 // LessThan returns true if b is smaller
37 func (l Loc) LessThan(b Loc) bool {
38         if l.Y < b.Y {
39                 return true
40         }
41         if l.Y == b.Y && l.X < b.X {
42                 return true
43         }
44         return false
45 }
46
47 // GreaterThan returns true if b is bigger
48 func (l Loc) GreaterThan(b Loc) bool {
49         if l.Y > b.Y {
50                 return true
51         }
52         if l.Y == b.Y && l.X > b.X {
53                 return true
54         }
55         return false
56 }
57
58 // GreaterEqual returns true if b is greater than or equal to b
59 func (l Loc) GreaterEqual(b Loc) bool {
60         if l.Y > b.Y {
61                 return true
62         }
63         if l.Y == b.Y && l.X > b.X {
64                 return true
65         }
66         if l == b {
67                 return true
68         }
69         return false
70 }
71
72 // LessEqual returns true if b is less than or equal to b
73 func (l Loc) LessEqual(b Loc) bool {
74         if l.Y < b.Y {
75                 return true
76         }
77         if l.Y == b.Y && l.X < b.X {
78                 return true
79         }
80         if l == b {
81                 return true
82         }
83         return false
84 }
85
86 // This moves the location one character to the right
87 func (l Loc) right(buf *Buffer) Loc {
88         if l == buf.End() {
89                 return Loc{l.X + 1, l.Y}
90         }
91         var res Loc
92         if l.X < Count(buf.Line(l.Y)) {
93                 res = Loc{l.X + 1, l.Y}
94         } else {
95                 res = Loc{0, l.Y + 1}
96         }
97         return res
98 }
99
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}
104         }
105         var res Loc
106         if l.X > 0 {
107                 res = Loc{l.X - 1, l.Y}
108         } else {
109                 res = Loc{Count(buf.Line(l.Y - 1)), l.Y - 1}
110         }
111         return res
112 }
113
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 {
117         if n > 0 {
118                 for i := 0; i < n; i++ {
119                         l = l.right(buf)
120                 }
121                 return l
122         }
123         for i := 0; i < Abs(n); i++ {
124                 return l.left(buf)
125         }
126         return l
127 }