]> git.lizzy.rs Git - micro.git/blob - cmd/micro/command.go
9d6913418713651d704de0590b99ca3d061950fc
[micro.git] / cmd / micro / command.go
1 package main
2
3 import (
4         "bytes"
5         "fmt"
6         "io"
7         "os"
8         "os/exec"
9         "os/signal"
10         "path/filepath"
11         "regexp"
12         "runtime"
13         "strconv"
14         "strings"
15
16         humanize "github.com/dustin/go-humanize"
17         "github.com/mitchellh/go-homedir"
18 )
19
20 // A Command contains a action (a function to call) as well as information about how to autocomplete the command
21 type Command struct {
22         action      func([]string)
23         completions []Completion
24 }
25
26 // A StrCommand is similar to a command but keeps the name of the action
27 type StrCommand struct {
28         action      string
29         completions []Completion
30 }
31
32 var commands map[string]Command
33
34 var commandActions map[string]func([]string)
35
36 func init() {
37         commandActions = map[string]func([]string){
38                 "Set":        Set,
39                 "SetLocal":   SetLocal,
40                 "Show":       Show,
41                 "Run":        Run,
42                 "Bind":       Bind,
43                 "Quit":       Quit,
44                 "Save":       Save,
45                 "Replace":    Replace,
46                 "ReplaceAll": ReplaceAll,
47                 "VSplit":     VSplit,
48                 "HSplit":     HSplit,
49                 "Tab":        NewTab,
50                 "Help":       Help,
51                 "Eval":       Eval,
52                 "ToggleLog":  ToggleLog,
53                 "Plugin":     PluginCmd,
54                 "Reload":     Reload,
55                 "Cd":         Cd,
56                 "Pwd":        Pwd,
57                 "Open":       Open,
58                 "TabSwitch":  TabSwitch,
59                 "MemUsage":   MemUsage,
60         }
61 }
62
63 // InitCommands initializes the default commands
64 func InitCommands() {
65         commands = make(map[string]Command)
66
67         defaults := DefaultCommands()
68         parseCommands(defaults)
69 }
70
71 func parseCommands(userCommands map[string]StrCommand) {
72         for k, v := range userCommands {
73                 MakeCommand(k, v.action, v.completions...)
74         }
75 }
76
77 // MakeCommand is a function to easily create new commands
78 // This can be called by plugins in Lua so that plugins can define their own commands
79 func MakeCommand(name, function string, completions ...Completion) {
80         action := commandActions[function]
81         if _, ok := commandActions[function]; !ok {
82                 // If the user seems to be binding a function that doesn't exist
83                 // We hope that it's a lua function that exists and bind it to that
84                 action = LuaFunctionCommand(function)
85         }
86
87         commands[name] = Command{action, completions}
88 }
89
90 // DefaultCommands returns a map containing micro's default commands
91 func DefaultCommands() map[string]StrCommand {
92         return map[string]StrCommand{
93                 "set":        {"Set", []Completion{OptionCompletion, NoCompletion}},
94                 "setlocal":   {"SetLocal", []Completion{OptionCompletion, NoCompletion}},
95                 "show":       {"Show", []Completion{OptionCompletion, NoCompletion}},
96                 "bind":       {"Bind", []Completion{NoCompletion}},
97                 "run":        {"Run", []Completion{NoCompletion}},
98                 "quit":       {"Quit", []Completion{NoCompletion}},
99                 "save":       {"Save", []Completion{NoCompletion}},
100                 "replace":    {"Replace", []Completion{NoCompletion}},
101                 "replaceall": {"ReplaceAll", []Completion{NoCompletion}},
102                 "vsplit":     {"VSplit", []Completion{FileCompletion, NoCompletion}},
103                 "hsplit":     {"HSplit", []Completion{FileCompletion, NoCompletion}},
104                 "tab":        {"Tab", []Completion{FileCompletion, NoCompletion}},
105                 "help":       {"Help", []Completion{HelpCompletion, NoCompletion}},
106                 "eval":       {"Eval", []Completion{NoCompletion}},
107                 "log":        {"ToggleLog", []Completion{NoCompletion}},
108                 "plugin":     {"Plugin", []Completion{PluginCmdCompletion, PluginNameCompletion}},
109                 "reload":     {"Reload", []Completion{NoCompletion}},
110                 "cd":         {"Cd", []Completion{FileCompletion}},
111                 "pwd":        {"Pwd", []Completion{NoCompletion}},
112                 "open":       {"Open", []Completion{FileCompletion}},
113                 "tabswitch":  {"TabSwitch", []Completion{NoCompletion}},
114                 "memusage":   {"MemUsage", []Completion{NoCompletion}},
115         }
116 }
117
118 // PluginCmd installs, removes, updates, lists, or searches for given plugins
119 func PluginCmd(args []string) {
120         if len(args) >= 1 {
121                 switch args[0] {
122                 case "install":
123                         installedVersions := GetInstalledVersions(false)
124                         for _, plugin := range args[1:] {
125                                 pp := GetAllPluginPackages().Get(plugin)
126                                 if pp == nil {
127                                         messenger.Error("Unknown plugin \"" + plugin + "\"")
128                                 } else if err := pp.IsInstallable(); err != nil {
129                                         messenger.Error("Error installing ", plugin, ": ", err)
130                                 } else {
131                                         for _, installed := range installedVersions {
132                                                 if pp.Name == installed.pack.Name {
133                                                         if pp.Versions[0].Version.Compare(installed.Version) == 1 {
134                                                                 messenger.Error(pp.Name, " is already installed but out-of-date: use 'plugin update ", pp.Name, "' to update")
135                                                         } else {
136                                                                 messenger.Error(pp.Name, " is already installed")
137                                                         }
138                                                 }
139                                         }
140                                         pp.Install()
141                                 }
142                         }
143                 case "remove":
144                         removed := ""
145                         for _, plugin := range args[1:] {
146                                 // check if the plugin exists.
147                                 if _, ok := loadedPlugins[plugin]; ok {
148                                         UninstallPlugin(plugin)
149                                         removed += plugin + " "
150                                         continue
151                                 }
152                         }
153                         if !IsSpaces(removed) {
154                                 messenger.Message("Removed ", removed)
155                         } else {
156                                 messenger.Error("The requested plugins do not exist")
157                         }
158                 case "update":
159                         UpdatePlugins(args[1:])
160                 case "list":
161                         plugins := GetInstalledVersions(false)
162                         messenger.AddLog("----------------")
163                         messenger.AddLog("The following plugins are currently installed:\n")
164                         for _, p := range plugins {
165                                 messenger.AddLog(fmt.Sprintf("%s (%s)", p.pack.Name, p.Version))
166                         }
167                         messenger.AddLog("----------------")
168                         if len(plugins) > 0 {
169                                 if CurView().Type != vtLog {
170                                         ToggleLog([]string{})
171                                 }
172                         }
173                 case "search":
174                         plugins := SearchPlugin(args[1:])
175                         messenger.Message(len(plugins), " plugins found")
176                         for _, p := range plugins {
177                                 messenger.AddLog("----------------")
178                                 messenger.AddLog(p.String())
179                         }
180                         messenger.AddLog("----------------")
181                         if len(plugins) > 0 {
182                                 if CurView().Type != vtLog {
183                                         ToggleLog([]string{})
184                                 }
185                         }
186                 case "available":
187                         packages := GetAllPluginPackages()
188                         messenger.AddLog("Available Plugins:")
189                         for _, pkg := range packages {
190                                 messenger.AddLog(pkg.Name)
191                         }
192                         if CurView().Type != vtLog {
193                                 ToggleLog([]string{})
194                         }
195                 }
196         } else {
197                 messenger.Error("Not enough arguments")
198         }
199 }
200
201 // TabSwitch switches to a given tab either by name or by number
202 func TabSwitch(args []string) {
203         if len(args) > 0 {
204                 num, err := strconv.Atoi(args[0])
205                 if err != nil {
206                         // Check for tab with this name
207
208                         found := false
209                         for _, t := range tabs {
210                                 v := t.views[t.CurView]
211                                 if v.Buf.GetName() == args[0] {
212                                         curTab = v.TabNum
213                                         found = true
214                                 }
215                         }
216                         if !found {
217                                 messenger.Error("Could not find tab: ", err)
218                         }
219                 } else {
220                         num--
221                         if num >= 0 && num < len(tabs) {
222                                 curTab = num
223                         } else {
224                                 messenger.Error("Invalid tab index")
225                         }
226                 }
227         }
228 }
229
230 // Cd changes the current working directory
231 func Cd(args []string) {
232         if len(args) > 0 {
233                 home, _ := homedir.Dir()
234                 path := strings.Replace(args[0], "~", home, 1)
235                 os.Chdir(path)
236                 for _, tab := range tabs {
237                         for _, view := range tab.views {
238                                 wd, _ := os.Getwd()
239                                 view.Buf.Path, _ = MakeRelative(view.Buf.AbsPath, wd)
240                                 if p, _ := filepath.Abs(view.Buf.Path); !strings.Contains(p, wd) {
241                                         view.Buf.Path = view.Buf.AbsPath
242                                 }
243                         }
244                 }
245         }
246 }
247
248 // MemUsage prints micro's memory usage
249 // Alloc shows how many bytes are currently in use
250 // Sys shows how many bytes have been requested from the operating system
251 // NumGC shows how many times the GC has been run
252 // Note that Go commonly reserves more memory from the OS than is currently in-use/required
253 // Additionally, even if Go returns memory to the OS, the OS does not always claim it because
254 // there may be plenty of memory to spare
255 func MemUsage(args []string) {
256         var mem runtime.MemStats
257         runtime.ReadMemStats(&mem)
258
259         messenger.Message(fmt.Sprintf("Alloc: %v, Sys: %v, NumGC: %v", humanize.Bytes(mem.Alloc), humanize.Bytes(mem.Sys), mem.NumGC))
260 }
261
262 // Pwd prints the current working directory
263 func Pwd(args []string) {
264         wd, err := os.Getwd()
265         if err != nil {
266                 messenger.Message(err.Error())
267         } else {
268                 messenger.Message(wd)
269         }
270 }
271
272 // Open opens a new buffer with a given filename
273 func Open(args []string) {
274         if len(args) > 0 {
275                 filename := args[0]
276                 // the filename might or might not be quoted, so unquote first then join the strings.
277                 filename = strings.Join(SplitCommandArgs(filename), " ")
278
279                 CurView().Open(filename)
280         } else {
281                 messenger.Error("No filename")
282         }
283 }
284
285 // ToggleLog toggles the log view
286 func ToggleLog(args []string) {
287         buffer := messenger.getBuffer()
288         if CurView().Type != vtLog {
289                 CurView().HSplit(buffer)
290                 CurView().Type = vtLog
291                 RedrawAll()
292                 buffer.Cursor.Loc = buffer.Start()
293                 CurView().Relocate()
294                 buffer.Cursor.Loc = buffer.End()
295                 CurView().Relocate()
296         } else {
297                 CurView().Quit(true)
298         }
299 }
300
301 // Reload reloads all files (syntax files, colorschemes...)
302 func Reload(args []string) {
303         LoadAll()
304 }
305
306 // Help tries to open the given help page in a horizontal split
307 func Help(args []string) {
308         if len(args) < 1 {
309                 // Open the default help if the user just typed "> help"
310                 CurView().openHelp("help")
311         } else {
312                 helpPage := args[0]
313                 if FindRuntimeFile(RTHelp, helpPage) != nil {
314                         CurView().openHelp(helpPage)
315                 } else {
316                         messenger.Error("Sorry, no help for ", helpPage)
317                 }
318         }
319 }
320
321 // VSplit opens a vertical split with file given in the first argument
322 // If no file is given, it opens an empty buffer in a new split
323 func VSplit(args []string) {
324         if len(args) == 0 {
325                 CurView().VSplit(NewBufferFromString("", ""))
326         } else {
327                 filename := args[0]
328                 home, _ := homedir.Dir()
329                 filename = strings.Replace(filename, "~", home, 1)
330                 file, err := os.Open(filename)
331                 fileInfo, _ := os.Stat(filename)
332
333                 if err == nil && fileInfo.IsDir() {
334                         messenger.Error(filename, " is a directory")
335                         return
336                 }
337
338                 defer file.Close()
339
340                 var buf *Buffer
341                 if err != nil {
342                         // File does not exist -- create an empty buffer with that name
343                         buf = NewBufferFromString("", filename)
344                 } else {
345                         buf = NewBuffer(file, FSize(file), filename)
346                 }
347                 CurView().VSplit(buf)
348         }
349 }
350
351 // HSplit opens a horizontal split with file given in the first argument
352 // If no file is given, it opens an empty buffer in a new split
353 func HSplit(args []string) {
354         if len(args) == 0 {
355                 CurView().HSplit(NewBufferFromString("", ""))
356         } else {
357                 filename := args[0]
358                 home, _ := homedir.Dir()
359                 filename = strings.Replace(filename, "~", home, 1)
360                 file, err := os.Open(filename)
361                 fileInfo, _ := os.Stat(filename)
362
363                 if err == nil && fileInfo.IsDir() {
364                         messenger.Error(filename, " is a directory")
365                         return
366                 }
367
368                 defer file.Close()
369
370                 var buf *Buffer
371                 if err != nil {
372                         // File does not exist -- create an empty buffer with that name
373                         buf = NewBufferFromString("", filename)
374                 } else {
375                         buf = NewBuffer(file, FSize(file), filename)
376                 }
377                 CurView().HSplit(buf)
378         }
379 }
380
381 // Eval evaluates a lua expression
382 func Eval(args []string) {
383         if len(args) >= 1 {
384                 err := L.DoString(args[0])
385                 if err != nil {
386                         messenger.Error(err)
387                 }
388         } else {
389                 messenger.Error("Not enough arguments")
390         }
391 }
392
393 // NewTab opens the given file in a new tab
394 func NewTab(args []string) {
395         if len(args) == 0 {
396                 CurView().AddTab(true)
397         } else {
398                 filename := args[0]
399                 home, _ := homedir.Dir()
400                 filename = strings.Replace(filename, "~", home, 1)
401                 file, err := os.Open(filename)
402                 fileInfo, _ := os.Stat(filename)
403
404                 if err == nil && fileInfo.IsDir() {
405                         messenger.Error(filename, " is a directory")
406                         return
407                 }
408
409                 defer file.Close()
410
411                 var buf *Buffer
412                 if err != nil {
413                         buf = NewBufferFromString("", filename)
414                 } else {
415                         buf = NewBuffer(file, FSize(file), filename)
416                 }
417
418                 tab := NewTabFromView(NewView(buf))
419                 tab.SetNum(len(tabs))
420                 tabs = append(tabs, tab)
421                 curTab = len(tabs) - 1
422                 if len(tabs) == 2 {
423                         for _, t := range tabs {
424                                 for _, v := range t.views {
425                                         v.ToggleTabbar()
426                                 }
427                         }
428                 }
429         }
430 }
431
432 // Set sets an option
433 func Set(args []string) {
434         if len(args) < 2 {
435                 messenger.Error("Not enough arguments")
436                 return
437         }
438
439         option := args[0]
440         value := args[1]
441
442         SetOptionAndSettings(option, value)
443 }
444
445 // SetLocal sets an option local to the buffer
446 func SetLocal(args []string) {
447         if len(args) < 2 {
448                 messenger.Error("Not enough arguments")
449                 return
450         }
451
452         option := args[0]
453         value := args[1]
454
455         err := SetLocalOption(option, value, CurView())
456         if err != nil {
457                 messenger.Error(err.Error())
458         }
459 }
460
461 // Show shows the value of the given option
462 func Show(args []string) {
463         if len(args) < 1 {
464                 messenger.Error("Please provide an option to show")
465                 return
466         }
467
468         option := GetOption(args[0])
469
470         if option == nil {
471                 messenger.Error(args[0], " is not a valid option")
472                 return
473         }
474
475         messenger.Message(option)
476 }
477
478 // Bind creates a new keybinding
479 func Bind(args []string) {
480         if len(args) < 2 {
481                 messenger.Error("Not enough arguments")
482                 return
483         }
484         BindKey(args[0], args[1])
485 }
486
487 // Run runs a shell command in the background
488 func Run(args []string) {
489         // Run a shell command in the background (openTerm is false)
490         HandleShellCommand(JoinCommandArgs(args...), false, true)
491 }
492
493 // Quit closes the main view
494 func Quit(args []string) {
495         // Close the main view
496         CurView().Quit(true)
497 }
498
499 // Save saves the buffer in the main view
500 func Save(args []string) {
501         if len(args) == 0 {
502                 // Save the main view
503                 CurView().Save(true)
504         } else {
505                 CurView().Buf.SaveAs(args[0])
506         }
507 }
508
509 // Replace runs search and replace
510 func Replace(args []string) {
511         if len(args) < 2 || len(args) > 3 {
512                 // We need to find both a search and replace expression
513                 messenger.Error("Invalid replace statement: " + strings.Join(args, " "))
514                 return
515         }
516
517         allAtOnce := false
518         if len(args) == 3 {
519                 // user added -a flag
520                 if args[2] == "-a" {
521                         allAtOnce = true
522                 } else {
523                         messenger.Error("Invalid replace flag: " + args[2])
524                         return
525                 }
526         }
527
528         search := string(args[0])
529         replace := string(args[1])
530
531         regex, err := regexp.Compile("(?m)" + search)
532         if err != nil {
533                 // There was an error with the user's regex
534                 messenger.Error(err.Error())
535                 return
536         }
537
538         view := CurView()
539
540         found := 0
541         if allAtOnce {
542                 // var deltas []Delta
543                 for i := 0; i < view.Buf.LinesNum(); i++ {
544                         // view.Buf.lines[i].data = regex.ReplaceAll(view.Buf.lines[i].data, []byte(replace))
545                         for {
546                                 m := regex.FindIndex(view.Buf.lines[i].data)
547
548                                 if m != nil {
549                                         from := Loc{m[0], i}
550                                         to := Loc{m[1], i}
551
552                                         // deltas = append(deltas, Delta{replace, from, to})
553                                         view.Buf.Replace(from, to, replace)
554                                         found++
555                                 } else {
556                                         break
557                                 }
558                         }
559                 }
560                 // view.Buf.MultipleReplace(deltas)
561
562         } else {
563                 for {
564                         // The 'check' flag was used
565                         Search(search, view, true)
566                         if !view.Cursor.HasSelection() {
567                                 break
568                         }
569                         view.Relocate()
570                         RedrawAll()
571                         choice, canceled := messenger.YesNoPrompt("Perform replacement? (y,n)")
572                         if canceled {
573                                 if view.Cursor.HasSelection() {
574                                         view.Cursor.Loc = view.Cursor.CurSelection[0]
575                                         view.Cursor.ResetSelection()
576                                 }
577                                 messenger.Reset()
578                                 break
579                         } else if choice {
580                                 view.Cursor.DeleteSelection()
581                                 view.Buf.Insert(view.Cursor.Loc, replace)
582                                 view.Cursor.ResetSelection()
583                                 messenger.Reset()
584                                 found++
585                         } else {
586                                 if view.Cursor.HasSelection() {
587                                         searchStart = ToCharPos(view.Cursor.CurSelection[1], view.Buf)
588                                 } else {
589                                         searchStart = ToCharPos(view.Cursor.Loc, view.Buf)
590                                 }
591                         }
592                 }
593         }
594         view.Cursor.Relocate()
595
596         if found > 1 {
597                 messenger.Message("Replaced ", found, " occurrences of ", search)
598         } else if found == 1 {
599                 messenger.Message("Replaced ", found, " occurrence of ", search)
600         } else {
601                 messenger.Message("Nothing matched ", search)
602         }
603 }
604
605 // ReplaceAll replaces search term all at once
606 func ReplaceAll(args []string) {
607         // aliased to Replace command
608         Replace(append(args, "-a"))
609 }
610
611 // RunShellCommand executes a shell command and returns the output/error
612 func RunShellCommand(input string) (string, error) {
613         inputCmd := SplitCommandArgs(input)[0]
614         args := SplitCommandArgs(input)[1:]
615
616         cmd := exec.Command(inputCmd, args...)
617         outputBytes := &bytes.Buffer{}
618         cmd.Stdout = outputBytes
619         cmd.Stderr = outputBytes
620         cmd.Start()
621         err := cmd.Wait() // wait for command to finish
622         outstring := outputBytes.String()
623         return outstring, err
624 }
625
626 // HandleShellCommand runs the shell command
627 // The openTerm argument specifies whether a terminal should be opened (for viewing output
628 // or interacting with stdin)
629 func HandleShellCommand(input string, openTerm bool, waitToFinish bool) string {
630         inputCmd := SplitCommandArgs(input)[0]
631         if !openTerm {
632                 // Simply run the command in the background and notify the user when it's done
633                 messenger.Message("Running...")
634                 go func() {
635                         output, err := RunShellCommand(input)
636                         totalLines := strings.Split(output, "\n")
637
638                         if len(totalLines) < 3 {
639                                 if err == nil {
640                                         messenger.Message(inputCmd, " exited without error")
641                                 } else {
642                                         messenger.Message(inputCmd, " exited with error: ", err, ": ", output)
643                                 }
644                         } else {
645                                 messenger.Message(output)
646                         }
647                         // We have to make sure to redraw
648                         RedrawAll()
649                 }()
650         } else {
651                 // Shut down the screen because we're going to interact directly with the shell
652                 screen.Fini()
653                 screen = nil
654
655                 args := SplitCommandArgs(input)[1:]
656
657                 // Set up everything for the command
658                 var outputBuf bytes.Buffer
659                 cmd := exec.Command(inputCmd, args...)
660                 cmd.Stdin = os.Stdin
661                 cmd.Stdout = io.MultiWriter(os.Stdout, &outputBuf)
662                 cmd.Stderr = os.Stderr
663
664                 // This is a trap for Ctrl-C so that it doesn't kill micro
665                 // Instead we trap Ctrl-C to kill the program we're running
666                 c := make(chan os.Signal, 1)
667                 signal.Notify(c, os.Interrupt)
668                 go func() {
669                         for range c {
670                                 cmd.Process.Kill()
671                         }
672                 }()
673
674                 cmd.Start()
675                 err := cmd.Wait()
676
677                 output := outputBuf.String()
678                 if err != nil {
679                         output = err.Error()
680                 }
681
682                 if waitToFinish {
683                         // This is just so we don't return right away and let the user press enter to return
684                         TermMessage("")
685                 }
686
687                 // Start the screen back up
688                 InitScreen()
689
690                 return output
691         }
692         return ""
693 }
694
695 // HandleCommand handles input from the user
696 func HandleCommand(input string) {
697         args := SplitCommandArgs(input)
698         inputCmd := args[0]
699
700         if _, ok := commands[inputCmd]; !ok {
701                 messenger.Error("Unknown command ", inputCmd)
702         } else {
703                 commands[inputCmd].action(args[1:])
704         }
705 }