From cd229c1f5b5258910b14074f6fb7e4889dfce8ff Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Mon, 3 Sep 2018 16:54:56 -0400 Subject: [PATCH] Add some more actions --- cmd/micro/action/actions.go | 50 ++++++++++++++++++++++++++++++++++++- cmd/micro/buffer/buffer.go | 7 ++++++ cmd/micro/micro.go | 2 +- cmd/micro/util/util.go | 5 ++++ 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/cmd/micro/action/actions.go b/cmd/micro/action/actions.go index f47bcd6a..6bae6b50 100644 --- a/cmd/micro/action/actions.go +++ b/cmd/micro/action/actions.go @@ -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 diff --git a/cmd/micro/buffer/buffer.go b/cmd/micro/buffer/buffer.go index f68c5b58..38e9057f 100644 --- a/cmd/micro/buffer/buffer.go +++ b/cmd/micro/buffer/buffer.go @@ -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" +} diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index 1c3ab4db..86374bac 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -1,5 +1,5 @@ package main -//asdf + import ( "flag" "fmt" diff --git a/cmd/micro/util/util.go b/cmd/micro/util/util.go index 90f2c15e..874f3b48 100644 --- a/cmd/micro/util/util.go +++ b/cmd/micro/util/util.go @@ -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 {