micro/cmd/micro/cursor.go

330 lines
7.8 KiB
Go
Raw Normal View History

2016-03-17 21:27:57 +00:00
package main
2016-03-19 00:40:00 +00:00
// The Cursor struct stores the location of the cursor in the view
2016-03-26 21:23:52 +00:00
// The complicated part about the cursor is storing its location.
// The cursor must be displayed at an x, y location, but since the buffer
// uses a rope to store text, to insert text we must have an index. It
// is also simpler to use character indicies for other tasks such as
// selection.
2016-03-17 21:27:57 +00:00
type Cursor struct {
buf *Buffer
2016-06-07 15:43:28 +00:00
Loc
2016-03-17 21:27:57 +00:00
2016-04-04 18:09:24 +00:00
// Last cursor x position
LastVisualX int
2016-04-04 18:09:24 +00:00
2016-03-28 12:43:08 +00:00
// The current selection as a range of character numbers (inclusive)
2016-06-07 15:43:28 +00:00
CurSelection [2]Loc
2016-03-28 12:43:08 +00: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 15:43:28 +00:00
OrigSelection [2]Loc
}
// Goto puts the cursor at the given cursor's location and gives the current cursor its selection too
2016-05-29 15:02:56 +00: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
}
2016-03-19 00:40:00 +00:00
// ResetSelection resets the user's selection
func (c *Cursor) ResetSelection() {
2016-06-07 15:43:28 +00:00
c.CurSelection[0] = c.buf.Start()
c.CurSelection[1] = c.buf.Start()
2016-03-17 21:27:57 +00:00
}
2016-03-19 00:40:00 +00: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-17 21:27:57 +00:00
}
// DeleteSelection deletes the currently selected text
func (c *Cursor) DeleteSelection() {
2016-06-07 15:43:28 +00:00
if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
c.buf.Remove(c.CurSelection[1], c.CurSelection[0])
2016-06-07 15:43:28 +00:00
c.Loc = c.CurSelection[1]
2016-04-24 01:29:09 +00:00
} else if c.GetSelection() == "" {
return
2016-03-19 00:01:05 +00:00
} else {
c.buf.Remove(c.CurSelection[0], c.CurSelection[1])
2016-06-07 15:43:28 +00:00
c.Loc = c.CurSelection[0]
2016-03-19 00:01:05 +00:00
}
2016-03-17 21:27:57 +00:00
}
// GetSelection returns the cursor's selection
func (c *Cursor) GetSelection() string {
2016-06-07 15:43:28 +00:00
if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
return c.buf.Substr(c.CurSelection[1], c.CurSelection[0])
}
return c.buf.Substr(c.CurSelection[0], c.CurSelection[1])
}
2016-03-26 20:32:19 +00:00
// SelectLine selects the current line
func (c *Cursor) SelectLine() {
c.Start()
2016-06-07 15:43:28 +00:00
c.CurSelection[0] = c.Loc
2016-03-26 20:32:19 +00:00
c.End()
if c.buf.NumLines-1 > c.Y {
2016-06-07 15:43:28 +00:00
c.CurSelection[1] = c.Loc.Move(1, c.buf)
2016-04-23 18:02:20 +00:00
} else {
2016-06-07 15:43:28 +00:00
c.CurSelection[1] = c.Loc
2016-04-23 18:02:20 +00:00
}
2016-03-27 20:35:54 +00:00
c.OrigSelection = c.CurSelection
2016-03-26 20:32:19 +00:00
}
// AddLineToSelection adds the current line to the selection
func (c *Cursor) AddLineToSelection() {
2016-06-07 15:43:28 +00:00
if c.Loc.LessThan(c.OrigSelection[0]) {
2016-03-28 12:43:08 +00:00
c.Start()
2016-06-07 15:43:28 +00:00
c.CurSelection[0] = c.Loc
c.CurSelection[1] = c.OrigSelection[1]
2016-03-28 12:43:08 +00:00
}
2016-06-07 15:43:28 +00:00
if c.Loc.GreaterThan(c.OrigSelection[1]) {
2016-03-28 12:43:08 +00:00
c.End()
2016-06-07 15:43:28 +00:00
c.CurSelection[1] = c.Loc.Move(1, c.buf)
c.CurSelection[0] = c.OrigSelection[0]
2016-03-28 12:43:08 +00:00
}
2016-06-07 15:43:28 +00:00
if c.Loc.LessThan(c.OrigSelection[1]) && c.Loc.GreaterThan(c.OrigSelection[0]) {
c.CurSelection = c.OrigSelection
2016-03-28 12:43:08 +00:00
}
}
// SelectWord selects the word the cursor is currently on
func (c *Cursor) SelectWord() {
2016-06-07 15:43:28 +00:00
if len(c.buf.Line(c.Y)) == 0 {
2016-04-04 19:11:14 +00:00
return
}
if !IsWordChar(string(c.RuneUnder(c.X))) {
2016-06-07 15:43:28 +00:00
c.CurSelection[0] = c.Loc
c.CurSelection[1] = c.Loc.Move(1, c.buf)
c.OrigSelection = c.CurSelection
2016-03-28 12:43:08 +00:00
return
}
forward, backward := c.X, c.X
2016-03-28 12:43:08 +00:00
for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
backward--
}
2016-06-07 15:43:28 +00:00
c.CurSelection[0] = Loc{backward, c.Y}
c.OrigSelection[0] = c.CurSelection[0]
2016-03-28 12:43:08 +00:00
2016-06-07 15:43:28 +00:00
for forward < Count(c.buf.Line(c.Y))-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
2016-03-28 12:43:08 +00:00
forward++
}
2016-06-07 15:43:28 +00:00
c.CurSelection[1] = Loc{forward, c.Y}.Move(1, c.buf)
c.OrigSelection[1] = c.CurSelection[1]
2016-06-07 15:43:28 +00:00
c.Loc = c.CurSelection[1]
2016-03-28 12:43:08 +00:00
}
// AddWordToSelection adds the word the cursor is currently on to the selection
func (c *Cursor) AddWordToSelection() {
2016-06-07 15:43:28 +00:00
if c.Loc.GreaterThan(c.OrigSelection[0]) && c.Loc.LessThan(c.OrigSelection[1]) {
c.CurSelection = c.OrigSelection
2016-03-27 20:35:54 +00:00
return
}
2016-06-07 15:43:28 +00:00
if c.Loc.LessThan(c.OrigSelection[0]) {
backward := c.X
2016-03-28 12:43:08 +00:00
for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
backward--
}
2016-06-07 15:43:28 +00:00
c.CurSelection[0] = Loc{backward, c.Y}
c.CurSelection[1] = c.OrigSelection[1]
2016-03-27 20:35:54 +00:00
}
2016-06-07 15:43:28 +00:00
if c.Loc.GreaterThan(c.OrigSelection[1]) {
forward := c.X
2016-03-28 12:43:08 +00:00
2016-06-07 15:43:28 +00:00
for forward < Count(c.buf.Line(c.Y))-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
2016-03-28 12:43:08 +00:00
forward++
}
2016-06-07 15:43:28 +00:00
c.CurSelection[1] = Loc{forward, c.Y}.Move(1, c.buf)
c.CurSelection[0] = c.OrigSelection[0]
2016-03-26 20:32:19 +00:00
}
2016-06-07 15:43:28 +00:00
c.Loc = c.CurSelection[1]
2016-03-26 20:32:19 +00:00
}
2016-04-27 00:18:56 +00:00
// SelectTo selects from the current cursor location to the given location
2016-06-07 15:43:28 +00:00
func (c *Cursor) SelectTo(loc Loc) {
if loc.GreaterThan(c.OrigSelection[0]) {
c.CurSelection[0] = c.OrigSelection[0]
c.CurSelection[1] = loc
} else {
c.CurSelection[0] = loc
c.CurSelection[1] = c.OrigSelection[0]
}
}
2016-04-27 00:18:56 +00:00
// WordRight moves the cursor one word to the right
func (c *Cursor) WordRight() {
for IsWhitespace(c.RuneUnder(c.X)) {
2016-06-07 15:43:28 +00:00
if c.X == Count(c.buf.Line(c.Y)) {
c.Right()
2016-04-30 18:06:00 +00:00
return
}
c.Right()
}
c.Right()
for IsWordChar(string(c.RuneUnder(c.X))) {
2016-06-07 15:43:28 +00:00
if c.X == Count(c.buf.Line(c.Y)) {
2016-04-30 18:06:00 +00:00
return
}
c.Right()
}
}
2016-04-27 00:18:56 +00:00
// WordLeft moves the cursor one word to the left
func (c *Cursor) WordLeft() {
c.Left()
for IsWhitespace(c.RuneUnder(c.X)) {
if c.X == 0 {
2016-04-30 18:06:00 +00:00
return
}
c.Left()
}
c.Left()
for IsWordChar(string(c.RuneUnder(c.X))) {
if c.X == 0 {
2016-04-30 18:06:00 +00:00
return
}
c.Left()
}
c.Right()
}
2016-03-28 12:43:08 +00:00
// RuneUnder returns the rune under the given x position
func (c *Cursor) RuneUnder(x int) rune {
2016-06-07 15:43:28 +00:00
line := []rune(c.buf.Line(c.Y))
if len(line) == 0 {
return '\n'
}
2016-03-28 12:43:08 +00:00
if x >= len(line) {
2016-04-26 23:53:43 +00:00
return '\n'
2016-03-28 12:43:08 +00:00
} else if x < 0 {
x = 0
}
2016-03-28 12:43:08 +00:00
return line[x]
}
// UpN moves the cursor up N lines (if possible)
func (c *Cursor) UpN(amount int) {
proposedY := c.Y - amount
if proposedY < 0 {
proposedY = 0
} else if proposedY >= c.buf.NumLines {
proposedY = c.buf.NumLines - 1
}
if proposedY == c.Y {
return
}
2016-03-17 21:27:57 +00:00
c.Y = proposedY
2016-06-07 15:43:28 +00:00
runes := []rune(c.buf.Line(c.Y))
c.X = c.GetCharPosInLine(c.Y, c.LastVisualX)
if c.X > len(runes) {
c.X = len(runes)
2016-03-17 21:27:57 +00:00
}
}
2016-03-19 00:40:00 +00: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 00:40:00 +00:00
// Down moves the cursor down one line (if possible)
func (c *Cursor) Down() {
c.DownN(1)
2016-03-17 21:27:57 +00:00
}
2016-03-19 00:40:00 +00:00
// Left moves the cursor left one cell (if possible) or to the last line if it is at the beginning
func (c *Cursor) Left() {
2016-06-07 15:43:28 +00:00
if c.Loc == c.buf.Start() {
2016-03-19 00:40:00 +00:00
return
}
if c.X > 0 {
c.X--
2016-03-17 21:27:57 +00:00
} else {
2016-03-19 00:40:00 +00:00
c.Up()
c.End()
2016-03-17 21:27:57 +00:00
}
c.LastVisualX = c.GetVisualX()
2016-03-17 21:27:57 +00:00
}
2016-03-19 00:40:00 +00:00
// Right moves the cursor right one cell (if possible) or to the next line if it is at the end
func (c *Cursor) Right() {
2016-06-07 15:43:28 +00:00
if c.Loc == c.buf.End() {
2016-03-19 00:40:00 +00:00
return
}
2016-06-07 15:43:28 +00:00
if c.X < Count(c.buf.Line(c.Y)) {
c.X++
2016-03-17 21:27:57 +00:00
} else {
2016-03-19 00:40:00 +00:00
c.Down()
c.Start()
2016-03-17 21:27:57 +00:00
}
c.LastVisualX = c.GetVisualX()
2016-03-17 21:27:57 +00:00
}
2016-03-19 00:40:00 +00:00
// End moves the cursor to the end of the line it is on
func (c *Cursor) End() {
2016-06-07 15:43:28 +00:00
c.X = Count(c.buf.Line(c.Y))
c.LastVisualX = c.GetVisualX()
2016-03-17 21:27:57 +00:00
}
2016-03-19 00:40:00 +00: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()
2016-03-17 21:27:57 +00:00
}
2016-03-19 00:40:00 +00: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(lineNum, visualPos int) int {
2016-03-26 21:23:52 +00:00
// Get the tab size
tabSize := int(settings["tabsize"].(float64))
2016-06-07 15:43:28 +00:00
visualLineLen := StringWidth(c.buf.Line(lineNum))
if visualPos > visualLineLen {
visualPos = visualLineLen
2016-03-17 21:27:57 +00:00
}
2016-06-07 15:43:28 +00:00
width := WidthOfLargeRunes(c.buf.Line(lineNum))
if visualPos >= width {
return visualPos - width
2016-03-19 00:01:05 +00:00
}
2016-03-19 00:40:00 +00:00
return visualPos / tabSize
2016-03-17 21:27:57 +00:00
}
2016-03-19 00:40:00 +00:00
// GetVisualX returns the x value of the cursor in visual spaces
func (c *Cursor) GetVisualX() int {
2016-06-07 15:43:28 +00:00
runes := []rune(c.buf.Line(c.Y))
return StringWidth(string(runes[:c.X]))
2016-03-17 22:20:07 +00:00
}
2016-04-24 21:26:42 +00: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
func (c *Cursor) Relocate() {
if c.Y < 0 {
c.Y = 0
} else if c.Y >= c.buf.NumLines {
c.Y = c.buf.NumLines - 1
2016-04-24 21:26:42 +00:00
}
if c.X < 0 {
c.X = 0
2016-06-07 15:43:28 +00:00
} else if c.X > Count(c.buf.Line(c.Y)) {
c.X = Count(c.buf.Line(c.Y))
2016-03-17 21:27:57 +00:00
}
}