]> git.lizzy.rs Git - micro.git/blob - internal/buffer/cursor.go
Support csharp-script syntax. (#1425)
[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 // End moves the cursor to the end of the line it is on
113 func (c *Cursor) End() {
114         c.X = utf8.RuneCount(c.buf.LineBytes(c.Y))
115         c.LastVisualX = c.GetVisualX()
116 }
117
118 // CopySelection copies the user's selection to either "primary"
119 // or "clipboard"
120 func (c *Cursor) CopySelection(target string) {
121         if c.HasSelection() {
122                 if target != "primary" || c.buf.Settings["useprimary"].(bool) {
123                         clipboard.WriteAll(string(c.GetSelection()), target)
124                 }
125         }
126 }
127
128 // ResetSelection resets the user's selection
129 func (c *Cursor) ResetSelection() {
130         c.CurSelection[0] = c.buf.Start()
131         c.CurSelection[1] = c.buf.Start()
132 }
133
134 // SetSelectionStart sets the start of the selection
135 func (c *Cursor) SetSelectionStart(pos Loc) {
136         c.CurSelection[0] = pos
137 }
138
139 // SetSelectionEnd sets the end of the selection
140 func (c *Cursor) SetSelectionEnd(pos Loc) {
141         c.CurSelection[1] = pos
142 }
143
144 // HasSelection returns whether or not the user has selected anything
145 func (c *Cursor) HasSelection() bool {
146         return c.CurSelection[0] != c.CurSelection[1]
147 }
148
149 // DeleteSelection deletes the currently selected text
150 func (c *Cursor) DeleteSelection() {
151         if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
152                 c.buf.Remove(c.CurSelection[1], c.CurSelection[0])
153                 c.Loc = c.CurSelection[1]
154         } else if !c.HasSelection() {
155                 return
156         } else {
157                 c.buf.Remove(c.CurSelection[0], c.CurSelection[1])
158                 c.Loc = c.CurSelection[0]
159         }
160 }
161
162 // Deselect closes the cursor's current selection
163 // Start indicates whether the cursor should be placed
164 // at the start or end of the selection
165 func (c *Cursor) Deselect(start bool) {
166         if c.HasSelection() {
167                 if start {
168                         c.Loc = c.CurSelection[0]
169                 } else {
170                         c.Loc = c.CurSelection[1].Move(-1, c.buf)
171                 }
172                 c.ResetSelection()
173                 c.StoreVisualX()
174         }
175 }
176
177 // GetSelection returns the cursor's selection
178 func (c *Cursor) GetSelection() []byte {
179         if InBounds(c.CurSelection[0], c.buf) && InBounds(c.CurSelection[1], c.buf) {
180                 if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
181                         return c.buf.Substr(c.CurSelection[1], c.CurSelection[0])
182                 }
183                 return c.buf.Substr(c.CurSelection[0], c.CurSelection[1])
184         }
185         return []byte{}
186 }
187
188 // SelectLine selects the current line
189 func (c *Cursor) SelectLine() {
190         c.Start()
191         c.SetSelectionStart(c.Loc)
192         c.End()
193         if len(c.buf.lines)-1 > c.Y {
194                 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
195         } else {
196                 c.SetSelectionEnd(c.Loc)
197         }
198
199         c.OrigSelection = c.CurSelection
200 }
201
202 // AddLineToSelection adds the current line to the selection
203 func (c *Cursor) AddLineToSelection() {
204         if c.Loc.LessThan(c.OrigSelection[0]) {
205                 c.Start()
206                 c.SetSelectionStart(c.Loc)
207                 c.SetSelectionEnd(c.OrigSelection[1])
208         }
209         if c.Loc.GreaterThan(c.OrigSelection[1]) {
210                 c.End()
211                 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
212                 c.SetSelectionStart(c.OrigSelection[0])
213         }
214
215         if c.Loc.LessThan(c.OrigSelection[1]) && c.Loc.GreaterThan(c.OrigSelection[0]) {
216                 c.CurSelection = c.OrigSelection
217         }
218 }
219
220 // UpN moves the cursor up N lines (if possible)
221 func (c *Cursor) UpN(amount int) {
222         proposedY := c.Y - amount
223         if proposedY < 0 {
224                 proposedY = 0
225         } else if proposedY >= len(c.buf.lines) {
226                 proposedY = len(c.buf.lines) - 1
227         }
228
229         bytes := c.buf.LineBytes(proposedY)
230         c.X = c.GetCharPosInLine(bytes, c.LastVisualX)
231
232         if c.X > utf8.RuneCount(bytes) || (amount < 0 && proposedY == c.Y) {
233                 c.X = utf8.RuneCount(bytes)
234         }
235
236         c.Y = proposedY
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
255 // the previous line if it is at the beginning
256 func (c *Cursor) Left() {
257         if c.Loc == c.buf.Start() {
258                 return
259         }
260         if c.X > 0 {
261                 c.X--
262         } else {
263                 c.Up()
264                 c.End()
265         }
266         c.StoreVisualX()
267 }
268
269 // Right moves the cursor right one cell (if possible) or
270 // to the next line if it is at the end
271 func (c *Cursor) Right() {
272         if c.Loc == c.buf.End() {
273                 return
274         }
275         if c.X < utf8.RuneCount(c.buf.LineBytes(c.Y)) {
276                 c.X++
277         } else {
278                 c.Down()
279                 c.Start()
280         }
281         c.StoreVisualX()
282 }
283
284 // Relocate makes sure that the cursor is inside the bounds
285 // of the buffer If it isn't, it moves it to be within the
286 // buffer's lines
287 func (c *Cursor) Relocate() {
288         if c.Y < 0 {
289                 c.Y = 0
290         } else if c.Y >= len(c.buf.lines) {
291                 c.Y = len(c.buf.lines) - 1
292         }
293
294         if c.X < 0 {
295                 c.X = 0
296         } else if c.X > utf8.RuneCount(c.buf.LineBytes(c.Y)) {
297                 c.X = utf8.RuneCount(c.buf.LineBytes(c.Y))
298         }
299 }
300
301 // SelectWord selects the word the cursor is currently on
302 func (c *Cursor) SelectWord() {
303         if len(c.buf.LineBytes(c.Y)) == 0 {
304                 return
305         }
306
307         if !util.IsWordChar(c.RuneUnder(c.X)) {
308                 c.SetSelectionStart(c.Loc)
309                 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
310                 c.OrigSelection = c.CurSelection
311                 return
312         }
313
314         forward, backward := c.X, c.X
315
316         for backward > 0 && util.IsWordChar(c.RuneUnder(backward-1)) {
317                 backward--
318         }
319
320         c.SetSelectionStart(Loc{backward, c.Y})
321         c.OrigSelection[0] = c.CurSelection[0]
322
323         lineLen := utf8.RuneCount(c.buf.LineBytes(c.Y)) - 1
324         for forward < lineLen && util.IsWordChar(c.RuneUnder(forward+1)) {
325                 forward++
326         }
327
328         c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
329         c.OrigSelection[1] = c.CurSelection[1]
330         c.Loc = c.CurSelection[1]
331 }
332
333 // AddWordToSelection adds the word the cursor is currently on
334 // to the selection
335 func (c *Cursor) AddWordToSelection() {
336         if c.Loc.GreaterThan(c.OrigSelection[0]) && c.Loc.LessThan(c.OrigSelection[1]) {
337                 c.CurSelection = c.OrigSelection
338                 return
339         }
340
341         if c.Loc.LessThan(c.OrigSelection[0]) {
342                 backward := c.X
343
344                 for backward > 0 && util.IsWordChar(c.RuneUnder(backward-1)) {
345                         backward--
346                 }
347
348                 c.SetSelectionStart(Loc{backward, c.Y})
349                 c.SetSelectionEnd(c.OrigSelection[1])
350         }
351
352         if c.Loc.GreaterThan(c.OrigSelection[1]) {
353                 forward := c.X
354
355                 lineLen := utf8.RuneCount(c.buf.LineBytes(c.Y)) - 1
356                 for forward < lineLen && util.IsWordChar(c.RuneUnder(forward+1)) {
357                         forward++
358                 }
359
360                 c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
361                 c.SetSelectionStart(c.OrigSelection[0])
362         }
363
364         c.Loc = c.CurSelection[1]
365 }
366
367 // SelectTo selects from the current cursor location to the given
368 // location
369 func (c *Cursor) SelectTo(loc Loc) {
370         if loc.GreaterThan(c.OrigSelection[0]) {
371                 c.SetSelectionStart(c.OrigSelection[0])
372                 c.SetSelectionEnd(loc)
373         } else {
374                 c.SetSelectionStart(loc)
375                 c.SetSelectionEnd(c.OrigSelection[0])
376         }
377 }
378
379 // WordRight moves the cursor one word to the right
380 func (c *Cursor) WordRight() {
381         for util.IsWhitespace(c.RuneUnder(c.X)) {
382                 if c.X == utf8.RuneCount(c.buf.LineBytes(c.Y)) {
383                         c.Right()
384                         return
385                 }
386                 c.Right()
387         }
388         c.Right()
389         for util.IsWordChar(c.RuneUnder(c.X)) {
390                 if c.X == utf8.RuneCount(c.buf.LineBytes(c.Y)) {
391                         return
392                 }
393                 c.Right()
394         }
395 }
396
397 // WordLeft moves the cursor one word to the left
398 func (c *Cursor) WordLeft() {
399         c.Left()
400         for util.IsWhitespace(c.RuneUnder(c.X)) {
401                 if c.X == 0 {
402                         return
403                 }
404                 c.Left()
405         }
406         c.Left()
407         for util.IsWordChar(c.RuneUnder(c.X)) {
408                 if c.X == 0 {
409                         return
410                 }
411                 c.Left()
412         }
413         c.Right()
414 }
415
416 // RuneUnder returns the rune under the given x position
417 func (c *Cursor) RuneUnder(x int) rune {
418         line := c.buf.LineBytes(c.Y)
419         if len(line) == 0 || x >= utf8.RuneCount(line) {
420                 return '\n'
421         } else if x < 0 {
422                 x = 0
423         }
424         i := 0
425         for len(line) > 0 {
426                 r, size := utf8.DecodeRune(line)
427                 line = line[size:]
428
429                 if i == x {
430                         return r
431                 }
432
433                 i++
434         }
435         return '\n'
436 }
437
438 func (c *Cursor) StoreVisualX() {
439         c.LastVisualX = c.GetVisualX()
440 }