6 runewidth "github.com/mattn/go-runewidth"
7 "github.com/zyedidia/clipboard"
8 "github.com/zyedidia/micro/cmd/micro/util"
11 // InBounds returns whether the given location is a valid character position in the given buffer
12 func InBounds(pos Loc, buf *Buffer) bool {
13 if pos.Y < 0 || pos.Y >= len(buf.lines) || pos.X < 0 || pos.X > utf8.RuneCount(buf.LineBytes(pos.Y)) {
20 // The Cursor struct stores the location of the cursor in the buffer
21 // as well as the selection
26 // Last cursor x position
29 // The current selection as a range of character numbers (inclusive)
31 // The original selection as a range of character numbers
32 // This is used for line and word selection where it is necessary
33 // to know what the original selection was
36 // Which cursor index is this (for multiple cursors)
40 // Goto puts the cursor at the given cursor's location and gives
41 // the current cursor its selection too
42 func (c *Cursor) Goto(b Cursor) {
43 c.X, c.Y, c.LastVisualX = b.X, b.Y, b.LastVisualX
44 c.OrigSelection, c.CurSelection = b.OrigSelection, b.CurSelection
47 // GotoLoc puts the cursor at the given cursor's location and gives
48 // the current cursor its selection too
49 func (c *Cursor) GotoLoc(l Loc) {
51 c.LastVisualX = c.GetVisualX()
54 // GetVisualX returns the x value of the cursor in visual spaces
55 func (c *Cursor) GetVisualX() int {
61 bytes := c.Buf.LineBytes(c.Y)
62 tabsize := int(c.Buf.Settings["tabsize"].(float64))
63 if c.X > utf8.RuneCount(bytes) {
64 c.X = utf8.RuneCount(bytes) - 1
67 return util.StringWidth(bytes, c.X, tabsize)
70 // GetCharPosInLine gets the char position of a visual x y
71 // coordinate (this is necessary because tabs are 1 char but
73 func (c *Cursor) GetCharPosInLine(b []byte, visualPos int) int {
74 tabsize := int(c.Buf.Settings["tabsize"].(float64))
76 // Scan rune by rune until we exceed the visual width that we are
77 // looking for. Then we can return the character position we have found
79 width := 0 // string visual width
81 r, size := utf8.DecodeRune(b)
86 ts := tabsize - (width % tabsize)
89 width += runewidth.RuneWidth(r)
94 if width >= visualPos {
102 // Start moves the cursor to the start of the line it is on
103 func (c *Cursor) Start() {
105 c.LastVisualX = c.GetVisualX()
108 // End moves the cursor to the end of the line it is on
109 func (c *Cursor) End() {
110 c.X = utf8.RuneCount(c.Buf.LineBytes(c.Y))
111 c.LastVisualX = c.GetVisualX()
114 // CopySelection copies the user's selection to either "primary"
116 func (c *Cursor) CopySelection(target string) {
117 if c.HasSelection() {
118 if target != "primary" || c.Buf.Settings["useprimary"].(bool) {
119 clipboard.WriteAll(string(c.GetSelection()), target)
124 // ResetSelection resets the user's selection
125 func (c *Cursor) ResetSelection() {
126 c.CurSelection[0] = c.Buf.Start()
127 c.CurSelection[1] = c.Buf.Start()
130 // SetSelectionStart sets the start of the selection
131 func (c *Cursor) SetSelectionStart(pos Loc) {
132 c.CurSelection[0] = pos
135 // SetSelectionEnd sets the end of the selection
136 func (c *Cursor) SetSelectionEnd(pos Loc) {
137 c.CurSelection[1] = pos
140 // HasSelection returns whether or not the user has selected anything
141 func (c *Cursor) HasSelection() bool {
142 return c.CurSelection[0] != c.CurSelection[1]
145 // DeleteSelection deletes the currently selected text
146 func (c *Cursor) DeleteSelection() {
147 if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
148 c.Buf.Remove(c.CurSelection[1], c.CurSelection[0])
149 c.Loc = c.CurSelection[1]
150 } else if !c.HasSelection() {
153 c.Buf.Remove(c.CurSelection[0], c.CurSelection[1])
154 c.Loc = c.CurSelection[0]
158 // GetSelection returns the cursor's selection
159 func (c *Cursor) GetSelection() []byte {
160 if InBounds(c.CurSelection[0], c.Buf) && InBounds(c.CurSelection[1], c.Buf) {
161 if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
162 return c.Buf.Substr(c.CurSelection[1], c.CurSelection[0])
164 return c.Buf.Substr(c.CurSelection[0], c.CurSelection[1])
169 // SelectLine selects the current line
170 func (c *Cursor) SelectLine() {
172 c.SetSelectionStart(c.Loc)
174 if len(c.Buf.lines)-1 > c.Y {
175 c.SetSelectionEnd(c.Loc.Move(1, c.Buf))
177 c.SetSelectionEnd(c.Loc)
180 c.OrigSelection = c.CurSelection
183 // AddLineToSelection adds the current line to the selection
184 func (c *Cursor) AddLineToSelection() {
185 if c.Loc.LessThan(c.OrigSelection[0]) {
187 c.SetSelectionStart(c.Loc)
188 c.SetSelectionEnd(c.OrigSelection[1])
190 if c.Loc.GreaterThan(c.OrigSelection[1]) {
192 c.SetSelectionEnd(c.Loc.Move(1, c.Buf))
193 c.SetSelectionStart(c.OrigSelection[0])
196 if c.Loc.LessThan(c.OrigSelection[1]) && c.Loc.GreaterThan(c.OrigSelection[0]) {
197 c.CurSelection = c.OrigSelection
201 // UpN moves the cursor up N lines (if possible)
202 func (c *Cursor) UpN(amount int) {
203 proposedY := c.Y - amount
206 } else if proposedY >= len(c.Buf.lines) {
207 proposedY = len(c.Buf.lines) - 1
210 bytes := c.Buf.LineBytes(proposedY)
211 c.X = c.GetCharPosInLine(bytes, c.LastVisualX)
213 if c.X > utf8.RuneCount(bytes) || (amount < 0 && proposedY == c.Y) {
214 c.X = utf8.RuneCount(bytes)
220 // DownN moves the cursor down N lines (if possible)
221 func (c *Cursor) DownN(amount int) {
225 // Up moves the cursor up one line (if possible)
226 func (c *Cursor) Up() {
230 // Down moves the cursor down one line (if possible)
231 func (c *Cursor) Down() {
235 // Left moves the cursor left one cell (if possible) or to
236 // the previous line if it is at the beginning
237 func (c *Cursor) Left() {
238 if c.Loc == c.Buf.Start() {
247 c.LastVisualX = c.GetVisualX()
250 // Right moves the cursor right one cell (if possible) or
251 // to the next line if it is at the end
252 func (c *Cursor) Right() {
253 if c.Loc == c.Buf.End() {
256 if c.X < utf8.RuneCount(c.Buf.LineBytes(c.Y)) {
262 c.LastVisualX = c.GetVisualX()
265 // Relocate makes sure that the cursor is inside the bounds
266 // of the buffer If it isn't, it moves it to be within the
268 func (c *Cursor) Relocate() {
271 } else if c.Y >= len(c.Buf.lines) {
272 c.Y = len(c.Buf.lines) - 1
277 } else if c.X > utf8.RuneCount(c.Buf.LineBytes(c.Y)) {
278 c.X = utf8.RuneCount(c.Buf.LineBytes(c.Y))