func (c *Cursor) GetCharPosInLine(lineNum, visualPos int) int {
	tabSize := int(c.buf.Settings["tabsize"].(float64))
	visualLineLen := StringWidth(c.buf.Line(lineNum), tabSize)
	if visualPos > visualLineLen {
		visualPos = visualLineLen
	}
	width := WidthOfLargeRunes(c.buf.Line(lineNum), tabSize)
	if visualPos >= width {
		return visualPos - width
	}
	return visualPos / tabSize
}
GetCharPosInLine gets the char position of a visual x y
coordinate (this is necessary because tabs are 1 char but
4 visual spaces)
package editor
type Cursor struct {
	buf *Buffer
	Loc
	LastVisualX int
	CurSelection [2]Loc
	OrigSelection [2]Loc
	Num int
}
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
}
func (c *Cursor) GotoLoc(l Loc) {
	c.X, c.Y = l.X, l.Y
	c.LastVisualX = c.GetVisualX()
}
func (c *Cursor) ResetSelection() {
	c.CurSelection[0] = c.buf.Start()
	c.CurSelection[1] = c.buf.Start()
}
func (c *Cursor) SetSelectionStart(pos Loc) {
	c.CurSelection[0] = pos
}
func (c *Cursor) SetSelectionEnd(pos Loc) {
	c.CurSelection[1] = pos
}
func (c *Cursor) HasSelection() bool {
	return c.CurSelection[0] != c.CurSelection[1]
}
func (c *Cursor) DeleteSelection() {
	if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
		c.buf.Remove(c.CurSelection[1], c.CurSelection[0])
		c.Loc = c.CurSelection[1]
	} else if !c.HasSelection() {
		return
	} else {
		c.buf.Remove(c.CurSelection[0], c.CurSelection[1])
		c.Loc = c.CurSelection[0]
	}
}
func (c *Cursor) GetSelection() string {
	if InBounds(c.CurSelection[0], c.buf) && InBounds(c.CurSelection[1], c.buf) {
		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])
	}
	return ""
}
func (c *Cursor) SelectLine() {
	c.Start()
	c.SetSelectionStart(c.Loc)
	c.End()
	if c.buf.NumLines-1 > c.Y {
		c.SetSelectionEnd(c.Loc.Move(1, c.buf))
	} else {
		c.SetSelectionEnd(c.Loc)
	}
	c.OrigSelection = c.CurSelection
}
func (c *Cursor) AddLineToSelection() {
	if c.Loc.LessThan(c.OrigSelection[0]) {
		c.Start()
		c.SetSelectionStart(c.Loc)
		c.SetSelectionEnd(c.OrigSelection[1])
	}
	if c.Loc.GreaterThan(c.OrigSelection[1]) {
		c.End()
		c.SetSelectionEnd(c.Loc.Move(1, c.buf))
		c.SetSelectionStart(c.OrigSelection[0])
	}
	if c.Loc.LessThan(c.OrigSelection[1]) && c.Loc.GreaterThan(c.OrigSelection[0]) {
		c.CurSelection = c.OrigSelection
	}
}
func (c *Cursor) SelectWord() {
	if len(c.buf.Line(c.Y)) == 0 {
		return
	}
	if !IsWordChar(string(c.RuneUnder(c.X))) {
		c.SetSelectionStart(c.Loc)
		c.SetSelectionEnd(c.Loc.Move(1, c.buf))
		c.OrigSelection = c.CurSelection
		return
	}
	forward, backward := c.X, c.X
	for backward > 0 && IsWordChar(string(c.RuneUnder(backward-1))) {
		backward--
	}
	c.SetSelectionStart(Loc{backward, c.Y})
	c.OrigSelection[0] = c.CurSelection[0]
	for forward < Count(c.buf.Line(c.Y))-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
		forward++
	}
	c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
	c.OrigSelection[1] = c.CurSelection[1]
	c.Loc = c.CurSelection[1]
}
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 && IsWordChar(string(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
		for forward < Count(c.buf.Line(c.Y))-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
			forward++
		}
		c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
		c.SetSelectionStart(c.OrigSelection[0])
	}
	c.Loc = c.CurSelection[1]
}
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])
	}
}
func (c *Cursor) WordRight() {
	for IsWhitespace(c.RuneUnder(c.X)) {
		if c.X == Count(c.buf.Line(c.Y)) {
			c.Right()
			return
		}
		c.Right()
	}
	c.Right()
	for IsWordChar(string(c.RuneUnder(c.X))) {
		if c.X == Count(c.buf.Line(c.Y)) {
			return
		}
		c.Right()
	}
}
func (c *Cursor) WordLeft() {
	c.Left()
	for IsWhitespace(c.RuneUnder(c.X)) {
		if c.X == 0 {
			return
		}
		c.Left()
	}
	c.Left()
	for IsWordChar(string(c.RuneUnder(c.X))) {
		if c.X == 0 {
			return
		}
		c.Left()
	}
	c.Right()
}
func (c *Cursor) RuneUnder(x int) rune {
	line := []rune(c.buf.Line(c.Y))
	if len(line) == 0 {
		return '\n'
	}
	if x >= len(line) {
		return '\n'
	} else if x < 0 {
		x = 0
	}
	return line[x]
}
func (c *Cursor) UpN(amount int) {
	proposedY := c.Y - amount
	if proposedY < 0 {
		proposedY = 0
		c.LastVisualX = 0
	} else if proposedY >= c.buf.NumLines {
		proposedY = c.buf.NumLines - 1
	}
	runes := []rune(c.buf.Line(proposedY))
	c.X = c.GetCharPosInLine(proposedY, c.LastVisualX)
	if c.X > len(runes) || (amount < 0 && proposedY == c.Y) {
		c.X = len(runes)
	}
	c.Y = proposedY
}
func (c *Cursor) DownN(amount int) {
	c.UpN(-amount)
}
func (c *Cursor) Up() {
	c.UpN(1)
}
func (c *Cursor) Down() {
	c.DownN(1)
}
func (c *Cursor) Left() {
	if c.Loc == c.buf.Start() {
		return
	}
	if c.X > 0 {
		c.X--
	} else {
		c.Up()
		c.End()
	}
	c.LastVisualX = c.GetVisualX()
}
func (c *Cursor) Right() {
	if c.Loc == c.buf.End() {
		return
	}
	if c.X < Count(c.buf.Line(c.Y)) {
		c.X++
	} else {
		c.Down()
		c.Start()
	}
	c.LastVisualX = c.GetVisualX()
}
func (c *Cursor) End() {
	c.X = Count(c.buf.Line(c.Y))
	c.LastVisualX = c.GetVisualX()
}
func (c *Cursor) Start() {
	c.X = 0
	c.LastVisualX = c.GetVisualX()
}
func (c *Cursor) StartOfText() {
	c.Start()
	for IsWhitespace(c.RuneUnder(c.X)) {
		if c.X == Count(c.buf.Line(c.Y)) {
			break
		}
		c.Right()
	}
}