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