Add some more actions

This commit is contained in:
Zachary Yedidia
2018-09-03 16:54:56 -04:00
parent 47c899ae46
commit cd229c1f5b
4 changed files with 62 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ import (
"os"
"unicode/utf8"
"github.com/zyedidia/micro/cmd/micro/buffer"
"github.com/zyedidia/micro/cmd/micro/screen"
"github.com/zyedidia/micro/cmd/micro/util"
"github.com/zyedidia/tcell"
@@ -308,21 +309,68 @@ func (h *BufHandler) Backspace() bool {
// DeleteWordRight deletes the word to the right of the cursor
func (h *BufHandler) DeleteWordRight() bool {
h.SelectWordRight()
if h.Cursor.HasSelection() {
h.Cursor.DeleteSelection()
h.Cursor.ResetSelection()
}
return true
}
// DeleteWordLeft deletes the word to the left of the cursor
func (h *BufHandler) DeleteWordLeft() bool {
h.SelectWordLeft()
if h.Cursor.HasSelection() {
h.Cursor.DeleteSelection()
h.Cursor.ResetSelection()
}
return true
}
// Delete deletes the next character
func (h *BufHandler) Delete() bool {
if h.Cursor.HasSelection() {
h.Cursor.DeleteSelection()
h.Cursor.ResetSelection()
} else {
loc := h.Cursor.Loc
if loc.LessThan(h.Buf.End()) {
h.Buf.Remove(loc, loc.Move(1, h.Buf))
}
}
return true
}
// IndentSelection indents the current selection
func (h *BufHandler) IndentSelection() bool {
if h.Cursor.HasSelection() {
start := h.Cursor.CurSelection[0]
end := h.Cursor.CurSelection[1]
if end.Y < start.Y {
start, end = end, start
h.Cursor.SetSelectionStart(start)
h.Cursor.SetSelectionEnd(end)
}
startY := start.Y
endY := end.Move(-1, h.Buf).Y
endX := end.Move(-1, h.Buf).X
tabsize := int(h.Buf.Settings["tabsize"].(float64))
indentsize := len(h.Buf.IndentString(tabsize))
for y := startY; y <= endY; y++ {
h.Buf.Insert(buffer.Loc{0, y}, h.Buf.IndentString(tabsize))
if y == startY && start.X > 0 {
h.Cursor.SetSelectionStart(start.Move(indentsize, h.Buf))
}
if y == endY {
h.Cursor.SetSelectionEnd(buffer.Loc{endX + indentsize + 1, endY})
}
}
h.Cursor.Relocate()
return true
}
return false
return false
}
@@ -333,7 +381,7 @@ func (h *BufHandler) OutdentLine() bool {
// OutdentSelection takes the current selection and moves it back one indent level
func (h *BufHandler) OutdentSelection() bool {
return false
return true
}
// InsertTab inserts a tab or spaces

View File

@@ -391,3 +391,10 @@ func (b *Buffer) UpdateRules() {
}
}
}
func (b *Buffer) IndentString(tabsize int) string {
if b.Settings["tabstospaces"].(bool) {
return Spaces(tabsize)
}
return "\t"
}

View File

@@ -1,5 +1,5 @@
package main
//asdf
import (
"flag"
"fmt"

View File

@@ -139,6 +139,11 @@ func IsWordChar(r rune) bool {
return (r >= '0' && r <= '9') || (r >= 'A' && r <= 'Z') || (r >= 'a' && r <= 'z') || (r == '_')
}
// Spaces returns a string with n spaces
func Spaces(n int) string {
return strings.Repeat(" ", n)
}
// IsSpaces checks if a given string is only spaces
func IsSpaces(str []byte) bool {
for _, c := range str {