]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/cursor.go
Merge pull request #87 from aerth/bindings-help
[micro.git] / cmd / micro / cursor.go
index 43f2cc22ed4169f615b99abe478f75c6bd63a287..3a734183e4f45e4e810f4ffc310517d4f848762c 100644 (file)
@@ -204,11 +204,48 @@ func (c *Cursor) AddWordToSelection() {
        }
 }
 
+// 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
        }
@@ -284,7 +321,7 @@ func (c *Cursor) Start() {
 // 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) {
@@ -300,10 +337,26 @@ func (c *Cursor) GetCharPosInLine(lineNum, visualPos int) int {
 // 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