]> git.lizzy.rs Git - micro.git/blob - cmd/micro/buffer/cursor.go
34741ad37c0f34a8094a1250da474c99413d67cb
[micro.git] / cmd / micro / buffer / cursor.go
1 package buffer
2
3 import (
4         "unicode/utf8"
5
6         runewidth "github.com/mattn/go-runewidth"
7         "github.com/zyedidia/clipboard"
8         "github.com/zyedidia/micro/cmd/micro/util"
9 )
10
11 // InBounds returns whether the given location is a valid character position in the given buffer
12 func InBounds(pos Loc, buf *Buffer) bool {
13         if pos.Y < 0 || pos.Y >= len(buf.lines) || pos.X < 0 || pos.X > utf8.RuneCount(buf.LineBytes(pos.Y)) {
14                 return false
15         }
16
17         return true
18 }
19
20 // The Cursor struct stores the location of the cursor in the buffer
21 // as well as the selection
22 type Cursor struct {
23         Buf *Buffer
24         Loc
25
26         // Last cursor x position
27         LastVisualX int
28
29         // The current selection as a range of character numbers (inclusive)
30         CurSelection [2]Loc
31         // The original selection as a range of character numbers
32         // This is used for line and word selection where it is necessary
33         // to know what the original selection was
34         OrigSelection [2]Loc
35
36         // Which cursor index is this (for multiple cursors)
37         Num int
38 }
39
40 // Goto puts the cursor at the given cursor's location and gives
41 // the current cursor its selection too
42 func (c *Cursor) Goto(b Cursor) {
43         c.X, c.Y, c.LastVisualX = b.X, b.Y, b.LastVisualX
44         c.OrigSelection, c.CurSelection = b.OrigSelection, b.CurSelection
45 }
46
47 // GotoLoc puts the cursor at the given cursor's location and gives
48 // the current cursor its selection too
49 func (c *Cursor) GotoLoc(l Loc) {
50         c.X, c.Y = l.X, l.Y
51         c.LastVisualX = c.GetVisualX()
52 }
53
54 // GetVisualX returns the x value of the cursor in visual spaces
55 func (c *Cursor) GetVisualX() int {
56         if c.X <= 0 {
57                 c.X = 0
58                 return 0
59         }
60
61         bytes := c.Buf.LineBytes(c.Y)
62         tabsize := int(c.Buf.Settings["tabsize"].(float64))
63         if c.X > utf8.RuneCount(bytes) {
64                 c.X = utf8.RuneCount(bytes) - 1
65         }
66
67         return util.StringWidth(bytes, c.X, tabsize)
68 }
69
70 // GetCharPosInLine gets the char position of a visual x y
71 // coordinate (this is necessary because tabs are 1 char but
72 // 4 visual spaces)
73 func (c *Cursor) GetCharPosInLine(b []byte, visualPos int) int {
74         tabsize := int(c.Buf.Settings["tabsize"].(float64))
75
76         // Scan rune by rune until we exceed the visual width that we are
77         // looking for. Then we can return the character position we have found
78         i := 0     // char pos
79         width := 0 // string visual width
80         for len(b) > 0 {
81                 r, size := utf8.DecodeRune(b)
82                 b = b[size:]
83
84                 switch r {
85                 case '\t':
86                         ts := tabsize - (width % tabsize)
87                         width += ts
88                 default:
89                         width += runewidth.RuneWidth(r)
90                 }
91
92                 i++
93
94                 if width >= visualPos {
95                         break
96                 }
97         }
98
99         return i
100 }
101
102 // Start moves the cursor to the start of the line it is on
103 func (c *Cursor) Start() {
104         c.X = 0
105         c.LastVisualX = c.GetVisualX()
106 }
107
108 // End moves the cursor to the end of the line it is on
109 func (c *Cursor) End() {
110         c.X = utf8.RuneCount(c.Buf.LineBytes(c.Y))
111         c.LastVisualX = c.GetVisualX()
112 }
113
114 // CopySelection copies the user's selection to either "primary"
115 // or "clipboard"
116 func (c *Cursor) CopySelection(target string) {
117         if c.HasSelection() {
118                 if target != "primary" || c.Buf.Settings["useprimary"].(bool) {
119                         clipboard.WriteAll(string(c.GetSelection()), target)
120                 }
121         }
122 }
123
124 // ResetSelection resets the user's selection
125 func (c *Cursor) ResetSelection() {
126         c.CurSelection[0] = c.Buf.Start()
127         c.CurSelection[1] = c.Buf.Start()
128 }
129
130 // SetSelectionStart sets the start of the selection
131 func (c *Cursor) SetSelectionStart(pos Loc) {
132         c.CurSelection[0] = pos
133 }
134
135 // SetSelectionEnd sets the end of the selection
136 func (c *Cursor) SetSelectionEnd(pos Loc) {
137         c.CurSelection[1] = pos
138 }
139
140 // HasSelection returns whether or not the user has selected anything
141 func (c *Cursor) HasSelection() bool {
142         return c.CurSelection[0] != c.CurSelection[1]
143 }
144
145 // DeleteSelection deletes the currently selected text
146 func (c *Cursor) DeleteSelection() {
147         if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
148                 c.Buf.Remove(c.CurSelection[1], c.CurSelection[0])
149                 c.Loc = c.CurSelection[1]
150         } else if !c.HasSelection() {
151                 return
152         } else {
153                 c.Buf.Remove(c.CurSelection[0], c.CurSelection[1])
154                 c.Loc = c.CurSelection[0]
155         }
156 }
157
158 // GetSelection returns the cursor's selection
159 func (c *Cursor) GetSelection() []byte {
160         if InBounds(c.CurSelection[0], c.Buf) && InBounds(c.CurSelection[1], c.Buf) {
161                 if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
162                         return c.Buf.Substr(c.CurSelection[1], c.CurSelection[0])
163                 }
164                 return c.Buf.Substr(c.CurSelection[0], c.CurSelection[1])
165         }
166         return []byte{}
167 }
168
169 // SelectLine selects the current line
170 func (c *Cursor) SelectLine() {
171         c.Start()
172         c.SetSelectionStart(c.Loc)
173         c.End()
174         if len(c.Buf.lines)-1 > c.Y {
175                 c.SetSelectionEnd(c.Loc.Move(1, c.Buf))
176         } else {
177                 c.SetSelectionEnd(c.Loc)
178         }
179
180         c.OrigSelection = c.CurSelection
181 }
182
183 // AddLineToSelection adds the current line to the selection
184 func (c *Cursor) AddLineToSelection() {
185         if c.Loc.LessThan(c.OrigSelection[0]) {
186                 c.Start()
187                 c.SetSelectionStart(c.Loc)
188                 c.SetSelectionEnd(c.OrigSelection[1])
189         }
190         if c.Loc.GreaterThan(c.OrigSelection[1]) {
191                 c.End()
192                 c.SetSelectionEnd(c.Loc.Move(1, c.Buf))
193                 c.SetSelectionStart(c.OrigSelection[0])
194         }
195
196         if c.Loc.LessThan(c.OrigSelection[1]) && c.Loc.GreaterThan(c.OrigSelection[0]) {
197                 c.CurSelection = c.OrigSelection
198         }
199 }
200
201 // UpN moves the cursor up N lines (if possible)
202 func (c *Cursor) UpN(amount int) {
203         proposedY := c.Y - amount
204         if proposedY < 0 {
205                 proposedY = 0
206         } else if proposedY >= len(c.Buf.lines) {
207                 proposedY = len(c.Buf.lines) - 1
208         }
209
210         bytes := c.Buf.LineBytes(proposedY)
211         c.X = c.GetCharPosInLine(bytes, c.LastVisualX)
212
213         if c.X > utf8.RuneCount(bytes) || (amount < 0 && proposedY == c.Y) {
214                 c.X = utf8.RuneCount(bytes)
215         }
216
217         c.Y = proposedY
218 }
219
220 // DownN moves the cursor down N lines (if possible)
221 func (c *Cursor) DownN(amount int) {
222         c.UpN(-amount)
223 }
224
225 // Up moves the cursor up one line (if possible)
226 func (c *Cursor) Up() {
227         c.UpN(1)
228 }
229
230 // Down moves the cursor down one line (if possible)
231 func (c *Cursor) Down() {
232         c.DownN(1)
233 }
234
235 // Left moves the cursor left one cell (if possible) or to
236 // the previous line if it is at the beginning
237 func (c *Cursor) Left() {
238         if c.Loc == c.Buf.Start() {
239                 return
240         }
241         if c.X > 0 {
242                 c.X--
243         } else {
244                 c.Up()
245                 c.End()
246         }
247         c.LastVisualX = c.GetVisualX()
248 }
249
250 // Right moves the cursor right one cell (if possible) or
251 // to the next line if it is at the end
252 func (c *Cursor) Right() {
253         if c.Loc == c.Buf.End() {
254                 return
255         }
256         if c.X < utf8.RuneCount(c.Buf.LineBytes(c.Y)) {
257                 c.X++
258         } else {
259                 c.Down()
260                 c.Start()
261         }
262         c.LastVisualX = c.GetVisualX()
263 }
264
265 // Relocate makes sure that the cursor is inside the bounds
266 // of the buffer If it isn't, it moves it to be within the
267 // buffer's lines
268 func (c *Cursor) Relocate() {
269         if c.Y < 0 {
270                 c.Y = 0
271         } else if c.Y >= len(c.Buf.lines) {
272                 c.Y = len(c.Buf.lines) - 1
273         }
274
275         if c.X < 0 {
276                 c.X = 0
277         } else if c.X > utf8.RuneCount(c.Buf.LineBytes(c.Y)) {
278                 c.X = utf8.RuneCount(c.Buf.LineBytes(c.Y))
279         }
280 }