]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/micro.go
Rename to tabstospaces for consistency
[micro.git] / cmd / micro / micro.go
index ecc9d2887c2d523b11ac7b34daf8dc9bf4e8b3c9..718d220d6b7937b09262217ea393b463b7acaa68 100644 (file)
@@ -5,12 +5,15 @@ import (
        "fmt"
        "io/ioutil"
        "os"
+       "runtime"
 
-       "github.com/gdamore/tcell"
-       "github.com/gdamore/tcell/encoding"
        "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"
 )
 
 const (
@@ -41,6 +44,9 @@ var (
 
        // Is the help screen open
        helpOpen = false
+
+       // L is the lua state
+       L *lua.LState
 )
 
 // LoadInput loads the file input for the editor
@@ -109,32 +115,8 @@ func InitConfigDir() {
        }
 }
 
-var flagVersion = flag.Bool("version", false, "Show version number")
-
-func main() {
-       flag.Parse()
-       if *flagVersion {
-               fmt.Println("Micro version:", Version)
-               os.Exit(0)
-       }
-
-       filename, input, err := LoadInput()
-       if err != nil {
-               fmt.Println(err)
-               os.Exit(1)
-       }
-
-       encoding.Register()
-
-       // Find the user's configuration directory (probably $XDG_CONFIG_HOME/micro)
-       InitConfigDir()
-       // Load the user's settings
-       InitSettings()
-       // Load the syntax files, including the colorscheme
-       LoadSyntaxFiles()
-
-       buf := NewBuffer(string(input), filename)
-
+// InitScreen creates and initializes the tcell screen
+func InitScreen() {
        // Should we enable true color?
        truecolor := os.Getenv("MICRO_TRUECOLOR") == "1"
 
@@ -146,6 +128,7 @@ func main() {
        }
 
        // Initilize tcell
+       var err error
        screen, err = tcell.NewScreen()
        if err != nil {
                fmt.Println(err)
@@ -161,18 +144,6 @@ func main() {
                os.Setenv("TERM", oldTerm)
        }
 
-       // This is just so if we have an error, we can exit cleanly and not completely
-       // mess up the terminal being worked in
-       defer func() {
-               if err := recover(); err != nil {
-                       screen.Fini()
-                       fmt.Println("Micro encountered an error:", err)
-                       // Print the stack trace too
-                       fmt.Print(errors.Wrap(err, 2).ErrorStack())
-                       os.Exit(1)
-               }
-       }()
-
        // Default style
        defStyle = tcell.StyleDefault.
                Foreground(tcell.ColorDefault).
@@ -185,18 +156,77 @@ func main() {
 
        screen.SetStyle(defStyle)
        screen.EnableMouse()
+}
+
+// Redraw redraws the screen and the given view
+func Redraw(view *View) {
+       screen.Clear()
+       view.Display()
+       messenger.Display()
+       screen.Show()
+}
+
+var flagVersion = flag.Bool("version", false, "Show version number")
+
+func main() {
+       flag.Parse()
+       if *flagVersion {
+               fmt.Println("Micro version:", Version)
+               os.Exit(0)
+       }
+
+       filename, input, err := LoadInput()
+       if err != nil {
+               fmt.Println(err)
+               os.Exit(1)
+       }
+
+       L = lua.NewState()
+       defer L.Close()
+
+       encoding.Register()
+       tcell.SetEncodingFallback(tcell.EncodingFallbackASCII)
+
+       // Find the user's configuration directory (probably $XDG_CONFIG_HOME/micro)
+       InitConfigDir()
+       // Load the user's settings
+       InitSettings()
+       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
+       defer func() {
+               if err := recover(); err != nil {
+                       screen.Fini()
+                       fmt.Println("Micro encountered an error:", err)
+                       // Print the stack trace too
+                       fmt.Print(errors.Wrap(err, 2).ErrorStack())
+                       os.Exit(1)
+               }
+       }()
 
        messenger = new(Messenger)
        view := NewView(buf)
 
-       for {
-               // Display everything
-               screen.Clear()
+       L.SetGlobal("OS", luar.New(L, runtime.GOOS))
+       L.SetGlobal("view", luar.New(L, view))
+       L.SetGlobal("messenger", luar.New(L, messenger))
+       L.SetGlobal("GetOption", luar.New(L, GetOption))
+       L.SetGlobal("AddOption", luar.New(L, AddOption))
 
-               view.Display()
-               messenger.Display()
+       LoadPlugins()
 
-               screen.Show()
+       for {
+               // Display everything
+               Redraw(view)
 
                // Wait for the user's action
                event := screen.PollEvent()
@@ -227,12 +257,12 @@ func main() {
                                case tcell.KeyCtrlB:
                                        input, canceled := messenger.Prompt("$ ")
                                        if !canceled {
-                                               HandleShellCommand(input, view)
+                                               HandleShellCommand(input, view, true)
                                        }
                                case tcell.KeyCtrlG:
                                        if !helpOpen {
-                                               helpBuffer := NewBuffer(helpTxt, "")
-                                               helpBuffer.name = "Help"
+                                               helpBuffer := NewBuffer(helpTxt, "help.md")
+                                               helpBuffer.Name = "Help"
                                                helpOpen = true
                                                view.OpenBuffer(helpBuffer)
                                        } else {