}
}
+// SelectTo selects from the current cursor location to the given location
+func (c *Cursor) SelectTo(loc int) {
+ if loc > c.origSelection[0] {
+ c.curSelection[0] = c.origSelection[0]
+ c.curSelection[1] = loc
+ } else {
+ c.curSelection[0] = loc
+ c.curSelection[1] = c.origSelection[0] + 1
+ }
+}
+
+// WordRight moves the cursor one word to the right
+func (c *Cursor) WordRight() {
+ c.Right()
+ for IsWhitespace(c.RuneUnder(c.x)) {
+ c.Right()
+ }
+ for !IsWhitespace(c.RuneUnder(c.x)) {
+ c.Right()
+ }
+}
+
+// WordLeft moves the cursor one word to the left
+func (c *Cursor) WordLeft() {
+ c.Left()
+ for IsWhitespace(c.RuneUnder(c.x)) {
+ c.Left()
+ }
+ for !IsWhitespace(c.RuneUnder(c.x)) {
+ 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])
+ if len(line) == 0 {
+ return '\n'
+ }
if x >= len(line) {
- x = len(line) - 1
+ return '\n'
} else if x < 0 {
x = 0
}
// 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)
if 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
+ tabSize := int(settings["tabsize"].(float64))
return c.x + NumOccurences(string(runes[:c.x]), '\t')*(tabSize-1)
}
+// 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 >= len(c.v.buf.lines) {
+ c.y = len(c.v.buf.lines) - 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