]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/micro.go
OutdentSelection works on all cases (whatever tabsize or tabstospace values)
[micro.git] / cmd / micro / micro.go
index 227c654b9360dd9fd9d999ad4a3eb70498feb4b8..d4668508448159a4d1bef9914d925edc0ab681d2 100644 (file)
@@ -11,18 +11,16 @@ 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 (
-       synLinesUp           = 75  // How many lines up to look to do syntax highlighting
-       synLinesDown         = 75  // How many lines down to look to do syntax highlighting
        doubleClickThreshold = 400 // How many milliseconds to wait before a second click is not a double click
        undoThreshold        = 500 // If two events are less than n milliseconds apart, undo both of them
        autosaveTime         = 8   // Number of seconds to wait before autosaving
@@ -84,26 +82,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 = ioutil.ReadFile(filename)
+                               input, err = os.Open(filename)
+                               stat, _ := input.Stat()
+                               defer input.Close()
                                if err != nil {
                                        TermMessage(err)
-                                       input = []byte{}
-                                       filename = ""
+                                       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
@@ -114,10 +123,10 @@ func LoadInput() []*Buffer {
                        TermMessage("Error reading from stdin: ", err)
                        input = []byte{}
                }
-               buffers = append(buffers, NewBuffer(input, filename))
+               buffers = append(buffers, NewBufferFromString(string(input), filename))
        } else {
                // Option 3, just open an empty buffer
-               buffers = append(buffers, NewBuffer(input, filename))
+               buffers = append(buffers, NewBufferFromString(string(input), filename))
        }
 
        return buffers
@@ -172,6 +181,10 @@ func InitScreen() {
        screen, err = tcell.NewScreen()
        if err != nil {
                fmt.Println(err)
+               if err == tcell.ErrTermNotFound {
+                       fmt.Println("Micro does not recognize your terminal:", oldTerm)
+                       fmt.Println("Please go to https://github.com/zyedidia/mkinfo to read about how to fix this problem (it should be easy to fix).")
+               }
                os.Exit(1)
        }
        if err = screen.Init(); err != nil {
@@ -191,6 +204,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()
        }
@@ -212,14 +233,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)
-                       }
                }
        }
 }
@@ -232,6 +250,7 @@ func main() {
        flag.Usage = func() {
                fmt.Println("Usage: micro [OPTIONS] [FILE]...")
                fmt.Print("Micro's options can be set via command line arguments for quick adjustments. For real configuration, please use the bindings.json file (see 'help options').\n\n")
+               flag.CommandLine.SetOutput(os.Stdout)
                flag.PrintDefaults()
        }
 
@@ -259,7 +278,17 @@ func main() {
        encoding.Register()
        tcell.SetEncodingFallback(tcell.EncodingFallbackASCII)
 
-       LoadAll()
+       // Find the user's configuration directory (probably $XDG_CONFIG_HOME/micro)
+       InitConfigDir()
+
+       // Build a list of available Extensions (Syntax, Colorscheme etc.)
+       InitRuntimeFiles()
+
+       // Load the user's settings
+       InitGlobalSettings()
+
+       InitCommands()
+       InitBindings()
 
        // Start the screen
        InitScreen()
@@ -284,6 +313,11 @@ func main() {
 
        // Now we load the input
        buffers := LoadInput()
+       if len(buffers) == 0 {
+               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))
@@ -292,9 +326,6 @@ func main() {
                for _, t := range tabs {
                        for _, v := range t.views {
                                v.Center(false)
-                               if globalSettings["syntax"].(bool) {
-                                       v.matches = Match(v)
-                               }
                        }
 
                        t.Resize()
@@ -325,7 +356,7 @@ func main() {
        L.SetGlobal("HandleShellCommand", luar.New(L, HandleShellCommand))
        L.SetGlobal("GetLeadingWhitespace", luar.New(L, GetLeadingWhitespace))
        L.SetGlobal("MakeCompletion", luar.New(L, MakeCompletion))
-       L.SetGlobal("NewBuffer", luar.New(L, NewBuffer))
+       L.SetGlobal("NewBuffer", luar.New(L, NewBufferFromString))
        L.SetGlobal("RuneStr", luar.New(L, func(r rune) string {
                return string(r)
        }))
@@ -341,6 +372,7 @@ func main() {
 
        // Used for asynchronous jobs
        L.SetGlobal("JobStart", luar.New(L, JobStart))
+       L.SetGlobal("JobSpawn", luar.New(L, JobSpawn))
        L.SetGlobal("JobSend", luar.New(L, JobSend))
        L.SetGlobal("JobStop", luar.New(L, JobStop))
 
@@ -349,6 +381,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)
@@ -356,30 +389,26 @@ 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)
-                       }
                }
        }
 
+       InitColorscheme()
+
        // Here is the event loop which runs in a separate thread
        go func() {
                for {
-                       events <- screen.PollEvent()
+                       if screen != nil {
+                               events <- screen.PollEvent()
+                       }
                }
        }()
 
@@ -411,26 +440,32 @@ 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
-                                       _, h := screen.Size()
-                                       x, y := e.Position()
-                                       if y == h-1 && messenger.message != "" && globalSettings["infobar"].(bool) {
-                                               // If the user clicked in the bottom bar, and there is a message down there
-                                               // we copy it to the clipboard.
-                                               // Often error messages are displayed down there so it can be useful to easily
-                                               // copy the message
-                                               clipboard.WriteAll(messenger.message, "primary")
-                                               break
-                                       }
+                               if !searching {
+                                       if e.Buttons() == tcell.Button1 {
+                                               // If the user left clicked we check a couple things
+                                               _, h := screen.Size()
+                                               x, y := e.Position()
+                                               if y == h-1 && messenger.message != "" && globalSettings["infobar"].(bool) {
+                                                       // If the user clicked in the bottom bar, and there is a message down there
+                                                       // we copy it to the clipboard.
+                                                       // Often error messages are displayed down there so it can be useful to easily
+                                                       // copy the message
+                                                       clipboard.WriteAll(messenger.message, "primary")
+                                                       break
+                                               }
 
-                                       if CurView().mouseReleased {
-                                               // We loop through each view in the current tab and make sure the current view
-                                               // is the one being clicked in
-                                               for _, v := range tabs[curTab].views {
-                                                       if x >= v.x && x < v.x+v.width && y >= v.y && y < v.y+v.height {
-                                                               tabs[curTab].curView = v.Num
+                                               if CurView().mouseReleased {
+                                                       // We loop through each view in the current tab and make sure the current view
+                                                       // is the one being clicked in
+                                                       for _, v := range tabs[curTab].views {
+                                                               if x >= v.x && x < v.x+v.Width && y >= v.y && y < v.y+v.Height {
+                                                                       tabs[curTab].CurView = v.Num
+                                                               }
                                                        }
                                                }
                                        }
@@ -458,7 +493,6 @@ func main() {
                        default:
                                event = nil
                        }
-
                }
        }
 }