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