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 // PluginCmd installs, removes, updates, lists, or searches for given plugins
124 func PluginCmd(args []string) {
128 installedVersions := GetInstalledVersions(false)
129 for _, plugin := range args[1:] {
130 pp := GetAllPluginPackages().Get(plugin)
132 messenger.Error("Unknown plugin \"" + plugin + "\"")
133 } else if err := pp.IsInstallable(); err != nil {
134 messenger.Error("Error installing ", plugin, ": ", err)
136 for _, installed := range installedVersions {
137 if pp.Name == installed.pack.Name {
138 if pp.Versions[0].Version.Compare(installed.Version) == 1 {
139 messenger.Error(pp.Name, " is already installed but out-of-date: use 'plugin update ", pp.Name, "' to update")
141 messenger.Error(pp.Name, " is already installed")
150 for _, plugin := range args[1:] {
151 // check if the plugin exists.
152 if _, ok := loadedPlugins[plugin]; ok {
153 UninstallPlugin(plugin)
154 removed += plugin + " "
158 if !IsSpaces(removed) {
159 messenger.Message("Removed ", removed)
161 messenger.Error("The requested plugins do not exist")
164 UpdatePlugins(args[1:])
166 plugins := GetInstalledVersions(false)
167 messenger.AddLog("----------------")
168 messenger.AddLog("The following plugins are currently installed:\n")
169 for _, p := range plugins {
170 messenger.AddLog(fmt.Sprintf("%s (%s)", p.pack.Name, p.Version))
172 messenger.AddLog("----------------")
173 if len(plugins) > 0 {
174 if CurView().Type != vtLog {
175 ToggleLog([]string{})
179 plugins := SearchPlugin(args[1:])
180 messenger.Message(len(plugins), " plugins found")
181 for _, p := range plugins {
182 messenger.AddLog("----------------")
183 messenger.AddLog(p.String())
185 messenger.AddLog("----------------")
186 if len(plugins) > 0 {
187 if CurView().Type != vtLog {
188 ToggleLog([]string{})
192 packages := GetAllPluginPackages()
193 messenger.AddLog("Available Plugins:")
194 for _, pkg := range packages {
195 messenger.AddLog(pkg.Name)
197 if CurView().Type != vtLog {
198 ToggleLog([]string{})
202 messenger.Error("Not enough arguments")
206 func Retab(args []string) {
207 CurView().Retab(true)
210 func Raw(args []string) {
211 buf := NewBufferFromString("", "Raw events")
214 view.Buf.Insert(view.Cursor.Loc, "Warning: Showing raw event escape codes\n")
215 view.Buf.Insert(view.Cursor.Loc, "Use CtrlQ to exit\n")
217 tab := NewTabFromView(view)
218 tab.SetNum(len(tabs))
219 tabs = append(tabs, tab)
220 curTab = len(tabs) - 1
222 for _, t := range tabs {
223 for _, v := range t.views {
230 // TabSwitch switches to a given tab either by name or by number
231 func TabSwitch(args []string) {
233 num, err := strconv.Atoi(args[0])
235 // Check for tab with this name
238 for _, t := range tabs {
239 v := t.views[t.CurView]
240 if v.Buf.GetName() == args[0] {
246 messenger.Error("Could not find tab: ", err)
250 if num >= 0 && num < len(tabs) {
253 messenger.Error("Invalid tab index")
259 // Cd changes the current working directory
260 func Cd(args []string) {
262 path := ReplaceHome(args[0])
263 err := os.Chdir(path)
265 messenger.Error("Error with cd: ", err)
269 for _, tab := range tabs {
270 for _, view := range tab.views {
271 if len(view.Buf.name) == 0 {
275 view.Buf.Path, _ = MakeRelative(view.Buf.AbsPath, wd)
276 if p, _ := filepath.Abs(view.Buf.Path); !strings.Contains(p, wd) {
277 view.Buf.Path = view.Buf.AbsPath
284 // MemUsage prints micro's memory usage
285 // Alloc shows how many bytes are currently in use
286 // Sys shows how many bytes have been requested from the operating system
287 // NumGC shows how many times the GC has been run
288 // Note that Go commonly reserves more memory from the OS than is currently in-use/required
289 // Additionally, even if Go returns memory to the OS, the OS does not always claim it because
290 // there may be plenty of memory to spare
291 func MemUsage(args []string) {
292 var mem runtime.MemStats
293 runtime.ReadMemStats(&mem)
295 messenger.Message(fmt.Sprintf("Alloc: %v, Sys: %v, NumGC: %v", humanize.Bytes(mem.Alloc), humanize.Bytes(mem.Sys), mem.NumGC))
298 // Pwd prints the current working directory
299 func Pwd(args []string) {
300 wd, err := os.Getwd()
302 messenger.Message(err.Error())
304 messenger.Message(wd)
308 // Open opens a new buffer with a given filename
309 func Open(args []string) {
312 // the filename might or might not be quoted, so unquote first then join the strings.
313 args, err := shellwords.Split(filename)
315 messenger.Error("Error parsing args ", err)
318 filename = strings.Join(args, " ")
320 CurView().Open(filename)
322 messenger.Error("No filename")
326 // ToggleLog toggles the log view
327 func ToggleLog(args []string) {
328 buffer := messenger.getBuffer()
329 if CurView().Type != vtLog {
330 CurView().HSplit(buffer)
331 CurView().Type = vtLog
333 buffer.Cursor.Loc = buffer.Start()
335 buffer.Cursor.Loc = buffer.End()
342 // Reload reloads all files (syntax files, colorschemes...)
343 func Reload(args []string) {
347 // Help tries to open the given help page in a horizontal split
348 func Help(args []string) {
350 // Open the default help if the user just typed "> help"
351 CurView().openHelp("help")
354 if FindRuntimeFile(RTHelp, helpPage) != nil {
355 CurView().openHelp(helpPage)
357 messenger.Error("Sorry, no help for ", helpPage)
362 // VSplit opens a vertical split with file given in the first argument
363 // If no file is given, it opens an empty buffer in a new split
364 func VSplit(args []string) {
366 CurView().VSplit(NewBufferFromString("", ""))
369 filename = ReplaceHome(filename)
370 file, err := os.Open(filename)
371 fileInfo, _ := os.Stat(filename)
373 if err == nil && fileInfo.IsDir() {
374 messenger.Error(filename, " is a directory")
382 // File does not exist -- create an empty buffer with that name
383 buf = NewBufferFromString("", filename)
385 buf = NewBuffer(file, FSize(file), filename)
387 CurView().VSplit(buf)
391 // HSplit opens a horizontal split with file given in the first argument
392 // If no file is given, it opens an empty buffer in a new split
393 func HSplit(args []string) {
395 CurView().HSplit(NewBufferFromString("", ""))
398 filename = ReplaceHome(filename)
399 file, err := os.Open(filename)
400 fileInfo, _ := os.Stat(filename)
402 if err == nil && fileInfo.IsDir() {
403 messenger.Error(filename, " is a directory")
411 // File does not exist -- create an empty buffer with that name
412 buf = NewBufferFromString("", filename)
414 buf = NewBuffer(file, FSize(file), filename)
416 CurView().HSplit(buf)
420 // Eval evaluates a lua expression
421 func Eval(args []string) {
423 err := L.DoString(args[0])
428 messenger.Error("Not enough arguments")
432 // NewTab opens the given file in a new tab
433 func NewTab(args []string) {
435 CurView().AddTab(true)
438 filename = ReplaceHome(filename)
439 file, err := os.Open(filename)
440 fileInfo, _ := os.Stat(filename)
442 if err == nil && fileInfo.IsDir() {
443 messenger.Error(filename, " is a directory")
451 buf = NewBufferFromString("", filename)
453 buf = NewBuffer(file, FSize(file), filename)
456 tab := NewTabFromView(NewView(buf))
457 tab.SetNum(len(tabs))
458 tabs = append(tabs, tab)
459 curTab = len(tabs) - 1
461 for _, t := range tabs {
462 for _, v := range t.views {
470 // Set sets an option
471 func Set(args []string) {
473 messenger.Error("Not enough arguments")
480 SetOptionAndSettings(option, value)
483 // SetLocal sets an option local to the buffer
484 func SetLocal(args []string) {
486 messenger.Error("Not enough arguments")
493 err := SetLocalOption(option, value, CurView())
495 messenger.Error(err.Error())
499 // Show shows the value of the given option
500 func Show(args []string) {
502 messenger.Error("Please provide an option to show")
506 option := GetOption(args[0])
509 messenger.Error(args[0], " is not a valid option")
513 messenger.Message(option)
516 // ShowKey displays the action that a key is bound to
517 func ShowKey(args []string) {
519 messenger.Error("Please provide a key to show")
523 if action, ok := bindingsStr[args[0]]; ok {
524 messenger.Message(action)
526 messenger.Message(args[0], " has no binding")
530 // Bind creates a new keybinding
531 func Bind(args []string) {
533 messenger.Error("Not enough arguments")
536 BindKey(args[0], args[1])
539 // Run runs a shell command in the background
540 func Run(args []string) {
541 // Run a shell command in the background (openTerm is false)
542 HandleShellCommand(shellwords.Join(args...), false, true)
545 // Quit closes the main view
546 func Quit(args []string) {
547 // Close the main view
551 // Save saves the buffer in the main view
552 func Save(args []string) {
554 // Save the main view
557 CurView().Buf.SaveAs(args[0])
561 // Replace runs search and replace
562 func Replace(args []string) {
563 if len(args) < 2 || len(args) > 4 {
564 // We need to find both a search and replace expression
565 messenger.Error("Invalid replace statement: " + strings.Join(args, " "))
573 for _, arg := range args[2:] {
580 messenger.Error("Invalid flag: " + arg)
586 search := string(args[0])
589 search = regexp.QuoteMeta(search)
592 replace := string(args[1])
594 regex, err := regexp.Compile("(?m)" + search)
596 // There was an error with the user's regex
597 messenger.Error(err.Error())
604 replaceAll := func() {
606 deltaXOffset := Count(replace) - Count(search)
607 for i := 0; i < view.Buf.LinesNum(); i++ {
608 matches := regex.FindAllIndex(view.Buf.lines[i].data, -1)
609 str := string(view.Buf.lines[i].data)
613 for _, m := range matches {
614 from := Loc{runePos(m[0], str) + xOffset, i}
615 to := Loc{runePos(m[1], str) + xOffset, i}
617 xOffset += deltaXOffset
619 deltas = append(deltas, Delta{replace, from, to})
624 view.Buf.MultipleReplace(deltas)
631 // The 'check' flag was used
632 Search(search, view, true)
633 if !view.Cursor.HasSelection() {
638 choice, canceled := messenger.LetterPrompt("Perform replacement? (y,n,a)", 'y', 'n', 'a')
640 if view.Cursor.HasSelection() {
641 view.Cursor.Loc = view.Cursor.CurSelection[0]
642 view.Cursor.ResetSelection()
646 } else if choice == 'a' {
647 if view.Cursor.HasSelection() {
648 view.Cursor.Loc = view.Cursor.CurSelection[0]
649 view.Cursor.ResetSelection()
654 } else if choice == 'y' {
655 view.Cursor.DeleteSelection()
656 view.Buf.Insert(view.Cursor.Loc, replace)
657 view.Cursor.ResetSelection()
661 if view.Cursor.HasSelection() {
662 searchStart = view.Cursor.CurSelection[1]
664 searchStart = view.Cursor.Loc
668 view.Cursor.Relocate()
671 messenger.Message("Replaced ", found, " occurrences of ", search)
672 } else if found == 1 {
673 messenger.Message("Replaced ", found, " occurrence of ", search)
675 messenger.Message("Nothing matched ", search)
679 // ReplaceAll replaces search term all at once
680 func ReplaceAll(args []string) {
681 // aliased to Replace command
682 Replace(append(args, "-a"))
685 // RunShellCommand executes a shell command and returns the output/error
686 func RunShellCommand(input string) (string, error) {
687 args, err := shellwords.Split(input)
693 cmd := exec.Command(inputCmd, args[1:]...)
694 outputBytes := &bytes.Buffer{}
695 cmd.Stdout = outputBytes
696 cmd.Stderr = outputBytes
698 err = cmd.Wait() // wait for command to finish
699 outstring := outputBytes.String()
700 return outstring, err
703 // HandleShellCommand runs the shell command
704 // The openTerm argument specifies whether a terminal should be opened (for viewing output
705 // or interacting with stdin)
706 func HandleShellCommand(input string, openTerm bool, waitToFinish bool) string {
707 args, err := shellwords.Split(input)
713 // Simply run the command in the background and notify the user when it's done
714 messenger.Message("Running...")
716 output, err := RunShellCommand(input)
717 totalLines := strings.Split(output, "\n")
719 if len(totalLines) < 3 {
721 messenger.Message(inputCmd, " exited without error")
723 messenger.Message(inputCmd, " exited with error: ", err, ": ", output)
726 messenger.Message(output)
728 // We have to make sure to redraw
732 // Shut down the screen because we're going to interact directly with the shell
738 // Set up everything for the command
740 cmd := exec.Command(inputCmd, args...)
742 cmd.Stdout = os.Stdout
743 cmd.Stderr = os.Stderr
745 // This is a trap for Ctrl-C so that it doesn't kill micro
746 // Instead we trap Ctrl-C to kill the program we're running
747 c := make(chan os.Signal, 1)
748 signal.Notify(c, os.Interrupt)
763 // This is just so we don't return right away and let the user press enter to return
767 // Start the screen back up
775 // HandleCommand handles input from the user
776 func HandleCommand(input string) {
777 args, err := shellwords.Split(input)
779 messenger.Error("Error parsing args ", err)
785 if _, ok := commands[inputCmd]; !ok {
786 messenger.Error("Unknown command ", inputCmd)
788 commands[inputCmd].action(args[1:])