3 import "github.com/zyedidia/clipboard"
5 // The Cursor struct stores the location of the cursor in the view
6 // The complicated part about the cursor is storing its location.
7 // The cursor must be displayed at an x, y location, but since the buffer
8 // uses a rope to store text, to insert text we must have an index. It
9 // is also simpler to use character indicies for other tasks such as
15 // Last cursor x position
18 // The current selection as a range of character numbers (inclusive)
20 // The original selection as a range of character numbers
21 // This is used for line and word selection where it is necessary
22 // to know what the original selection was
26 // Goto puts the cursor at the given cursor's location and gives the current cursor its selection too
27 func (c *Cursor) Goto(b Cursor) {
28 c.X, c.Y, c.LastVisualX = b.X, b.Y, b.LastVisualX
29 c.OrigSelection, c.CurSelection = b.OrigSelection, b.CurSelection
32 // ResetSelection resets the user's selection
33 func (c *Cursor) ResetSelection() {
34 c.CurSelection[0] = c.buf.Start()
35 c.CurSelection[1] = c.buf.Start()
38 // SetSelectionStart sets the start of the selection
39 func (c *Cursor) SetSelectionStart(pos Loc) {
40 c.CurSelection[0] = pos
41 // Copy to primary clipboard for linux
43 clipboard.WriteAll(c.GetSelection(), "primary")
47 // SetSelectionEnd sets the end of the selection
48 func (c *Cursor) SetSelectionEnd(pos Loc) {
49 c.CurSelection[1] = pos
50 // Copy to primary clipboard for linux
52 clipboard.WriteAll(c.GetSelection(), "primary")
56 // HasSelection returns whether or not the user has selected anything
57 func (c *Cursor) HasSelection() bool {
58 return c.CurSelection[0] != c.CurSelection[1]
61 // DeleteSelection deletes the currently selected text
62 func (c *Cursor) DeleteSelection() {
63 if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
64 c.buf.Remove(c.CurSelection[1], c.CurSelection[0])
65 c.Loc = c.CurSelection[1]
66 } else if !c.HasSelection() {
69 c.buf.Remove(c.CurSelection[0], c.CurSelection[1])
70 c.Loc = c.CurSelection[0]
74 // GetSelection returns the cursor's selection
75 func (c *Cursor) GetSelection() string {
76 if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
77 return c.buf.Substr(c.CurSelection[1], c.CurSelection[0])
79 return c.buf.Substr(c.CurSelection[0], c.CurSelection[1])
82 // SelectLine selects the current line
83 func (c *Cursor) SelectLine() {
85 c.SetSelectionStart(c.Loc)
87 if c.buf.NumLines-1 > c.Y {
88 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
90 c.SetSelectionEnd(c.Loc)
93 c.OrigSelection = c.CurSelection
96 // AddLineToSelection adds the current line to the selection
97 func (c *Cursor) AddLineToSelection() {
98 if c.Loc.LessThan(c.OrigSelection[0]) {
100 c.SetSelectionStart(c.Loc)
101 c.SetSelectionEnd(c.OrigSelection[1])
103 if c.Loc.GreaterThan(c.OrigSelection[1]) {
105 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
106 c.SetSelectionStart(c.OrigSelection[0])
109 if c.Loc.LessThan(c.OrigSelection[1]) && c.Loc.GreaterThan(c.OrigSelection[0]) {
110 c.CurSelection = c.OrigSelection
114 // SelectWord selects the word the cursor is currently on
115 func (c *Cursor) SelectWord() {
116 if len(c.buf.Line(c.Y)) == 0 {
120 if !IsWordChar(string(c.RuneUnder(c.X))) {
121 c.SetSelectionStart(c.Loc)
122 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
123 c.OrigSelection = c.CurSelection
127 forward, backward := c.X, c.X
129 for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
133 c.SetSelectionStart(Loc{backward, c.Y})
134 c.OrigSelection[0] = c.CurSelection[0]
136 for forward < Count(c.buf.Line(c.Y))-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
140 c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
141 c.OrigSelection[1] = c.CurSelection[1]
142 c.Loc = c.CurSelection[1]
145 // AddWordToSelection adds the word the cursor is currently on to the selection
146 func (c *Cursor) AddWordToSelection() {
147 if c.Loc.GreaterThan(c.OrigSelection[0]) && c.Loc.LessThan(c.OrigSelection[1]) {
148 c.CurSelection = c.OrigSelection
152 if c.Loc.LessThan(c.OrigSelection[0]) {
155 for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
159 c.SetSelectionStart(Loc{backward, c.Y})
160 c.SetSelectionEnd(c.OrigSelection[1])
163 if c.Loc.GreaterThan(c.OrigSelection[1]) {
166 for forward < Count(c.buf.Line(c.Y))-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
170 c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
171 c.SetSelectionStart(c.OrigSelection[0])
174 c.Loc = c.CurSelection[1]
177 // SelectTo selects from the current cursor location to the given location
178 func (c *Cursor) SelectTo(loc Loc) {
179 if loc.GreaterThan(c.OrigSelection[0]) {
180 c.SetSelectionStart(c.OrigSelection[0])
181 c.SetSelectionEnd(loc)
183 c.SetSelectionStart(loc)
184 c.SetSelectionEnd(c.OrigSelection[0])
188 // WordRight moves the cursor one word to the right
189 func (c *Cursor) WordRight() {
190 for IsWhitespace(c.RuneUnder(c.X)) {
191 if c.X == Count(c.buf.Line(c.Y)) {
198 for IsWordChar(string(c.RuneUnder(c.X))) {
199 if c.X == Count(c.buf.Line(c.Y)) {
206 // WordLeft moves the cursor one word to the left
207 func (c *Cursor) WordLeft() {
209 for IsWhitespace(c.RuneUnder(c.X)) {
216 for IsWordChar(string(c.RuneUnder(c.X))) {
225 // RuneUnder returns the rune under the given x position
226 func (c *Cursor) RuneUnder(x int) rune {
227 line := []rune(c.buf.Line(c.Y))
239 // UpN moves the cursor up N lines (if possible)
240 func (c *Cursor) UpN(amount int) {
241 proposedY := c.Y - amount
244 } else if proposedY >= c.buf.NumLines {
245 proposedY = c.buf.NumLines - 1
247 if proposedY == c.Y {
252 runes := []rune(c.buf.Line(c.Y))
253 c.X = c.GetCharPosInLine(c.Y, c.LastVisualX)
254 if c.X > len(runes) {
259 // DownN moves the cursor down N lines (if possible)
260 func (c *Cursor) DownN(amount int) {
264 // Up moves the cursor up one line (if possible)
265 func (c *Cursor) Up() {
269 // Down moves the cursor down one line (if possible)
270 func (c *Cursor) Down() {
274 // Left moves the cursor left one cell (if possible) or to the last line if it is at the beginning
275 func (c *Cursor) Left() {
276 if c.Loc == c.buf.Start() {
285 c.LastVisualX = c.GetVisualX()
288 // Right moves the cursor right one cell (if possible) or to the next line if it is at the end
289 func (c *Cursor) Right() {
290 if c.Loc == c.buf.End() {
293 if c.X < Count(c.buf.Line(c.Y)) {
299 c.LastVisualX = c.GetVisualX()
302 // End moves the cursor to the end of the line it is on
303 func (c *Cursor) End() {
304 c.X = Count(c.buf.Line(c.Y))
305 c.LastVisualX = c.GetVisualX()
308 // Start moves the cursor to the start of the line it is on
309 func (c *Cursor) Start() {
311 c.LastVisualX = c.GetVisualX()
314 // GetCharPosInLine gets the char position of a visual x y coordinate (this is necessary because tabs are 1 char but 4 visual spaces)
315 func (c *Cursor) GetCharPosInLine(lineNum, visualPos int) int {
317 tabSize := int(c.buf.Settings["tabsize"].(float64))
318 visualLineLen := StringWidth(c.buf.Line(lineNum), tabSize)
319 if visualPos > visualLineLen {
320 visualPos = visualLineLen
322 width := WidthOfLargeRunes(c.buf.Line(lineNum), tabSize)
323 if visualPos >= width {
324 return visualPos - width
326 return visualPos / tabSize
329 // GetVisualX returns the x value of the cursor in visual spaces
330 func (c *Cursor) GetVisualX() int {
331 runes := []rune(c.buf.Line(c.Y))
332 tabSize := int(c.buf.Settings["tabsize"].(float64))
333 return StringWidth(string(runes[:c.X]), tabSize)
337 // Relocate makes sure that the cursor is inside the bounds of the buffer
338 // If it isn't, it moves it to be within the buffer's lines
339 func (c *Cursor) Relocate() {
342 } else if c.Y >= c.buf.NumLines {
343 c.Y = c.buf.NumLines - 1
348 } else if c.X > Count(c.buf.Line(c.Y)) {
349 c.X = Count(c.buf.Line(c.Y))