+func DefaultCommands() map[string]StrCommand {
+ return map[string]StrCommand{
+ "set": {"Set", []Completion{OptionCompletion, NoCompletion}},
+ "setlocal": {"SetLocal", []Completion{OptionCompletion, NoCompletion}},
+ "show": {"Show", []Completion{OptionCompletion, NoCompletion}},
+ "bind": {"Bind", []Completion{NoCompletion}},
+ "run": {"Run", []Completion{NoCompletion}},
+ "quit": {"Quit", []Completion{NoCompletion}},
+ "save": {"Save", []Completion{NoCompletion}},
+ "replace": {"Replace", []Completion{NoCompletion}},
+ "vsplit": {"VSplit", []Completion{FileCompletion, NoCompletion}},
+ "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 {
+ // Open the default help if the user just typed "> help"
+ CurView().openHelp("help")
+ } else {
+ helpPage := args[0]
+ if FindRuntimeFile(RTHelp, helpPage) != nil {
+ CurView().openHelp(helpPage)
+ } else {
+ messenger.Error("Sorry, no help for ", helpPage)
+ }
+ }
+}
+
+// VSplit opens a vertical split with file given in the first argument
+// 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(strings.NewReader(""), ""))
+ } else {
+ filename := args[0]
+ home, _ := homedir.Dir()
+ filename = strings.Replace(filename, "~", home, 1)
+ 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(strings.NewReader(""), filename)
+ } else {
+ buf = NewBuffer(file, filename)
+ }
+ CurView().VSplit(buf)
+ }
+}
+
+// HSplit opens a horizontal split with file given in the first argument
+// 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(strings.NewReader(""), ""))
+ } else {
+ filename := args[0]
+ home, _ := homedir.Dir()
+ filename = strings.Replace(filename, "~", home, 1)
+ 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(strings.NewReader(""), filename)
+ } else {
+ buf = NewBuffer(file, filename)
+ }
+ CurView().HSplit(buf)
+ }
+}
+
+// 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 {
+ CurView().AddTab(true)
+ } else {
+ filename := args[0]
+ home, _ := homedir.Dir()
+ filename = strings.Replace(filename, "~", home, 1)
+ file, _ := os.Open(filename)
+ defer file.Close()
+
+ tab := NewTabFromView(NewView(NewBuffer(file, filename)))
+ tab.SetNum(len(tabs))
+ tabs = append(tabs, tab)
+ curTab = len(tabs) - 1
+ if len(tabs) == 2 {
+ for _, t := range tabs {
+ for _, v := range t.views {
+ v.ToggleTabbar()
+ }
+ }
+ }