]> git.lizzy.rs Git - micro.git/commitdiff
Add some more actions
authorZachary Yedidia <zyedidia@gmail.com>
Mon, 3 Sep 2018 20:54:56 +0000 (16:54 -0400)
committerZachary Yedidia <zyedidia@gmail.com>
Wed, 25 Dec 2019 22:05:10 +0000 (17:05 -0500)
cmd/micro/action/actions.go
cmd/micro/buffer/buffer.go
cmd/micro/micro.go
cmd/micro/util/util.go

index f47bcd6a83d27e1422cf625197575a46a0ad1d0c..6bae6b50ec9bb5e79da6763dbb61273510b6b9d7 100644 (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
index f68c5b58c07fb037d85d0e3cba15a6caf4e4a65b..38e9057f8131d382349864b111db8f7b06710da0 100644 (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"
+}
index 1c3ab4dbedfa3416ed4b951a3bab37485f3f4dc8..86374baccf37f4d9b0420de52f4bfc33b8ca9d5e 100644 (file)
@@ -1,5 +1,5 @@
 package main
-//asdf
+
 import (
        "flag"
        "fmt"
index 90f2c15e3896866d63e00eabe1d015e6570dcf9e..874f3b48d4c9d68235cf532b8530024ae4d09799 100644 (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 {