]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/command.go
Fix: mouse clicking with softwrap
[micro.git] / cmd / micro / command.go
index ac2fa5f2e0d3e2dcd721c2083e6984d121835fc2..0c582e2025ce9d75f4867695dae651c741b3caaf 100644 (file)
@@ -4,10 +4,10 @@ import (
        "bytes"
        "fmt"
        "io"
-       "io/ioutil"
        "os"
        "os/exec"
        "os/signal"
+       "path/filepath"
        "regexp"
        "strings"
 
@@ -46,6 +46,9 @@ func init() {
                "ToggleLog": ToggleLog,
                "Plugin":    PluginCmd,
                "Reload":    Reload,
+               "Cd":        Cd,
+               "Pwd":       Pwd,
+               "Open":      Open,
        }
 }
 
@@ -95,6 +98,9 @@ func DefaultCommands() map[string]StrCommand {
                "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}},
        }
 }
 
@@ -168,17 +174,69 @@ func PluginCmd(args []string) {
                                        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)
        }
@@ -207,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)
                }
@@ -229,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)
                }
@@ -267,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 {