micro/src/cursor.go

212 lines
5 KiB
Go
Raw Normal View History

2016-03-18 00:27:57 +03:00
package main
import (
"strings"
)
2016-03-27 00:23:52 +03:00
// FromCharPos converts from a character position to an x, y position
func FromCharPos(loc int, buf *Buffer) (int, int) {
charNum := 0
x, y := 0, 0
2016-03-27 19:27:26 +03:00
for charNum+Count(buf.lines[y])+1 <= loc {
2016-03-27 00:23:52 +03:00
charNum += Count(buf.lines[y]) + 1
y++
}
2016-03-27 19:27:26 +03:00
x = loc - charNum
2016-03-27 00:23:52 +03:00
return x, y
}
// ToCharPos converts from an x, y position to a character position
func ToCharPos(x, y int, buf *Buffer) int {
loc := 0
for i := 0; i < y; i++ {
// + 1 for the newline
loc += Count(buf.lines[i]) + 1
}
loc += x
return loc
}
2016-03-19 03:40:00 +03:00
// The Cursor struct stores the location of the cursor in the view
2016-03-27 00:23:52 +03: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-18 00:27:57 +03:00
type Cursor struct {
v *View
2016-03-27 00:23:52 +03:00
// The cursor display location
x int
y int
2016-03-18 00:27:57 +03:00
2016-03-19 03:40:00 +03:00
selectionStart int
2016-03-27 00:23:52 +03:00
selectionEnd int
}
2016-03-19 03:40:00 +03:00
2016-03-27 00:23:52 +03:00
// SetLoc sets the location of the cursor in terms of character number
// and not x, y location
// It's just a simple wrapper of FromCharPos
func (c *Cursor) SetLoc(loc int) {
c.x, c.y = FromCharPos(loc, c.v.buf)
}
2016-03-19 03:40:00 +03:00
2016-03-27 00:23:52 +03:00
// Loc gets the cursor location in terms of character number instead
// of x, y location
// It's just a simple wrapper of ToCharPos
func (c *Cursor) Loc() int {
return ToCharPos(c.x, c.y, c.v.buf)
2016-03-18 00:27:57 +03:00
}
2016-03-19 03:40:00 +03:00
// ResetSelection resets the user's selection
func (c *Cursor) ResetSelection() {
2016-03-18 00:27:57 +03:00
c.selectionStart = 0
c.selectionEnd = 0
}
2016-03-19 03:40:00 +03:00
// HasSelection returns whether or not the user has selected anything
func (c *Cursor) HasSelection() bool {
2016-03-19 03:01:05 +03:00
return c.selectionEnd != c.selectionStart
2016-03-18 00:27:57 +03:00
}
// DeleteSelection deletes the currently selected text
func (c *Cursor) DeleteSelection() {
2016-03-19 03:01:05 +03:00
if c.selectionStart > c.selectionEnd {
2016-03-20 01:16:10 +03:00
c.v.eh.Remove(c.selectionEnd, c.selectionStart+1)
2016-03-27 00:23:52 +03:00
c.SetLoc(c.selectionEnd)
2016-03-19 03:01:05 +03:00
} else {
2016-03-20 01:16:10 +03:00
c.v.eh.Remove(c.selectionStart, c.selectionEnd+1)
2016-03-27 00:23:52 +03:00
c.SetLoc(c.selectionStart)
2016-03-19 03:01:05 +03:00
}
2016-03-18 00:27:57 +03: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-26 23:32:19 +03:00
// SelectLine selects the current line
func (c *Cursor) SelectLine() {
c.Start()
c.selectionStart = c.Loc()
c.End()
c.selectionEnd = c.Loc()
}
// AddLineToSelection adds the current line to the selection
func (c *Cursor) AddLineToSelection() {
2016-03-27 20:02:02 +03:00
if c.Loc() < c.selectionStart {
2016-03-26 23:32:19 +03:00
c.Start()
c.selectionStart = c.Loc()
2016-03-27 20:02:02 +03:00
} else if c.Loc() > c.selectionEnd {
2016-03-26 23:32:19 +03:00
c.End()
c.selectionEnd = c.Loc()
}
}
2016-03-19 03:40:00 +03:00
// RuneUnder returns the rune under the cursor
func (c *Cursor) RuneUnder() rune {
line := c.v.buf.lines[c.y]
2016-03-19 03:40:00 +03:00
if c.x >= Count(line) {
return ' '
}
return []rune(line)[c.x]
}
2016-03-19 03:40:00 +03:00
// Up moves the cursor up one line (if possible)
func (c *Cursor) Up() {
2016-03-18 00:27:57 +03:00
if c.y > 0 {
c.y--
2016-03-27 00:23:52 +03:00
runes := []rune(c.v.buf.lines[c.y])
2016-03-22 01:52:56 +03:00
if c.x > len(runes) {
c.x = len(runes)
2016-03-18 00:27:57 +03:00
}
}
}
2016-03-19 03:40:00 +03:00
// Down moves the cursor down one line (if possible)
func (c *Cursor) Down() {
2016-03-18 00:27:57 +03:00
if c.y < len(c.v.buf.lines)-1 {
c.y++
2016-03-27 00:23:52 +03:00
runes := []rune(c.v.buf.lines[c.y])
2016-03-22 01:52:56 +03:00
if c.x > len(runes) {
c.x = len(runes)
2016-03-18 00:27:57 +03:00
}
}
}
2016-03-19 03:40:00 +03: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-03-27 00:23:52 +03:00
if c.Loc() == 0 {
2016-03-19 03:40:00 +03:00
return
}
2016-03-18 00:27:57 +03:00
if c.x > 0 {
c.x--
} else {
2016-03-19 03:40:00 +03:00
c.Up()
c.End()
2016-03-18 00:27:57 +03:00
}
}
2016-03-19 03:40:00 +03: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-03-27 00:23:52 +03:00
if c.Loc() == c.v.buf.Len() {
2016-03-19 03:40:00 +03:00
return
}
if c.x < Count(c.v.buf.lines[c.y]) {
2016-03-18 00:27:57 +03:00
c.x++
} else {
2016-03-19 03:40:00 +03:00
c.Down()
c.Start()
2016-03-18 00:27:57 +03:00
}
}
2016-03-19 03:40:00 +03:00
// End moves the cursor to the end of the line it is on
func (c *Cursor) End() {
2016-03-27 00:23:52 +03:00
c.x = Count(c.v.buf.lines[c.y])
2016-03-18 00:27:57 +03:00
}
2016-03-19 03:40:00 +03:00
// Start moves the cursor to the start of the line it is on
func (c *Cursor) Start() {
2016-03-18 00:27:57 +03:00
c.x = 0
}
2016-03-19 03:40:00 +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(lineNum, visualPos int) int {
2016-03-27 00:23:52 +03:00
// Get the tab size
2016-03-26 17:54:18 +03:00
tabSize := options["tabsize"].(int)
2016-03-27 00:23:52 +03:00
// This is the visual line -- every \t replaced with the correct number of spaces
2016-03-25 19:14:22 +03:00
visualLine := strings.Replace(c.v.buf.lines[lineNum], "\t", "\t"+Spaces(tabSize-1), -1)
2016-03-19 03:40:00 +03:00
if visualPos > Count(visualLine) {
visualPos = Count(visualLine)
2016-03-18 00:27:57 +03:00
}
2016-03-19 03:40:00 +03:00
numTabs := NumOccurences(visualLine[:visualPos], '\t')
2016-03-19 03:01:05 +03:00
if visualPos >= (tabSize-1)*numTabs {
return visualPos - (tabSize-1)*numTabs
}
2016-03-19 03:40:00 +03:00
return visualPos / tabSize
2016-03-18 00:27:57 +03:00
}
2016-03-19 03:40:00 +03:00
// GetVisualX returns the x value of the cursor in visual spaces
func (c *Cursor) GetVisualX() int {
2016-03-22 01:52:56 +03:00
runes := []rune(c.v.buf.lines[c.y])
2016-03-26 17:54:18 +03:00
tabSize := options["tabsize"].(int)
2016-03-22 01:52:56 +03:00
return c.x + NumOccurences(string(runes[:c.x]), '\t')*(tabSize-1)
2016-03-18 01:20:07 +03:00
}
2016-03-19 03:40:00 +03:00
// Display draws the cursor to the screen at the correct position
func (c *Cursor) Display() {
2016-03-18 01:20:07 +03:00
if c.y-c.v.topline < 0 || c.y-c.v.topline > c.v.height-1 {
2016-03-25 19:14:22 +03:00
screen.HideCursor()
2016-03-18 00:27:57 +03:00
} else {
2016-03-25 19:14:22 +03:00
screen.ShowCursor(c.GetVisualX()+c.v.lineNumOffset, c.y-c.v.topline)
2016-03-18 00:27:57 +03:00
}
}