]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/micro.go
Fix replace cursor relocation
[micro.git] / cmd / micro / micro.go
index a05761fbb74de27743bd14480af47949985f1084..69ac08278df51d6cc8dff4257086c137ea08e510 100644 (file)
@@ -5,10 +5,14 @@ import (
        "fmt"
        "io/ioutil"
        "os"
+       "runtime"
 
+       "github.com/atotto/clipboard"
        "github.com/go-errors/errors"
+       "github.com/layeh/gopher-luar"
        "github.com/mattn/go-isatty"
        "github.com/mitchellh/go-homedir"
+       "github.com/yuin/gopher-lua"
        "github.com/zyedidia/tcell"
        "github.com/zyedidia/tcell/encoding"
 )
@@ -27,7 +31,8 @@ var (
        // Object to send messages and prompts to the user
        messenger *Messenger
 
-       // The default style
+       // The default highlighting style
+       // This simply defines the default foreground and background colors
        defStyle tcell.Style
 
        // Where the user's configuration is
@@ -35,17 +40,28 @@ var (
        // If $XDG_CONFIG_HOME is not set, it is ~/.config/micro
        configDir string
 
-       // Version is the version number.
+       // Version is the version number or commit hash
        // This should be set by the linker
        Version = "Unknown"
 
-       // Is the help screen open
-       helpOpen = false
+       // L is the lua state
+       // This is the VM that runs the plugins
+       L *lua.LState
+
+       // The list of views
+       tabs []*Tab
+       // This is the currently open tab
+       // It's just an index to the tab in the tabs array
+       curTab int
+
+       jobs   chan JobFunction
+       events chan tcell.Event
 )
 
 // LoadInput loads the file input for the editor
-func LoadInput() (string, []byte, error) {
+func LoadInput() []*Buffer {
        // There are a number of ways micro should start given its input
+
        // 1. If it is given a file in os.Args, it should open that
 
        // 2. If there is no input file and the input is not a terminal, that means
@@ -60,34 +76,45 @@ func LoadInput() (string, []byte, error) {
        var filename string
        var input []byte
        var err error
+       var buffers []*Buffer
 
        if len(os.Args) > 1 {
                // Option 1
-               filename = os.Args[1]
-               // Check that the file exists
-               if _, e := os.Stat(filename); e == nil {
-                       input, err = ioutil.ReadFile(filename)
+               for i := 1; i < len(os.Args); i++ {
+                       filename = os.Args[i]
+                       // Check that the file exists
+                       if _, e := os.Stat(filename); e == nil {
+                               input, err = ioutil.ReadFile(filename)
+                               if err != nil {
+                                       TermMessage(err)
+                                       continue
+                               }
+                       }
+                       buffers = append(buffers, NewBuffer(input, filename))
                }
        } else if !isatty.IsTerminal(os.Stdin.Fd()) {
                // Option 2
                // The input is not a terminal, so something is being piped in
                // and we should read from stdin
                input, err = ioutil.ReadAll(os.Stdin)
+               buffers = append(buffers, NewBuffer(input, filename))
+       } else {
+               // Option 3, just open an empty buffer
+               buffers = append(buffers, NewBuffer(input, filename))
        }
 
-       // Option 3, or just return whatever we got
-       return filename, input, err
+       return buffers
 }
 
-// InitConfigDir finds the configuration directory for micro according to the
-// XDG spec.
+// InitConfigDir finds the configuration directory for micro according to the XDG spec.
 // If no directory is found, it creates one.
 func InitConfigDir() {
        xdgHome := os.Getenv("XDG_CONFIG_HOME")
        if xdgHome == "" {
+               // The user has not set $XDG_CONFIG_HOME so we should act like it was set to ~/.config
                home, err := homedir.Dir()
                if err != nil {
-                       TermMessage("Error finding your home directory\nCan't load syntax files")
+                       TermMessage("Error finding your home directory\nCan't load config files")
                        return
                }
                xdgHome = home + "/.config"
@@ -95,6 +122,7 @@ func InitConfigDir() {
        configDir = xdgHome + "/micro"
 
        if _, err := os.Stat(xdgHome); os.IsNotExist(err) {
+               // If the xdgHome doesn't exist we should create it
                err = os.Mkdir(xdgHome, os.ModePerm)
                if err != nil {
                        TermMessage("Error creating XDG_CONFIG_HOME directory: " + err.Error())
@@ -102,6 +130,7 @@ func InitConfigDir() {
        }
 
        if _, err := os.Stat(configDir); os.IsNotExist(err) {
+               // If the micro specific config directory doesn't exist we should create that too
                err = os.Mkdir(configDir, os.ModePerm)
                if err != nil {
                        TermMessage("Error creating configuration directory: " + err.Error())
@@ -138,24 +167,17 @@ func InitScreen() {
                os.Setenv("TERM", oldTerm)
        }
 
-       // Default style
-       defStyle = tcell.StyleDefault.
-               Foreground(tcell.ColorDefault).
-               Background(tcell.ColorDefault)
-
-       // There may be another default style defined in the colorscheme
-       if style, ok := colorscheme["default"]; ok {
-               defStyle = style
-       }
-
        screen.SetStyle(defStyle)
        screen.EnableMouse()
 }
 
-// Redraw redraws the screen and the given view
-func Redraw(view *View) {
-       screen.Clear()
-       view.Display()
+// RedrawAll redraws everything -- all the views and the messenger
+func RedrawAll() {
+       messenger.Clear()
+       for _, v := range tabs[curTab].views {
+               v.Display()
+       }
+       DisplayTabs()
        messenger.Display()
        screen.Show()
 }
@@ -169,12 +191,10 @@ func main() {
                os.Exit(0)
        }
 
-       filename, input, err := LoadInput()
-       if err != nil {
-               fmt.Println(err)
-               os.Exit(1)
-       }
+       L = lua.NewState()
+       defer L.Close()
 
+       // Some encoding stuff in case the user isn't using UTF-8
        encoding.Register()
        tcell.SetEncodingFallback(tcell.EncodingFallbackASCII)
 
@@ -182,18 +202,18 @@ func main() {
        InitConfigDir()
        // Load the user's settings
        InitSettings()
+       InitCommands()
        InitBindings()
        // Load the syntax files, including the colorscheme
        LoadSyntaxFiles()
        // Load the help files
        LoadHelp()
 
-       buf := NewBuffer(string(input), filename)
-
        InitScreen()
 
        // This is just so if we have an error, we can exit cleanly and not completely
        // mess up the terminal being worked in
+       // In other words we need to shut down tcell before the program crashes
        defer func() {
                if err := recover(); err != nil {
                        screen.Fini()
@@ -205,58 +225,81 @@ func main() {
        }()
 
        messenger = new(Messenger)
-       view := NewView(buf)
+       messenger.history = make(map[string][]string)
+
+       buffers := LoadInput()
+       for _, buf := range buffers {
+               tab := NewTabFromView(NewView(buf))
+               tab.SetNum(len(tabs))
+               tabs = append(tabs, tab)
+               for _, t := range tabs {
+                       for _, v := range t.views {
+                               v.Resize(screen.Size())
+                       }
+               }
+       }
+
+       L.SetGlobal("OS", luar.New(L, runtime.GOOS))
+       L.SetGlobal("tabs", luar.New(L, tabs))
+       L.SetGlobal("curTab", luar.New(L, curTab))
+       L.SetGlobal("messenger", luar.New(L, messenger))
+       L.SetGlobal("GetOption", luar.New(L, GetOption))
+       L.SetGlobal("AddOption", luar.New(L, AddOption))
+       L.SetGlobal("BindKey", luar.New(L, BindKey))
+       L.SetGlobal("MakeCommand", luar.New(L, MakeCommand))
+       L.SetGlobal("CurView", luar.New(L, CurView))
+       L.SetGlobal("IsWordChar", luar.New(L, IsWordChar))
+
+       L.SetGlobal("JobStart", luar.New(L, JobStart))
+       L.SetGlobal("JobSend", luar.New(L, JobSend))
+       L.SetGlobal("JobStop", luar.New(L, JobStop))
+
+       LoadPlugins()
+
+       jobs = make(chan JobFunction, 100)
+       events = make(chan tcell.Event)
+
+       go func() {
+               for {
+                       events <- screen.PollEvent()
+               }
+       }()
 
        for {
                // Display everything
-               Redraw(view)
-
-               // Wait for the user's action
-               event := screen.PollEvent()
+               RedrawAll()
+
+               var event tcell.Event
+               select {
+               case f := <-jobs:
+                       f.function(f.output, f.args...)
+                       continue
+               case event = <-events:
+               }
 
-               if searching {
-                       HandleSearchEvent(event, view)
-               } else {
-                       // Check if we should quit
-                       switch e := event.(type) {
-                       case *tcell.EventKey:
-                               switch e.Key() {
-                               case tcell.KeyCtrlQ:
-                                       // Make sure not to quit if there are unsaved changes
-                                       if helpOpen {
-                                               view.OpenBuffer(buf)
-                                               helpOpen = false
-                                       } else {
-                                               if view.CanClose("Quit anyway? (yes, no, save) ") {
-                                                       screen.Fini()
-                                                       os.Exit(0)
-                                               }
-                                       }
-                               case tcell.KeyCtrlE:
-                                       input, canceled := messenger.Prompt("> ")
-                                       if !canceled {
-                                               HandleCommand(input, view)
-                                       }
-                               case tcell.KeyCtrlB:
-                                       input, canceled := messenger.Prompt("$ ")
-                                       if !canceled {
-                                               HandleShellCommand(input, view, true)
-                                       }
-                               case tcell.KeyCtrlG:
-                                       if !helpOpen {
-                                               helpBuffer := NewBuffer(helpTxt, "help.md")
-                                               helpBuffer.name = "Help"
-                                               helpOpen = true
-                                               view.OpenBuffer(helpBuffer)
-                                       } else {
-                                               view.OpenBuffer(buf)
-                                               helpOpen = false
-                                       }
+               switch e := event.(type) {
+               case *tcell.EventMouse:
+                       if e.Buttons() == tcell.Button1 {
+                               _, h := screen.Size()
+                               _, y := e.Position()
+                               if y == h-1 && messenger.message != "" {
+                                       clipboard.WriteAll(messenger.message)
+                                       continue
                                }
                        }
+               }
 
+               if TabbarHandleMouseEvent(event) {
+                       continue
+               }
+
+               if searching {
+                       // Since searching is done in real time, we need to redraw every time
+                       // there is a new event in the search bar
+                       HandleSearchEvent(event, CurView())
+               } else {
                        // Send it to the view
-                       view.HandleEvent(event)
+                       CurView().HandleEvent(event)
                }
        }
 }