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