]> git.lizzy.rs Git - micro.git/blob - internal/buffer/cursor.go
Merge branch 'jwarner112-jwarner112-copyline'
[micro.git] / internal / buffer / cursor.go
1 package buffer
2
3 import (
4         "unicode/utf8"
5
6         "github.com/zyedidia/clipboard"
7         "github.com/zyedidia/micro/internal/util"
8 )
9
10 // InBounds returns whether the given location is a valid character position in the given buffer
11 func InBounds(pos Loc, buf *Buffer) bool {
12         if pos.Y < 0 || pos.Y >= len(buf.lines) || pos.X < 0 || pos.X > utf8.RuneCount(buf.LineBytes(pos.Y)) {
13                 return false
14         }
15
16         return true
17 }
18
19 // The Cursor struct stores the location of the cursor in the buffer
20 // as well as the selection
21 type Cursor struct {
22         buf *Buffer
23         Loc
24
25         // Last cursor x position
26         LastVisualX int
27
28         // The current selection as a range of character numbers (inclusive)
29         CurSelection [2]Loc
30         // The original selection as a range of character numbers
31         // This is used for line and word selection where it is necessary
32         // to know what the original selection was
33         OrigSelection [2]Loc
34
35         // Which cursor index is this (for multiple cursors)
36         Num int
37 }
38
39 func NewCursor(b *Buffer, l Loc) *Cursor {
40         c := &Cursor{
41                 buf: b,
42                 Loc: l,
43         }
44         c.StoreVisualX()
45         return c
46 }
47
48 func (c *Cursor) SetBuf(b *Buffer) {
49         c.buf = b
50 }
51
52 func (c *Cursor) Buf() *Buffer {
53         return c.buf
54 }
55
56 // Goto puts the cursor at the given cursor's location and gives
57 // the current cursor its selection too
58 func (c *Cursor) Goto(b Cursor) {
59         c.X, c.Y, c.LastVisualX = b.X, b.Y, b.LastVisualX
60         c.OrigSelection, c.CurSelection = b.OrigSelection, b.CurSelection
61 }
62
63 // GotoLoc puts the cursor at the given cursor's location and gives
64 // the current cursor its selection too
65 func (c *Cursor) GotoLoc(l Loc) {
66         c.X, c.Y = l.X, l.Y
67         c.StoreVisualX()
68 }
69
70 // GetVisualX returns the x value of the cursor in visual spaces
71 func (c *Cursor) GetVisualX() int {
72         if c.X <= 0 {
73                 c.X = 0
74                 return 0
75         }
76
77         bytes := c.buf.LineBytes(c.Y)
78         tabsize := int(c.buf.Settings["tabsize"].(float64))
79         if c.X > utf8.RuneCount(bytes) {
80                 c.X = utf8.RuneCount(bytes) - 1
81         }
82
83         return util.StringWidth(bytes, c.X, tabsize)
84 }
85
86 // GetCharPosInLine gets the char position of a visual x y
87 // coordinate (this is necessary because tabs are 1 char but
88 // 4 visual spaces)
89 func (c *Cursor) GetCharPosInLine(b []byte, visualPos int) int {
90         tabsize := int(c.buf.Settings["tabsize"].(float64))
91         return util.GetCharPosInLine(b, visualPos, tabsize)
92 }
93
94 // Start moves the cursor to the start of the line it is on
95 func (c *Cursor) Start() {
96         c.X = 0
97         c.LastVisualX = c.GetVisualX()
98 }
99
100 // StartOfText moves the cursor to the first non-whitespace rune of
101 // the line it is on
102 func (c *Cursor) StartOfText() {
103         c.Start()
104         for util.IsWhitespace(c.RuneUnder(c.X)) {
105                 if c.X == utf8.RuneCount(c.buf.LineBytes(c.Y)) {
106                         break
107                 }
108                 c.Right()
109         }
110 }
111
112 // IsStartOfText returns whether the cursor is at the first
113 // non-whitespace rune of the line it is on
114 func (c *Cursor) IsStartOfText() bool {
115         x := 0
116         for util.IsWhitespace(c.RuneUnder(x)) {
117                 if x == utf8.RuneCount(c.buf.LineBytes(c.Y)) {
118                         break
119                 }
120                 x++
121         }
122         return c.X == x
123 }
124
125 // End moves the cursor to the end of the line it is on
126 func (c *Cursor) End() {
127         c.X = utf8.RuneCount(c.buf.LineBytes(c.Y))
128         c.LastVisualX = c.GetVisualX()
129 }
130
131 // CopySelection copies the user's selection to either "primary"
132 // or "clipboard"
133 func (c *Cursor) CopySelection(target string) {
134         if c.HasSelection() {
135                 if target != "primary" || c.buf.Settings["useprimary"].(bool) {
136                         clipboard.WriteAll(string(c.GetSelection()), target)
137                 }
138         }
139 }
140
141 // ResetSelection resets the user's selection
142 func (c *Cursor) ResetSelection() {
143         c.CurSelection[0] = c.buf.Start()
144         c.CurSelection[1] = c.buf.Start()
145 }
146
147 // SetSelectionStart sets the start of the selection
148 func (c *Cursor) SetSelectionStart(pos Loc) {
149         c.CurSelection[0] = pos
150 }
151
152 // SetSelectionEnd sets the end of the selection
153 func (c *Cursor) SetSelectionEnd(pos Loc) {
154         c.CurSelection[1] = pos
155 }
156
157 // HasSelection returns whether or not the user has selected anything
158 func (c *Cursor) HasSelection() bool {
159         return c.CurSelection[0] != c.CurSelection[1]
160 }
161
162 // DeleteSelection deletes the currently selected text
163 func (c *Cursor) DeleteSelection() {
164         if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
165                 c.buf.Remove(c.CurSelection[1], c.CurSelection[0])
166                 c.Loc = c.CurSelection[1]
167         } else if !c.HasSelection() {
168                 return
169         } else {
170                 c.buf.Remove(c.CurSelection[0], c.CurSelection[1])
171                 c.Loc = c.CurSelection[0]
172         }
173 }
174
175 // Deselect closes the cursor's current selection
176 // Start indicates whether the cursor should be placed
177 // at the start or end of the selection
178 func (c *Cursor) Deselect(start bool) {
179         if c.HasSelection() {
180                 if start {
181                         c.Loc = c.CurSelection[0]
182                 } else {
183                         c.Loc = c.CurSelection[1].Move(-1, c.buf)
184                 }
185                 c.ResetSelection()
186                 c.StoreVisualX()
187         }
188 }
189
190 // GetSelection returns the cursor's selection
191 func (c *Cursor) GetSelection() []byte {
192         if InBounds(c.CurSelection[0], c.buf) && InBounds(c.CurSelection[1], c.buf) {
193                 if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
194                         return c.buf.Substr(c.CurSelection[1], c.CurSelection[0])
195                 }
196                 return c.buf.Substr(c.CurSelection[0], c.CurSelection[1])
197         }
198         return []byte{}
199 }
200
201 // SelectLine selects the current line
202 func (c *Cursor) SelectLine() {
203         c.Start()
204         c.SetSelectionStart(c.Loc)
205         c.End()
206         if len(c.buf.lines)-1 > c.Y {
207                 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
208         } else {
209                 c.SetSelectionEnd(c.Loc)
210         }
211
212         c.OrigSelection = c.CurSelection
213 }
214
215 // AddLineToSelection adds the current line to the selection
216 func (c *Cursor) AddLineToSelection() {
217         if c.Loc.LessThan(c.OrigSelection[0]) {
218                 c.Start()
219                 c.SetSelectionStart(c.Loc)
220                 c.SetSelectionEnd(c.OrigSelection[1])
221         }
222         if c.Loc.GreaterThan(c.OrigSelection[1]) {
223                 c.End()
224                 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
225                 c.SetSelectionStart(c.OrigSelection[0])
226         }
227
228         if c.Loc.LessThan(c.OrigSelection[1]) && c.Loc.GreaterThan(c.OrigSelection[0]) {
229                 c.CurSelection = c.OrigSelection
230         }
231 }
232
233 // UpN moves the cursor up N lines (if possible)
234 func (c *Cursor) UpN(amount int) {
235         proposedY := c.Y - amount
236         if proposedY < 0 {
237                 proposedY = 0
238         } else if proposedY >= len(c.buf.lines) {
239                 proposedY = len(c.buf.lines) - 1
240         }
241
242         bytes := c.buf.LineBytes(proposedY)
243         c.X = c.GetCharPosInLine(bytes, c.LastVisualX)
244
245         if c.X > utf8.RuneCount(bytes) || (amount < 0 && proposedY == c.Y) {
246                 c.X = utf8.RuneCount(bytes)
247         }
248
249         c.Y = proposedY
250 }
251
252 // DownN moves the cursor down N lines (if possible)
253 func (c *Cursor) DownN(amount int) {
254         c.UpN(-amount)
255 }
256
257 // Up moves the cursor up one line (if possible)
258 func (c *Cursor) Up() {
259         c.UpN(1)
260 }
261
262 // Down moves the cursor down one line (if possible)
263 func (c *Cursor) Down() {
264         c.DownN(1)
265 }
266
267 // Left moves the cursor left one cell (if possible) or to
268 // the previous line if it is at the beginning
269 func (c *Cursor) Left() {
270         if c.Loc == c.buf.Start() {
271                 return
272         }
273         if c.X > 0 {
274                 c.X--
275         } else {
276                 c.Up()
277                 c.End()
278         }
279         c.StoreVisualX()
280 }
281
282 // Right moves the cursor right one cell (if possible) or
283 // to the next line if it is at the end
284 func (c *Cursor) Right() {
285         if c.Loc == c.buf.End() {
286                 return
287         }
288         if c.X < utf8.RuneCount(c.buf.LineBytes(c.Y)) {
289                 c.X++
290         } else {
291                 c.Down()
292                 c.Start()
293         }
294         c.StoreVisualX()
295 }
296
297 // Relocate makes sure that the cursor is inside the bounds
298 // of the buffer If it isn't, it moves it to be within the
299 // buffer's lines
300 func (c *Cursor) Relocate() {
301         if c.Y < 0 {
302                 c.Y = 0
303         } else if c.Y >= len(c.buf.lines) {
304                 c.Y = len(c.buf.lines) - 1
305         }
306
307         if c.X < 0 {
308                 c.X = 0
309         } else if c.X > utf8.RuneCount(c.buf.LineBytes(c.Y)) {
310                 c.X = utf8.RuneCount(c.buf.LineBytes(c.Y))
311         }
312 }
313
314 // SelectWord selects the word the cursor is currently on
315 func (c *Cursor) SelectWord() {
316         if len(c.buf.LineBytes(c.Y)) == 0 {
317                 return
318         }
319
320         if !util.IsWordChar(c.RuneUnder(c.X)) {
321                 c.SetSelectionStart(c.Loc)
322                 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
323                 c.OrigSelection = c.CurSelection
324                 return
325         }
326
327         forward, backward := c.X, c.X
328
329         for backward > 0 && util.IsWordChar(c.RuneUnder(backward-1)) {
330                 backward--
331         }
332
333         c.SetSelectionStart(Loc{backward, c.Y})
334         c.OrigSelection[0] = c.CurSelection[0]
335
336         lineLen := utf8.RuneCount(c.buf.LineBytes(c.Y)) - 1
337         for forward < lineLen && util.IsWordChar(c.RuneUnder(forward+1)) {
338                 forward++
339         }
340
341         c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
342         c.OrigSelection[1] = c.CurSelection[1]
343         c.Loc = c.CurSelection[1]
344 }
345
346 // AddWordToSelection adds the word the cursor is currently on
347 // to the selection
348 func (c *Cursor) AddWordToSelection() {
349         if c.Loc.GreaterThan(c.OrigSelection[0]) && c.Loc.LessThan(c.OrigSelection[1]) {
350                 c.CurSelection = c.OrigSelection
351                 return
352         }
353
354         if c.Loc.LessThan(c.OrigSelection[0]) {
355                 backward := c.X
356
357                 for backward > 0 && util.IsWordChar(c.RuneUnder(backward-1)) {
358                         backward--
359                 }
360
361                 c.SetSelectionStart(Loc{backward, c.Y})
362                 c.SetSelectionEnd(c.OrigSelection[1])
363         }
364
365         if c.Loc.GreaterThan(c.OrigSelection[1]) {
366                 forward := c.X
367
368                 lineLen := utf8.RuneCount(c.buf.LineBytes(c.Y)) - 1
369                 for forward < lineLen && util.IsWordChar(c.RuneUnder(forward+1)) {
370                         forward++
371                 }
372
373                 c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
374                 c.SetSelectionStart(c.OrigSelection[0])
375         }
376
377         c.Loc = c.CurSelection[1]
378 }
379
380 // SelectTo selects from the current cursor location to the given
381 // location
382 func (c *Cursor) SelectTo(loc Loc) {
383         if loc.GreaterThan(c.OrigSelection[0]) {
384                 c.SetSelectionStart(c.OrigSelection[0])
385                 c.SetSelectionEnd(loc)
386         } else {
387                 c.SetSelectionStart(loc)
388                 c.SetSelectionEnd(c.OrigSelection[0])
389         }
390 }
391
392 // WordRight moves the cursor one word to the right
393 func (c *Cursor) WordRight() {
394         for util.IsWhitespace(c.RuneUnder(c.X)) {
395                 if c.X == utf8.RuneCount(c.buf.LineBytes(c.Y)) {
396                         c.Right()
397                         return
398                 }
399                 c.Right()
400         }
401         c.Right()
402         for util.IsWordChar(c.RuneUnder(c.X)) {
403                 if c.X == utf8.RuneCount(c.buf.LineBytes(c.Y)) {
404                         return
405                 }
406                 c.Right()
407         }
408 }
409
410 // WordLeft moves the cursor one word to the left
411 func (c *Cursor) WordLeft() {
412         c.Left()
413         for util.IsWhitespace(c.RuneUnder(c.X)) {
414                 if c.X == 0 {
415                         return
416                 }
417                 c.Left()
418         }
419         c.Left()
420         for util.IsWordChar(c.RuneUnder(c.X)) {
421                 if c.X == 0 {
422                         return
423                 }
424                 c.Left()
425         }
426         c.Right()
427 }
428
429 // RuneUnder returns the rune under the given x position
430 func (c *Cursor) RuneUnder(x int) rune {
431         line := c.buf.LineBytes(c.Y)
432         if len(line) == 0 || x >= utf8.RuneCount(line) {
433                 return '\n'
434         } else if x < 0 {
435                 x = 0
436         }
437         i := 0
438         for len(line) > 0 {
439                 r, size := utf8.DecodeRune(line)
440                 line = line[size:]
441
442                 if i == x {
443                         return r
444                 }
445
446                 i++
447         }
448         return '\n'
449 }
450
451 func (c *Cursor) StoreVisualX() {
452         c.LastVisualX = c.GetVisualX()
453 }