3 // The Cursor struct stores the location of the cursor in the view
4 // The complicated part about the cursor is storing its location.
5 // The cursor must be displayed at an x, y location, but since the buffer
6 // uses a rope to store text, to insert text we must have an index. It
7 // is also simpler to use character indicies for other tasks such as
13 // Last cursor x position
16 // The current selection as a range of character numbers (inclusive)
18 // The original selection as a range of character numbers
19 // This is used for line and word selection where it is necessary
20 // to know what the original selection was
24 // Goto puts the cursor at the given cursor's location and gives the current cursor its selection too
25 func (c *Cursor) Goto(b Cursor) {
26 c.X, c.Y, c.LastVisualX = b.X, b.Y, b.LastVisualX
27 c.OrigSelection, c.CurSelection = b.OrigSelection, b.CurSelection
30 // ResetSelection resets the user's selection
31 func (c *Cursor) ResetSelection() {
32 c.CurSelection[0] = c.buf.Start()
33 c.CurSelection[1] = c.buf.Start()
36 // HasSelection returns whether or not the user has selected anything
37 func (c *Cursor) HasSelection() bool {
38 return c.CurSelection[0] != c.CurSelection[1]
41 // DeleteSelection deletes the currently selected text
42 func (c *Cursor) DeleteSelection() {
43 if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
44 c.buf.Remove(c.CurSelection[1], c.CurSelection[0])
45 c.Loc = c.CurSelection[1]
46 } else if c.GetSelection() == "" {
49 c.buf.Remove(c.CurSelection[0], c.CurSelection[1])
50 c.Loc = c.CurSelection[0]
54 // GetSelection returns the cursor's selection
55 func (c *Cursor) GetSelection() string {
56 if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
57 return c.buf.Substr(c.CurSelection[1], c.CurSelection[0])
59 return c.buf.Substr(c.CurSelection[0], c.CurSelection[1])
62 // SelectLine selects the current line
63 func (c *Cursor) SelectLine() {
65 c.CurSelection[0] = c.Loc
67 if c.buf.NumLines-1 > c.Y {
68 c.CurSelection[1] = c.Loc.Move(1, c.buf)
70 c.CurSelection[1] = c.Loc
73 c.OrigSelection = c.CurSelection
76 // AddLineToSelection adds the current line to the selection
77 func (c *Cursor) AddLineToSelection() {
78 if c.Loc.LessThan(c.OrigSelection[0]) {
80 c.CurSelection[0] = c.Loc
81 c.CurSelection[1] = c.OrigSelection[1]
83 if c.Loc.GreaterThan(c.OrigSelection[1]) {
85 c.CurSelection[1] = c.Loc.Move(1, c.buf)
86 c.CurSelection[0] = c.OrigSelection[0]
89 if c.Loc.LessThan(c.OrigSelection[1]) && c.Loc.GreaterThan(c.OrigSelection[0]) {
90 c.CurSelection = c.OrigSelection
94 // SelectWord selects the word the cursor is currently on
95 func (c *Cursor) SelectWord() {
96 if len(c.buf.Line(c.Y)) == 0 {
100 if !IsWordChar(string(c.RuneUnder(c.X))) {
101 c.CurSelection[0] = c.Loc
102 c.CurSelection[1] = c.Loc.Move(1, c.buf)
103 c.OrigSelection = c.CurSelection
107 forward, backward := c.X, c.X
109 for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
113 c.CurSelection[0] = Loc{backward, c.Y}
114 c.OrigSelection[0] = c.CurSelection[0]
116 for forward < Count(c.buf.Line(c.Y))-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
120 c.CurSelection[1] = Loc{forward, c.Y}.Move(1, c.buf)
121 c.OrigSelection[1] = c.CurSelection[1]
122 c.Loc = c.CurSelection[1]
125 // AddWordToSelection adds the word the cursor is currently on to the selection
126 func (c *Cursor) AddWordToSelection() {
127 if c.Loc.GreaterThan(c.OrigSelection[0]) && c.Loc.LessThan(c.OrigSelection[1]) {
128 c.CurSelection = c.OrigSelection
132 if c.Loc.LessThan(c.OrigSelection[0]) {
135 for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
139 c.CurSelection[0] = Loc{backward, c.Y}
140 c.CurSelection[1] = c.OrigSelection[1]
143 if c.Loc.GreaterThan(c.OrigSelection[1]) {
146 for forward < Count(c.buf.Line(c.Y))-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
150 c.CurSelection[1] = Loc{forward, c.Y}.Move(1, c.buf)
151 c.CurSelection[0] = c.OrigSelection[0]
154 c.Loc = c.CurSelection[1]
157 // SelectTo selects from the current cursor location to the given location
158 func (c *Cursor) SelectTo(loc Loc) {
159 if loc.GreaterThan(c.OrigSelection[0]) {
160 c.CurSelection[0] = c.OrigSelection[0]
161 c.CurSelection[1] = loc
163 c.CurSelection[0] = loc
164 c.CurSelection[1] = c.OrigSelection[0]
168 // WordRight moves the cursor one word to the right
169 func (c *Cursor) WordRight() {
170 for IsWhitespace(c.RuneUnder(c.X)) {
171 if c.X == Count(c.buf.Line(c.Y)) {
178 for IsWordChar(string(c.RuneUnder(c.X))) {
179 if c.X == Count(c.buf.Line(c.Y)) {
186 // WordLeft moves the cursor one word to the left
187 func (c *Cursor) WordLeft() {
189 for IsWhitespace(c.RuneUnder(c.X)) {
196 for IsWordChar(string(c.RuneUnder(c.X))) {
205 // RuneUnder returns the rune under the given x position
206 func (c *Cursor) RuneUnder(x int) rune {
207 line := []rune(c.buf.Line(c.Y))
219 // UpN moves the cursor up N lines (if possible)
220 func (c *Cursor) UpN(amount int) {
221 proposedY := c.Y - amount
224 } else if proposedY >= c.buf.NumLines {
225 proposedY = c.buf.NumLines - 1
227 if proposedY == c.Y {
232 runes := []rune(c.buf.Line(c.Y))
233 c.X = c.GetCharPosInLine(c.Y, c.LastVisualX)
234 if c.X > len(runes) {
239 // DownN moves the cursor down N lines (if possible)
240 func (c *Cursor) DownN(amount int) {
244 // Up moves the cursor up one line (if possible)
245 func (c *Cursor) Up() {
249 // Down moves the cursor down one line (if possible)
250 func (c *Cursor) Down() {
254 // Left moves the cursor left one cell (if possible) or to the last line if it is at the beginning
255 func (c *Cursor) Left() {
256 if c.Loc == c.buf.Start() {
265 c.LastVisualX = c.GetVisualX()
268 // Right moves the cursor right one cell (if possible) or to the next line if it is at the end
269 func (c *Cursor) Right() {
270 if c.Loc == c.buf.End() {
273 if c.X < Count(c.buf.Line(c.Y)) {
279 c.LastVisualX = c.GetVisualX()
282 // End moves the cursor to the end of the line it is on
283 func (c *Cursor) End() {
284 c.X = Count(c.buf.Line(c.Y))
285 c.LastVisualX = c.GetVisualX()
288 // Start moves the cursor to the start of the line it is on
289 func (c *Cursor) Start() {
291 c.LastVisualX = c.GetVisualX()
294 // GetCharPosInLine gets the char position of a visual x y coordinate (this is necessary because tabs are 1 char but 4 visual spaces)
295 func (c *Cursor) GetCharPosInLine(lineNum, visualPos int) int {
297 tabSize := int(settings["tabsize"].(float64))
298 visualLineLen := StringWidth(c.buf.Line(lineNum))
299 if visualPos > visualLineLen {
300 visualPos = visualLineLen
302 width := WidthOfLargeRunes(c.buf.Line(lineNum))
303 if visualPos >= width {
304 return visualPos - width
306 return visualPos / tabSize
309 // GetVisualX returns the x value of the cursor in visual spaces
310 func (c *Cursor) GetVisualX() int {
311 runes := []rune(c.buf.Line(c.Y))
312 return StringWidth(string(runes[:c.X]))
315 // Relocate makes sure that the cursor is inside the bounds of the buffer
316 // If it isn't, it moves it to be within the buffer's lines
317 func (c *Cursor) Relocate() {
320 } else if c.Y >= c.buf.NumLines {
321 c.Y = c.buf.NumLines - 1
326 } else if c.X > Count(c.buf.Line(c.Y)) {
327 c.X = Count(c.buf.Line(c.Y))