]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/command.go
Count replacements in replaceall correctly
[micro.git] / cmd / micro / command.go
index 98e0bb673b238c3296e0776bfc6af82ab88841cf..89f2d22fd586f85f8adc6c98f7cb31cf0d20a12d 100644 (file)
@@ -1,16 +1,14 @@
 package main
 
 import (
-       "bytes"
        "fmt"
        "os"
-       "os/exec"
-       "os/signal"
        "path/filepath"
        "regexp"
        "runtime"
        "strconv"
        "strings"
+       "unicode/utf8"
 
        humanize "github.com/dustin/go-humanize"
        "github.com/zyedidia/micro/cmd/micro/shellwords"
@@ -56,6 +54,7 @@ func init() {
                "Pwd":        Pwd,
                "Open":       Open,
                "TabSwitch":  TabSwitch,
+               "Term":       Term,
                "MemUsage":   MemUsage,
                "Retab":      Retab,
                "Raw":        Raw,
@@ -114,12 +113,35 @@ func DefaultCommands() map[string]StrCommand {
                "pwd":        {"Pwd", []Completion{NoCompletion}},
                "open":       {"Open", []Completion{FileCompletion}},
                "tabswitch":  {"TabSwitch", []Completion{NoCompletion}},
+               "term":       {"Term", []Completion{NoCompletion}},
                "memusage":   {"MemUsage", []Completion{NoCompletion}},
                "retab":      {"Retab", []Completion{NoCompletion}},
                "raw":        {"Raw", []Completion{NoCompletion}},
        }
 }
 
+// CommandEditAction returns a bindable function that opens a prompt with
+// the given string and executes the command when the user presses
+// enter
+func CommandEditAction(prompt string) func(*View, bool) bool {
+       return func(v *View, usePlugin bool) bool {
+               input, canceled := messenger.Prompt("> ", prompt, "Command", CommandCompletion)
+               if !canceled {
+                       HandleCommand(input)
+               }
+               return false
+       }
+}
+
+// CommandAction returns a bindable function which executes the
+// given command
+func CommandAction(cmd string) func(*View, bool) bool {
+       return func(v *View, usePlugin bool) bool {
+               HandleCommand(cmd)
+               return false
+       }
+}
+
 // PluginCmd installs, removes, updates, lists, or searches for given plugins
 func PluginCmd(args []string) {
        if len(args) >= 1 {
@@ -155,7 +177,7 @@ func PluginCmd(args []string) {
                                        continue
                                }
                        }
-                       if !IsSpaces(removed) {
+                       if !IsSpaces([]byte(removed)) {
                                messenger.Message("Removed ", removed)
                        } else {
                                messenger.Error("The requested plugins do not exist")
@@ -203,10 +225,14 @@ func PluginCmd(args []string) {
        }
 }
 
+// Retab changes all spaces to tabs or all tabs to spaces
+// depending on the user's settings
 func Retab(args []string) {
        CurView().Retab(true)
 }
 
+// Raw opens a new raw view which displays the escape sequences micro
+// is receiving in real-time
 func Raw(args []string) {
        buf := NewBufferFromString("", "Raw events")
 
@@ -365,24 +391,10 @@ func VSplit(args []string) {
        if len(args) == 0 {
                CurView().VSplit(NewBufferFromString("", ""))
        } else {
-               filename := args[0]
-               filename = ReplaceHome(filename)
-               file, err := os.Open(filename)
-               fileInfo, _ := os.Stat(filename)
-
-               if err == nil && fileInfo.IsDir() {
-                       messenger.Error(filename, " is a directory")
-                       return
-               }
-
-               defer file.Close()
-
-               var buf *Buffer
+               buf, err := NewBufferFromFile(args[0])
                if err != nil {
-                       // File does not exist -- create an empty buffer with that name
-                       buf = NewBufferFromString("", filename)
-               } else {
-                       buf = NewBuffer(file, FSize(file), filename)
+                       messenger.Error(err)
+                       return
                }
                CurView().VSplit(buf)
        }
@@ -394,24 +406,10 @@ func HSplit(args []string) {
        if len(args) == 0 {
                CurView().HSplit(NewBufferFromString("", ""))
        } else {
-               filename := args[0]
-               filename = ReplaceHome(filename)
-               file, err := os.Open(filename)
-               fileInfo, _ := os.Stat(filename)
-
-               if err == nil && fileInfo.IsDir() {
-                       messenger.Error(filename, " is a directory")
-                       return
-               }
-
-               defer file.Close()
-
-               var buf *Buffer
+               buf, err := NewBufferFromFile(args[0])
                if err != nil {
-                       // File does not exist -- create an empty buffer with that name
-                       buf = NewBufferFromString("", filename)
-               } else {
-                       buf = NewBuffer(file, FSize(file), filename)
+                       messenger.Error(err)
+                       return
                }
                CurView().HSplit(buf)
        }
@@ -434,23 +432,10 @@ func NewTab(args []string) {
        if len(args) == 0 {
                CurView().AddTab(true)
        } else {
-               filename := args[0]
-               filename = ReplaceHome(filename)
-               file, err := os.Open(filename)
-               fileInfo, _ := os.Stat(filename)
-
-               if err == nil && fileInfo.IsDir() {
-                       messenger.Error(filename, " is a directory")
-                       return
-               }
-
-               defer file.Close()
-
-               var buf *Buffer
+               buf, err := NewBufferFromFile(args[0])
                if err != nil {
-                       buf = NewBufferFromString("", filename)
-               } else {
-                       buf = NewBuffer(file, FSize(file), filename)
+                       messenger.Error(err)
+                       return
                }
 
                tab := NewTabFromView(NewView(buf))
@@ -590,6 +575,7 @@ func Replace(args []string) {
        }
 
        replace := string(args[1])
+       replaceBytes := []byte(replace)
 
        regex, err := regexp.Compile("(?m)" + search)
        if err != nil {
@@ -603,23 +589,16 @@ func Replace(args []string) {
        found := 0
        replaceAll := func() {
                var deltas []Delta
-               deltaXOffset := Count(replace) - Count(search)
                for i := 0; i < view.Buf.LinesNum(); i++ {
-                       matches := regex.FindAllIndex(view.Buf.lines[i].data, -1)
-                       str := string(view.Buf.lines[i].data)
-
-                       if matches != nil {
-                               xOffset := 0
-                               for _, m := range matches {
-                                       from := Loc{runePos(m[0], str) + xOffset, i}
-                                       to := Loc{runePos(m[1], str) + xOffset, i}
+                       newText := regex.ReplaceAllFunc(view.Buf.lines[i].data, func(in []byte) []byte {
+                               found++
+                               return replaceBytes
+                       })
 
-                                       xOffset += deltaXOffset
+                       from := Loc{0, i}
+                       to := Loc{utf8.RuneCount(view.Buf.lines[i].data), i}
 
-                                       deltas = append(deltas, Delta{replace, from, to})
-                                       found++
-                               }
-                       }
+                       deltas = append(deltas, Delta{string(newText), from, to})
                }
                view.Buf.MultipleReplace(deltas)
        }
@@ -682,94 +661,17 @@ func ReplaceAll(args []string) {
        Replace(append(args, "-a"))
 }
 
-// RunShellCommand executes a shell command and returns the output/error
-func RunShellCommand(input string) (string, error) {
-       args, err := shellwords.Split(input)
-       if err != nil {
-               return "", err
+// Term opens a terminal in the current view
+func Term(args []string) {
+       var err error
+       if len(args) == 0 {
+               err = CurView().StartTerminal([]string{os.Getenv("SHELL"), "-i"}, true, false, "")
+       } else {
+               err = CurView().StartTerminal(args, true, false, "")
        }
-       inputCmd := args[0]
-
-       cmd := exec.Command(inputCmd, args[1:]...)
-       outputBytes := &bytes.Buffer{}
-       cmd.Stdout = outputBytes
-       cmd.Stderr = outputBytes
-       cmd.Start()
-       err = cmd.Wait() // wait for command to finish
-       outstring := outputBytes.String()
-       return outstring, err
-}
-
-// HandleShellCommand runs the shell command
-// The openTerm argument specifies whether a terminal should be opened (for viewing output
-// or interacting with stdin)
-func HandleShellCommand(input string, openTerm bool, waitToFinish bool) string {
-       args, err := shellwords.Split(input)
        if err != nil {
-               return ""
-       }
-       inputCmd := args[0]
-       if !openTerm {
-               // Simply run the command in the background and notify the user when it's done
-               messenger.Message("Running...")
-               go func() {
-                       output, err := RunShellCommand(input)
-                       totalLines := strings.Split(output, "\n")
-
-                       if len(totalLines) < 3 {
-                               if err == nil {
-                                       messenger.Message(inputCmd, " exited without error")
-                               } else {
-                                       messenger.Message(inputCmd, " exited with error: ", err, ": ", output)
-                               }
-                       } else {
-                               messenger.Message(output)
-                       }
-                       // We have to make sure to redraw
-                       RedrawAll()
-               }()
-       } else {
-               // Shut down the screen because we're going to interact directly with the shell
-               screen.Fini()
-               screen = nil
-
-               args := args[1:]
-
-               // Set up everything for the command
-               var output string
-               cmd := exec.Command(inputCmd, args...)
-               cmd.Stdin = os.Stdin
-               cmd.Stdout = os.Stdout
-               cmd.Stderr = os.Stderr
-
-               // This is a trap for Ctrl-C so that it doesn't kill micro
-               // Instead we trap Ctrl-C to kill the program we're running
-               c := make(chan os.Signal, 1)
-               signal.Notify(c, os.Interrupt)
-               go func() {
-                       for range c {
-                               cmd.Process.Kill()
-                       }
-               }()
-
-               cmd.Start()
-               err := cmd.Wait()
-
-               if err != nil {
-                       output = err.Error()
-               }
-
-               if waitToFinish {
-                       // This is just so we don't return right away and let the user press enter to return
-                       TermMessage("")
-               }
-
-               // Start the screen back up
-               InitScreen()
-
-               return output
+               messenger.Error(err)
        }
-       return ""
 }
 
 // HandleCommand handles input from the user