]> git.lizzy.rs Git - micro.git/blob - cmd/micro/cursor.go
Fix draw ordering
[micro.git] / cmd / micro / cursor.go
1 package main
2
3 // The Cursor struct stores the location of the cursor in the view
4 // The complicated part about the cursor is storing its location.
5 // The cursor must be displayed at an x, y location, but since the buffer
6 // uses a rope to store text, to insert text we must have an index. It
7 // is also simpler to use character indicies for other tasks such as
8 // selection.
9 type Cursor struct {
10         buf *Buffer
11         Loc
12
13         // Last cursor x position
14         LastVisualX int
15
16         // The current selection as a range of character numbers (inclusive)
17         CurSelection [2]Loc
18         // The original selection as a range of character numbers
19         // This is used for line and word selection where it is necessary
20         // to know what the original selection was
21         OrigSelection [2]Loc
22 }
23
24 // Goto puts the cursor at the given cursor's location and gives the current cursor its selection too
25 func (c *Cursor) Goto(b Cursor) {
26         c.X, c.Y, c.LastVisualX = b.X, b.Y, b.LastVisualX
27         c.OrigSelection, c.CurSelection = b.OrigSelection, b.CurSelection
28 }
29
30 // ResetSelection resets the user's selection
31 func (c *Cursor) ResetSelection() {
32         c.CurSelection[0] = c.buf.Start()
33         c.CurSelection[1] = c.buf.Start()
34 }
35
36 // HasSelection returns whether or not the user has selected anything
37 func (c *Cursor) HasSelection() bool {
38         return c.CurSelection[0] != c.CurSelection[1]
39 }
40
41 // DeleteSelection deletes the currently selected text
42 func (c *Cursor) DeleteSelection() {
43         if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
44                 c.buf.Remove(c.CurSelection[1], c.CurSelection[0])
45                 c.Loc = c.CurSelection[1]
46         } else if c.GetSelection() == "" {
47                 return
48         } else {
49                 c.buf.Remove(c.CurSelection[0], c.CurSelection[1])
50                 c.Loc = c.CurSelection[0]
51         }
52 }
53
54 // GetSelection returns the cursor's selection
55 func (c *Cursor) GetSelection() string {
56         if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
57                 return c.buf.Substr(c.CurSelection[1], c.CurSelection[0])
58         }
59         return c.buf.Substr(c.CurSelection[0], c.CurSelection[1])
60 }
61
62 // SelectLine selects the current line
63 func (c *Cursor) SelectLine() {
64         c.Start()
65         c.CurSelection[0] = c.Loc
66         c.End()
67         if c.buf.NumLines-1 > c.Y {
68                 c.CurSelection[1] = c.Loc.Move(1, c.buf)
69         } else {
70                 c.CurSelection[1] = c.Loc
71         }
72
73         c.OrigSelection = c.CurSelection
74 }
75
76 // AddLineToSelection adds the current line to the selection
77 func (c *Cursor) AddLineToSelection() {
78         if c.Loc.LessThan(c.OrigSelection[0]) {
79                 c.Start()
80                 c.CurSelection[0] = c.Loc
81                 c.CurSelection[1] = c.OrigSelection[1]
82         }
83         if c.Loc.GreaterThan(c.OrigSelection[1]) {
84                 c.End()
85                 c.CurSelection[1] = c.Loc.Move(1, c.buf)
86                 c.CurSelection[0] = c.OrigSelection[0]
87         }
88
89         if c.Loc.LessThan(c.OrigSelection[1]) && c.Loc.GreaterThan(c.OrigSelection[0]) {
90                 c.CurSelection = c.OrigSelection
91         }
92 }
93
94 // SelectWord selects the word the cursor is currently on
95 func (c *Cursor) SelectWord() {
96         if len(c.buf.Line(c.Y)) == 0 {
97                 return
98         }
99
100         if !IsWordChar(string(c.RuneUnder(c.X))) {
101                 c.CurSelection[0] = c.Loc
102                 c.CurSelection[1] = c.Loc.Move(1, c.buf)
103                 c.OrigSelection = c.CurSelection
104                 return
105         }
106
107         forward, backward := c.X, c.X
108
109         for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
110                 backward--
111         }
112
113         c.CurSelection[0] = Loc{backward, c.Y}
114         c.OrigSelection[0] = c.CurSelection[0]
115
116         for forward < Count(c.buf.Line(c.Y))-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
117                 forward++
118         }
119
120         c.CurSelection[1] = Loc{forward, c.Y}.Move(1, c.buf)
121         c.OrigSelection[1] = c.CurSelection[1]
122         c.Loc = c.CurSelection[1]
123 }
124
125 // AddWordToSelection adds the word the cursor is currently on to the selection
126 func (c *Cursor) AddWordToSelection() {
127         if c.Loc.GreaterThan(c.OrigSelection[0]) && c.Loc.LessThan(c.OrigSelection[1]) {
128                 c.CurSelection = c.OrigSelection
129                 return
130         }
131
132         if c.Loc.LessThan(c.OrigSelection[0]) {
133                 backward := c.X
134
135                 for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
136                         backward--
137                 }
138
139                 c.CurSelection[0] = Loc{backward, c.Y}
140                 c.CurSelection[1] = c.OrigSelection[1]
141         }
142
143         if c.Loc.GreaterThan(c.OrigSelection[1]) {
144                 forward := c.X
145
146                 for forward < Count(c.buf.Line(c.Y))-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
147                         forward++
148                 }
149
150                 c.CurSelection[1] = Loc{forward, c.Y}.Move(1, c.buf)
151                 c.CurSelection[0] = c.OrigSelection[0]
152         }
153
154         c.Loc = c.CurSelection[1]
155 }
156
157 // SelectTo selects from the current cursor location to the given location
158 func (c *Cursor) SelectTo(loc Loc) {
159         if loc.GreaterThan(c.OrigSelection[0]) {
160                 c.CurSelection[0] = c.OrigSelection[0]
161                 c.CurSelection[1] = loc
162         } else {
163                 c.CurSelection[0] = loc
164                 c.CurSelection[1] = c.OrigSelection[0]
165         }
166 }
167
168 // WordRight moves the cursor one word to the right
169 func (c *Cursor) WordRight() {
170         c.Right()
171         for IsWhitespace(c.RuneUnder(c.X)) {
172                 if c.X == Count(c.buf.Line(c.Y)) {
173                         return
174                 }
175                 c.Right()
176         }
177         for !IsWhitespace(c.RuneUnder(c.X)) {
178                 if c.X == Count(c.buf.Line(c.Y)) {
179                         return
180                 }
181                 c.Right()
182         }
183 }
184
185 // WordLeft moves the cursor one word to the left
186 func (c *Cursor) WordLeft() {
187         c.Left()
188         for IsWhitespace(c.RuneUnder(c.X)) {
189                 if c.X == 0 {
190                         return
191                 }
192                 c.Left()
193         }
194         for !IsWhitespace(c.RuneUnder(c.X)) {
195                 if c.X == 0 {
196                         return
197                 }
198                 c.Left()
199         }
200         c.Right()
201 }
202
203 // RuneUnder returns the rune under the given x position
204 func (c *Cursor) RuneUnder(x int) rune {
205         line := []rune(c.buf.Line(c.Y))
206         if len(line) == 0 {
207                 return '\n'
208         }
209         if x >= len(line) {
210                 return '\n'
211         } else if x < 0 {
212                 x = 0
213         }
214         return line[x]
215 }
216
217 // UpN moves the cursor up N lines (if possible)
218 func (c *Cursor) UpN(amount int) {
219         proposedY := c.Y - amount
220         if proposedY < 0 {
221                 proposedY = 0
222         } else if proposedY >= c.buf.NumLines {
223                 proposedY = c.buf.NumLines - 1
224         }
225         if proposedY == c.Y {
226                 return
227         }
228
229         c.Y = proposedY
230         runes := []rune(c.buf.Line(c.Y))
231         c.X = c.GetCharPosInLine(c.Y, c.LastVisualX)
232         if c.X > len(runes) {
233                 c.X = len(runes)
234         }
235 }
236
237 // DownN moves the cursor down N lines (if possible)
238 func (c *Cursor) DownN(amount int) {
239         c.UpN(-amount)
240 }
241
242 // Up moves the cursor up one line (if possible)
243 func (c *Cursor) Up() {
244         c.UpN(1)
245 }
246
247 // Down moves the cursor down one line (if possible)
248 func (c *Cursor) Down() {
249         c.DownN(1)
250 }
251
252 // Left moves the cursor left one cell (if possible) or to the last line if it is at the beginning
253 func (c *Cursor) Left() {
254         if c.Loc == c.buf.Start() {
255                 return
256         }
257         if c.X > 0 {
258                 c.X--
259         } else {
260                 c.Up()
261                 c.End()
262         }
263         c.LastVisualX = c.GetVisualX()
264 }
265
266 // Right moves the cursor right one cell (if possible) or to the next line if it is at the end
267 func (c *Cursor) Right() {
268         if c.Loc == c.buf.End() {
269                 return
270         }
271         if c.X < Count(c.buf.Line(c.Y)) {
272                 c.X++
273         } else {
274                 c.Down()
275                 c.Start()
276         }
277         c.LastVisualX = c.GetVisualX()
278 }
279
280 // End moves the cursor to the end of the line it is on
281 func (c *Cursor) End() {
282         c.X = Count(c.buf.Line(c.Y))
283         c.LastVisualX = c.GetVisualX()
284 }
285
286 // Start moves the cursor to the start of the line it is on
287 func (c *Cursor) Start() {
288         c.X = 0
289         c.LastVisualX = c.GetVisualX()
290 }
291
292 // GetCharPosInLine gets the char position of a visual x y coordinate (this is necessary because tabs are 1 char but 4 visual spaces)
293 func (c *Cursor) GetCharPosInLine(lineNum, visualPos int) int {
294         // Get the tab size
295         tabSize := int(settings["tabsize"].(float64))
296         // This is the visual line -- every \t replaced with the correct number of spaces
297         visualLineLen := StringWidth(c.buf.Line(lineNum))
298         if visualPos > visualLineLen {
299                 visualPos = visualLineLen
300         }
301         width := WidthOfLargeRunes(c.buf.Line(lineNum))
302         if visualPos >= width {
303                 return visualPos - width
304         }
305         return visualPos / tabSize
306 }
307
308 // GetVisualX returns the x value of the cursor in visual spaces
309 func (c *Cursor) GetVisualX() int {
310         runes := []rune(c.buf.Line(c.Y))
311         return StringWidth(string(runes[:c.X]))
312 }
313
314 // Relocate makes sure that the cursor is inside the bounds of the buffer
315 // If it isn't, it moves it to be within the buffer's lines
316 func (c *Cursor) Relocate() {
317         if c.Y < 0 {
318                 c.Y = 0
319         } else if c.Y >= c.buf.NumLines {
320                 c.Y = c.buf.NumLines - 1
321         }
322
323         if c.X < 0 {
324                 c.X = 0
325         } else if c.X > Count(c.buf.Line(c.Y)) {
326                 c.X = Count(c.buf.Line(c.Y))
327         }
328 }