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