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
32 // the current cursor its selection too
33 func (c *Cursor) Goto(b Cursor) {
34 c.X, c.Y, c.LastVisualX = b.X, b.Y, b.LastVisualX
35 c.OrigSelection, c.CurSelection = b.OrigSelection, b.CurSelection
38 // GotoLoc puts the cursor at the given cursor's location and gives
39 // the current cursor its selection too
40 func (c *Cursor) GotoLoc(l Loc) {
42 c.LastVisualX = c.GetVisualX()
45 // CopySelection copies the user's selection to either "primary"
47 func (c *Cursor) CopySelection(target string) {
49 if target != "primary" || c.buf.Settings["useprimary"].(bool) {
50 clipboard.WriteAll(c.GetSelection(), target)
55 // ResetSelection resets the user's selection
56 func (c *Cursor) ResetSelection() {
57 c.CurSelection[0] = c.buf.Start()
58 c.CurSelection[1] = c.buf.Start()
61 // SetSelectionStart sets the start of the selection
62 func (c *Cursor) SetSelectionStart(pos Loc) {
63 c.CurSelection[0] = pos
66 // SetSelectionEnd sets the end of the selection
67 func (c *Cursor) SetSelectionEnd(pos Loc) {
68 c.CurSelection[1] = pos
71 // HasSelection returns whether or not the user has selected anything
72 func (c *Cursor) HasSelection() bool {
73 return c.CurSelection[0] != c.CurSelection[1]
76 // DeleteSelection deletes the currently selected text
77 func (c *Cursor) DeleteSelection() {
78 if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
79 c.buf.Remove(c.CurSelection[1], c.CurSelection[0])
80 c.Loc = c.CurSelection[1]
81 } else if !c.HasSelection() {
84 c.buf.Remove(c.CurSelection[0], c.CurSelection[1])
85 c.Loc = c.CurSelection[0]
89 // GetSelection returns the cursor's selection
90 func (c *Cursor) GetSelection() string {
91 if InBounds(c.CurSelection[0], c.buf) && InBounds(c.CurSelection[1], c.buf) {
92 if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
93 return c.buf.Substr(c.CurSelection[1], c.CurSelection[0])
95 return c.buf.Substr(c.CurSelection[0], c.CurSelection[1])
100 // SelectLine selects the current line
101 func (c *Cursor) SelectLine() {
103 c.SetSelectionStart(c.Loc)
105 if c.buf.NumLines-1 > c.Y {
106 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
108 c.SetSelectionEnd(c.Loc)
111 c.OrigSelection = c.CurSelection
114 // AddLineToSelection adds the current line to the selection
115 func (c *Cursor) AddLineToSelection() {
116 if c.Loc.LessThan(c.OrigSelection[0]) {
118 c.SetSelectionStart(c.Loc)
119 c.SetSelectionEnd(c.OrigSelection[1])
121 if c.Loc.GreaterThan(c.OrigSelection[1]) {
123 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
124 c.SetSelectionStart(c.OrigSelection[0])
127 if c.Loc.LessThan(c.OrigSelection[1]) && c.Loc.GreaterThan(c.OrigSelection[0]) {
128 c.CurSelection = c.OrigSelection
132 // SelectWord selects the word the cursor is currently on
133 func (c *Cursor) SelectWord() {
134 if len(c.buf.Line(c.Y)) == 0 {
138 if !IsWordChar(string(c.RuneUnder(c.X))) {
139 c.SetSelectionStart(c.Loc)
140 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
141 c.OrigSelection = c.CurSelection
145 forward, backward := c.X, c.X
147 for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
151 c.SetSelectionStart(Loc{backward, c.Y})
152 c.OrigSelection[0] = c.CurSelection[0]
154 for forward < Count(c.buf.Line(c.Y))-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
158 c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
159 c.OrigSelection[1] = c.CurSelection[1]
160 c.Loc = c.CurSelection[1]
163 // AddWordToSelection adds the word the cursor is currently on
165 func (c *Cursor) AddWordToSelection() {
166 if c.Loc.GreaterThan(c.OrigSelection[0]) && c.Loc.LessThan(c.OrigSelection[1]) {
167 c.CurSelection = c.OrigSelection
171 if c.Loc.LessThan(c.OrigSelection[0]) {
174 for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
178 c.SetSelectionStart(Loc{backward, c.Y})
179 c.SetSelectionEnd(c.OrigSelection[1])
182 if c.Loc.GreaterThan(c.OrigSelection[1]) {
185 for forward < Count(c.buf.Line(c.Y))-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
189 c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
190 c.SetSelectionStart(c.OrigSelection[0])
193 c.Loc = c.CurSelection[1]
196 // SelectTo selects from the current cursor location to the given
198 func (c *Cursor) SelectTo(loc Loc) {
199 if loc.GreaterThan(c.OrigSelection[0]) {
200 c.SetSelectionStart(c.OrigSelection[0])
201 c.SetSelectionEnd(loc)
203 c.SetSelectionStart(loc)
204 c.SetSelectionEnd(c.OrigSelection[0])
208 // WordRight moves the cursor one word to the right
209 func (c *Cursor) WordRight() {
210 for IsWhitespace(c.RuneUnder(c.X)) {
211 if c.X == Count(c.buf.Line(c.Y)) {
218 for IsWordChar(string(c.RuneUnder(c.X))) {
219 if c.X == Count(c.buf.Line(c.Y)) {
226 // WordLeft moves the cursor one word to the left
227 func (c *Cursor) WordLeft() {
229 for IsWhitespace(c.RuneUnder(c.X)) {
236 for IsWordChar(string(c.RuneUnder(c.X))) {
245 // RuneUnder returns the rune under the given x position
246 func (c *Cursor) RuneUnder(x int) rune {
247 line := []rune(c.buf.Line(c.Y))
259 // UpN moves the cursor up N lines (if possible)
260 func (c *Cursor) UpN(amount int) {
261 proposedY := c.Y - amount
265 } else if proposedY >= c.buf.NumLines {
266 proposedY = c.buf.NumLines - 1
269 runes := []rune(c.buf.Line(c.Y))
270 c.X = c.GetCharPosInLine(proposedY, c.LastVisualX)
272 if c.X > len(runes) || (amount < 0 && proposedY == c.Y) {
279 // DownN moves the cursor down N lines (if possible)
280 func (c *Cursor) DownN(amount int) {
284 // Up moves the cursor up one line (if possible)
285 func (c *Cursor) Up() {
289 // Down moves the cursor down one line (if possible)
290 func (c *Cursor) Down() {
294 // Left moves the cursor left one cell (if possible) or to
295 // the previous line if it is at the beginning
296 func (c *Cursor) Left() {
297 if c.Loc == c.buf.Start() {
306 c.LastVisualX = c.GetVisualX()
309 // Right moves the cursor right one cell (if possible) or
310 // to the next line if it is at the end
311 func (c *Cursor) Right() {
312 if c.Loc == c.buf.End() {
315 if c.X < Count(c.buf.Line(c.Y)) {
321 c.LastVisualX = c.GetVisualX()
324 // End moves the cursor to the end of the line it is on
325 func (c *Cursor) End() {
326 c.X = Count(c.buf.Line(c.Y))
327 c.LastVisualX = c.GetVisualX()
330 // Start moves the cursor to the start of the line it is on
331 func (c *Cursor) Start() {
333 c.LastVisualX = c.GetVisualX()
336 // GetCharPosInLine gets the char position of a visual x y
337 // coordinate (this is necessary because tabs are 1 char but
339 func (c *Cursor) GetCharPosInLine(lineNum, visualPos int) int {
341 tabSize := int(c.buf.Settings["tabsize"].(float64))
342 visualLineLen := StringWidth(c.buf.Line(lineNum), tabSize)
343 if visualPos > visualLineLen {
344 visualPos = visualLineLen
346 width := WidthOfLargeRunes(c.buf.Line(lineNum), tabSize)
347 if visualPos >= width {
348 return visualPos - width
350 return visualPos / tabSize
353 // GetVisualX returns the x value of the cursor in visual spaces
354 func (c *Cursor) GetVisualX() int {
355 runes := []rune(c.buf.Line(c.Y))
356 tabSize := int(c.buf.Settings["tabsize"].(float64))
357 if c.X > len(runes) {
365 return StringWidth(string(runes[:c.X]), tabSize)
368 // StoreVisualX stores the current visual x value in the cursor
369 func (c *Cursor) StoreVisualX() {
370 c.LastVisualX = c.GetVisualX()
373 // Relocate makes sure that the cursor is inside the bounds
374 // of the buffer If it isn't, it moves it to be within the
376 func (c *Cursor) Relocate() {
379 } else if c.Y >= c.buf.NumLines {
380 c.Y = c.buf.NumLines - 1
385 } else if c.X > Count(c.buf.Line(c.Y)) {
386 c.X = Count(c.buf.Line(c.Y))