]> git.lizzy.rs Git - micro.git/blob - cmd/micro/cursor.go
Fix SelectLine
[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 {
95                 c.v.eh.Remove(c.curSelection[0], c.curSelection[1])
96                 c.SetLoc(c.curSelection[0])
97         }
98 }
99
100 // GetSelection returns the cursor's selection
101 func (c *Cursor) GetSelection() string {
102         if c.curSelection[0] > c.curSelection[1] {
103                 return string([]rune(c.v.buf.text)[c.curSelection[1]:c.curSelection[0]])
104         }
105         return string([]rune(c.v.buf.text)[c.curSelection[0]:c.curSelection[1]])
106 }
107
108 // SelectLine selects the current line
109 func (c *Cursor) SelectLine() {
110         c.Start()
111         c.curSelection[0] = c.Loc()
112         c.End()
113         if len(c.v.buf.lines)-1 > c.y {
114                 c.curSelection[1] = c.Loc() + 1
115         } else {
116                 c.curSelection[1] = c.Loc()
117         }
118
119         c.origSelection = c.curSelection
120 }
121
122 // AddLineToSelection adds the current line to the selection
123 func (c *Cursor) AddLineToSelection() {
124         loc := c.Loc()
125
126         if loc < c.origSelection[0] {
127                 c.Start()
128                 c.curSelection[0] = c.Loc()
129                 c.curSelection[1] = c.origSelection[1]
130         }
131         if loc > c.origSelection[1] {
132                 c.End()
133                 c.curSelection[1] = c.Loc()
134                 c.curSelection[0] = c.origSelection[0]
135         }
136
137         if loc < c.origSelection[1] && loc > c.origSelection[0] {
138                 c.curSelection = c.origSelection
139         }
140 }
141
142 // SelectWord selects the word the cursor is currently on
143 func (c *Cursor) SelectWord() {
144         if len(c.v.buf.lines[c.y]) == 0 {
145                 return
146         }
147
148         if !IsWordChar(string(c.RuneUnder(c.x))) {
149                 loc := c.Loc()
150                 c.curSelection[0] = loc
151                 c.curSelection[1] = loc + 1
152                 c.origSelection = c.curSelection
153                 return
154         }
155
156         forward, backward := c.x, c.x
157
158         for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
159                 backward--
160         }
161
162         c.curSelection[0] = ToCharPos(backward, c.y, c.v.buf)
163         c.origSelection[0] = c.curSelection[0]
164
165         for forward < Count(c.v.buf.lines[c.y])-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
166                 forward++
167         }
168
169         c.curSelection[1] = ToCharPos(forward, c.y, c.v.buf) + 1
170         c.origSelection[1] = c.curSelection[1]
171 }
172
173 // AddWordToSelection adds the word the cursor is currently on to the selection
174 func (c *Cursor) AddWordToSelection() {
175         loc := c.Loc()
176
177         if loc > c.origSelection[0] && loc < c.origSelection[1] {
178                 c.curSelection = c.origSelection
179                 return
180         }
181
182         if loc < c.origSelection[0] {
183                 backward := c.x
184
185                 for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
186                         backward--
187                 }
188
189                 c.curSelection[0] = ToCharPos(backward, c.y, c.v.buf)
190                 c.curSelection[1] = c.origSelection[1]
191         }
192
193         if loc > c.origSelection[1] {
194                 forward := c.x
195
196                 for forward < Count(c.v.buf.lines[c.y])-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
197                         forward++
198                 }
199
200                 c.curSelection[1] = ToCharPos(forward, c.y, c.v.buf) + 1
201                 c.curSelection[0] = c.origSelection[0]
202         }
203 }
204
205 // RuneUnder returns the rune under the given x position
206 func (c *Cursor) RuneUnder(x int) rune {
207         line := []rune(c.v.buf.lines[c.y])
208         if x >= len(line) {
209                 x = len(line) - 1
210         } else if x < 0 {
211                 x = 0
212         }
213         return line[x]
214 }
215
216 // Up moves the cursor up one line (if possible)
217 func (c *Cursor) Up() {
218         if c.y > 0 {
219                 c.y--
220
221                 runes := []rune(c.v.buf.lines[c.y])
222                 c.x = c.GetCharPosInLine(c.y, c.lastVisualX)
223                 if c.x > len(runes) {
224                         c.x = len(runes)
225                 }
226         }
227 }
228
229 // Down moves the cursor down one line (if possible)
230 func (c *Cursor) Down() {
231         if c.y < len(c.v.buf.lines)-1 {
232                 c.y++
233
234                 runes := []rune(c.v.buf.lines[c.y])
235                 c.x = c.GetCharPosInLine(c.y, c.lastVisualX)
236                 if c.x > len(runes) {
237                         c.x = len(runes)
238                 }
239         }
240 }
241
242 // Left moves the cursor left one cell (if possible) or to the last line if it is at the beginning
243 func (c *Cursor) Left() {
244         if c.Loc() == 0 {
245                 return
246         }
247         if c.x > 0 {
248                 c.x--
249         } else {
250                 c.Up()
251                 c.End()
252         }
253         c.lastVisualX = c.GetVisualX()
254 }
255
256 // Right moves the cursor right one cell (if possible) or to the next line if it is at the end
257 func (c *Cursor) Right() {
258         if c.Loc() == c.v.buf.Len() {
259                 return
260         }
261         if c.x < Count(c.v.buf.lines[c.y]) {
262                 c.x++
263         } else {
264                 c.Down()
265                 c.Start()
266         }
267         c.lastVisualX = c.GetVisualX()
268 }
269
270 // End moves the cursor to the end of the line it is on
271 func (c *Cursor) End() {
272         c.x = Count(c.v.buf.lines[c.y])
273         c.lastVisualX = c.GetVisualX()
274 }
275
276 // Start moves the cursor to the start of the line it is on
277 func (c *Cursor) Start() {
278         c.x = 0
279         c.lastVisualX = c.GetVisualX()
280 }
281
282 // GetCharPosInLine gets the char position of a visual x y coordinate (this is necessary because tabs are 1 char but 4 visual spaces)
283 func (c *Cursor) GetCharPosInLine(lineNum, visualPos int) int {
284         // Get the tab size
285         tabSize := settings.TabSize
286         // This is the visual line -- every \t replaced with the correct number of spaces
287         visualLine := strings.Replace(c.v.buf.lines[lineNum], "\t", "\t"+Spaces(tabSize-1), -1)
288         if visualPos > Count(visualLine) {
289                 visualPos = Count(visualLine)
290         }
291         numTabs := NumOccurences(visualLine[:visualPos], '\t')
292         if visualPos >= (tabSize-1)*numTabs {
293                 return visualPos - (tabSize-1)*numTabs
294         }
295         return visualPos / tabSize
296 }
297
298 // GetVisualX returns the x value of the cursor in visual spaces
299 func (c *Cursor) GetVisualX() int {
300         runes := []rune(c.v.buf.lines[c.y])
301         tabSize := settings.TabSize
302         return c.x + NumOccurences(string(runes[:c.x]), '\t')*(tabSize-1)
303 }
304
305 // Display draws the cursor to the screen at the correct position
306 func (c *Cursor) Display() {
307         // Don't draw the cursor if it is out of the viewport or if it has a selection
308         if (c.y-c.v.topline < 0 || c.y-c.v.topline > c.v.height-1) || c.HasSelection() {
309                 screen.HideCursor()
310         } else {
311                 screen.ShowCursor(c.GetVisualX()+c.v.lineNumOffset-c.v.leftCol, c.y-c.v.topline)
312         }
313 }