charNum := startLoc
x, y := startX, startY
- lineLen := Count(buf.lines[y]) + 1
+ lineLen := Count(buf.Lines[y]) + 1
for charNum+lineLen <= loc {
charNum += lineLen
y++
- lineLen = Count(buf.lines[y]) + 1
+ lineLen = Count(buf.Lines[y]) + 1
}
x = loc - charNum
loc := 0
for i := 0; i < y; i++ {
// + 1 for the newline
- loc += Count(buf.lines[i]) + 1
+ loc += Count(buf.Lines[i]) + 1
}
loc += x
return loc
// is also simpler to use character indicies for other tasks such as
// selection.
type Cursor struct {
- v *View
+ Buf *Buffer
// The cursor display location
x int
// 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)
+ c.x, c.y = FromCharPos(loc, c.Buf)
c.lastVisualX = c.GetVisualX()
}
// 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)
+ return ToCharPos(c.x, c.y, c.Buf)
}
// ResetSelection resets the user's selection
// DeleteSelection deletes the currently selected text
func (c *Cursor) DeleteSelection() {
if c.curSelection[0] > c.curSelection[1] {
- c.v.eh.Remove(c.curSelection[1], c.curSelection[0])
+ c.Buf.Remove(c.curSelection[1], c.curSelection[0])
c.SetLoc(c.curSelection[1])
} else if c.GetSelection() == "" {
return
} else {
- c.v.eh.Remove(c.curSelection[0], c.curSelection[1])
+ c.Buf.Remove(c.curSelection[0], c.curSelection[1])
c.SetLoc(c.curSelection[0])
}
}
// GetSelection returns the cursor's selection
func (c *Cursor) GetSelection() string {
if c.curSelection[0] > c.curSelection[1] {
- return string([]rune(c.v.buf.text)[c.curSelection[1]:c.curSelection[0]])
+ return c.Buf.Substr(c.curSelection[1], c.curSelection[0])
}
- return string([]rune(c.v.buf.text)[c.curSelection[0]:c.curSelection[1]])
+ return c.Buf.Substr(c.curSelection[0], c.curSelection[1])
}
// SelectLine selects the current line
c.Start()
c.curSelection[0] = c.Loc()
c.End()
- if len(c.v.buf.lines)-1 > c.y {
+ if c.Buf.NumLines-1 > c.y {
c.curSelection[1] = c.Loc() + 1
} else {
c.curSelection[1] = c.Loc()
// SelectWord selects the word the cursor is currently on
func (c *Cursor) SelectWord() {
- if len(c.v.buf.lines[c.y]) == 0 {
+ if len(c.Buf.Lines[c.y]) == 0 {
return
}
backward--
}
- c.curSelection[0] = ToCharPos(backward, c.y, c.v.buf)
+ c.curSelection[0] = ToCharPos(backward, c.y, c.Buf)
c.origSelection[0] = c.curSelection[0]
- for forward < Count(c.v.buf.lines[c.y])-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
+ for forward < Count(c.Buf.Lines[c.y])-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
forward++
}
- c.curSelection[1] = ToCharPos(forward, c.y, c.v.buf) + 1
+ c.curSelection[1] = ToCharPos(forward, c.y, c.Buf) + 1
c.origSelection[1] = c.curSelection[1]
+ c.SetLoc(c.curSelection[1])
}
// AddWordToSelection adds the word the cursor is currently on to the selection
backward--
}
- c.curSelection[0] = ToCharPos(backward, c.y, c.v.buf)
+ c.curSelection[0] = ToCharPos(backward, c.y, c.Buf)
c.curSelection[1] = c.origSelection[1]
}
if loc > c.origSelection[1] {
forward := c.x
- for forward < Count(c.v.buf.lines[c.y])-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
+ for forward < Count(c.Buf.Lines[c.y])-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
forward++
}
- c.curSelection[1] = ToCharPos(forward, c.y, c.v.buf) + 1
+ c.curSelection[1] = ToCharPos(forward, c.y, c.Buf) + 1
c.curSelection[0] = c.origSelection[0]
}
+
+ c.SetLoc(c.curSelection[1])
}
// SelectTo selects from the current cursor location to the given location
c.curSelection[1] = loc
} else {
c.curSelection[0] = loc
- c.curSelection[1] = c.origSelection[0] + 1
+ c.curSelection[1] = c.origSelection[0]
}
}
func (c *Cursor) WordRight() {
c.Right()
for IsWhitespace(c.RuneUnder(c.x)) {
+ if c.x == Count(c.Buf.Lines[c.y]) {
+ return
+ }
c.Right()
}
for !IsWhitespace(c.RuneUnder(c.x)) {
+ if c.x == Count(c.Buf.Lines[c.y]) {
+ return
+ }
c.Right()
}
}
func (c *Cursor) WordLeft() {
c.Left()
for IsWhitespace(c.RuneUnder(c.x)) {
+ if c.x == 0 {
+ return
+ }
c.Left()
}
for !IsWhitespace(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 {
- line := []rune(c.v.buf.lines[c.y])
+ line := []rune(c.Buf.Lines[c.y])
if len(line) == 0 {
return '\n'
}
if c.y > 0 {
c.y--
- runes := []rune(c.v.buf.lines[c.y])
+ runes := []rune(c.Buf.Lines[c.y])
c.x = c.GetCharPosInLine(c.y, c.lastVisualX)
if c.x > len(runes) {
c.x = len(runes)
// Down moves the cursor down one line (if possible)
func (c *Cursor) Down() {
- if c.y < len(c.v.buf.lines)-1 {
+ if c.y < c.Buf.NumLines-1 {
c.y++
- runes := []rune(c.v.buf.lines[c.y])
+ runes := []rune(c.Buf.Lines[c.y])
c.x = c.GetCharPosInLine(c.y, c.lastVisualX)
if c.x > len(runes) {
c.x = len(runes)
// 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() {
+ if c.Loc() == c.Buf.Len() {
return
}
- if c.x < Count(c.v.buf.lines[c.y]) {
+ if c.x < Count(c.Buf.Lines[c.y]) {
c.x++
} else {
c.Down()
// End moves the cursor to the end of the line it is on
func (c *Cursor) End() {
- c.x = Count(c.v.buf.lines[c.y])
+ c.x = Count(c.Buf.Lines[c.y])
c.lastVisualX = c.GetVisualX()
}
// 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 {
// Get the tab size
- tabSize := settings.TabSize
+ tabSize := int(settings["tabsize"].(float64))
// This is the visual line -- every \t replaced with the correct number of spaces
- visualLine := strings.Replace(c.v.buf.lines[lineNum], "\t", "\t"+Spaces(tabSize-1), -1)
+ visualLine := strings.Replace(c.Buf.Lines[lineNum], "\t", "\t"+Spaces(tabSize-1), -1)
if visualPos > Count(visualLine) {
visualPos = Count(visualLine)
}
// GetVisualX returns the x value of the cursor in visual spaces
func (c *Cursor) GetVisualX() int {
- runes := []rune(c.v.buf.lines[c.y])
- tabSize := settings.TabSize
+ runes := []rune(c.Buf.Lines[c.y])
+ tabSize := int(settings["tabsize"].(float64))
return c.x + NumOccurences(string(runes[:c.x]), '\t')*(tabSize-1)
}
func (c *Cursor) Relocate() {
if c.y < 0 {
c.y = 0
- } else if c.y >= len(c.v.buf.lines) {
- c.y = len(c.v.buf.lines) - 1
+ } else if c.y >= c.Buf.NumLines {
+ c.y = c.Buf.NumLines - 1
}
if c.x < 0 {
c.x = 0
- } else if c.x > Count(c.v.buf.lines[c.y]) {
- c.x = Count(c.v.buf.lines[c.y])
- }
-}
-
-// Display draws the cursor to the screen at the correct position
-func (c *Cursor) Display() {
- // Don't draw the cursor if it is out of the viewport or if it has a selection
- if (c.y-c.v.topline < 0 || c.y-c.v.topline > c.v.height-1) || c.HasSelection() {
- screen.HideCursor()
- } else {
- screen.ShowCursor(c.GetVisualX()+c.v.lineNumOffset-c.v.leftCol, c.y-c.v.topline)
+ } else if c.x > Count(c.Buf.Lines[c.y]) {
+ c.x = Count(c.Buf.Lines[c.y])
}
}