micro/src/cursor.go

223 lines
5.1 KiB
Go
Raw Normal View History

2016-03-17 21:27:57 +00:00
package main
import (
"strings"
)
2016-03-19 00:40:00 +00:00
// The Cursor struct stores the location of the cursor in the view
2016-03-17 21:27:57 +00:00
type Cursor struct {
v *View
2016-03-19 00:40:00 +00:00
// We need three variables here because we insert text at loc but
// display the cursor at x, y
2016-03-17 21:27:57 +00:00
x int
y int
loc int
2016-03-19 00:40:00 +00:00
// Start of the selection in charNum
selectionStart int
// We store the x, y of the start because when the user deletes the selection
// the cursor needs to go back to the start, and this is the simplest way
2016-03-19 00:01:05 +00:00
selectionStartX int
selectionStartY int
2016-03-19 00:40:00 +00:00
// End of the selection in charNum
// We don't need to store the x, y here because when if the user is selecting backwards
// and they delete the selection, the cursor is already in the right place
selectionEnd int
2016-03-17 21:27:57 +00:00
}
2016-03-19 00:40:00 +00:00
// ResetSelection resets the user's selection
func (c *Cursor) ResetSelection() {
2016-03-17 21:27:57 +00:00
c.selectionStart = 0
c.selectionEnd = 0
}
2016-03-19 00:40:00 +00:00
// HasSelection returns whether or not the user has selected anything
func (c *Cursor) HasSelection() bool {
2016-03-19 00:01:05 +00:00
return c.selectionEnd != c.selectionStart
2016-03-17 21:27:57 +00:00
}
// DeleteSelection deletes the currently selected text
func (c *Cursor) DeleteSelection() {
2016-03-19 00:01:05 +00:00
if c.selectionStart > c.selectionEnd {
2016-03-19 22:16:10 +00:00
c.v.eh.Remove(c.selectionEnd, c.selectionStart+1)
2016-03-19 00:01:05 +00:00
// Since the cursor is already at the selection start we don't need to move
} else {
2016-03-19 22:16:10 +00:00
c.v.eh.Remove(c.selectionStart, c.selectionEnd+1)
2016-03-19 00:01:05 +00:00
c.loc -= c.selectionEnd - c.selectionStart
c.x = c.selectionStartX
c.y = c.selectionStartY
}
2016-03-17 21:27:57 +00:00
}
// GetSelection returns the cursor's selection
func (c *Cursor) GetSelection() string {
if c.selectionStart > c.selectionEnd {
return string([]rune(c.v.buf.text)[c.selectionEnd : c.selectionStart+1])
}
return string([]rune(c.v.buf.text)[c.selectionStart : c.selectionEnd+1])
}
2016-03-19 00:40:00 +00:00
// RuneUnder returns the rune under the cursor
func (c *Cursor) RuneUnder() rune {
line := c.v.buf.lines[c.y]
2016-03-19 00:40:00 +00:00
if c.x >= Count(line) {
return ' '
}
return []rune(line)[c.x]
}
2016-03-19 00:40:00 +00:00
// Up moves the cursor up one line (if possible)
func (c *Cursor) Up() {
2016-03-17 21:27:57 +00:00
if c.y > 0 {
2016-03-21 22:52:56 +00:00
runes := []rune(c.v.buf.lines[c.y])
c.loc -= len(runes[:c.x])
2016-03-17 21:27:57 +00:00
// Count the newline
c.loc--
c.y--
2016-03-21 22:52:56 +00:00
runes = []rune(c.v.buf.lines[c.y])
2016-03-17 21:27:57 +00:00
2016-03-21 22:52:56 +00:00
if c.x > len(runes) {
c.x = len(runes)
2016-03-17 21:27:57 +00:00
}
2016-03-21 22:52:56 +00:00
c.loc -= len(runes[c.x:])
2016-03-17 21:27:57 +00:00
}
}
2016-03-19 00:40:00 +00:00
// Down moves the cursor down one line (if possible)
func (c *Cursor) Down() {
2016-03-17 21:27:57 +00:00
if c.y < len(c.v.buf.lines)-1 {
2016-03-21 22:52:56 +00:00
runes := []rune(c.v.buf.lines[c.y])
c.loc += len(runes[c.x:])
2016-03-17 21:27:57 +00:00
// Count the newline
c.loc++
c.y++
2016-03-21 22:52:56 +00:00
runes = []rune(c.v.buf.lines[c.y])
2016-03-17 21:27:57 +00:00
2016-03-21 22:52:56 +00:00
if c.x > len(runes) {
c.x = len(runes)
2016-03-17 21:27:57 +00:00
}
2016-03-21 22:52:56 +00:00
c.loc += len(runes[:c.x])
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() {
if c.loc == 0 {
return
}
2016-03-17 21:27:57 +00:00
if c.x > 0 {
c.loc--
c.x--
} else {
2016-03-19 00:40:00 +00:00
c.Up()
c.End()
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() {
if c.loc == c.v.buf.Len() {
return
}
if c.x < Count(c.v.buf.lines[c.y]) {
2016-03-17 21:27:57 +00:00
c.loc++
c.x++
} else {
2016-03-19 00:40:00 +00:00
c.Down()
c.Start()
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-03-21 22:52:56 +00:00
runes := []rune(c.v.buf.lines[c.y])
c.loc += len(runes[c.x:])
c.x = len(runes)
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() {
2016-03-21 22:52:56 +00:00
runes := []rune(c.v.buf.lines[c.y])
c.loc -= len(runes[:c.x])
2016-03-17 21:27:57 +00:00
c.x = 0
}
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 14:54:18 +00:00
tabSize := options["tabsize"].(int)
2016-03-25 16:14:22 +00:00
visualLine := strings.Replace(c.v.buf.lines[lineNum], "\t", "\t"+Spaces(tabSize-1), -1)
2016-03-19 00:40:00 +00:00
if visualPos > Count(visualLine) {
visualPos = Count(visualLine)
2016-03-17 21:27:57 +00:00
}
2016-03-19 00:40:00 +00:00
numTabs := NumOccurences(visualLine[:visualPos], '\t')
2016-03-19 00:01:05 +00:00
if visualPos >= (tabSize-1)*numTabs {
return visualPos - (tabSize-1)*numTabs
}
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-03-21 22:52:56 +00:00
runes := []rune(c.v.buf.lines[c.y])
2016-03-26 14:54:18 +00:00
tabSize := options["tabsize"].(int)
2016-03-21 22:52:56 +00:00
return c.x + NumOccurences(string(runes[:c.x]), '\t')*(tabSize-1)
2016-03-17 22:20:07 +00:00
}
2016-03-19 00:40:00 +00:00
// Distance returns the distance between the cursor and x, y in runes
func (c *Cursor) Distance(x, y int) int {
2016-03-17 21:27:57 +00:00
// Same line
if y == c.y {
return x - c.x
}
var distance int
2016-03-21 22:52:56 +00:00
runes := []rune(c.v.buf.lines[c.y])
2016-03-17 21:27:57 +00:00
if y > c.y {
2016-03-21 22:52:56 +00:00
distance += len(runes[c.x:])
2016-03-17 21:27:57 +00:00
// Newline
distance++
i := 1
for y != c.y+i {
2016-03-19 00:40:00 +00:00
distance += Count(c.v.buf.lines[c.y+i])
2016-03-17 21:27:57 +00:00
// Newline
distance++
i++
}
2016-03-19 00:40:00 +00:00
if x < Count(c.v.buf.lines[y]) {
2016-03-21 22:52:56 +00:00
distance += len([]rune(c.v.buf.lines[y])[:x])
2016-03-17 21:27:57 +00:00
} else {
2016-03-19 00:40:00 +00:00
distance += Count(c.v.buf.lines[y])
2016-03-17 21:27:57 +00:00
}
return distance
}
2016-03-21 22:52:56 +00:00
distance -= len(runes[:c.x])
2016-03-17 21:27:57 +00:00
// Newline
distance--
i := 1
for y != c.y-i {
2016-03-19 00:40:00 +00:00
distance -= Count(c.v.buf.lines[c.y-i])
2016-03-17 21:27:57 +00:00
// Newline
distance--
i++
}
if x >= 0 {
2016-03-21 22:52:56 +00:00
distance -= len([]rune(c.v.buf.lines[y])[x:])
2016-03-17 21:27:57 +00:00
}
return distance
}
2016-03-19 00:40:00 +00:00
// Display draws the cursor to the screen at the correct position
func (c *Cursor) Display() {
2016-03-17 22:20:07 +00:00
if c.y-c.v.topline < 0 || c.y-c.v.topline > c.v.height-1 {
2016-03-25 16:14:22 +00:00
screen.HideCursor()
2016-03-17 21:27:57 +00:00
} else {
2016-03-25 16:14:22 +00:00
screen.ShowCursor(c.GetVisualX()+c.v.lineNumOffset, c.y-c.v.topline)
2016-03-17 21:27:57 +00:00
}
}