6 "github.com/zyedidia/clipboard"
7 "github.com/zyedidia/micro/internal/util"
10 // InBounds returns whether the given location is a valid character position in the given buffer
11 func InBounds(pos Loc, buf *Buffer) bool {
12 if pos.Y < 0 || pos.Y >= len(buf.lines) || pos.X < 0 || pos.X > utf8.RuneCount(buf.LineBytes(pos.Y)) {
19 // The Cursor struct stores the location of the cursor in the buffer
20 // as well as the selection
25 // Last cursor x position
28 // The current selection as a range of character numbers (inclusive)
30 // The original selection as a range of character numbers
31 // This is used for line and word selection where it is necessary
32 // to know what the original selection was
35 // Which cursor index is this (for multiple cursors)
39 func NewCursor(b *Buffer, l Loc) *Cursor {
48 func (c *Cursor) SetBuf(b *Buffer) {
52 func (c *Cursor) Buf() *Buffer {
56 // Goto puts the cursor at the given cursor's location and gives
57 // the current cursor its selection too
58 func (c *Cursor) Goto(b Cursor) {
59 c.X, c.Y, c.LastVisualX = b.X, b.Y, b.LastVisualX
60 c.OrigSelection, c.CurSelection = b.OrigSelection, b.CurSelection
63 // GotoLoc puts the cursor at the given cursor's location and gives
64 // the current cursor its selection too
65 func (c *Cursor) GotoLoc(l Loc) {
70 // GetVisualX returns the x value of the cursor in visual spaces
71 func (c *Cursor) GetVisualX() int {
77 bytes := c.buf.LineBytes(c.Y)
78 tabsize := int(c.buf.Settings["tabsize"].(float64))
79 if c.X > utf8.RuneCount(bytes) {
80 c.X = utf8.RuneCount(bytes) - 1
83 return util.StringWidth(bytes, c.X, tabsize)
86 // GetCharPosInLine gets the char position of a visual x y
87 // coordinate (this is necessary because tabs are 1 char but
89 func (c *Cursor) GetCharPosInLine(b []byte, visualPos int) int {
90 tabsize := int(c.buf.Settings["tabsize"].(float64))
91 return util.GetCharPosInLine(b, visualPos, tabsize)
94 // Start moves the cursor to the start of the line it is on
95 func (c *Cursor) Start() {
97 c.LastVisualX = c.GetVisualX()
100 // StartOfText moves the cursor to the first non-whitespace rune of
102 func (c *Cursor) StartOfText() {
104 for util.IsWhitespace(c.RuneUnder(c.X)) {
105 if c.X == utf8.RuneCount(c.buf.LineBytes(c.Y)) {
112 // IsStartOfText returns whether the cursor is at the first
113 // non-whitespace rune of the line it is on
114 func (c *Cursor) IsStartOfText() bool {
116 for util.IsWhitespace(c.RuneUnder(x)) {
117 if x == utf8.RuneCount(c.buf.LineBytes(c.Y)) {
125 // End moves the cursor to the end of the line it is on
126 func (c *Cursor) End() {
127 c.X = utf8.RuneCount(c.buf.LineBytes(c.Y))
128 c.LastVisualX = c.GetVisualX()
131 // CopySelection copies the user's selection to either "primary"
133 func (c *Cursor) CopySelection(target string) {
134 if c.HasSelection() {
135 if target != "primary" || c.buf.Settings["useprimary"].(bool) {
136 clipboard.WriteAll(string(c.GetSelection()), target)
141 // ResetSelection resets the user's selection
142 func (c *Cursor) ResetSelection() {
143 c.CurSelection[0] = c.buf.Start()
144 c.CurSelection[1] = c.buf.Start()
147 // SetSelectionStart sets the start of the selection
148 func (c *Cursor) SetSelectionStart(pos Loc) {
149 c.CurSelection[0] = pos
152 // SetSelectionEnd sets the end of the selection
153 func (c *Cursor) SetSelectionEnd(pos Loc) {
154 c.CurSelection[1] = pos
157 // HasSelection returns whether or not the user has selected anything
158 func (c *Cursor) HasSelection() bool {
159 return c.CurSelection[0] != c.CurSelection[1]
162 // DeleteSelection deletes the currently selected text
163 func (c *Cursor) DeleteSelection() {
164 if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
165 c.buf.Remove(c.CurSelection[1], c.CurSelection[0])
166 c.Loc = c.CurSelection[1]
167 } else if !c.HasSelection() {
170 c.buf.Remove(c.CurSelection[0], c.CurSelection[1])
171 c.Loc = c.CurSelection[0]
175 // Deselect closes the cursor's current selection
176 // Start indicates whether the cursor should be placed
177 // at the start or end of the selection
178 func (c *Cursor) Deselect(start bool) {
179 if c.HasSelection() {
181 c.Loc = c.CurSelection[0]
183 c.Loc = c.CurSelection[1].Move(-1, c.buf)
190 // GetSelection returns the cursor's selection
191 func (c *Cursor) GetSelection() []byte {
192 if InBounds(c.CurSelection[0], c.buf) && InBounds(c.CurSelection[1], c.buf) {
193 if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
194 return c.buf.Substr(c.CurSelection[1], c.CurSelection[0])
196 return c.buf.Substr(c.CurSelection[0], c.CurSelection[1])
201 // SelectLine selects the current line
202 func (c *Cursor) SelectLine() {
204 c.SetSelectionStart(c.Loc)
206 if len(c.buf.lines)-1 > c.Y {
207 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
209 c.SetSelectionEnd(c.Loc)
212 c.OrigSelection = c.CurSelection
215 // AddLineToSelection adds the current line to the selection
216 func (c *Cursor) AddLineToSelection() {
217 if c.Loc.LessThan(c.OrigSelection[0]) {
219 c.SetSelectionStart(c.Loc)
220 c.SetSelectionEnd(c.OrigSelection[1])
222 if c.Loc.GreaterThan(c.OrigSelection[1]) {
224 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
225 c.SetSelectionStart(c.OrigSelection[0])
228 if c.Loc.LessThan(c.OrigSelection[1]) && c.Loc.GreaterThan(c.OrigSelection[0]) {
229 c.CurSelection = c.OrigSelection
233 // UpN moves the cursor up N lines (if possible)
234 func (c *Cursor) UpN(amount int) {
235 proposedY := c.Y - amount
238 } else if proposedY >= len(c.buf.lines) {
239 proposedY = len(c.buf.lines) - 1
242 bytes := c.buf.LineBytes(proposedY)
243 c.X = c.GetCharPosInLine(bytes, c.LastVisualX)
245 if c.X > utf8.RuneCount(bytes) || (amount < 0 && proposedY == c.Y) {
246 c.X = utf8.RuneCount(bytes)
252 // DownN moves the cursor down N lines (if possible)
253 func (c *Cursor) DownN(amount int) {
257 // Up moves the cursor up one line (if possible)
258 func (c *Cursor) Up() {
262 // Down moves the cursor down one line (if possible)
263 func (c *Cursor) Down() {
267 // Left moves the cursor left one cell (if possible) or to
268 // the previous line if it is at the beginning
269 func (c *Cursor) Left() {
270 if c.Loc == c.buf.Start() {
282 // Right moves the cursor right one cell (if possible) or
283 // to the next line if it is at the end
284 func (c *Cursor) Right() {
285 if c.Loc == c.buf.End() {
288 if c.X < utf8.RuneCount(c.buf.LineBytes(c.Y)) {
297 // Relocate makes sure that the cursor is inside the bounds
298 // of the buffer If it isn't, it moves it to be within the
300 func (c *Cursor) Relocate() {
303 } else if c.Y >= len(c.buf.lines) {
304 c.Y = len(c.buf.lines) - 1
309 } else if c.X > utf8.RuneCount(c.buf.LineBytes(c.Y)) {
310 c.X = utf8.RuneCount(c.buf.LineBytes(c.Y))
314 // SelectWord selects the word the cursor is currently on
315 func (c *Cursor) SelectWord() {
316 if len(c.buf.LineBytes(c.Y)) == 0 {
320 if !util.IsWordChar(c.RuneUnder(c.X)) {
321 c.SetSelectionStart(c.Loc)
322 c.SetSelectionEnd(c.Loc.Move(1, c.buf))
323 c.OrigSelection = c.CurSelection
327 forward, backward := c.X, c.X
329 for backward > 0 && util.IsWordChar(c.RuneUnder(backward-1)) {
333 c.SetSelectionStart(Loc{backward, c.Y})
334 c.OrigSelection[0] = c.CurSelection[0]
336 lineLen := utf8.RuneCount(c.buf.LineBytes(c.Y)) - 1
337 for forward < lineLen && util.IsWordChar(c.RuneUnder(forward+1)) {
341 c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
342 c.OrigSelection[1] = c.CurSelection[1]
343 c.Loc = c.CurSelection[1]
346 // AddWordToSelection adds the word the cursor is currently on
348 func (c *Cursor) AddWordToSelection() {
349 if c.Loc.GreaterThan(c.OrigSelection[0]) && c.Loc.LessThan(c.OrigSelection[1]) {
350 c.CurSelection = c.OrigSelection
354 if c.Loc.LessThan(c.OrigSelection[0]) {
357 for backward > 0 && util.IsWordChar(c.RuneUnder(backward-1)) {
361 c.SetSelectionStart(Loc{backward, c.Y})
362 c.SetSelectionEnd(c.OrigSelection[1])
365 if c.Loc.GreaterThan(c.OrigSelection[1]) {
368 lineLen := utf8.RuneCount(c.buf.LineBytes(c.Y)) - 1
369 for forward < lineLen && util.IsWordChar(c.RuneUnder(forward+1)) {
373 c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
374 c.SetSelectionStart(c.OrigSelection[0])
377 c.Loc = c.CurSelection[1]
380 // SelectTo selects from the current cursor location to the given
382 func (c *Cursor) SelectTo(loc Loc) {
383 if loc.GreaterThan(c.OrigSelection[0]) {
384 c.SetSelectionStart(c.OrigSelection[0])
385 c.SetSelectionEnd(loc)
387 c.SetSelectionStart(loc)
388 c.SetSelectionEnd(c.OrigSelection[0])
392 // WordRight moves the cursor one word to the right
393 func (c *Cursor) WordRight() {
394 for util.IsWhitespace(c.RuneUnder(c.X)) {
395 if c.X == utf8.RuneCount(c.buf.LineBytes(c.Y)) {
402 for util.IsWordChar(c.RuneUnder(c.X)) {
403 if c.X == utf8.RuneCount(c.buf.LineBytes(c.Y)) {
410 // WordLeft moves the cursor one word to the left
411 func (c *Cursor) WordLeft() {
413 for util.IsWhitespace(c.RuneUnder(c.X)) {
420 for util.IsWordChar(c.RuneUnder(c.X)) {
429 // RuneUnder returns the rune under the given x position
430 func (c *Cursor) RuneUnder(x int) rune {
431 line := c.buf.LineBytes(c.Y)
432 if len(line) == 0 || x >= utf8.RuneCount(line) {
439 r, size := utf8.DecodeRune(line)
451 func (c *Cursor) StoreVisualX() {
452 c.LastVisualX = c.GetVisualX()