]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/micro.go
Correctly detect synatx ft from header
[micro.git] / cmd / micro / micro.go
index 3a441ceed943e3921f3ac26e88c3dfac82c7c348..663b217b1a4d0c11a58b6dd149031d278e3e298b 100644 (file)
@@ -11,13 +11,13 @@ import (
        "time"
 
        "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/clipboard"
        "github.com/zyedidia/tcell"
        "github.com/zyedidia/tcell/encoding"
+       "layeh.com/gopher-luar"
 )
 
 const (
@@ -84,27 +84,37 @@ func LoadInput() []*Buffer {
        var filename string
        var input []byte
        var err error
-       var buffers []*Buffer
+       args := flag.Args()
+       buffers := make([]*Buffer, 0, len(args))
 
-       if len(flag.Args()) > 0 {
+       if len(args) > 0 {
                // Option 1
                // We go through each file and load it
-               for i := 0; i < len(flag.Args()); i++ {
-                       filename = flag.Args()[i]
+               for i := 0; i < len(args); i++ {
+                       filename = args[i]
 
                        // Check that the file exists
                        var input *os.File
                        if _, e := os.Stat(filename); e == nil {
                                // If it exists we load it into a buffer
                                input, err = os.Open(filename)
+                               stat, _ := input.Stat()
                                defer input.Close()
                                if err != nil {
                                        TermMessage(err)
                                        continue
                                }
+                               if stat.IsDir() {
+                                       TermMessage("Cannot read", filename, "because it is a directory")
+                                       continue
+                               }
                        }
                        // If the file didn't exist, input will be empty, and we'll open an empty buffer
-                       buffers = append(buffers, NewBuffer(input, filename))
+                       if input != nil {
+                               buffers = append(buffers, NewBuffer(input, FSize(input), filename))
+                       } else {
+                               buffers = append(buffers, NewBufferFromString("", filename))
+                       }
                }
        } else if !isatty.IsTerminal(os.Stdin.Fd()) {
                // Option 2
@@ -115,10 +125,10 @@ func LoadInput() []*Buffer {
                        TermMessage("Error reading from stdin: ", err)
                        input = []byte{}
                }
-               buffers = append(buffers, NewBuffer(strings.NewReader(string(input)), filename))
+               buffers = append(buffers, NewBufferFromString(string(input), filename))
        } else {
                // Option 3, just open an empty buffer
-               buffers = append(buffers, NewBuffer(strings.NewReader(string(input)), filename))
+               buffers = append(buffers, NewBufferFromString(string(input), filename))
        }
 
        return buffers
@@ -192,6 +202,14 @@ func InitScreen() {
 // RedrawAll redraws everything -- all the views and the messenger
 func RedrawAll() {
        messenger.Clear()
+
+       w, h := screen.Size()
+       for x := 0; x < w; x++ {
+               for y := 0; y < h; y++ {
+                       screen.SetContent(x, y, ' ', nil, defStyle)
+               }
+       }
+
        for _, v := range tabs[curTab].views {
                v.Display()
        }
@@ -213,14 +231,11 @@ func LoadAll() {
        InitCommands()
        InitBindings()
 
-       LoadSyntaxFiles()
+       InitColorscheme()
 
        for _, tab := range tabs {
                for _, v := range tab.views {
                        v.Buf.UpdateRules()
-                       if v.Buf.Settings["syntax"].(bool) {
-                               v.matches = Match(v)
-                       }
                }
        }
 }
@@ -292,6 +307,7 @@ func main() {
        // This is used for sending the user messages in the bottom of the editor
        messenger = new(Messenger)
        messenger.history = make(map[string][]string)
+       InitColorscheme()
 
        // Now we load the input
        buffers := LoadInput()
@@ -299,6 +315,7 @@ func main() {
                screen.Fini()
                os.Exit(1)
        }
+
        for _, buf := range buffers {
                // For each buffer we create a new tab and place the view in that tab
                tab := NewTabFromView(NewView(buf))
@@ -362,6 +379,7 @@ func main() {
        L.SetGlobal("ListRuntimeFiles", luar.New(L, PluginListRuntimeFiles))
        L.SetGlobal("AddRuntimeFile", luar.New(L, PluginAddRuntimeFile))
        L.SetGlobal("AddRuntimeFilesFromDirectory", luar.New(L, PluginAddRuntimeFilesFromDirectory))
+       L.SetGlobal("AddRuntimeFileFromMemory", luar.New(L, PluginAddRuntimeFileFromMemory))
 
        jobs = make(chan JobFunction, 100)
        events = make(chan tcell.Event, 100)
@@ -369,23 +387,15 @@ func main() {
 
        LoadPlugins()
 
-       // Load the syntax files, including the colorscheme
-       LoadSyntaxFiles()
-
        for _, t := range tabs {
                for _, v := range t.views {
-                       v.Buf.FindFileType()
-                       v.Buf.UpdateRules()
-                       for _, pl := range loadedPlugins {
+                       for pl := range loadedPlugins {
                                _, err := Call(pl+".onViewOpen", v)
                                if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
                                        TermMessage(err)
                                        continue
                                }
                        }
-                       if v.Buf.Settings["syntax"].(bool) {
-                               v.matches = Match(v)
-                       }
                }
        }
 
@@ -424,6 +434,10 @@ func main() {
 
                for event != nil {
                        switch e := event.(type) {
+                       case *tcell.EventResize:
+                               for _, t := range tabs {
+                                       t.Resize()
+                               }
                        case *tcell.EventMouse:
                                if e.Buttons() == tcell.Button1 {
                                        // If the user left clicked we check a couple things
@@ -471,7 +485,6 @@ func main() {
                        default:
                                event = nil
                        }
-
                }
        }
 }