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:]
14 commands := []string{"set", "quit", "save", "replace"}
19 for _, c := range commands {
20 if strings.HasPrefix(c, inputCmd) {
33 if view.CanClose("Quit anyway? ") {
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, " "))
48 if len(replaceCmd) == 3 {
49 // The user included some flags
53 search := string(replaceCmd[0])
54 replace := string(replaceCmd[1])
56 if strings.HasPrefix(search, `"`) && strings.HasSuffix(search, `"`) {
57 search = search[1 : len(search)-1]
59 if strings.HasPrefix(replace, `"`) && strings.HasSuffix(replace, `"`) {
60 replace = replace[1 : len(replace)-1]
63 search = strings.Replace(search, `\"`, `"`, -1)
64 replace = strings.Replace(replace, `\"`, `"`, -1)
66 // messenger.Error(search + " -> " + replace)
68 regex, err := regexp.Compile(search)
70 messenger.Error(err.Error())
76 match := regex.FindStringIndex(view.buf.text)
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)
89 view.eh.Replace(match[0], match[1], replace)
92 messenger.Message("Nothing matched " + search)
95 messenger.Error("Unknown command: " + inputCmd)