micro/internal/buffer/cursor.go

441 lines
9.9 KiB
Go
Raw Normal View History

2018-08-27 22:53:10 +03:00
package buffer
2016-03-18 00:27:57 +03:00
import (
2018-08-26 06:06:44 +03:00
"unicode/utf8"
"github.com/zyedidia/clipboard"
2019-02-04 07:17:24 +03:00
"github.com/zyedidia/micro/internal/util"
)
2018-08-26 06:06:44 +03:00
// InBounds returns whether the given location is a valid character position in the given buffer
func InBounds(pos Loc, buf *Buffer) bool {
if pos.Y < 0 || pos.Y >= len(buf.lines) || pos.X < 0 || pos.X > utf8.RuneCount(buf.LineBytes(pos.Y)) {
return false
}
return true
}
// The Cursor struct stores the location of the cursor in the buffer
// as well as the selection
2016-03-18 00:27:57 +03:00
type Cursor struct {
2018-08-30 03:53:40 +03:00
buf *Buffer
2016-06-07 18:43:28 +03:00
Loc
2016-03-18 00:27:57 +03:00
2016-04-04 21:09:24 +03:00
// Last cursor x position
LastVisualX int
2016-04-04 21:09:24 +03:00
2016-03-28 15:43:08 +03:00
// The current selection as a range of character numbers (inclusive)
2016-06-07 18:43:28 +03:00
CurSelection [2]Loc
2016-03-28 15:43:08 +03:00
// The original selection as a range of character numbers
// This is used for line and word selection where it is necessary
// to know what the original selection was
2016-06-07 18:43:28 +03:00
OrigSelection [2]Loc
// Which cursor index is this (for multiple cursors)
Num int
}
2018-08-30 03:53:40 +03:00
func NewCursor(b *Buffer, l Loc) *Cursor {
c := &Cursor{
buf: b,
Loc: l,
}
c.StoreVisualX()
return c
}
func (c *Cursor) SetBuf(b *Buffer) {
c.buf = b
}
func (c *Cursor) Buf() *Buffer {
return c.buf
}
2017-11-24 21:35:11 +03:00
// Goto puts the cursor at the given cursor's location and gives
// the current cursor its selection too
2016-05-29 18:02:56 +03:00
func (c *Cursor) Goto(b Cursor) {
c.X, c.Y, c.LastVisualX = b.X, b.Y, b.LastVisualX
c.OrigSelection, c.CurSelection = b.OrigSelection, b.CurSelection
}
// GotoLoc puts the cursor at the given cursor's location and gives
// the current cursor its selection too
func (c *Cursor) GotoLoc(l Loc) {
c.X, c.Y = l.X, l.Y
2018-08-28 00:55:28 +03:00
c.StoreVisualX()
}
2018-08-26 06:06:44 +03:00
// GetVisualX returns the x value of the cursor in visual spaces
func (c *Cursor) GetVisualX() int {
if c.X <= 0 {
c.X = 0
return 0
}
2018-08-30 03:53:40 +03:00
bytes := c.buf.LineBytes(c.Y)
tabsize := int(c.buf.Settings["tabsize"].(float64))
2018-08-26 06:06:44 +03:00
if c.X > utf8.RuneCount(bytes) {
c.X = utf8.RuneCount(bytes) - 1
}
2018-08-27 22:53:10 +03:00
return util.StringWidth(bytes, c.X, tabsize)
2018-08-26 06:06:44 +03:00
}
// GetCharPosInLine gets the char position of a visual x y
// coordinate (this is necessary because tabs are 1 char but
// 4 visual spaces)
func (c *Cursor) GetCharPosInLine(b []byte, visualPos int) int {
2018-08-30 03:53:40 +03:00
tabsize := int(c.buf.Settings["tabsize"].(float64))
2019-01-03 04:07:48 +03:00
return util.GetCharPosInLine(b, visualPos, tabsize)
2018-08-26 06:06:44 +03:00
}
// Start moves the cursor to the start of the line it is on
func (c *Cursor) Start() {
c.X = 0
c.LastVisualX = c.GetVisualX()
}
2018-08-29 01:44:52 +03:00
// StartOfText moves the cursor to the first non-whitespace rune of
// the line it is on
func (c *Cursor) StartOfText() {
c.Start()
for util.IsWhitespace(c.RuneUnder(c.X)) {
2018-08-30 03:53:40 +03:00
if c.X == utf8.RuneCount(c.buf.LineBytes(c.Y)) {
2018-08-29 01:44:52 +03:00
break
}
c.Right()
}
}
2018-08-26 06:06:44 +03:00
// End moves the cursor to the end of the line it is on
func (c *Cursor) End() {
2018-08-30 03:53:40 +03:00
c.X = utf8.RuneCount(c.buf.LineBytes(c.Y))
2018-08-26 06:06:44 +03:00
c.LastVisualX = c.GetVisualX()
}
2017-11-24 21:35:11 +03:00
// CopySelection copies the user's selection to either "primary"
// or "clipboard"
func (c *Cursor) CopySelection(target string) {
if c.HasSelection() {
2018-08-30 03:53:40 +03:00
if target != "primary" || c.buf.Settings["useprimary"].(bool) {
2018-08-26 06:06:44 +03:00
clipboard.WriteAll(string(c.GetSelection()), target)
}
}
}
2016-03-19 03:40:00 +03:00
// ResetSelection resets the user's selection
func (c *Cursor) ResetSelection() {
2018-08-30 03:53:40 +03:00
c.CurSelection[0] = c.buf.Start()
c.CurSelection[1] = c.buf.Start()
}
// SetSelectionStart sets the start of the selection
func (c *Cursor) SetSelectionStart(pos Loc) {
2016-09-05 04:28:40 +03:00
c.CurSelection[0] = pos
}
// SetSelectionEnd sets the end of the selection
func (c *Cursor) SetSelectionEnd(pos Loc) {
2016-09-05 04:28:40 +03:00
c.CurSelection[1] = pos
2016-03-18 00:27:57 +03:00
}
2016-03-19 03:40:00 +03:00
// HasSelection returns whether or not the user has selected anything
func (c *Cursor) HasSelection() bool {
return c.CurSelection[0] != c.CurSelection[1]
2016-03-18 00:27:57 +03:00
}
// DeleteSelection deletes the currently selected text
func (c *Cursor) DeleteSelection() {
2016-06-07 18:43:28 +03:00
if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
2018-08-30 03:53:40 +03:00
c.buf.Remove(c.CurSelection[1], c.CurSelection[0])
2016-06-07 18:43:28 +03:00
c.Loc = c.CurSelection[1]
} else if !c.HasSelection() {
2016-04-24 04:29:09 +03:00
return
2016-03-19 03:01:05 +03:00
} else {
2018-08-30 03:53:40 +03:00
c.buf.Remove(c.CurSelection[0], c.CurSelection[1])
2016-06-07 18:43:28 +03:00
c.Loc = c.CurSelection[0]
2016-03-19 03:01:05 +03:00
}
2016-03-18 00:27:57 +03:00
}
2018-08-28 00:55:28 +03:00
// Deselect closes the cursor's current selection
// Start indicates whether the cursor should be placed
// at the start or end of the selection
func (c *Cursor) Deselect(start bool) {
if c.HasSelection() {
if start {
c.Loc = c.CurSelection[0]
} else {
2019-01-05 05:48:19 +03:00
c.Loc = c.CurSelection[1].Move(-1, c.buf)
2018-08-28 00:55:28 +03:00
}
c.ResetSelection()
c.StoreVisualX()
}
}
// GetSelection returns the cursor's selection
2018-08-26 06:06:44 +03:00
func (c *Cursor) GetSelection() []byte {
2018-08-30 03:53:40 +03:00
if InBounds(c.CurSelection[0], c.buf) && InBounds(c.CurSelection[1], c.buf) {
if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
2018-08-30 03:53:40 +03:00
return c.buf.Substr(c.CurSelection[1], c.CurSelection[0])
}
2018-08-30 03:53:40 +03:00
return c.buf.Substr(c.CurSelection[0], c.CurSelection[1])
}
2018-08-26 06:06:44 +03:00
return []byte{}
}
2016-03-26 23:32:19 +03:00
// SelectLine selects the current line
func (c *Cursor) SelectLine() {
c.Start()
c.SetSelectionStart(c.Loc)
2016-03-26 23:32:19 +03:00
c.End()
2018-08-30 03:53:40 +03:00
if len(c.buf.lines)-1 > c.Y {
c.SetSelectionEnd(c.Loc.Move(1, c.buf))
2016-04-23 21:02:20 +03:00
} else {
c.SetSelectionEnd(c.Loc)
2016-04-23 21:02:20 +03:00
}
2016-03-27 23:35:54 +03:00
c.OrigSelection = c.CurSelection
2016-03-26 23:32:19 +03:00
}
// AddLineToSelection adds the current line to the selection
func (c *Cursor) AddLineToSelection() {
2016-06-07 18:43:28 +03:00
if c.Loc.LessThan(c.OrigSelection[0]) {
2016-03-28 15:43:08 +03:00
c.Start()
c.SetSelectionStart(c.Loc)
c.SetSelectionEnd(c.OrigSelection[1])
2016-03-28 15:43:08 +03:00
}
2016-06-07 18:43:28 +03:00
if c.Loc.GreaterThan(c.OrigSelection[1]) {
2016-03-28 15:43:08 +03:00
c.End()
2018-08-30 03:53:40 +03:00
c.SetSelectionEnd(c.Loc.Move(1, c.buf))
c.SetSelectionStart(c.OrigSelection[0])
2016-03-28 15:43:08 +03:00
}
2016-06-07 18:43:28 +03:00
if c.Loc.LessThan(c.OrigSelection[1]) && c.Loc.GreaterThan(c.OrigSelection[0]) {
c.CurSelection = c.OrigSelection
2016-03-28 15:43:08 +03:00
}
}
// UpN moves the cursor up N lines (if possible)
func (c *Cursor) UpN(amount int) {
proposedY := c.Y - amount
if proposedY < 0 {
2017-09-07 23:44:05 +03:00
proposedY = 0
2018-08-30 03:53:40 +03:00
} else if proposedY >= len(c.buf.lines) {
proposedY = len(c.buf.lines) - 1
}
2016-03-18 00:27:57 +03:00
2018-08-30 03:53:40 +03:00
bytes := c.buf.LineBytes(proposedY)
2018-08-26 06:06:44 +03:00
c.X = c.GetCharPosInLine(bytes, c.LastVisualX)
if c.X > utf8.RuneCount(bytes) || (amount < 0 && proposedY == c.Y) {
c.X = utf8.RuneCount(bytes)
2016-03-18 00:27:57 +03:00
}
c.Y = proposedY
2016-03-18 00:27:57 +03:00
}
2016-03-19 03:40:00 +03:00
// DownN moves the cursor down N lines (if possible)
func (c *Cursor) DownN(amount int) {
c.UpN(-amount)
}
// Up moves the cursor up one line (if possible)
func (c *Cursor) Up() {
c.UpN(1)
}
2016-03-19 03:40:00 +03:00
// Down moves the cursor down one line (if possible)
func (c *Cursor) Down() {
c.DownN(1)
2016-03-18 00:27:57 +03:00
}
2016-03-19 03:40:00 +03:00
2017-11-24 21:35:11 +03:00
// Left moves the cursor left one cell (if possible) or to
// the previous line if it is at the beginning
2016-03-19 03:40:00 +03:00
func (c *Cursor) Left() {
2018-08-30 03:53:40 +03:00
if c.Loc == c.buf.Start() {
2016-03-19 03:40:00 +03:00
return
}
if c.X > 0 {
c.X--
2016-03-18 00:27:57 +03:00
} else {
2016-03-19 03:40:00 +03:00
c.Up()
c.End()
2016-03-18 00:27:57 +03:00
}
2018-08-28 00:55:28 +03:00
c.StoreVisualX()
2016-03-18 00:27:57 +03:00
}
2016-03-19 03:40:00 +03:00
2017-11-24 21:35:11 +03:00
// Right moves the cursor right one cell (if possible) or
// to the next line if it is at the end
2016-03-19 03:40:00 +03:00
func (c *Cursor) Right() {
2018-08-30 03:53:40 +03:00
if c.Loc == c.buf.End() {
2016-03-19 03:40:00 +03:00
return
}
2018-08-30 03:53:40 +03:00
if c.X < utf8.RuneCount(c.buf.LineBytes(c.Y)) {
c.X++
2016-03-18 00:27:57 +03:00
} else {
2016-03-19 03:40:00 +03:00
c.Down()
c.Start()
2016-03-18 00:27:57 +03:00
}
2018-08-28 00:55:28 +03:00
c.StoreVisualX()
2016-03-18 00:27:57 +03:00
}
2017-11-24 21:35:11 +03:00
// Relocate makes sure that the cursor is inside the bounds
// of the buffer If it isn't, it moves it to be within the
// buffer's lines
2016-04-25 00:26:42 +03:00
func (c *Cursor) Relocate() {
if c.Y < 0 {
c.Y = 0
2018-08-30 03:53:40 +03:00
} else if c.Y >= len(c.buf.lines) {
c.Y = len(c.buf.lines) - 1
2016-04-25 00:26:42 +03:00
}
if c.X < 0 {
c.X = 0
2018-08-30 03:53:40 +03:00
} else if c.X > utf8.RuneCount(c.buf.LineBytes(c.Y)) {
c.X = utf8.RuneCount(c.buf.LineBytes(c.Y))
2016-03-18 00:27:57 +03:00
}
}
2018-08-28 00:55:28 +03:00
2018-08-29 01:44:52 +03:00
// SelectWord selects the word the cursor is currently on
func (c *Cursor) SelectWord() {
2018-08-30 03:53:40 +03:00
if len(c.buf.LineBytes(c.Y)) == 0 {
2018-08-29 01:44:52 +03:00
return
}
if !util.IsWordChar(c.RuneUnder(c.X)) {
c.SetSelectionStart(c.Loc)
2018-08-30 03:53:40 +03:00
c.SetSelectionEnd(c.Loc.Move(1, c.buf))
2018-08-29 01:44:52 +03:00
c.OrigSelection = c.CurSelection
return
}
forward, backward := c.X, c.X
for backward > 0 && util.IsWordChar(c.RuneUnder(backward-1)) {
backward--
}
c.SetSelectionStart(Loc{backward, c.Y})
c.OrigSelection[0] = c.CurSelection[0]
2018-08-30 03:53:40 +03:00
lineLen := utf8.RuneCount(c.buf.LineBytes(c.Y)) - 1
2018-08-29 01:44:52 +03:00
for forward < lineLen && util.IsWordChar(c.RuneUnder(forward+1)) {
forward++
}
2018-08-30 03:53:40 +03:00
c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
2018-08-29 01:44:52 +03:00
c.OrigSelection[1] = c.CurSelection[1]
c.Loc = c.CurSelection[1]
}
// AddWordToSelection adds the word the cursor is currently on
// to the selection
func (c *Cursor) AddWordToSelection() {
if c.Loc.GreaterThan(c.OrigSelection[0]) && c.Loc.LessThan(c.OrigSelection[1]) {
c.CurSelection = c.OrigSelection
return
}
if c.Loc.LessThan(c.OrigSelection[0]) {
backward := c.X
for backward > 0 && util.IsWordChar(c.RuneUnder(backward-1)) {
backward--
}
c.SetSelectionStart(Loc{backward, c.Y})
c.SetSelectionEnd(c.OrigSelection[1])
}
if c.Loc.GreaterThan(c.OrigSelection[1]) {
forward := c.X
2018-08-30 03:53:40 +03:00
lineLen := utf8.RuneCount(c.buf.LineBytes(c.Y)) - 1
2018-08-29 01:44:52 +03:00
for forward < lineLen && util.IsWordChar(c.RuneUnder(forward+1)) {
forward++
}
2018-08-30 03:53:40 +03:00
c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
2018-08-29 01:44:52 +03:00
c.SetSelectionStart(c.OrigSelection[0])
}
c.Loc = c.CurSelection[1]
}
// SelectTo selects from the current cursor location to the given
// location
func (c *Cursor) SelectTo(loc Loc) {
if loc.GreaterThan(c.OrigSelection[0]) {
c.SetSelectionStart(c.OrigSelection[0])
c.SetSelectionEnd(loc)
} else {
c.SetSelectionStart(loc)
c.SetSelectionEnd(c.OrigSelection[0])
}
}
// WordRight moves the cursor one word to the right
func (c *Cursor) WordRight() {
for util.IsWhitespace(c.RuneUnder(c.X)) {
2018-08-30 03:53:40 +03:00
if c.X == utf8.RuneCount(c.buf.LineBytes(c.Y)) {
2018-08-29 01:44:52 +03:00
c.Right()
return
}
c.Right()
}
c.Right()
for util.IsWordChar(c.RuneUnder(c.X)) {
2018-08-30 03:53:40 +03:00
if c.X == utf8.RuneCount(c.buf.LineBytes(c.Y)) {
2018-08-29 01:44:52 +03:00
return
}
c.Right()
}
}
// WordLeft moves the cursor one word to the left
func (c *Cursor) WordLeft() {
c.Left()
for util.IsWhitespace(c.RuneUnder(c.X)) {
if c.X == 0 {
return
}
c.Left()
}
c.Left()
for util.IsWordChar(c.RuneUnder(c.X)) {
if c.X == 0 {
return
}
c.Left()
}
c.Right()
}
// RuneUnder returns the rune under the given x position
func (c *Cursor) RuneUnder(x int) rune {
2018-08-30 03:53:40 +03:00
line := c.buf.LineBytes(c.Y)
2018-08-29 01:44:52 +03:00
if len(line) == 0 || x >= utf8.RuneCount(line) {
return '\n'
} else if x < 0 {
x = 0
}
i := 0
for len(line) > 0 {
r, size := utf8.DecodeRune(line)
line = line[size:]
if i == x {
return r
}
i++
}
return '\n'
}
2018-08-28 00:55:28 +03:00
func (c *Cursor) StoreVisualX() {
c.LastVisualX = c.GetVisualX()
}