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