]> git.lizzy.rs Git - micro.git/blob - cmd/micro/command.go
Small fix to redraw location
[micro.git] / cmd / micro / command.go
1 package main
2
3 import (
4         "bytes"
5         "io/ioutil"
6         "os"
7         "os/exec"
8         "os/signal"
9         "regexp"
10         "strings"
11
12         "github.com/mitchellh/go-homedir"
13 )
14
15 type Command struct {
16         action      func([]string)
17         completions []Completion
18 }
19
20 type StrCommand struct {
21         action      string
22         completions []Completion
23 }
24
25 var commands map[string]Command
26
27 var commandActions = map[string]func([]string){
28         "Set":     Set,
29         "Run":     Run,
30         "Bind":    Bind,
31         "Quit":    Quit,
32         "Save":    Save,
33         "Replace": Replace,
34         "VSplit":  VSplit,
35         "HSplit":  HSplit,
36         "Tab":     NewTab,
37         "Help":    Help,
38 }
39
40 // InitCommands initializes the default commands
41 func InitCommands() {
42         commands = make(map[string]Command)
43
44         defaults := DefaultCommands()
45         parseCommands(defaults)
46 }
47
48 func parseCommands(userCommands map[string]StrCommand) {
49         for k, v := range userCommands {
50                 MakeCommand(k, v.action, v.completions...)
51         }
52 }
53
54 // MakeCommand is a function to easily create new commands
55 // This can be called by plugins in Lua so that plugins can define their own commands
56 func MakeCommand(name, function string, completions ...Completion) {
57         action := commandActions[function]
58         if _, ok := commandActions[function]; !ok {
59                 // If the user seems to be binding a function that doesn't exist
60                 // We hope that it's a lua function that exists and bind it to that
61                 action = LuaFunctionCommand(function)
62         }
63
64         commands[name] = Command{action, completions}
65 }
66
67 // DefaultCommands returns a map containing micro's default commands
68 func DefaultCommands() map[string]StrCommand {
69         return map[string]StrCommand{
70                 "set":     StrCommand{"Set", []Completion{OptionCompletion, NoCompletion}},
71                 "bind":    StrCommand{"Bind", []Completion{NoCompletion}},
72                 "run":     StrCommand{"Run", []Completion{NoCompletion}},
73                 "quit":    StrCommand{"Quit", []Completion{NoCompletion}},
74                 "save":    StrCommand{"Save", []Completion{NoCompletion}},
75                 "replace": StrCommand{"Replace", []Completion{NoCompletion}},
76                 "vsplit":  StrCommand{"VSplit", []Completion{FileCompletion, NoCompletion}},
77                 "hsplit":  StrCommand{"HSplit", []Completion{FileCompletion, NoCompletion}},
78                 "tab":     StrCommand{"Tab", []Completion{FileCompletion, NoCompletion}},
79                 "help":    StrCommand{"Help", []Completion{HelpCompletion, NoCompletion}},
80         }
81 }
82
83 // Help tries to open the given help page in a horizontal split
84 func Help(args []string) {
85         if len(args) < 1 {
86                 // Open the default help if the user just typed "> help"
87                 CurView().openHelp("help")
88         } else {
89                 helpPage := args[0]
90                 if _, ok := helpPages[helpPage]; ok {
91                         CurView().openHelp(helpPage)
92                 } else {
93                         messenger.Error("Sorry, no help for ", helpPage)
94                 }
95         }
96 }
97
98 // VSplit opens a vertical split with file given in the first argument
99 // If no file is given, it opens an empty buffer in a new split
100 func VSplit(args []string) {
101         if len(args) == 0 {
102                 CurView().VSplit(NewBuffer([]byte{}, ""))
103         } else {
104                 filename := args[0]
105                 home, _ := homedir.Dir()
106                 filename = strings.Replace(filename, "~", home, 1)
107                 file, err := ioutil.ReadFile(filename)
108
109                 var buf *Buffer
110                 if err != nil {
111                         // File does not exist -- create an empty buffer with that name
112                         buf = NewBuffer([]byte{}, filename)
113                 } else {
114                         buf = NewBuffer(file, filename)
115                 }
116                 CurView().VSplit(buf)
117         }
118 }
119
120 // HSplit opens a horizontal split with file given in the first argument
121 // If no file is given, it opens an empty buffer in a new split
122 func HSplit(args []string) {
123         if len(args) == 0 {
124                 CurView().HSplit(NewBuffer([]byte{}, ""))
125         } else {
126                 filename := args[0]
127                 home, _ := homedir.Dir()
128                 filename = strings.Replace(filename, "~", home, 1)
129                 file, err := ioutil.ReadFile(filename)
130
131                 var buf *Buffer
132                 if err != nil {
133                         // File does not exist -- create an empty buffer with that name
134                         buf = NewBuffer([]byte{}, filename)
135                 } else {
136                         buf = NewBuffer(file, filename)
137                 }
138                 CurView().HSplit(buf)
139         }
140 }
141
142 // NewTab opens the given file in a new tab
143 func NewTab(args []string) {
144         if len(args) == 0 {
145                 CurView().AddTab(true)
146         } else {
147                 filename := args[0]
148                 home, _ := homedir.Dir()
149                 filename = strings.Replace(filename, "~", home, 1)
150                 file, _ := ioutil.ReadFile(filename)
151
152                 tab := NewTabFromView(NewView(NewBuffer(file, filename)))
153                 tab.SetNum(len(tabs))
154                 tabs = append(tabs, tab)
155                 curTab++
156                 if len(tabs) == 2 {
157                         for _, t := range tabs {
158                                 for _, v := range t.views {
159                                         v.ToggleTabbar()
160                                 }
161                         }
162                 }
163         }
164 }
165
166 // Set sets an option
167 func Set(args []string) {
168         if len(args) < 2 {
169                 return
170         }
171
172         option := strings.TrimSpace(args[0])
173         value := strings.TrimSpace(args[1])
174
175         SetOptionAndSettings(option, value)
176 }
177
178 // Bind creates a new keybinding
179 func Bind(args []string) {
180         if len(args) != 2 {
181                 messenger.Error("Incorrect number of arguments")
182                 return
183         }
184         BindKey(args[0], args[1])
185 }
186
187 // Run runs a shell command in the background
188 func Run(args []string) {
189         // Run a shell command in the background (openTerm is false)
190         HandleShellCommand(strings.Join(args, " "), false)
191 }
192
193 // Quit closes the main view
194 func Quit(args []string) {
195         // Close the main view
196         CurView().Quit(true)
197 }
198
199 // Save saves the buffer in the main view
200 func Save(args []string) {
201         // Save the main view
202         CurView().Save(true)
203 }
204
205 // Replace runs search and replace
206 func Replace(args []string) {
207         // This is a regex to parse the replace expression
208         // We allow no quotes if there are no spaces, but if you want to search
209         // for or replace an expression with spaces, you can add double quotes
210         r := regexp.MustCompile(`"[^"\\]*(?:\\.[^"\\]*)*"|[^\s]*`)
211         replaceCmd := r.FindAllString(strings.Join(args, " "), -1)
212         if len(replaceCmd) < 2 {
213                 // We need to find both a search and replace expression
214                 messenger.Error("Invalid replace statement: " + strings.Join(args, " "))
215                 return
216         }
217
218         var flags string
219         if len(replaceCmd) == 3 {
220                 // The user included some flags
221                 flags = replaceCmd[2]
222         }
223
224         search := string(replaceCmd[0])
225         replace := string(replaceCmd[1])
226
227         // If the search and replace expressions have quotes, we need to remove those
228         if strings.HasPrefix(search, `"`) && strings.HasSuffix(search, `"`) {
229                 search = search[1 : len(search)-1]
230         }
231         if strings.HasPrefix(replace, `"`) && strings.HasSuffix(replace, `"`) {
232                 replace = replace[1 : len(replace)-1]
233         }
234
235         // We replace all escaped double quotes to real double quotes
236         search = strings.Replace(search, `\"`, `"`, -1)
237         replace = strings.Replace(replace, `\"`, `"`, -1)
238         // Replace some things so users can actually insert newlines and tabs in replacements
239         replace = strings.Replace(replace, "\\n", "\n", -1)
240         replace = strings.Replace(replace, "\\t", "\t", -1)
241
242         regex, err := regexp.Compile(search)
243         if err != nil {
244                 // There was an error with the user's regex
245                 messenger.Error(err.Error())
246                 return
247         }
248
249         view := CurView()
250
251         found := 0
252         for {
253                 match := regex.FindStringIndex(view.Buf.String())
254                 if match == nil {
255                         break
256                 }
257                 found++
258                 if strings.Contains(flags, "c") {
259                         // The 'check' flag was used
260                         Search(search, view, true)
261                         view.Relocate()
262                         if settings["syntax"].(bool) {
263                                 view.matches = Match(view)
264                         }
265                         RedrawAll()
266                         choice, canceled := messenger.YesNoPrompt("Perform replacement? (y,n)")
267                         if canceled {
268                                 if view.Cursor.HasSelection() {
269                                         view.Cursor.Loc = view.Cursor.CurSelection[0]
270                                         view.Cursor.ResetSelection()
271                                 }
272                                 messenger.Reset()
273                                 return
274                         }
275                         if choice {
276                                 view.Cursor.DeleteSelection()
277                                 view.Buf.Insert(FromCharPos(match[0], view.Buf), replace)
278                                 view.Cursor.ResetSelection()
279                                 messenger.Reset()
280                         } else {
281                                 if view.Cursor.HasSelection() {
282                                         searchStart = ToCharPos(view.Cursor.CurSelection[1], view.Buf)
283                                 } else {
284                                         searchStart = ToCharPos(view.Cursor.Loc, view.Buf)
285                                 }
286                                 continue
287                         }
288                 } else {
289                         view.Buf.Replace(FromCharPos(match[0], view.Buf), FromCharPos(match[1], view.Buf), replace)
290                 }
291         }
292         view.Cursor.Relocate()
293
294         if found > 1 {
295                 messenger.Message("Replaced ", found, " occurences of ", search)
296         } else if found == 1 {
297                 messenger.Message("Replaced ", found, " occurence of ", search)
298         } else {
299                 messenger.Message("Nothing matched ", search)
300         }
301 }
302
303 // RunShellCommand executes a shell command and returns the output/error
304 func RunShellCommand(input string) (string, error) {
305         inputCmd := strings.Split(input, " ")[0]
306         args := strings.Split(input, " ")[1:]
307
308         cmd := exec.Command(inputCmd, args...)
309         outputBytes := &bytes.Buffer{}
310         cmd.Stdout = outputBytes
311         cmd.Stderr = outputBytes
312         cmd.Start()
313         err := cmd.Wait() // wait for command to finish
314         outstring := outputBytes.String()
315         return outstring, err
316 }
317
318 // HandleShellCommand runs the shell command
319 // The openTerm argument specifies whether a terminal should be opened (for viewing output
320 // or interacting with stdin)
321 func HandleShellCommand(input string, openTerm bool) {
322         inputCmd := strings.Split(input, " ")[0]
323         if !openTerm {
324                 // Simply run the command in the background and notify the user when it's done
325                 messenger.Message("Running...")
326                 go func() {
327                         output, err := RunShellCommand(input)
328                         totalLines := strings.Split(output, "\n")
329
330                         if len(totalLines) < 3 {
331                                 if err == nil {
332                                         messenger.Message(inputCmd, " exited without error")
333                                 } else {
334                                         messenger.Message(inputCmd, " exited with error: ", err, ": ", output)
335                                 }
336                         } else {
337                                 messenger.Message(output)
338                         }
339                         // We have to make sure to redraw
340                         RedrawAll()
341                 }()
342         } else {
343                 // Shut down the screen because we're going to interact directly with the shell
344                 screen.Fini()
345                 screen = nil
346
347                 args := strings.Split(input, " ")[1:]
348
349                 // Set up everything for the command
350                 cmd := exec.Command(inputCmd, args...)
351                 cmd.Stdin = os.Stdin
352                 cmd.Stdout = os.Stdout
353                 cmd.Stderr = os.Stderr
354
355                 // This is a trap for Ctrl-C so that it doesn't kill micro
356                 // Instead we trap Ctrl-C to kill the program we're running
357                 c := make(chan os.Signal, 1)
358                 signal.Notify(c, os.Interrupt)
359                 go func() {
360                         for range c {
361                                 cmd.Process.Kill()
362                         }
363                 }()
364
365                 // Start the command
366                 cmd.Start()
367                 cmd.Wait()
368
369                 // This is just so we don't return right away and let the user press enter to return
370                 TermMessage("")
371
372                 // Start the screen back up
373                 InitScreen()
374         }
375 }
376
377 // HandleCommand handles input from the user
378 func HandleCommand(input string) {
379         inputCmd := strings.Split(input, " ")[0]
380         args := strings.Split(input, " ")[1:]
381
382         if _, ok := commands[inputCmd]; !ok {
383                 messenger.Error("Unkown command ", inputCmd)
384         } else {
385                 commands[inputCmd].action(args)
386         }
387 }