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 // CopySelection copies the user's selection to either "primary" or "clipboard"
33 func (c *Cursor) CopySelection(target string) {
35 if target != "primary" || c.buf.Settings["useprimary"].(bool) {
36 clipboard.WriteAll(c.GetSelection(), target)
41 // ResetSelection resets the user's selection
42 func (c *Cursor) ResetSelection() {
43 c.CurSelection[0] = c.buf.Start()
44 c.CurSelection[1] = c.buf.Start()
47 // SetSelectionStart sets the start of the selection
48 func (c *Cursor) SetSelectionStart(pos Loc) {
49 c.CurSelection[0] = pos
52 // SetSelectionEnd sets the end of the selection
53 func (c *Cursor) SetSelectionEnd(pos Loc) {
54 c.CurSelection[1] = pos
57 // HasSelection returns whether or not the user has selected anything
58 func (c *Cursor) HasSelection() bool {
59 return c.CurSelection[0] != c.CurSelection[1]
62 // DeleteSelection deletes the currently selected text
63 func (c *Cursor) DeleteSelection() {
64 if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
65 c.buf.Remove(c.CurSelection[1], c.CurSelection[0])
66 c.Loc = c.CurSelection[1]
67 } else if !c.HasSelection() {
70 c.buf.Remove(c.CurSelection[0], c.CurSelection[1])
71 c.Loc = c.CurSelection[0]
75 // GetSelection returns the cursor's selection
76 func (c *Cursor) GetSelection() string {
77 if InBounds(c.CurSelection[0], c.buf) && InBounds(c.CurSelection[1], c.buf) {
78 if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
79 return c.buf.Substr(c.CurSelection[1], c.CurSelection[0])
81 return c.buf.Substr(c.CurSelection[0], c.CurSelection[1])
86 // SelectLine selects the current line
87 func (c *Cursor) SelectLine() {
89 c.SetSelectionStart(c.Loc)
91 if c.buf.NumLines-1 > c.Y {
92 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
94 c.SetSelectionEnd(c.Loc)
97 c.OrigSelection = c.CurSelection
100 // AddLineToSelection adds the current line to the selection
101 func (c *Cursor) AddLineToSelection() {
102 if c.Loc.LessThan(c.OrigSelection[0]) {
104 c.SetSelectionStart(c.Loc)
105 c.SetSelectionEnd(c.OrigSelection[1])
107 if c.Loc.GreaterThan(c.OrigSelection[1]) {
109 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
110 c.SetSelectionStart(c.OrigSelection[0])
113 if c.Loc.LessThan(c.OrigSelection[1]) && c.Loc.GreaterThan(c.OrigSelection[0]) {
114 c.CurSelection = c.OrigSelection
118 // SelectWord selects the word the cursor is currently on
119 func (c *Cursor) SelectWord() {
120 if len(c.buf.Line(c.Y)) == 0 {
124 if !IsWordChar(string(c.RuneUnder(c.X))) {
125 c.SetSelectionStart(c.Loc)
126 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
127 c.OrigSelection = c.CurSelection
131 forward, backward := c.X, c.X
133 for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
137 c.SetSelectionStart(Loc{backward, c.Y})
138 c.OrigSelection[0] = c.CurSelection[0]
140 for forward < Count(c.buf.Line(c.Y))-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
144 c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
145 c.OrigSelection[1] = c.CurSelection[1]
146 c.Loc = c.CurSelection[1]
149 // AddWordToSelection adds the word the cursor is currently on to the selection
150 func (c *Cursor) AddWordToSelection() {
151 if c.Loc.GreaterThan(c.OrigSelection[0]) && c.Loc.LessThan(c.OrigSelection[1]) {
152 c.CurSelection = c.OrigSelection
156 if c.Loc.LessThan(c.OrigSelection[0]) {
159 for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
163 c.SetSelectionStart(Loc{backward, c.Y})
164 c.SetSelectionEnd(c.OrigSelection[1])
167 if c.Loc.GreaterThan(c.OrigSelection[1]) {
170 for forward < Count(c.buf.Line(c.Y))-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
174 c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
175 c.SetSelectionStart(c.OrigSelection[0])
178 c.Loc = c.CurSelection[1]
181 // SelectTo selects from the current cursor location to the given location
182 func (c *Cursor) SelectTo(loc Loc) {
183 if loc.GreaterThan(c.OrigSelection[0]) {
184 c.SetSelectionStart(c.OrigSelection[0])
185 c.SetSelectionEnd(loc)
187 c.SetSelectionStart(loc)
188 c.SetSelectionEnd(c.OrigSelection[0])
192 // WordRight moves the cursor one word to the right
193 func (c *Cursor) WordRight() {
194 for IsWhitespace(c.RuneUnder(c.X)) {
195 if c.X == Count(c.buf.Line(c.Y)) {
202 for IsWordChar(string(c.RuneUnder(c.X))) {
203 if c.X == Count(c.buf.Line(c.Y)) {
210 // WordLeft moves the cursor one word to the left
211 func (c *Cursor) WordLeft() {
213 for IsWhitespace(c.RuneUnder(c.X)) {
220 for IsWordChar(string(c.RuneUnder(c.X))) {
229 // RuneUnder returns the rune under the given x position
230 func (c *Cursor) RuneUnder(x int) rune {
231 line := []rune(c.buf.Line(c.Y))
243 // UpN moves the cursor up N lines (if possible)
244 func (c *Cursor) UpN(amount int) {
245 proposedY := c.Y - amount
248 } else if proposedY >= c.buf.NumLines {
249 proposedY = c.buf.NumLines - 1
251 if proposedY == c.Y {
256 runes := []rune(c.buf.Line(c.Y))
257 c.X = c.GetCharPosInLine(c.Y, c.LastVisualX)
258 if c.X > len(runes) {
263 // DownN moves the cursor down N lines (if possible)
264 func (c *Cursor) DownN(amount int) {
268 // Up moves the cursor up one line (if possible)
269 func (c *Cursor) Up() {
273 // Down moves the cursor down one line (if possible)
274 func (c *Cursor) Down() {
278 // Left moves the cursor left one cell (if possible) or to the last line if it is at the beginning
279 func (c *Cursor) Left() {
280 if c.Loc == c.buf.Start() {
289 c.LastVisualX = c.GetVisualX()
292 // Right moves the cursor right one cell (if possible) or to the next line if it is at the end
293 func (c *Cursor) Right() {
294 if c.Loc == c.buf.End() {
297 if c.X < Count(c.buf.Line(c.Y)) {
303 c.LastVisualX = c.GetVisualX()
306 // End moves the cursor to the end of the line it is on
307 func (c *Cursor) End() {
308 c.X = Count(c.buf.Line(c.Y))
309 c.LastVisualX = c.GetVisualX()
312 // Start moves the cursor to the start of the line it is on
313 func (c *Cursor) Start() {
315 c.LastVisualX = c.GetVisualX()
318 // GetCharPosInLine gets the char position of a visual x y coordinate (this is necessary because tabs are 1 char but 4 visual spaces)
319 func (c *Cursor) GetCharPosInLine(lineNum, visualPos int) int {
321 tabSize := int(c.buf.Settings["tabsize"].(float64))
322 visualLineLen := StringWidth(c.buf.Line(lineNum), tabSize)
323 if visualPos > visualLineLen {
324 visualPos = visualLineLen
326 width := WidthOfLargeRunes(c.buf.Line(lineNum), tabSize)
327 if visualPos >= width {
328 return visualPos - width
330 return visualPos / tabSize
333 // GetVisualX returns the x value of the cursor in visual spaces
334 func (c *Cursor) GetVisualX() int {
335 runes := []rune(c.buf.Line(c.Y))
336 tabSize := int(c.buf.Settings["tabsize"].(float64))
337 return StringWidth(string(runes[:c.X]), tabSize)
340 // Relocate makes sure that the cursor is inside the bounds of the buffer
341 // If it isn't, it moves it to be within the buffer's lines
342 func (c *Cursor) Relocate() {
345 } else if c.Y >= c.buf.NumLines {
346 c.Y = c.buf.NumLines - 1
351 } else if c.X > Count(c.buf.Line(c.Y)) {
352 c.X = Count(c.buf.Line(c.Y))