4 "github.com/zyedidia/clipboard"
7 // The Cursor struct stores the location of the cursor in the view
8 // The complicated part about the cursor is storing its location.
9 // The cursor must be displayed at an x, y location, but since the buffer
10 // uses a rope to store text, to insert text we must have an index. It
11 // is also simpler to use character indicies for other tasks such as
17 // Last cursor x position
20 // The current selection as a range of character numbers (inclusive)
22 // The original selection as a range of character numbers
23 // This is used for line and word selection where it is necessary
24 // to know what the original selection was
27 // Which cursor index is this (for multiple cursors)
31 // Goto puts the cursor at the given cursor's location and gives the current cursor its selection too
32 func (c *Cursor) Goto(b Cursor) {
33 c.X, c.Y, c.LastVisualX = b.X, b.Y, b.LastVisualX
34 c.OrigSelection, c.CurSelection = b.OrigSelection, b.CurSelection
37 // CopySelection copies the user's selection to either "primary" or "clipboard"
38 func (c *Cursor) CopySelection(target string) {
40 if target != "primary" || c.buf.Settings["useprimary"].(bool) {
41 clipboard.WriteAll(c.GetSelection(), target)
46 // ResetSelection resets the user's selection
47 func (c *Cursor) ResetSelection() {
48 c.CurSelection[0] = c.buf.Start()
49 c.CurSelection[1] = c.buf.Start()
52 // SetSelectionStart sets the start of the selection
53 func (c *Cursor) SetSelectionStart(pos Loc) {
54 c.CurSelection[0] = pos
57 // SetSelectionEnd sets the end of the selection
58 func (c *Cursor) SetSelectionEnd(pos Loc) {
59 c.CurSelection[1] = pos
62 // HasSelection returns whether or not the user has selected anything
63 func (c *Cursor) HasSelection() bool {
64 return c.CurSelection[0] != c.CurSelection[1]
67 // DeleteSelection deletes the currently selected text
68 func (c *Cursor) DeleteSelection() {
69 if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
70 c.buf.Remove(c.CurSelection[1], c.CurSelection[0])
71 c.Loc = c.CurSelection[1]
72 } else if !c.HasSelection() {
75 c.buf.Remove(c.CurSelection[0], c.CurSelection[1])
76 c.Loc = c.CurSelection[0]
80 // GetSelection returns the cursor's selection
81 func (c *Cursor) GetSelection() string {
82 if InBounds(c.CurSelection[0], c.buf) && InBounds(c.CurSelection[1], c.buf) {
83 if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
84 return c.buf.Substr(c.CurSelection[1], c.CurSelection[0])
86 return c.buf.Substr(c.CurSelection[0], c.CurSelection[1])
91 // SelectLine selects the current line
92 func (c *Cursor) SelectLine() {
94 c.SetSelectionStart(c.Loc)
96 if c.buf.NumLines-1 > c.Y {
97 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
99 c.SetSelectionEnd(c.Loc)
102 c.OrigSelection = c.CurSelection
105 // AddLineToSelection adds the current line to the selection
106 func (c *Cursor) AddLineToSelection() {
107 if c.Loc.LessThan(c.OrigSelection[0]) {
109 c.SetSelectionStart(c.Loc)
110 c.SetSelectionEnd(c.OrigSelection[1])
112 if c.Loc.GreaterThan(c.OrigSelection[1]) {
114 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
115 c.SetSelectionStart(c.OrigSelection[0])
118 if c.Loc.LessThan(c.OrigSelection[1]) && c.Loc.GreaterThan(c.OrigSelection[0]) {
119 c.CurSelection = c.OrigSelection
123 // SelectWord selects the word the cursor is currently on
124 func (c *Cursor) SelectWord() {
125 if len(c.buf.Line(c.Y)) == 0 {
129 if !IsWordChar(string(c.RuneUnder(c.X))) {
130 c.SetSelectionStart(c.Loc)
131 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
132 c.OrigSelection = c.CurSelection
136 forward, backward := c.X, c.X
138 for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
142 c.SetSelectionStart(Loc{backward, c.Y})
143 c.OrigSelection[0] = c.CurSelection[0]
145 for forward < Count(c.buf.Line(c.Y))-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
149 c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
150 c.OrigSelection[1] = c.CurSelection[1]
151 c.Loc = c.CurSelection[1]
154 // AddWordToSelection adds the word the cursor is currently on to the selection
155 func (c *Cursor) AddWordToSelection() {
156 if c.Loc.GreaterThan(c.OrigSelection[0]) && c.Loc.LessThan(c.OrigSelection[1]) {
157 c.CurSelection = c.OrigSelection
161 if c.Loc.LessThan(c.OrigSelection[0]) {
164 for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
168 c.SetSelectionStart(Loc{backward, c.Y})
169 c.SetSelectionEnd(c.OrigSelection[1])
172 if c.Loc.GreaterThan(c.OrigSelection[1]) {
175 for forward < Count(c.buf.Line(c.Y))-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
179 c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
180 c.SetSelectionStart(c.OrigSelection[0])
183 c.Loc = c.CurSelection[1]
186 // SelectTo selects from the current cursor location to the given location
187 func (c *Cursor) SelectTo(loc Loc) {
188 if loc.GreaterThan(c.OrigSelection[0]) {
189 c.SetSelectionStart(c.OrigSelection[0])
190 c.SetSelectionEnd(loc)
192 c.SetSelectionStart(loc)
193 c.SetSelectionEnd(c.OrigSelection[0])
197 // WordRight moves the cursor one word to the right
198 func (c *Cursor) WordRight() {
199 for IsWhitespace(c.RuneUnder(c.X)) {
200 if c.X == Count(c.buf.Line(c.Y)) {
207 for IsWordChar(string(c.RuneUnder(c.X))) {
208 if c.X == Count(c.buf.Line(c.Y)) {
215 // WordLeft moves the cursor one word to the left
216 func (c *Cursor) WordLeft() {
218 for IsWhitespace(c.RuneUnder(c.X)) {
225 for IsWordChar(string(c.RuneUnder(c.X))) {
234 // RuneUnder returns the rune under the given x position
235 func (c *Cursor) RuneUnder(x int) rune {
236 line := []rune(c.buf.Line(c.Y))
248 // UpN moves the cursor up N lines (if possible)
249 func (c *Cursor) UpN(amount int) {
250 proposedY := c.Y - amount
254 } else if proposedY >= c.buf.NumLines {
255 proposedY = c.buf.NumLines - 1
258 runes := []rune(c.buf.Line(c.Y))
259 c.X = c.GetCharPosInLine(proposedY, c.LastVisualX)
261 if c.X > len(runes) || (amount < 0 && proposedY == c.Y) {
268 // DownN moves the cursor down N lines (if possible)
269 func (c *Cursor) DownN(amount int) {
273 // Up moves the cursor up one line (if possible)
274 func (c *Cursor) Up() {
278 // Down moves the cursor down one line (if possible)
279 func (c *Cursor) Down() {
283 // Left moves the cursor left one cell (if possible) or to the last line if it is at the beginning
284 func (c *Cursor) Left() {
285 if c.Loc == c.buf.Start() {
294 c.LastVisualX = c.GetVisualX()
297 // Right moves the cursor right one cell (if possible) or to the next line if it is at the end
298 func (c *Cursor) Right() {
299 if c.Loc == c.buf.End() {
302 if c.X < Count(c.buf.Line(c.Y)) {
308 c.LastVisualX = c.GetVisualX()
311 // End moves the cursor to the end of the line it is on
312 func (c *Cursor) End() {
313 c.X = Count(c.buf.Line(c.Y))
314 c.LastVisualX = c.GetVisualX()
317 // Start moves the cursor to the start of the line it is on
318 func (c *Cursor) Start() {
320 c.LastVisualX = c.GetVisualX()
323 // GetCharPosInLine gets the char position of a visual x y coordinate (this is necessary because tabs are 1 char but 4 visual spaces)
324 func (c *Cursor) GetCharPosInLine(lineNum, visualPos int) int {
326 tabSize := int(c.buf.Settings["tabsize"].(float64))
327 visualLineLen := StringWidth(c.buf.Line(lineNum), tabSize)
328 if visualPos > visualLineLen {
329 visualPos = visualLineLen
331 width := WidthOfLargeRunes(c.buf.Line(lineNum), tabSize)
332 if visualPos >= width {
333 return visualPos - width
335 return visualPos / tabSize
338 // GetVisualX returns the x value of the cursor in visual spaces
339 func (c *Cursor) GetVisualX() int {
340 runes := []rune(c.buf.Line(c.Y))
341 tabSize := int(c.buf.Settings["tabsize"].(float64))
342 if c.X > len(runes) {
350 return StringWidth(string(runes[:c.X]), tabSize)
353 // StoreVisualX stores the current visual x value in the cursor
354 func (c *Cursor) StoreVisualX() {
355 c.LastVisualX = c.GetVisualX()
358 // Relocate makes sure that the cursor is inside the bounds of the buffer
359 // If it isn't, it moves it to be within the buffer's lines
360 func (c *Cursor) Relocate() {
363 } else if c.Y >= c.buf.NumLines {
364 c.Y = c.buf.NumLines - 1
369 } else if c.X > Count(c.buf.Line(c.Y)) {
370 c.X = Count(c.buf.Line(c.Y))