X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=cmd%2Fmicro%2Fcommand.go;h=0c582e2025ce9d75f4867695dae651c741b3caaf;hb=37ae99ccd9b7b3a658a2e773af74ca08c88c0d07;hp=a47910a0ff19d11e843d786931d4d3cebec3a4a8;hpb=dce56a2b85f4248db43247deafd7598759ef8374;p=micro.git diff --git a/cmd/micro/command.go b/cmd/micro/command.go index a47910a0..0c582e20 100644 --- a/cmd/micro/command.go +++ b/cmd/micro/command.go @@ -2,11 +2,12 @@ package main import ( "bytes" + "fmt" "io" - "io/ioutil" "os" "os/exec" "os/signal" + "path/filepath" "regexp" "strings" @@ -25,19 +26,30 @@ 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, +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, + "Open": Open, + } } // InitCommands initializes the default commands @@ -82,9 +94,158 @@ func DefaultCommands() map[string]StrCommand { "hsplit": {"HSplit", []Completion{FileCompletion, NoCompletion}}, "tab": {"Tab", []Completion{FileCompletion, NoCompletion}}, "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}}, + "open": {"Open", []Completion{FileCompletion}}, + } +} + +// 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 Open(args []string) { + if len(args) > 0 { + filename := args[0] + // the filename might or might not be quoted, so unquote first then join the strings. + filename = strings.Join(SplitCommandArgs(filename), " ") + + CurView().Open(filename) + } else { + messenger.Error("No filename") } } +func ToggleLog(args []string) { + buffer := messenger.getBuffer() + if CurView().Type != vtLog { + 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 { @@ -92,7 +253,7 @@ func Help(args []string) { CurView().openHelp("help") } else { helpPage := args[0] - if _, ok := helpPages[helpPage]; ok { + if FindRuntimeFile(RTHelp, helpPage) != nil { CurView().openHelp(helpPage) } else { messenger.Error("Sorry, no help for ", helpPage) @@ -104,17 +265,18 @@ func Help(args []string) { // If no file is given, it opens an empty buffer in a new split func VSplit(args []string) { if len(args) == 0 { - CurView().VSplit(NewBuffer([]byte{}, "")) + CurView().VSplit(NewBuffer(strings.NewReader(""), "")) } else { filename := args[0] home, _ := homedir.Dir() filename = strings.Replace(filename, "~", home, 1) - file, err := ioutil.ReadFile(filename) + file, err := os.Open(filename) + defer file.Close() var buf *Buffer if err != nil { // File does not exist -- create an empty buffer with that name - buf = NewBuffer([]byte{}, filename) + buf = NewBuffer(strings.NewReader(""), filename) } else { buf = NewBuffer(file, filename) } @@ -126,17 +288,18 @@ func VSplit(args []string) { // If no file is given, it opens an empty buffer in a new split func HSplit(args []string) { if len(args) == 0 { - CurView().HSplit(NewBuffer([]byte{}, "")) + CurView().HSplit(NewBuffer(strings.NewReader(""), "")) } else { filename := args[0] home, _ := homedir.Dir() filename = strings.Replace(filename, "~", home, 1) - file, err := ioutil.ReadFile(filename) + file, err := os.Open(filename) + defer file.Close() var buf *Buffer if err != nil { // File does not exist -- create an empty buffer with that name - buf = NewBuffer([]byte{}, filename) + buf = NewBuffer(strings.NewReader(""), filename) } else { buf = NewBuffer(file, filename) } @@ -144,6 +307,18 @@ func HSplit(args []string) { } } +// Eval evaluates a lua expression +func Eval(args []string) { + if len(args) >= 1 { + err := L.DoString(args[0]) + if err != nil { + messenger.Error(err) + } + } else { + messenger.Error("Not enough arguments") + } +} + // NewTab opens the given file in a new tab func NewTab(args []string) { if len(args) == 0 { @@ -152,12 +327,13 @@ func NewTab(args []string) { filename := args[0] home, _ := homedir.Dir() filename = strings.Replace(filename, "~", home, 1) - file, _ := ioutil.ReadFile(filename) + file, _ := os.Open(filename) + defer file.Close() tab := NewTabFromView(NewView(NewBuffer(file, filename))) tab.SetNum(len(tabs)) tabs = append(tabs, tab) - curTab++ + curTab = len(tabs) - 1 if len(tabs) == 2 { for _, t := range tabs { for _, v := range t.views { @@ -226,7 +402,7 @@ func Bind(args []string) { // Run runs a shell command in the background func Run(args []string) { // Run a shell command in the background (openTerm is false) - HandleShellCommand(strings.Join(args, " "), false, true) + HandleShellCommand(JoinCommandArgs(args...), false, true) } // Quit closes the main view @@ -237,48 +413,32 @@ func Quit(args []string) { // Save saves the buffer in the main view func Save(args []string) { - // Save the main view - CurView().Save(true) + if len(args) == 0 { + // Save the main view + CurView().Save(true) + } else { + CurView().Buf.SaveAs(args[0]) + } } // Replace runs search and replace func Replace(args []string) { - // This is a regex to parse the replace expression - // We allow no quotes if there are no spaces, but if you want to search - // for or replace an expression with spaces, you can add double quotes - r := regexp.MustCompile(`"[^"\\]*(?:\\.[^"\\]*)*"|[^\s]*`) - replaceCmd := r.FindAllString(strings.Join(args, " "), -1) - if len(replaceCmd) < 2 { + if len(args) < 2 { // We need to find both a search and replace expression messenger.Error("Invalid replace statement: " + strings.Join(args, " ")) return } var flags string - if len(replaceCmd) == 3 { + if len(args) == 3 { // The user included some flags - flags = replaceCmd[2] - } - - search := string(replaceCmd[0]) - replace := string(replaceCmd[1]) - - // If the search and replace expressions have quotes, we need to remove those - if strings.HasPrefix(search, `"`) && strings.HasSuffix(search, `"`) { - search = search[1 : len(search)-1] - } - if strings.HasPrefix(replace, `"`) && strings.HasSuffix(replace, `"`) { - replace = replace[1 : len(replace)-1] + flags = args[2] } - // We replace all escaped double quotes to real double quotes - search = strings.Replace(search, `\"`, `"`, -1) - replace = strings.Replace(replace, `\"`, `"`, -1) - // Replace some things so users can actually insert newlines and tabs in replacements - replace = strings.Replace(replace, "\\n", "\n", -1) - replace = strings.Replace(replace, "\\t", "\t", -1) + 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()) @@ -325,13 +485,27 @@ func Replace(args []string) { } } } else { - for { - match := regex.FindStringIndex(view.Buf.String()) - if match == nil { - break + bufStr := view.Buf.String() + matches := regex.FindAllStringIndex(bufStr, -1) + if matches != nil && len(matches) > 0 { + 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++ + 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) + prevMatchCount = matchCount + adjust = Count(replace) - searchCount + } } - found++ - view.Buf.Replace(FromCharPos(match[0], view.Buf), FromCharPos(match[1], view.Buf), replace) } } view.Cursor.Relocate() @@ -347,8 +521,8 @@ func Replace(args []string) { // RunShellCommand executes a shell command and returns the output/error func RunShellCommand(input string) (string, error) { - inputCmd := strings.Split(input, " ")[0] - args := strings.Split(input, " ")[1:] + inputCmd := SplitCommandArgs(input)[0] + args := SplitCommandArgs(input)[1:] cmd := exec.Command(inputCmd, args...) outputBytes := &bytes.Buffer{} @@ -364,7 +538,7 @@ func RunShellCommand(input string) (string, error) { // The openTerm argument specifies whether a terminal should be opened (for viewing output // or interacting with stdin) func HandleShellCommand(input string, openTerm bool, waitToFinish bool) string { - inputCmd := strings.Split(input, " ")[0] + inputCmd := SplitCommandArgs(input)[0] if !openTerm { // Simply run the command in the background and notify the user when it's done messenger.Message("Running...") @@ -389,7 +563,7 @@ func HandleShellCommand(input string, openTerm bool, waitToFinish bool) string { screen.Fini() screen = nil - args := strings.Split(input, " ")[1:] + args := SplitCommandArgs(input)[1:] // Set up everything for the command var outputBuf bytes.Buffer @@ -431,12 +605,12 @@ func HandleShellCommand(input string, openTerm bool, waitToFinish bool) string { // HandleCommand handles input from the user func HandleCommand(input string) { - inputCmd := strings.Split(input, " ")[0] - args := strings.Split(input, " ")[1:] + args := SplitCommandArgs(input) + inputCmd := args[0] if _, ok := commands[inputCmd]; !ok { messenger.Error("Unknown command ", inputCmd) } else { - commands[inputCmd].action(args) + commands[inputCmd].action(args[1:]) } }