13 humanize "github.com/dustin/go-humanize"
14 "github.com/zyedidia/micro/cmd/micro/shellwords"
17 // A Command contains a action (a function to call) as well as information about how to autocomplete the command
20 completions []Completion
23 // A StrCommand is similar to a command but keeps the name of the action
24 type StrCommand struct {
26 completions []Completion
29 var commands map[string]Command
31 var commandActions map[string]func([]string)
34 commandActions = map[string]func([]string){
44 "ReplaceAll": ReplaceAll,
50 "ToggleLog": ToggleLog,
56 "TabSwitch": TabSwitch,
64 // InitCommands initializes the default commands
66 commands = make(map[string]Command)
68 defaults := DefaultCommands()
69 parseCommands(defaults)
72 func parseCommands(userCommands map[string]StrCommand) {
73 for k, v := range userCommands {
74 MakeCommand(k, v.action, v.completions...)
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)
88 commands[name] = Command{action, completions}
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}},
123 // CommandEditAction returns a bindable function that opens a prompt with
124 // the given string and executes the command when the user presses
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)
136 // CommandAction returns a bindable function which executes the
138 func CommandAction(cmd string) func(*View, bool) bool {
139 return func(v *View, usePlugin bool) bool {
145 // PluginCmd installs, removes, updates, lists, or searches for given plugins
146 func PluginCmd(args []string) {
150 installedVersions := GetInstalledVersions(false)
151 for _, plugin := range args[1:] {
152 pp := GetAllPluginPackages().Get(plugin)
154 messenger.Error("Unknown plugin \"" + plugin + "\"")
155 } else if err := pp.IsInstallable(); err != nil {
156 messenger.Error("Error installing ", plugin, ": ", err)
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")
163 messenger.Error(pp.Name, " is already installed")
172 for _, plugin := range args[1:] {
173 // check if the plugin exists.
174 if _, ok := loadedPlugins[plugin]; ok {
175 UninstallPlugin(plugin)
176 removed += plugin + " "
180 if !IsSpaces([]byte(removed)) {
181 messenger.Message("Removed ", removed)
183 messenger.Error("The requested plugins do not exist")
186 UpdatePlugins(args[1:])
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))
194 messenger.AddLog("----------------")
195 if len(plugins) > 0 {
196 if CurView().Type != vtLog {
197 ToggleLog([]string{})
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())
207 messenger.AddLog("----------------")
208 if len(plugins) > 0 {
209 if CurView().Type != vtLog {
210 ToggleLog([]string{})
214 packages := GetAllPluginPackages()
215 messenger.AddLog("Available Plugins:")
216 for _, pkg := range packages {
217 messenger.AddLog(pkg.Name)
219 if CurView().Type != vtLog {
220 ToggleLog([]string{})
224 messenger.Error("Not enough arguments")
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)
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")
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")
243 tab := NewTabFromView(view)
244 tab.SetNum(len(tabs))
245 tabs = append(tabs, tab)
246 curTab = len(tabs) - 1
248 for _, t := range tabs {
249 for _, v := range t.Views {
256 // TabSwitch switches to a given tab either by name or by number
257 func TabSwitch(args []string) {
259 num, err := strconv.Atoi(args[0])
261 // Check for tab with this name
264 for _, t := range tabs {
265 v := t.Views[t.CurView]
266 if v.Buf.GetName() == args[0] {
272 messenger.Error("Could not find tab: ", err)
276 if num >= 0 && num < len(tabs) {
279 messenger.Error("Invalid tab index")
285 // Cd changes the current working directory
286 func Cd(args []string) {
288 path := ReplaceHome(args[0])
289 err := os.Chdir(path)
291 messenger.Error("Error with cd: ", err)
295 for _, tab := range tabs {
296 for _, view := range tab.Views {
297 if len(view.Buf.name) == 0 {
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
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)
321 messenger.Message(fmt.Sprintf("Alloc: %v, Sys: %v, NumGC: %v", humanize.Bytes(mem.Alloc), humanize.Bytes(mem.Sys), mem.NumGC))
324 // Pwd prints the current working directory
325 func Pwd(args []string) {
326 wd, err := os.Getwd()
328 messenger.Message(err.Error())
330 messenger.Message(wd)
334 // Open opens a new buffer with a given filename
335 func Open(args []string) {
338 // the filename might or might not be quoted, so unquote first then join the strings.
339 args, err := shellwords.Split(filename)
341 messenger.Error("Error parsing args ", err)
344 filename = strings.Join(args, " ")
346 CurView().Open(filename)
348 messenger.Error("No filename")
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
359 buffer.Cursor.Loc = buffer.Start()
361 buffer.Cursor.Loc = buffer.End()
368 // Reload reloads all files (syntax files, colorschemes...)
369 func Reload(args []string) {
373 // Help tries to open the given help page in a horizontal split
374 func Help(args []string) {
376 // Open the default help if the user just typed "> help"
377 CurView().openHelp("help")
380 if FindRuntimeFile(RTHelp, helpPage) != nil {
381 CurView().openHelp(helpPage)
383 messenger.Error("Sorry, no help for ", helpPage)
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) {
392 CurView().VSplit(NewBufferFromString("", ""))
394 buf, err := NewBufferFromFile(args[0])
399 CurView().VSplit(buf)
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) {
407 CurView().HSplit(NewBufferFromString("", ""))
409 buf, err := NewBufferFromFile(args[0])
414 CurView().HSplit(buf)
418 // Eval evaluates a lua expression
419 func Eval(args []string) {
421 err := L.DoString(args[0])
426 messenger.Error("Not enough arguments")
430 // NewTab opens the given file in a new tab
431 func NewTab(args []string) {
433 CurView().AddTab(true)
435 buf, err := NewBufferFromFile(args[0])
441 tab := NewTabFromView(NewView(buf))
442 tab.SetNum(len(tabs))
443 tabs = append(tabs, tab)
444 curTab = len(tabs) - 1
446 for _, t := range tabs {
447 for _, v := range t.Views {
455 // Set sets an option
456 func Set(args []string) {
458 messenger.Error("Not enough arguments")
465 SetOptionAndSettings(option, value)
468 // SetLocal sets an option local to the buffer
469 func SetLocal(args []string) {
471 messenger.Error("Not enough arguments")
478 err := SetLocalOption(option, value, CurView())
480 messenger.Error(err.Error())
484 // Show shows the value of the given option
485 func Show(args []string) {
487 messenger.Error("Please provide an option to show")
491 option := GetOption(args[0])
494 messenger.Error(args[0], " is not a valid option")
498 messenger.Message(option)
501 // ShowKey displays the action that a key is bound to
502 func ShowKey(args []string) {
504 messenger.Error("Please provide a key to show")
508 if action, ok := bindingsStr[args[0]]; ok {
509 messenger.Message(action)
511 messenger.Message(args[0], " has no binding")
515 // Bind creates a new keybinding
516 func Bind(args []string) {
518 messenger.Error("Not enough arguments")
521 BindKey(args[0], args[1])
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)
530 // Quit closes the main view
531 func Quit(args []string) {
532 // Close the main view
536 // Save saves the buffer in the main view
537 func Save(args []string) {
539 // Save the main view
542 CurView().Buf.SaveAs(args[0])
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, " "))
558 for _, arg := range args[2:] {
565 messenger.Error("Invalid flag: " + arg)
571 search := string(args[0])
574 search = regexp.QuoteMeta(search)
577 replace := string(args[1])
578 replaceBytes := []byte(replace)
580 regex, err := regexp.Compile("(?m)" + search)
582 // There was an error with the user's regex
583 messenger.Error(err.Error())
590 replaceAll := func() {
592 for i := 0; i < view.Buf.LinesNum(); i++ {
593 newText := regex.ReplaceAllFunc(view.Buf.lines[i].data, func(in []byte) []byte {
599 to := Loc{utf8.RuneCount(view.Buf.lines[i].data), i}
601 deltas = append(deltas, Delta{string(newText), from, to})
603 view.Buf.MultipleReplace(deltas)
610 // The 'check' flag was used
611 Search(search, view, true)
612 if !view.Cursor.HasSelection() {
617 choice, canceled := messenger.LetterPrompt("Perform replacement? (y,n,a)", 'y', 'n', 'a')
619 if view.Cursor.HasSelection() {
620 view.Cursor.Loc = view.Cursor.CurSelection[0]
621 view.Cursor.ResetSelection()
625 } else if choice == 'a' {
626 if view.Cursor.HasSelection() {
627 view.Cursor.Loc = view.Cursor.CurSelection[0]
628 view.Cursor.ResetSelection()
633 } else if choice == 'y' {
634 view.Cursor.DeleteSelection()
635 view.Buf.Insert(view.Cursor.Loc, replace)
636 view.Cursor.ResetSelection()
640 if view.Cursor.HasSelection() {
641 searchStart = view.Cursor.CurSelection[1]
643 searchStart = view.Cursor.Loc
647 view.Cursor.Relocate()
650 messenger.Message("Replaced ", found, " occurrences of ", search)
651 } else if found == 1 {
652 messenger.Message("Replaced ", found, " occurrence of ", search)
654 messenger.Message("Nothing matched ", search)
658 // ReplaceAll replaces search term all at once
659 func ReplaceAll(args []string) {
660 // aliased to Replace command
661 Replace(append(args, "-a"))
664 // Term opens a terminal in the current view
665 func Term(args []string) {
668 err = CurView().StartTerminal([]string{os.Getenv("SHELL"), "-i"}, true, false, "")
670 err = CurView().StartTerminal(args, true, false, "")
677 // HandleCommand handles input from the user
678 func HandleCommand(input string) {
679 args, err := shellwords.Split(input)
681 messenger.Error("Error parsing args ", err)
687 if _, ok := commands[inputCmd]; !ok {
688 messenger.Error("Unknown command ", inputCmd)
690 commands[inputCmd].action(args[1:])