15 humanize "github.com/dustin/go-humanize"
16 "github.com/zyedidia/micro/cmd/micro/shellwords"
19 // A Command contains a action (a function to call) as well as information about how to autocomplete the command
22 completions []Completion
25 // A StrCommand is similar to a command but keeps the name of the action
26 type StrCommand struct {
28 completions []Completion
31 var commands map[string]Command
33 var commandActions map[string]func([]string)
36 commandActions = map[string]func([]string){
46 "ReplaceAll": ReplaceAll,
52 "ToggleLog": ToggleLog,
58 "TabSwitch": TabSwitch,
65 // InitCommands initializes the default commands
67 commands = make(map[string]Command)
69 defaults := DefaultCommands()
70 parseCommands(defaults)
73 func parseCommands(userCommands map[string]StrCommand) {
74 for k, v := range userCommands {
75 MakeCommand(k, v.action, v.completions...)
79 // MakeCommand is a function to easily create new commands
80 // This can be called by plugins in Lua so that plugins can define their own commands
81 func MakeCommand(name, function string, completions ...Completion) {
82 action := commandActions[function]
83 if _, ok := commandActions[function]; !ok {
84 // If the user seems to be binding a function that doesn't exist
85 // We hope that it's a lua function that exists and bind it to that
86 action = LuaFunctionCommand(function)
89 commands[name] = Command{action, completions}
92 // DefaultCommands returns a map containing micro's default commands
93 func DefaultCommands() map[string]StrCommand {
94 return map[string]StrCommand{
95 "set": {"Set", []Completion{OptionCompletion, OptionValueCompletion}},
96 "setlocal": {"SetLocal", []Completion{OptionCompletion, OptionValueCompletion}},
97 "show": {"Show", []Completion{OptionCompletion, NoCompletion}},
98 "showkey": {"ShowKey", []Completion{NoCompletion}},
99 "bind": {"Bind", []Completion{NoCompletion}},
100 "run": {"Run", []Completion{NoCompletion}},
101 "quit": {"Quit", []Completion{NoCompletion}},
102 "save": {"Save", []Completion{NoCompletion}},
103 "replace": {"Replace", []Completion{NoCompletion}},
104 "replaceall": {"ReplaceAll", []Completion{NoCompletion}},
105 "vsplit": {"VSplit", []Completion{FileCompletion, NoCompletion}},
106 "hsplit": {"HSplit", []Completion{FileCompletion, NoCompletion}},
107 "tab": {"Tab", []Completion{FileCompletion, NoCompletion}},
108 "help": {"Help", []Completion{HelpCompletion, NoCompletion}},
109 "eval": {"Eval", []Completion{NoCompletion}},
110 "log": {"ToggleLog", []Completion{NoCompletion}},
111 "plugin": {"Plugin", []Completion{PluginCmdCompletion, PluginNameCompletion}},
112 "reload": {"Reload", []Completion{NoCompletion}},
113 "cd": {"Cd", []Completion{FileCompletion}},
114 "pwd": {"Pwd", []Completion{NoCompletion}},
115 "open": {"Open", []Completion{FileCompletion}},
116 "tabswitch": {"TabSwitch", []Completion{NoCompletion}},
117 "memusage": {"MemUsage", []Completion{NoCompletion}},
118 "retab": {"Retab", []Completion{NoCompletion}},
119 "raw": {"Raw", []Completion{NoCompletion}},
123 func CommandAction(cmd string) func(*View, bool) bool {
124 return func(v *View, usePlugin bool) bool {
130 // PluginCmd installs, removes, updates, lists, or searches for given plugins
131 func PluginCmd(args []string) {
135 installedVersions := GetInstalledVersions(false)
136 for _, plugin := range args[1:] {
137 pp := GetAllPluginPackages().Get(plugin)
139 messenger.Error("Unknown plugin \"" + plugin + "\"")
140 } else if err := pp.IsInstallable(); err != nil {
141 messenger.Error("Error installing ", plugin, ": ", err)
143 for _, installed := range installedVersions {
144 if pp.Name == installed.pack.Name {
145 if pp.Versions[0].Version.Compare(installed.Version) == 1 {
146 messenger.Error(pp.Name, " is already installed but out-of-date: use 'plugin update ", pp.Name, "' to update")
148 messenger.Error(pp.Name, " is already installed")
157 for _, plugin := range args[1:] {
158 // check if the plugin exists.
159 if _, ok := loadedPlugins[plugin]; ok {
160 UninstallPlugin(plugin)
161 removed += plugin + " "
165 if !IsSpaces(removed) {
166 messenger.Message("Removed ", removed)
168 messenger.Error("The requested plugins do not exist")
171 UpdatePlugins(args[1:])
173 plugins := GetInstalledVersions(false)
174 messenger.AddLog("----------------")
175 messenger.AddLog("The following plugins are currently installed:\n")
176 for _, p := range plugins {
177 messenger.AddLog(fmt.Sprintf("%s (%s)", p.pack.Name, p.Version))
179 messenger.AddLog("----------------")
180 if len(plugins) > 0 {
181 if CurView().Type != vtLog {
182 ToggleLog([]string{})
186 plugins := SearchPlugin(args[1:])
187 messenger.Message(len(plugins), " plugins found")
188 for _, p := range plugins {
189 messenger.AddLog("----------------")
190 messenger.AddLog(p.String())
192 messenger.AddLog("----------------")
193 if len(plugins) > 0 {
194 if CurView().Type != vtLog {
195 ToggleLog([]string{})
199 packages := GetAllPluginPackages()
200 messenger.AddLog("Available Plugins:")
201 for _, pkg := range packages {
202 messenger.AddLog(pkg.Name)
204 if CurView().Type != vtLog {
205 ToggleLog([]string{})
209 messenger.Error("Not enough arguments")
213 func Retab(args []string) {
214 CurView().Retab(true)
217 func Raw(args []string) {
218 buf := NewBufferFromString("", "Raw events")
221 view.Buf.Insert(view.Cursor.Loc, "Warning: Showing raw event escape codes\n")
222 view.Buf.Insert(view.Cursor.Loc, "Use CtrlQ to exit\n")
224 tab := NewTabFromView(view)
225 tab.SetNum(len(tabs))
226 tabs = append(tabs, tab)
227 curTab = len(tabs) - 1
229 for _, t := range tabs {
230 for _, v := range t.views {
237 // TabSwitch switches to a given tab either by name or by number
238 func TabSwitch(args []string) {
240 num, err := strconv.Atoi(args[0])
242 // Check for tab with this name
245 for _, t := range tabs {
246 v := t.views[t.CurView]
247 if v.Buf.GetName() == args[0] {
253 messenger.Error("Could not find tab: ", err)
257 if num >= 0 && num < len(tabs) {
260 messenger.Error("Invalid tab index")
266 // Cd changes the current working directory
267 func Cd(args []string) {
269 path := ReplaceHome(args[0])
270 err := os.Chdir(path)
272 messenger.Error("Error with cd: ", err)
276 for _, tab := range tabs {
277 for _, view := range tab.views {
278 if len(view.Buf.name) == 0 {
282 view.Buf.Path, _ = MakeRelative(view.Buf.AbsPath, wd)
283 if p, _ := filepath.Abs(view.Buf.Path); !strings.Contains(p, wd) {
284 view.Buf.Path = view.Buf.AbsPath
291 // MemUsage prints micro's memory usage
292 // Alloc shows how many bytes are currently in use
293 // Sys shows how many bytes have been requested from the operating system
294 // NumGC shows how many times the GC has been run
295 // Note that Go commonly reserves more memory from the OS than is currently in-use/required
296 // Additionally, even if Go returns memory to the OS, the OS does not always claim it because
297 // there may be plenty of memory to spare
298 func MemUsage(args []string) {
299 var mem runtime.MemStats
300 runtime.ReadMemStats(&mem)
302 messenger.Message(fmt.Sprintf("Alloc: %v, Sys: %v, NumGC: %v", humanize.Bytes(mem.Alloc), humanize.Bytes(mem.Sys), mem.NumGC))
305 // Pwd prints the current working directory
306 func Pwd(args []string) {
307 wd, err := os.Getwd()
309 messenger.Message(err.Error())
311 messenger.Message(wd)
315 // Open opens a new buffer with a given filename
316 func Open(args []string) {
319 // the filename might or might not be quoted, so unquote first then join the strings.
320 args, err := shellwords.Split(filename)
322 messenger.Error("Error parsing args ", err)
325 filename = strings.Join(args, " ")
327 CurView().Open(filename)
329 messenger.Error("No filename")
333 // ToggleLog toggles the log view
334 func ToggleLog(args []string) {
335 buffer := messenger.getBuffer()
336 if CurView().Type != vtLog {
337 CurView().HSplit(buffer)
338 CurView().Type = vtLog
340 buffer.Cursor.Loc = buffer.Start()
342 buffer.Cursor.Loc = buffer.End()
349 // Reload reloads all files (syntax files, colorschemes...)
350 func Reload(args []string) {
354 // Help tries to open the given help page in a horizontal split
355 func Help(args []string) {
357 // Open the default help if the user just typed "> help"
358 CurView().openHelp("help")
361 if FindRuntimeFile(RTHelp, helpPage) != nil {
362 CurView().openHelp(helpPage)
364 messenger.Error("Sorry, no help for ", helpPage)
369 // VSplit opens a vertical split with file given in the first argument
370 // If no file is given, it opens an empty buffer in a new split
371 func VSplit(args []string) {
373 CurView().VSplit(NewBufferFromString("", ""))
376 filename = ReplaceHome(filename)
377 file, err := os.Open(filename)
378 fileInfo, _ := os.Stat(filename)
380 if err == nil && fileInfo.IsDir() {
381 messenger.Error(filename, " is a directory")
389 // File does not exist -- create an empty buffer with that name
390 buf = NewBufferFromString("", filename)
392 buf = NewBuffer(file, FSize(file), filename)
394 CurView().VSplit(buf)
398 // HSplit opens a horizontal split with file given in the first argument
399 // If no file is given, it opens an empty buffer in a new split
400 func HSplit(args []string) {
402 CurView().HSplit(NewBufferFromString("", ""))
405 filename = ReplaceHome(filename)
406 file, err := os.Open(filename)
407 fileInfo, _ := os.Stat(filename)
409 if err == nil && fileInfo.IsDir() {
410 messenger.Error(filename, " is a directory")
418 // File does not exist -- create an empty buffer with that name
419 buf = NewBufferFromString("", filename)
421 buf = NewBuffer(file, FSize(file), filename)
423 CurView().HSplit(buf)
427 // Eval evaluates a lua expression
428 func Eval(args []string) {
430 err := L.DoString(args[0])
435 messenger.Error("Not enough arguments")
439 // NewTab opens the given file in a new tab
440 func NewTab(args []string) {
442 CurView().AddTab(true)
445 filename = ReplaceHome(filename)
446 file, err := os.Open(filename)
447 fileInfo, _ := os.Stat(filename)
449 if err == nil && fileInfo.IsDir() {
450 messenger.Error(filename, " is a directory")
458 buf = NewBufferFromString("", filename)
460 buf = NewBuffer(file, FSize(file), filename)
463 tab := NewTabFromView(NewView(buf))
464 tab.SetNum(len(tabs))
465 tabs = append(tabs, tab)
466 curTab = len(tabs) - 1
468 for _, t := range tabs {
469 for _, v := range t.views {
477 // Set sets an option
478 func Set(args []string) {
480 messenger.Error("Not enough arguments")
487 SetOptionAndSettings(option, value)
490 // SetLocal sets an option local to the buffer
491 func SetLocal(args []string) {
493 messenger.Error("Not enough arguments")
500 err := SetLocalOption(option, value, CurView())
502 messenger.Error(err.Error())
506 // Show shows the value of the given option
507 func Show(args []string) {
509 messenger.Error("Please provide an option to show")
513 option := GetOption(args[0])
516 messenger.Error(args[0], " is not a valid option")
520 messenger.Message(option)
523 // ShowKey displays the action that a key is bound to
524 func ShowKey(args []string) {
526 messenger.Error("Please provide a key to show")
530 if action, ok := bindingsStr[args[0]]; ok {
531 messenger.Message(action)
533 messenger.Message(args[0], " has no binding")
537 // Bind creates a new keybinding
538 func Bind(args []string) {
540 messenger.Error("Not enough arguments")
543 BindKey(args[0], args[1])
546 // Run runs a shell command in the background
547 func Run(args []string) {
548 // Run a shell command in the background (openTerm is false)
549 HandleShellCommand(shellwords.Join(args...), false, true)
552 // Quit closes the main view
553 func Quit(args []string) {
554 // Close the main view
558 // Save saves the buffer in the main view
559 func Save(args []string) {
561 // Save the main view
564 CurView().Buf.SaveAs(args[0])
568 // Replace runs search and replace
569 func Replace(args []string) {
570 if len(args) < 2 || len(args) > 4 {
571 // We need to find both a search and replace expression
572 messenger.Error("Invalid replace statement: " + strings.Join(args, " "))
580 for _, arg := range args[2:] {
587 messenger.Error("Invalid flag: " + arg)
593 search := string(args[0])
596 search = regexp.QuoteMeta(search)
599 replace := string(args[1])
601 regex, err := regexp.Compile("(?m)" + search)
603 // There was an error with the user's regex
604 messenger.Error(err.Error())
611 replaceAll := func() {
613 deltaXOffset := Count(replace) - Count(search)
614 for i := 0; i < view.Buf.LinesNum(); i++ {
615 matches := regex.FindAllIndex(view.Buf.lines[i].data, -1)
616 str := string(view.Buf.lines[i].data)
620 for _, m := range matches {
621 from := Loc{runePos(m[0], str) + xOffset, i}
622 to := Loc{runePos(m[1], str) + xOffset, i}
624 xOffset += deltaXOffset
626 deltas = append(deltas, Delta{replace, from, to})
631 view.Buf.MultipleReplace(deltas)
638 // The 'check' flag was used
639 Search(search, view, true)
640 if !view.Cursor.HasSelection() {
645 choice, canceled := messenger.LetterPrompt("Perform replacement? (y,n,a)", 'y', 'n', 'a')
647 if view.Cursor.HasSelection() {
648 view.Cursor.Loc = view.Cursor.CurSelection[0]
649 view.Cursor.ResetSelection()
653 } else if choice == 'a' {
654 if view.Cursor.HasSelection() {
655 view.Cursor.Loc = view.Cursor.CurSelection[0]
656 view.Cursor.ResetSelection()
661 } else if choice == 'y' {
662 view.Cursor.DeleteSelection()
663 view.Buf.Insert(view.Cursor.Loc, replace)
664 view.Cursor.ResetSelection()
668 if view.Cursor.HasSelection() {
669 searchStart = view.Cursor.CurSelection[1]
671 searchStart = view.Cursor.Loc
675 view.Cursor.Relocate()
678 messenger.Message("Replaced ", found, " occurrences of ", search)
679 } else if found == 1 {
680 messenger.Message("Replaced ", found, " occurrence of ", search)
682 messenger.Message("Nothing matched ", search)
686 // ReplaceAll replaces search term all at once
687 func ReplaceAll(args []string) {
688 // aliased to Replace command
689 Replace(append(args, "-a"))
692 // RunShellCommand executes a shell command and returns the output/error
693 func RunShellCommand(input string) (string, error) {
694 args, err := shellwords.Split(input)
700 cmd := exec.Command(inputCmd, args[1:]...)
701 outputBytes := &bytes.Buffer{}
702 cmd.Stdout = outputBytes
703 cmd.Stderr = outputBytes
705 err = cmd.Wait() // wait for command to finish
706 outstring := outputBytes.String()
707 return outstring, err
710 // HandleShellCommand runs the shell command
711 // The openTerm argument specifies whether a terminal should be opened (for viewing output
712 // or interacting with stdin)
713 func HandleShellCommand(input string, openTerm bool, waitToFinish bool) string {
714 args, err := shellwords.Split(input)
720 // Simply run the command in the background and notify the user when it's done
721 messenger.Message("Running...")
723 output, err := RunShellCommand(input)
724 totalLines := strings.Split(output, "\n")
726 if len(totalLines) < 3 {
728 messenger.Message(inputCmd, " exited without error")
730 messenger.Message(inputCmd, " exited with error: ", err, ": ", output)
733 messenger.Message(output)
735 // We have to make sure to redraw
739 // Shut down the screen because we're going to interact directly with the shell
745 // Set up everything for the command
747 cmd := exec.Command(inputCmd, args...)
749 cmd.Stdout = os.Stdout
750 cmd.Stderr = os.Stderr
752 // This is a trap for Ctrl-C so that it doesn't kill micro
753 // Instead we trap Ctrl-C to kill the program we're running
754 c := make(chan os.Signal, 1)
755 signal.Notify(c, os.Interrupt)
770 // This is just so we don't return right away and let the user press enter to return
774 // Start the screen back up
782 // HandleCommand handles input from the user
783 func HandleCommand(input string) {
784 args, err := shellwords.Split(input)
786 messenger.Error("Error parsing args ", err)
792 if _, ok := commands[inputCmd]; !ok {
793 messenger.Error("Unknown command ", inputCmd)
795 commands[inputCmd].action(args[1:])