X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=cmd%2Fmicro%2Fcommand.go;h=b36df74bd46cfcfc4b2a5f961778f62b9fd75fef;hb=3ecdd96931ac75039105b5a4ac2fdd3b5526dbf6;hp=d1ef2c535ea7e8e77ac36abb5cbd8049da153577;hpb=c070e3e8f70e1d060bab255ad23db4ac5d77c735;p=micro.git diff --git a/cmd/micro/command.go b/cmd/micro/command.go index d1ef2c53..b36df74b 100644 --- a/cmd/micro/command.go +++ b/cmd/micro/command.go @@ -2,11 +2,13 @@ package main import ( "bytes" + "fmt" "io" "io/ioutil" "os" "os/exec" "os/signal" + "path/filepath" "regexp" "strings" @@ -25,21 +27,29 @@ type StrCommand struct { var commands map[string]Command -var commandActions = map[string]func([]string){ - "Set": Set, - "SetLocal": SetLocal, - "Show": Show, - "Run": Run, - "Bind": Bind, - "Quit": Quit, - "Save": Save, - "Replace": Replace, - "VSplit": VSplit, - "HSplit": HSplit, - "Tab": NewTab, - "Help": Help, - "Eval": Eval, - "ToggleLog": ToggleLog, +var commandActions map[string]func([]string) + +func init() { + commandActions = map[string]func([]string){ + "Set": Set, + "SetLocal": SetLocal, + "Show": Show, + "Run": Run, + "Bind": Bind, + "Quit": Quit, + "Save": Save, + "Replace": Replace, + "VSplit": VSplit, + "HSplit": HSplit, + "Tab": NewTab, + "Help": Help, + "Eval": Eval, + "ToggleLog": ToggleLog, + "Plugin": PluginCmd, + "Reload": Reload, + "Cd": Cd, + "Pwd": Pwd, + } } // InitCommands initializes the default commands @@ -86,19 +96,143 @@ func DefaultCommands() map[string]StrCommand { "help": {"Help", []Completion{HelpCompletion, NoCompletion}}, "eval": {"Eval", []Completion{NoCompletion}}, "log": {"ToggleLog", []Completion{NoCompletion}}, + "plugin": {"Plugin", []Completion{PluginCmdCompletion, PluginNameCompletion}}, + "reload": {"Reload", []Completion{NoCompletion}}, + "cd": {"Cd", []Completion{FileCompletion}}, + "pwd": {"Pwd", []Completion{NoCompletion}}, + } +} + +// PluginCmd installs, removes, updates, lists, or searches for given plugins +func PluginCmd(args []string) { + if len(args) >= 1 { + switch args[0] { + case "install": + installedVersions := GetInstalledVersions(false) + for _, plugin := range args[1:] { + pp := GetAllPluginPackages().Get(plugin) + if pp == nil { + messenger.Error("Unknown plugin \"" + plugin + "\"") + } else if err := pp.IsInstallable(); err != nil { + messenger.Error("Error installing ", plugin, ": ", err) + } else { + for _, installed := range installedVersions { + if pp.Name == installed.pack.Name { + if pp.Versions[0].Version.Compare(installed.Version) == 1 { + messenger.Error(pp.Name, " is already installed but out-of-date: use 'plugin update ", pp.Name, "' to update") + } else { + messenger.Error(pp.Name, " is already installed") + } + } + } + pp.Install() + } + } + case "remove": + removed := "" + for _, plugin := range args[1:] { + // check if the plugin exists. + for _, lp := range loadedPlugins { + if lp == plugin { + UninstallPlugin(plugin) + removed += plugin + " " + continue + } + } + } + if !IsSpaces(removed) { + messenger.Message("Removed ", removed) + } else { + messenger.Error("The requested plugins do not exist") + } + case "update": + UpdatePlugins(args[1:]) + case "list": + plugins := GetInstalledVersions(false) + messenger.AddLog("----------------") + messenger.AddLog("The following plugins are currently installed:\n") + for _, p := range plugins { + messenger.AddLog(fmt.Sprintf("%s (%s)", p.pack.Name, p.Version)) + } + messenger.AddLog("----------------") + if len(plugins) > 0 { + if CurView().Type != vtLog { + ToggleLog([]string{}) + } + } + case "search": + plugins := SearchPlugin(args[1:]) + messenger.Message(len(plugins), " plugins found") + for _, p := range plugins { + messenger.AddLog("----------------") + messenger.AddLog(p.String()) + } + messenger.AddLog("----------------") + if len(plugins) > 0 { + if CurView().Type != vtLog { + ToggleLog([]string{}) + } + } + case "available": + packages := GetAllPluginPackages() + messenger.AddLog("Available Plugins:") + for _, pkg := range packages { + messenger.AddLog(pkg.Name) + } + if CurView().Type != vtLog { + ToggleLog([]string{}) + } + } + } else { + messenger.Error("Not enough arguments") + } +} + +func Cd(args []string) { + if len(args) > 0 { + home, _ := homedir.Dir() + path := strings.Replace(args[0], "~", home, 1) + os.Chdir(path) + for _, tab := range tabs { + for _, view := range tab.views { + wd, _ := os.Getwd() + view.Buf.Path, _ = MakeRelative(view.Buf.AbsPath, wd) + if p, _ := filepath.Abs(view.Buf.Path); !strings.Contains(p, wd) { + view.Buf.Path = view.Buf.AbsPath + } + } + } + } +} + +func Pwd(args []string) { + wd, err := os.Getwd() + if err != nil { + messenger.Message(err.Error()) + } else { + messenger.Message(wd) } } func ToggleLog(args []string) { buffer := messenger.getBuffer() if CurView().Type != vtLog { - CurView().VSplit(buffer) + CurView().HSplit(buffer) CurView().Type = vtLog + RedrawAll() + buffer.Cursor.Loc = buffer.Start() + CurView().Relocate() + buffer.Cursor.Loc = buffer.End() + CurView().Relocate() } else { CurView().Quit(true) } } +func Reload(args []string) { + LoadAll() +} + // Help tries to open the given help page in a horizontal split func Help(args []string) { if len(args) < 1 { @@ -288,7 +422,7 @@ func Replace(args []string) { search := string(args[0]) replace := string(args[1]) - regex, err := regexp.Compile(search) + regex, err := regexp.Compile("(?m)" + search) if err != nil { // There was an error with the user's regex messenger.Error(err.Error()) @@ -335,23 +469,25 @@ func Replace(args []string) { } } } else { - matches := regex.FindAllStringIndex(view.Buf.String(), -1) + bufStr := view.Buf.String() + matches := regex.FindAllStringIndex(bufStr, -1) if matches != nil && len(matches) > 0 { - adjust := 0 - prevMatch := matches[0] - from := FromCharPos(prevMatch[0], view.Buf) - to := from.Move(Count(search), view.Buf) - adjust += Count(replace) - Count(search) + prevMatchCount := runePos(matches[0][0], bufStr) + searchCount := runePos(matches[0][1], bufStr) - prevMatchCount + from := FromCharPos(matches[0][0], view.Buf) + to := from.Move(searchCount, view.Buf) + adjust := Count(replace) - searchCount view.Buf.Replace(from, to, replace) if len(matches) > 1 { for _, match := range matches[1:] { found++ - from = from.Move(match[0]-prevMatch[0]+adjust, view.Buf) - to := from.Move(Count(search), view.Buf) - // TermMessage(match[0], " ", prevMatch[0], " ", adjust, "\n", from, " ", to) + matchCount := runePos(match[0], bufStr) + searchCount = runePos(match[1], bufStr) - matchCount + from = from.Move(matchCount-prevMatchCount+adjust, view.Buf) + to = from.Move(searchCount, view.Buf) view.Buf.Replace(from, to, replace) - prevMatch = match - // adjust += Count(replace) - Count(search) + prevMatchCount = matchCount + adjust = Count(replace) - searchCount } } }