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() {
171 for IsWhitespace(c.RuneUnder(c.X)) {
172 if c.X == Count(c.buf.Line(c.Y)) {
177 for !IsWhitespace(c.RuneUnder(c.X)) {
178 if c.X == Count(c.buf.Line(c.Y)) {
185 // WordLeft moves the cursor one word to the left
186 func (c *Cursor) WordLeft() {
188 for IsWhitespace(c.RuneUnder(c.X)) {
194 for !IsWhitespace(c.RuneUnder(c.X)) {
203 // RuneUnder returns the rune under the given x position
204 func (c *Cursor) RuneUnder(x int) rune {
205 line := []rune(c.buf.Line(c.Y))
217 // UpN moves the cursor up N lines (if possible)
218 func (c *Cursor) UpN(amount int) {
219 proposedY := c.Y - amount
222 } else if proposedY >= c.buf.NumLines {
223 proposedY = c.buf.NumLines - 1
225 if proposedY == c.Y {
230 runes := []rune(c.buf.Line(c.Y))
231 c.X = c.GetCharPosInLine(c.Y, c.LastVisualX)
232 if c.X > len(runes) {
237 // DownN moves the cursor down N lines (if possible)
238 func (c *Cursor) DownN(amount int) {
242 // Up moves the cursor up one line (if possible)
243 func (c *Cursor) Up() {
247 // Down moves the cursor down one line (if possible)
248 func (c *Cursor) Down() {
252 // Left moves the cursor left one cell (if possible) or to the last line if it is at the beginning
253 func (c *Cursor) Left() {
254 if c.Loc == c.buf.Start() {
263 c.LastVisualX = c.GetVisualX()
266 // Right moves the cursor right one cell (if possible) or to the next line if it is at the end
267 func (c *Cursor) Right() {
268 if c.Loc == c.buf.End() {
271 if c.X < Count(c.buf.Line(c.Y)) {
277 c.LastVisualX = c.GetVisualX()
280 // End moves the cursor to the end of the line it is on
281 func (c *Cursor) End() {
282 c.X = Count(c.buf.Line(c.Y))
283 c.LastVisualX = c.GetVisualX()
286 // Start moves the cursor to the start of the line it is on
287 func (c *Cursor) Start() {
289 c.LastVisualX = c.GetVisualX()
292 // GetCharPosInLine gets the char position of a visual x y coordinate (this is necessary because tabs are 1 char but 4 visual spaces)
293 func (c *Cursor) GetCharPosInLine(lineNum, visualPos int) int {
295 tabSize := int(settings["tabsize"].(float64))
296 // This is the visual line -- every \t replaced with the correct number of spaces
297 visualLineLen := StringWidth(c.buf.Line(lineNum))
298 if visualPos > visualLineLen {
299 visualPos = visualLineLen
301 width := WidthOfLargeRunes(c.buf.Line(lineNum))
302 if visualPos >= width {
303 return visualPos - width
305 return visualPos / tabSize
308 // GetVisualX returns the x value of the cursor in visual spaces
309 func (c *Cursor) GetVisualX() int {
310 runes := []rune(c.buf.Line(c.Y))
311 return StringWidth(string(runes[:c.X]))
314 // Relocate makes sure that the cursor is inside the bounds of the buffer
315 // If it isn't, it moves it to be within the buffer's lines
316 func (c *Cursor) Relocate() {
319 } else if c.Y >= c.buf.NumLines {
320 c.Y = c.buf.NumLines - 1
325 } else if c.X > Count(c.buf.Line(c.Y)) {
326 c.X = Count(c.buf.Line(c.Y))