]> git.lizzy.rs Git - micro.git/blob - cmd/micro/cursor.go
cd1a165700a6752c3143352ae0edfb7d8014ac20
[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         for IsWhitespace(c.RuneUnder(c.X)) {
171                 if c.X == Count(c.buf.Line(c.Y)) {
172                         c.Right()
173                         return
174                 }
175                 c.Right()
176         }
177         c.Right()
178         for IsWordChar(string(c.RuneUnder(c.X))) {
179                 if c.X == Count(c.buf.Line(c.Y)) {
180                         return
181                 }
182                 c.Right()
183         }
184 }
185
186 // WordLeft moves the cursor one word to the left
187 func (c *Cursor) WordLeft() {
188         c.Left()
189         for IsWhitespace(c.RuneUnder(c.X)) {
190                 if c.X == 0 {
191                         return
192                 }
193                 c.Left()
194         }
195         c.Left()
196         for IsWordChar(string(c.RuneUnder(c.X))) {
197                 if c.X == 0 {
198                         return
199                 }
200                 c.Left()
201         }
202         c.Right()
203 }
204
205 // RuneUnder returns the rune under the given x position
206 func (c *Cursor) RuneUnder(x int) rune {
207         line := []rune(c.buf.Line(c.Y))
208         if len(line) == 0 {
209                 return '\n'
210         }
211         if x >= len(line) {
212                 return '\n'
213         } else if x < 0 {
214                 x = 0
215         }
216         return line[x]
217 }
218
219 // UpN moves the cursor up N lines (if possible)
220 func (c *Cursor) UpN(amount int) {
221         proposedY := c.Y - amount
222         if proposedY < 0 {
223                 proposedY = 0
224         } else if proposedY >= c.buf.NumLines {
225                 proposedY = c.buf.NumLines - 1
226         }
227         if proposedY == c.Y {
228                 return
229         }
230
231         c.Y = proposedY
232         runes := []rune(c.buf.Line(c.Y))
233         c.X = c.GetCharPosInLine(c.Y, c.LastVisualX)
234         if c.X > len(runes) {
235                 c.X = len(runes)
236         }
237 }
238
239 // DownN moves the cursor down N lines (if possible)
240 func (c *Cursor) DownN(amount int) {
241         c.UpN(-amount)
242 }
243
244 // Up moves the cursor up one line (if possible)
245 func (c *Cursor) Up() {
246         c.UpN(1)
247 }
248
249 // Down moves the cursor down one line (if possible)
250 func (c *Cursor) Down() {
251         c.DownN(1)
252 }
253
254 // Left moves the cursor left one cell (if possible) or to the last line if it is at the beginning
255 func (c *Cursor) Left() {
256         if c.Loc == c.buf.Start() {
257                 return
258         }
259         if c.X > 0 {
260                 c.X--
261         } else {
262                 c.Up()
263                 c.End()
264         }
265         c.LastVisualX = c.GetVisualX()
266 }
267
268 // Right moves the cursor right one cell (if possible) or to the next line if it is at the end
269 func (c *Cursor) Right() {
270         if c.Loc == c.buf.End() {
271                 return
272         }
273         if c.X < Count(c.buf.Line(c.Y)) {
274                 c.X++
275         } else {
276                 c.Down()
277                 c.Start()
278         }
279         c.LastVisualX = c.GetVisualX()
280 }
281
282 // End moves the cursor to the end of the line it is on
283 func (c *Cursor) End() {
284         c.X = Count(c.buf.Line(c.Y))
285         c.LastVisualX = c.GetVisualX()
286 }
287
288 // Start moves the cursor to the start of the line it is on
289 func (c *Cursor) Start() {
290         c.X = 0
291         c.LastVisualX = c.GetVisualX()
292 }
293
294 // GetCharPosInLine gets the char position of a visual x y coordinate (this is necessary because tabs are 1 char but 4 visual spaces)
295 func (c *Cursor) GetCharPosInLine(lineNum, visualPos int) int {
296         // Get the tab size
297         tabSize := int(c.buf.Settings["tabsize"].(float64))
298         visualLineLen := StringWidth(c.buf.Line(lineNum), tabSize)
299         if visualPos > visualLineLen {
300                 visualPos = visualLineLen
301         }
302         width := WidthOfLargeRunes(c.buf.Line(lineNum), tabSize)
303         if visualPos >= width {
304                 return visualPos - width
305         }
306         return visualPos / tabSize
307 }
308
309 // GetVisualX returns the x value of the cursor in visual spaces
310 func (c *Cursor) GetVisualX() int {
311         runes := []rune(c.buf.Line(c.Y))
312         tabSize := int(c.buf.Settings["tabsize"].(float64))
313         return StringWidth(string(runes[:c.X]), tabSize)
314 }
315
316 // Relocate makes sure that the cursor is inside the bounds of the buffer
317 // If it isn't, it moves it to be within the buffer's lines
318 func (c *Cursor) Relocate() {
319         if c.Y < 0 {
320                 c.Y = 0
321         } else if c.Y >= c.buf.NumLines {
322                 c.Y = c.buf.NumLines - 1
323         }
324
325         if c.X < 0 {
326                 c.X = 0
327         } else if c.X > Count(c.buf.Line(c.Y)) {
328                 c.X = Count(c.buf.Line(c.Y))
329         }
330 }