X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=internal%2Faction%2Fcommand.go;h=3a445caa0a64e0aa76dadb118834147e2a838f18;hb=90304fb472fab5a9809feb0d9b23b0930619cf0d;hp=31fc9f9cb77ff1a33b3ac39149c03445bfa50200;hpb=3b66a3364cf7c865fd94464a0c49b8fba0f1f00d;p=micro.git diff --git a/internal/action/command.go b/internal/action/command.go index 31fc9f9c..3a445caa 100644 --- a/internal/action/command.go +++ b/internal/action/command.go @@ -12,16 +12,12 @@ import ( "strings" "unicode/utf8" - luar "layeh.com/gopher-luar" - - lua "github.com/yuin/gopher-lua" + shellquote "github.com/kballard/go-shellquote" "github.com/zyedidia/micro/internal/buffer" "github.com/zyedidia/micro/internal/config" - ulua "github.com/zyedidia/micro/internal/lua" "github.com/zyedidia/micro/internal/screen" "github.com/zyedidia/micro/internal/shell" "github.com/zyedidia/micro/internal/util" - "github.com/zyedidia/micro/pkg/shellwords" ) // A Command contains information about how to execute a command @@ -71,29 +67,9 @@ func InitCommands() { // MakeCommand is a function to easily create new commands // This can be called by plugins in Lua so that plugins can define their own commands -func LuaMakeCommand(name, function string, completer buffer.Completer) { - action := LuaFunctionCommand(function) - commands[name] = Command{action, completer} -} - -// LuaFunctionCommand returns a normal function -// so that a command can be bound to a lua function -func LuaFunctionCommand(fn string) func(*BufPane, []string) { - luaFn := strings.Split(fn, ".") - if len(luaFn) <= 1 { - return nil - } - plName, plFn := luaFn[0], luaFn[1] - pl := config.FindPlugin(plName) - if pl == nil { - return nil - } - return func(bp *BufPane, args []string) { - luaArgs := []lua.LValue{luar.New(ulua.L, bp), luar.New(ulua.L, args)} - _, err := pl.Call(plFn, luaArgs...) - if err != nil { - screen.TermMessage(err) - } +func MakeCommand(name string, action func(bp *BufPane, args []string), completer buffer.Completer) { + if action != nil { + commands[name] = Command{action, completer} } } @@ -120,106 +96,20 @@ func CommandAction(cmd string) BufKeyAction { } } -var PluginCmds = []string{"list", "info", "version"} +var PluginCmds = []string{"install", "remove", "update", "available", "list", "search"} // PluginCmd installs, removes, updates, lists, or searches for given plugins func (h *BufPane) PluginCmd(args []string) { - if len(args) <= 0 { - InfoBar.Error("Not enough arguments, see 'help commands'") - return - } - - valid := true - switch args[0] { - case "list": - for _, pl := range config.Plugins { - var en string - if pl.IsEnabled() { - en = "enabled" - } else { - en = "disabled" - } - WriteLog(fmt.Sprintf("%s: %s", pl.Name, en)) - if pl.Default { - WriteLog(" (default)\n") - } else { - WriteLog("\n") - } - } - WriteLog("Default plugins come pre-installed with micro.") - case "version": - if len(args) <= 1 { - InfoBar.Error("No plugin provided to give info for") - return - } - found := false - for _, pl := range config.Plugins { - if pl.Name == args[1] { - found = true - if pl.Info == nil { - InfoBar.Message("Sorry no version for", pl.Name) - return - } - - WriteLog("Version: " + pl.Info.Vstr + "\n") - } - } - if !found { - InfoBar.Message(args[1], "is not installed") - } - case "info": - if len(args) <= 1 { - InfoBar.Error("No plugin provided to give info for") - return - } - found := false - for _, pl := range config.Plugins { - if pl.Name == args[1] { - found = true - if pl.Info == nil { - InfoBar.Message("Sorry no info for ", pl.Name) - return - } - - var buffer bytes.Buffer - buffer.WriteString("Name: ") - buffer.WriteString(pl.Info.Name) - buffer.WriteString("\n") - buffer.WriteString("Description: ") - buffer.WriteString(pl.Info.Desc) - buffer.WriteString("\n") - buffer.WriteString("Website: ") - buffer.WriteString(pl.Info.Site) - buffer.WriteString("\n") - buffer.WriteString("Installation link: ") - buffer.WriteString(pl.Info.Install) - buffer.WriteString("\n") - buffer.WriteString("Version: ") - buffer.WriteString(pl.Info.Vstr) - buffer.WriteString("\n") - buffer.WriteString("Requirements:") - buffer.WriteString("\n") - for _, r := range pl.Info.Require { - buffer.WriteString(" - ") - buffer.WriteString(r) - buffer.WriteString("\n") - } - - WriteLog(buffer.String()) - } - } - if !found { - InfoBar.Message(args[1], "is not installed") - return - } - default: - InfoBar.Error("Not a valid plugin command") + if len(args) < 1 { + InfoBar.Error("Not enough arguments") return } - if valid && h.Buf.Type != buffer.BTLog { + if h.Buf.Type != buffer.BTLog { OpenLogBuf(h) } + + config.PluginCommand(buffer.LogBuf, args[0], args[1:]) } // RetabCmd changes all spaces to tabs or all tabs to spaces @@ -233,7 +123,7 @@ func (h *BufPane) RetabCmd(args []string) { func (h *BufPane) RawCmd(args []string) { width, height := screen.Screen.Size() iOffset := config.GetInfoBarOffset() - tp := NewTabFromPane(0, 0, width, height-iOffset, NewRawPane()) + tp := NewTabFromPane(0, 0, width, height-iOffset, NewRawPane(nil)) Tabs.AddTab(tp) Tabs.SetActive(len(Tabs.List) - 1) } @@ -344,11 +234,14 @@ func (h *BufPane) OpenCmd(args []string) { if len(args) > 0 { filename := args[0] // the filename might or might not be quoted, so unquote first then join the strings. - args, err := shellwords.Split(filename) + args, err := shellquote.Split(filename) if err != nil { InfoBar.Error("Error parsing args ", err) return } + if len(args) == 0 { + return + } filename = strings.Join(args, " ") open := func() { @@ -497,6 +390,7 @@ func (h *BufPane) HSplitCmd(args []string) { // EvalCmd evaluates a lua expression func (h *BufPane) EvalCmd(args []string) { + InfoBar.Error("Eval unsupported") } // NewTabCmd opens the given file in a new tab @@ -523,37 +417,55 @@ func (h *BufPane) NewTabCmd(args []string) { } func SetGlobalOptionNative(option string, nativeValue interface{}) error { - config.GlobalSettings[option] = nativeValue - - if option == "colorscheme" { - // LoadSyntaxFiles() - config.InitColorscheme() - for _, b := range buffer.OpenBuffers { - b.UpdateRules() + local := false + for _, s := range config.LocalSettings { + if s == option { + local = true + break } - } else if option == "infobar" || option == "keymenu" { - Tabs.Resize() - } else if option == "mouse" { - if !nativeValue.(bool) { - screen.Screen.DisableMouse() + } + + if !local { + config.GlobalSettings[option] = nativeValue + + if option == "colorscheme" { + // LoadSyntaxFiles() + config.InitColorscheme() + for _, b := range buffer.OpenBuffers { + b.UpdateRules() + } + } else if option == "infobar" || option == "keymenu" { + Tabs.Resize() + } else if option == "mouse" { + if !nativeValue.(bool) { + screen.Screen.DisableMouse() + } else { + screen.Screen.EnableMouse() + } + } else if option == "autosave" { + if nativeValue.(float64) > 0 { + config.SetAutoTime(int(nativeValue.(float64))) + config.StartAutoSave() + } else { + config.SetAutoTime(0) + } + } else if option == "paste" { + screen.Screen.SetPaste(nativeValue.(bool)) } else { - screen.Screen.EnableMouse() - } - // } else if option == "autosave" { - // if nativeValue.(float64) > 0 { - // config.SetAutoTime(int(nativeValue.(float64))) - // config.StartAutoSave() - // } else { - // config.SetAutoTime(0) - // } - } else { - for _, pl := range config.Plugins { - if option == pl.Name { - if nativeValue.(bool) && !pl.Loaded { - pl.Load() - pl.Call("init") - } else if !nativeValue.(bool) && pl.Loaded { - pl.Call("deinit") + for _, pl := range config.Plugins { + if option == pl.Name { + if nativeValue.(bool) && !pl.Loaded { + pl.Load() + _, err := pl.Call("init") + if err != nil && err != config.ErrNoSuchFunction { + screen.TermMessage(err) + } + } else if !nativeValue.(bool) && pl.Loaded { + _, err := pl.Call("deinit") + if err != nil && err != config.ErrNoSuchFunction { + screen.TermMessage(err) + } + } } } } @@ -703,7 +615,7 @@ func (h *BufPane) UnbindCmd(args []string) { // RunCmd runs a shell command in the background func (h *BufPane) RunCmd(args []string) { - runf, err := shell.RunBackgroundShell(shellwords.Join(args...)) + runf, err := shell.RunBackgroundShell(shellquote.Join(args...)) if err != nil { InfoBar.Error(err) } else { @@ -893,7 +805,12 @@ func (h *BufPane) ReplaceAllCmd(args []string) { // TermCmd opens a terminal in the current view func (h *BufPane) TermCmd(args []string) { - ps := MainTab().Panes + ps := h.tab.Panes + + if !TermEmuSupported { + InfoBar.Error("Terminal emulator not supported on this system") + return + } if len(args) == 0 { sh := os.Getenv("SHELL") @@ -906,7 +823,7 @@ func (h *BufPane) TermCmd(args []string) { term := func(i int, newtab bool) { t := new(shell.Terminal) - t.Start(args, false, true, "", nil) + t.Start(args, false, true, nil, nil) id := h.ID() if newtab { @@ -918,7 +835,12 @@ func (h *BufPane) TermCmd(args []string) { } v := h.GetView() - MainTab().Panes[i] = NewTermPane(v.X, v.Y, v.Width, v.Height, t, id) + tp, err := NewTermPane(v.X, v.Y, v.Width, v.Height, t, id, MainTab()) + if err != nil { + InfoBar.Error(err) + return + } + MainTab().Panes[i] = tp MainTab().SetActive(i) } @@ -950,12 +872,16 @@ func (h *BufPane) TermCmd(args []string) { // HandleCommand handles input from the user func (h *BufPane) HandleCommand(input string) { - args, err := shellwords.Split(input) + args, err := shellquote.Split(input) if err != nil { InfoBar.Error("Error parsing args ", err) return } + if len(args) == 0 { + return + } + inputCmd := args[0] if _, ok := commands[inputCmd]; !ok {