]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/micro.go
Fix v2 import path for go mod
[micro.git] / cmd / micro / micro.go
index b1e70187a4b982e8cae6a197906f98787afcbdcc..45e0e25bffbff332bedeea4cbaef1c8d03710f3a 100644 (file)
@@ -5,18 +5,20 @@ import (
        "fmt"
        "io/ioutil"
        "os"
+       "regexp"
        "runtime"
        "sort"
        "time"
 
        "github.com/go-errors/errors"
        isatty "github.com/mattn/go-isatty"
-       "github.com/zyedidia/micro/internal/action"
-       "github.com/zyedidia/micro/internal/buffer"
-       "github.com/zyedidia/micro/internal/config"
-       "github.com/zyedidia/micro/internal/screen"
-       "github.com/zyedidia/micro/internal/shell"
-       "github.com/zyedidia/micro/internal/util"
+       lua "github.com/yuin/gopher-lua"
+       "github.com/zyedidia/micro/v2/internal/action"
+       "github.com/zyedidia/micro/v2/internal/buffer"
+       "github.com/zyedidia/micro/v2/internal/config"
+       "github.com/zyedidia/micro/v2/internal/screen"
+       "github.com/zyedidia/micro/v2/internal/shell"
+       "github.com/zyedidia/micro/v2/internal/util"
        "github.com/zyedidia/tcell"
 )
 
@@ -43,6 +45,7 @@ func InitFlags() {
                fmt.Println("-config-dir dir")
                fmt.Println("    \tSpecify a custom location for the configuration directory")
                fmt.Println("[FILE]:LINE:COL")
+               fmt.Println("+LINE:COL")
                fmt.Println("    \tSpecify a line and column to start the cursor at when opening a buffer")
                fmt.Println("-options")
                fmt.Println("    \tShow all option help")
@@ -146,11 +149,32 @@ func LoadInput() []*buffer.Buffer {
        args := flag.Args()
        buffers := make([]*buffer.Buffer, 0, len(args))
 
-       if len(args) > 0 {
+       btype := buffer.BTDefault
+       if !isatty.IsTerminal(os.Stdout.Fd()) {
+               btype = buffer.BTStdout
+       }
+
+       files := make([]string, 0, len(args))
+       flagStartPos := ""
+       flagr := regexp.MustCompile(`^\+\d+(:\d+)?$`)
+       for _, a := range args {
+               if flagr.MatchString(a) {
+                       flagStartPos = a[1:]
+               } else {
+                       if flagStartPos != "" {
+                               files = append(files, a+":"+flagStartPos)
+                               flagStartPos = ""
+                       } else {
+                               files = append(files, a)
+                       }
+               }
+       }
+
+       if len(files) > 0 {
                // Option 1
                // We go through each file and load it
-               for i := 0; i < len(args); i++ {
-                       buf, err := buffer.NewBufferFromFile(args[i], buffer.BTDefault)
+               for i := 0; i < len(files); i++ {
+                       buf, err := buffer.NewBufferFromFile(files[i], btype)
                        if err != nil {
                                screen.TermMessage(err)
                                continue
@@ -167,17 +191,22 @@ func LoadInput() []*buffer.Buffer {
                        screen.TermMessage("Error reading from stdin: ", err)
                        input = []byte{}
                }
-               buffers = append(buffers, buffer.NewBufferFromString(string(input), filename, buffer.BTDefault))
+               buffers = append(buffers, buffer.NewBufferFromString(string(input), filename, btype))
        } else {
                // Option 3, just open an empty buffer
-               buffers = append(buffers, buffer.NewBufferFromString(string(input), filename, buffer.BTDefault))
+               buffers = append(buffers, buffer.NewBufferFromString(string(input), filename, btype))
        }
 
        return buffers
 }
 
 func main() {
-       defer os.Exit(0)
+       defer func() {
+               if util.Stdout.Len() > 0 {
+                       fmt.Fprint(os.Stdout, util.Stdout.String())
+               }
+               os.Exit(0)
+       }()
 
        // runtime.SetCPUProfileRate(400)
        // f, _ := os.Create("micro.prof")
@@ -218,9 +247,6 @@ func main() {
 
        screen.Init()
 
-       // 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.Screen.Fini()
@@ -280,52 +306,68 @@ func main() {
 
        // clear the drawchan so we don't redraw excessively
        // if someone requested a redraw before we started displaying
-       for len(screen.DrawChan) > 0 {
-               <-screen.DrawChan
+       for len(screen.DrawChan()) > 0 {
+               <-screen.DrawChan()
        }
 
-       var event tcell.Event
-
        // wait for initial resize event
        select {
-       case event = <-events:
+       case event := <-events:
                action.Tabs.HandleEvent(event)
        case <-time.After(10 * time.Millisecond):
                // time out after 10ms
        }
 
+       // Since this loop is very slow (waits for user input every time) it's
+       // okay to be inefficient and run it via a function every time
+       // We do this so we can recover from panics without crashing the editor
        for {
-               // Display everything
-               screen.Screen.Fill(' ', config.DefStyle)
-               screen.Screen.HideCursor()
-               action.Tabs.Display()
-               for _, ep := range action.MainTab().Panes {
-                       ep.Display()
-               }
-               action.MainTab().Display()
-               action.InfoBar.Display()
-               screen.Screen.Show()
-
-               event = nil
-
-               // Check for new events
-               select {
-               case f := <-shell.Jobs:
-                       // If a new job has finished while running in the background we should execute the callback
-                       f.Function(f.Output, f.Args)
-               case <-config.Autosave:
-                       for _, b := range buffer.OpenBuffers {
-                               b.Save()
+               DoEvent()
+       }
+}
+
+// DoEvent runs the main action loop of the editor
+func DoEvent() {
+       var event tcell.Event
+
+       // recover from errors without crashing the editor
+       defer func() {
+               if err := recover(); err != nil {
+                       if e, ok := err.(*lua.ApiError); ok {
+                               screen.TermMessage("Lua API error:", e)
+                       } else {
+                               screen.TermMessage("Micro encountered an error:", errors.Wrap(err, 2).ErrorStack(), "\nIf you can reproduce this error, please report it at https://github.com/zyedidia/micro/issues")
                        }
-               case <-shell.CloseTerms:
-               case event = <-events:
-               case <-screen.DrawChan:
                }
+       }()
+       // Display everything
+       screen.Screen.Fill(' ', config.DefStyle)
+       screen.Screen.HideCursor()
+       action.Tabs.Display()
+       for _, ep := range action.MainTab().Panes {
+               ep.Display()
+       }
+       action.MainTab().Display()
+       action.InfoBar.Display()
+       screen.Screen.Show()
 
-               if action.InfoBar.HasPrompt {
-                       action.InfoBar.HandleEvent(event)
-               } else {
-                       action.Tabs.HandleEvent(event)
+       // Check for new events
+       select {
+       case f := <-shell.Jobs:
+               // If a new job has finished while running in the background we should execute the callback
+               f.Function(f.Output, f.Args)
+       case <-config.Autosave:
+               for _, b := range buffer.OpenBuffers {
+                       b.Save()
                }
+       case <-shell.CloseTerms:
+       case event = <-events:
+       case <-screen.DrawChan():
+       }
+
+       if action.InfoBar.HasPrompt {
+               action.InfoBar.HandleEvent(event)
+       } else {
+               action.Tabs.HandleEvent(event)
        }
 }