]> git.lizzy.rs Git - micro.git/blob - cmd/micro/command.go
Boss mode
[micro.git] / cmd / micro / command.go
1 package main
2
3 import (
4         "os"
5         "os/exec"
6         "regexp"
7         "strings"
8 )
9
10 func HandleShellCommand(input string, view *View) {
11         inputCmd := strings.Split(input, " ")[0]
12         args := strings.Split(input, " ")[1:]
13
14         // Execute Command
15         cmdout := exec.Command(inputCmd, args...)
16         output, err := cmdout.Output()
17         if err != nil {
18                 messenger.Error("Error: " + err.Error())
19                 return
20         }
21
22         // Display last line of output
23         messenger.Message(string(output))
24
25 }
26
27 // HandleCommand handles input from the user
28 func HandleCommand(input string, view *View) {
29         inputCmd := strings.Split(input, " ")[0]
30         args := strings.Split(input, " ")[1:]
31
32         commands := []string{"set", "quit", "save", "replace"}
33
34         i := 0
35         cmd := inputCmd
36
37         for _, c := range commands {
38                 if strings.HasPrefix(c, inputCmd) {
39                         i++
40                         cmd = c
41                 }
42         }
43         if i == 1 {
44                 inputCmd = cmd
45         }
46
47         switch inputCmd {
48         case "set":
49                 SetOption(view, args)
50         case "quit":
51                 if view.CanClose("Quit anyway? ") {
52                         screen.Fini()
53                         os.Exit(0)
54                 }
55         case "save":
56                 view.Save()
57         case "replace":
58                 r := regexp.MustCompile(`"[^"\\]*(?:\\.[^"\\]*)*"|[^\s]*`)
59                 replaceCmd := r.FindAllString(strings.Join(args, " "), -1)
60                 if len(replaceCmd) < 2 {
61                         messenger.Error("Invalid replace statement: " + strings.Join(args, " "))
62                         return
63                 }
64
65                 var flags string
66                 if len(replaceCmd) == 3 {
67                         // The user included some flags
68                         flags = replaceCmd[2]
69                 }
70
71                 search := string(replaceCmd[0])
72                 replace := string(replaceCmd[1])
73
74                 if strings.HasPrefix(search, `"`) && strings.HasSuffix(search, `"`) {
75                         search = search[1 : len(search)-1]
76                 }
77                 if strings.HasPrefix(replace, `"`) && strings.HasSuffix(replace, `"`) {
78                         replace = replace[1 : len(replace)-1]
79                 }
80
81                 search = strings.Replace(search, `\"`, `"`, -1)
82                 replace = strings.Replace(replace, `\"`, `"`, -1)
83
84                 // messenger.Error(search + " -> " + replace)
85
86                 regex, err := regexp.Compile(search)
87                 if err != nil {
88                         messenger.Error(err.Error())
89                         return
90                 }
91
92                 found := false
93                 for {
94                         match := regex.FindStringIndex(view.buf.text)
95                         if match == nil {
96                                 break
97                         }
98                         found = true
99                         if strings.Contains(flags, "c") {
100                                 //      // The 'check' flag was used
101                                 //      if messenger.YesNoPrompt("Perform replacement?") {
102                                 //              view.eh.Replace(match[0], match[1], replace)
103                                 //      } else {
104                                 //              continue
105                                 //      }
106                         }
107                         view.eh.Replace(match[0], match[1], replace)
108                 }
109                 if !found {
110                         messenger.Message("Nothing matched " + search)
111                 }
112         default:
113                 messenger.Error("Unknown command: " + inputCmd)
114         }
115 }