15 "github.com/mitchellh/go-homedir"
20 completions []Completion
23 type StrCommand struct {
25 completions []Completion
28 var commands map[string]Command
30 var commandActions map[string]func([]string)
33 commandActions = map[string]func([]string){
47 "ToggleLog": ToggleLog,
55 // InitCommands initializes the default commands
57 commands = make(map[string]Command)
59 defaults := DefaultCommands()
60 parseCommands(defaults)
63 func parseCommands(userCommands map[string]StrCommand) {
64 for k, v := range userCommands {
65 MakeCommand(k, v.action, v.completions...)
69 // MakeCommand is a function to easily create new commands
70 // This can be called by plugins in Lua so that plugins can define their own commands
71 func MakeCommand(name, function string, completions ...Completion) {
72 action := commandActions[function]
73 if _, ok := commandActions[function]; !ok {
74 // If the user seems to be binding a function that doesn't exist
75 // We hope that it's a lua function that exists and bind it to that
76 action = LuaFunctionCommand(function)
79 commands[name] = Command{action, completions}
82 // DefaultCommands returns a map containing micro's default commands
83 func DefaultCommands() map[string]StrCommand {
84 return map[string]StrCommand{
85 "set": {"Set", []Completion{OptionCompletion, NoCompletion}},
86 "setlocal": {"SetLocal", []Completion{OptionCompletion, NoCompletion}},
87 "show": {"Show", []Completion{OptionCompletion, NoCompletion}},
88 "bind": {"Bind", []Completion{NoCompletion}},
89 "run": {"Run", []Completion{NoCompletion}},
90 "quit": {"Quit", []Completion{NoCompletion}},
91 "save": {"Save", []Completion{NoCompletion}},
92 "replace": {"Replace", []Completion{NoCompletion}},
93 "vsplit": {"VSplit", []Completion{FileCompletion, NoCompletion}},
94 "hsplit": {"HSplit", []Completion{FileCompletion, NoCompletion}},
95 "tab": {"Tab", []Completion{FileCompletion, NoCompletion}},
96 "help": {"Help", []Completion{HelpCompletion, NoCompletion}},
97 "eval": {"Eval", []Completion{NoCompletion}},
98 "log": {"ToggleLog", []Completion{NoCompletion}},
99 "plugin": {"Plugin", []Completion{PluginCmdCompletion, PluginNameCompletion}},
100 "reload": {"Reload", []Completion{NoCompletion}},
101 "cd": {"Cd", []Completion{FileCompletion}},
102 "pwd": {"Pwd", []Completion{NoCompletion}},
106 // PluginCmd installs, removes, updates, lists, or searches for given plugins
107 func PluginCmd(args []string) {
111 installedVersions := GetInstalledVersions(false)
112 for _, plugin := range args[1:] {
113 pp := GetAllPluginPackages().Get(plugin)
115 messenger.Error("Unknown plugin \"" + plugin + "\"")
116 } else if err := pp.IsInstallable(); err != nil {
117 messenger.Error("Error installing ", plugin, ": ", err)
119 for _, installed := range installedVersions {
120 if pp.Name == installed.pack.Name {
121 if pp.Versions[0].Version.Compare(installed.Version) == 1 {
122 messenger.Error(pp.Name, " is already installed but out-of-date: use 'plugin update ", pp.Name, "' to update")
124 messenger.Error(pp.Name, " is already installed")
133 for _, plugin := range args[1:] {
134 // check if the plugin exists.
135 for _, lp := range loadedPlugins {
137 UninstallPlugin(plugin)
138 removed += plugin + " "
143 if !IsSpaces(removed) {
144 messenger.Message("Removed ", removed)
146 messenger.Error("The requested plugins do not exist")
149 UpdatePlugins(args[1:])
151 plugins := GetInstalledVersions(false)
152 messenger.AddLog("----------------")
153 messenger.AddLog("The following plugins are currently installed:\n")
154 for _, p := range plugins {
155 messenger.AddLog(fmt.Sprintf("%s (%s)", p.pack.Name, p.Version))
157 messenger.AddLog("----------------")
158 if len(plugins) > 0 {
159 if CurView().Type != vtLog {
160 ToggleLog([]string{})
164 plugins := SearchPlugin(args[1:])
165 messenger.Message(len(plugins), " plugins found")
166 for _, p := range plugins {
167 messenger.AddLog("----------------")
168 messenger.AddLog(p.String())
170 messenger.AddLog("----------------")
171 if len(plugins) > 0 {
172 if CurView().Type != vtLog {
173 ToggleLog([]string{})
177 packages := GetAllPluginPackages()
178 messenger.AddLog("Available Plugins:")
179 for _, pkg := range packages {
180 messenger.AddLog(pkg.Name)
182 if CurView().Type != vtLog {
183 ToggleLog([]string{})
187 messenger.Error("Not enough arguments")
191 func Cd(args []string) {
193 home, _ := homedir.Dir()
194 path := strings.Replace(args[0], "~", home, 1)
196 for _, tab := range tabs {
197 for _, view := range tab.views {
199 view.Buf.Path, _ = MakeRelative(view.Buf.AbsPath, wd)
200 if p, _ := filepath.Abs(view.Buf.Path); !strings.Contains(p, wd) {
201 view.Buf.Path = view.Buf.AbsPath
208 func Pwd(args []string) {
209 wd, err := os.Getwd()
211 messenger.Message(err.Error())
213 messenger.Message(wd)
217 func ToggleLog(args []string) {
218 buffer := messenger.getBuffer()
219 if CurView().Type != vtLog {
220 CurView().HSplit(buffer)
221 CurView().Type = vtLog
223 buffer.Cursor.Loc = buffer.Start()
225 buffer.Cursor.Loc = buffer.End()
232 func Reload(args []string) {
236 // Help tries to open the given help page in a horizontal split
237 func Help(args []string) {
239 // Open the default help if the user just typed "> help"
240 CurView().openHelp("help")
243 if FindRuntimeFile(RTHelp, helpPage) != nil {
244 CurView().openHelp(helpPage)
246 messenger.Error("Sorry, no help for ", helpPage)
251 // VSplit opens a vertical split with file given in the first argument
252 // If no file is given, it opens an empty buffer in a new split
253 func VSplit(args []string) {
255 CurView().VSplit(NewBuffer([]byte{}, ""))
258 home, _ := homedir.Dir()
259 filename = strings.Replace(filename, "~", home, 1)
260 file, err := ioutil.ReadFile(filename)
264 // File does not exist -- create an empty buffer with that name
265 buf = NewBuffer([]byte{}, filename)
267 buf = NewBuffer(file, filename)
269 CurView().VSplit(buf)
273 // HSplit opens a horizontal split with file given in the first argument
274 // If no file is given, it opens an empty buffer in a new split
275 func HSplit(args []string) {
277 CurView().HSplit(NewBuffer([]byte{}, ""))
280 home, _ := homedir.Dir()
281 filename = strings.Replace(filename, "~", home, 1)
282 file, err := ioutil.ReadFile(filename)
286 // File does not exist -- create an empty buffer with that name
287 buf = NewBuffer([]byte{}, filename)
289 buf = NewBuffer(file, filename)
291 CurView().HSplit(buf)
295 // Eval evaluates a lua expression
296 func Eval(args []string) {
298 err := L.DoString(args[0])
303 messenger.Error("Not enough arguments")
307 // NewTab opens the given file in a new tab
308 func NewTab(args []string) {
310 CurView().AddTab(true)
313 home, _ := homedir.Dir()
314 filename = strings.Replace(filename, "~", home, 1)
315 file, _ := ioutil.ReadFile(filename)
317 tab := NewTabFromView(NewView(NewBuffer(file, filename)))
318 tab.SetNum(len(tabs))
319 tabs = append(tabs, tab)
322 for _, t := range tabs {
323 for _, v := range t.views {
331 // Set sets an option
332 func Set(args []string) {
334 messenger.Error("Not enough arguments")
338 option := strings.TrimSpace(args[0])
339 value := strings.TrimSpace(args[1])
341 SetOptionAndSettings(option, value)
344 // SetLocal sets an option local to the buffer
345 func SetLocal(args []string) {
347 messenger.Error("Not enough arguments")
351 option := strings.TrimSpace(args[0])
352 value := strings.TrimSpace(args[1])
354 err := SetLocalOption(option, value, CurView())
356 messenger.Error(err.Error())
360 // Show shows the value of the given option
361 func Show(args []string) {
363 messenger.Error("Please provide an option to show")
367 option := GetOption(args[0])
370 messenger.Error(args[0], " is not a valid option")
374 messenger.Message(option)
377 // Bind creates a new keybinding
378 func Bind(args []string) {
380 messenger.Error("Not enough arguments")
383 BindKey(args[0], args[1])
386 // Run runs a shell command in the background
387 func Run(args []string) {
388 // Run a shell command in the background (openTerm is false)
389 HandleShellCommand(JoinCommandArgs(args...), false, true)
392 // Quit closes the main view
393 func Quit(args []string) {
394 // Close the main view
398 // Save saves the buffer in the main view
399 func Save(args []string) {
401 // Save the main view
404 CurView().Buf.SaveAs(args[0])
408 // Replace runs search and replace
409 func Replace(args []string) {
411 // We need to find both a search and replace expression
412 messenger.Error("Invalid replace statement: " + strings.Join(args, " "))
418 // The user included some flags
422 search := string(args[0])
423 replace := string(args[1])
425 regex, err := regexp.Compile("(?m)" + search)
427 // There was an error with the user's regex
428 messenger.Error(err.Error())
435 if strings.Contains(flags, "c") {
437 // The 'check' flag was used
438 Search(search, view, true)
439 if !view.Cursor.HasSelection() {
443 if view.Buf.Settings["syntax"].(bool) {
444 view.matches = Match(view)
447 choice, canceled := messenger.YesNoPrompt("Perform replacement? (y,n)")
449 if view.Cursor.HasSelection() {
450 view.Cursor.Loc = view.Cursor.CurSelection[0]
451 view.Cursor.ResetSelection()
457 view.Cursor.DeleteSelection()
458 view.Buf.Insert(view.Cursor.Loc, replace)
459 view.Cursor.ResetSelection()
463 if view.Cursor.HasSelection() {
464 searchStart = ToCharPos(view.Cursor.CurSelection[1], view.Buf)
466 searchStart = ToCharPos(view.Cursor.Loc, view.Buf)
472 bufStr := view.Buf.String()
473 matches := regex.FindAllStringIndex(bufStr, -1)
474 if matches != nil && len(matches) > 0 {
475 prevMatchCount := runePos(matches[0][0], bufStr)
476 searchCount := runePos(matches[0][1], bufStr) - prevMatchCount
477 from := FromCharPos(matches[0][0], view.Buf)
478 to := from.Move(searchCount, view.Buf)
479 adjust := Count(replace) - searchCount
480 view.Buf.Replace(from, to, replace)
481 if len(matches) > 1 {
482 for _, match := range matches[1:] {
484 matchCount := runePos(match[0], bufStr)
485 searchCount = runePos(match[1], bufStr) - matchCount
486 from = from.Move(matchCount-prevMatchCount+adjust, view.Buf)
487 to = from.Move(searchCount, view.Buf)
488 view.Buf.Replace(from, to, replace)
489 prevMatchCount = matchCount
490 adjust = Count(replace) - searchCount
495 view.Cursor.Relocate()
498 messenger.Message("Replaced ", found, " occurrences of ", search)
499 } else if found == 1 {
500 messenger.Message("Replaced ", found, " occurrence of ", search)
502 messenger.Message("Nothing matched ", search)
506 // RunShellCommand executes a shell command and returns the output/error
507 func RunShellCommand(input string) (string, error) {
508 inputCmd := SplitCommandArgs(input)[0]
509 args := SplitCommandArgs(input)[1:]
511 cmd := exec.Command(inputCmd, args...)
512 outputBytes := &bytes.Buffer{}
513 cmd.Stdout = outputBytes
514 cmd.Stderr = outputBytes
516 err := cmd.Wait() // wait for command to finish
517 outstring := outputBytes.String()
518 return outstring, err
521 // HandleShellCommand runs the shell command
522 // The openTerm argument specifies whether a terminal should be opened (for viewing output
523 // or interacting with stdin)
524 func HandleShellCommand(input string, openTerm bool, waitToFinish bool) string {
525 inputCmd := SplitCommandArgs(input)[0]
527 // Simply run the command in the background and notify the user when it's done
528 messenger.Message("Running...")
530 output, err := RunShellCommand(input)
531 totalLines := strings.Split(output, "\n")
533 if len(totalLines) < 3 {
535 messenger.Message(inputCmd, " exited without error")
537 messenger.Message(inputCmd, " exited with error: ", err, ": ", output)
540 messenger.Message(output)
542 // We have to make sure to redraw
546 // Shut down the screen because we're going to interact directly with the shell
550 args := SplitCommandArgs(input)[1:]
552 // Set up everything for the command
553 var outputBuf bytes.Buffer
554 cmd := exec.Command(inputCmd, args...)
556 cmd.Stdout = io.MultiWriter(os.Stdout, &outputBuf)
557 cmd.Stderr = os.Stderr
559 // This is a trap for Ctrl-C so that it doesn't kill micro
560 // Instead we trap Ctrl-C to kill the program we're running
561 c := make(chan os.Signal, 1)
562 signal.Notify(c, os.Interrupt)
572 output := outputBuf.String()
578 // This is just so we don't return right away and let the user press enter to return
582 // Start the screen back up
590 // HandleCommand handles input from the user
591 func HandleCommand(input string) {
592 args := SplitCommandArgs(input)
595 if _, ok := commands[inputCmd]; !ok {
596 messenger.Error("Unknown command ", inputCmd)
598 commands[inputCmd].action(args[1:])