import (
"flag"
"fmt"
+ "io/ioutil"
"os"
- "time"
+ "sort"
"github.com/go-errors/errors"
- homedir "github.com/mitchellh/go-homedir"
- "github.com/zyedidia/micro/cmd/micro/terminfo"
+ 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"
"github.com/zyedidia/tcell"
)
-const (
- 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
-)
-
var (
- // The main screen
- screen tcell.Screen
-
- // Where the user's configuration is
- // This should be $XDG_CONFIG_HOME/micro
- // If $XDG_CONFIG_HOME is not set, it is ~/.config/micro
- configDir string
-
- // Version is the version number or commit hash
- // These variables should be set by the linker when compiling
- Version = "0.0.0-unknown"
- // CommitHash is the commit this version was built on
- CommitHash = "Unknown"
- // CompileDate is the date this binary was compiled on
- CompileDate = "Unknown"
- // Debug logging
- Debug = "ON"
-
// Event channel
events chan tcell.Event
autosave chan bool
- // How many redraws have happened
- numRedraw uint
-
// Command line flags
flagVersion = flag.Bool("version", false, "Show the version number and information")
- flagStartPos = flag.String("startpos", "", "LINE,COL to start the cursor at when opening a buffer.")
flagConfigDir = flag.String("config-dir", "", "Specify a custom location for the configuration directory")
flagOptions = flag.Bool("options", false, "Show all option help")
+ optionFlags map[string]*string
)
-// 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 config files")
- return
- }
- xdgHome = home + "/.config"
- }
- configDir = xdgHome + "/micro"
-
- if len(*flagConfigDir) > 0 {
- if _, err := os.Stat(*flagConfigDir); os.IsNotExist(err) {
- TermMessage("Error: " + *flagConfigDir + " does not exist. Defaulting to " + configDir + ".")
- } else {
- configDir = *flagConfigDir
- return
- }
- }
-
- 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())
- }
- }
-
- 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())
- }
- }
-}
-
-// InitScreen creates and initializes the tcell screen
-func InitScreen() {
- // Should we enable true color?
- truecolor := os.Getenv("MICRO_TRUECOLOR") == "1"
-
- tcelldb := os.Getenv("TCELLDB")
- os.Setenv("TCELLDB", configDir+"/.tcelldb")
-
- // In order to enable true color, we have to set the TERM to `xterm-truecolor` when
- // initializing tcell, but after that, we can set the TERM back to whatever it was
- oldTerm := os.Getenv("TERM")
- if truecolor {
- os.Setenv("TERM", "xterm-truecolor")
- }
-
- // Initilize tcell
- var err error
- screen, err = tcell.NewScreen()
- if err != nil {
- if err == tcell.ErrTermNotFound {
- err = terminfo.WriteDB(configDir + "/.tcelldb")
- if err != nil {
- fmt.Println(err)
- fmt.Println("Fatal: Micro could not create tcelldb")
- os.Exit(1)
- }
- screen, err = tcell.NewScreen()
- if err != nil {
- fmt.Println(err)
- fmt.Println("Fatal: Micro could not initialize a screen.")
- os.Exit(1)
- }
- } else {
- fmt.Println(err)
- fmt.Println("Fatal: Micro could not initialize a screen.")
- os.Exit(1)
- }
- }
- if err = screen.Init(); err != nil {
- fmt.Println(err)
- os.Exit(1)
- }
-
- // Now we can put the TERM back to what it was before
- if truecolor {
- os.Setenv("TERM", oldTerm)
- }
-
- if GetGlobalOption("mouse").(bool) {
- screen.EnableMouse()
- }
-
- os.Setenv("TCELLDB", tcelldb)
-
- // screen.SetStyle(defStyle)
-}
-
func InitFlags() {
flag.Usage = func() {
fmt.Println("Usage: micro [OPTIONS] [FILE]...")
fmt.Println("-config-dir dir")
fmt.Println(" \tSpecify a custom location for the configuration directory")
- fmt.Println("-startpos LINE,COL")
- fmt.Println("+LINE:COL")
+ fmt.Println("[FILE]:LINE:COL")
fmt.Println(" \tSpecify a line and column to start the cursor at when opening a buffer")
fmt.Println(" \tThis can also be done by opening file:LINE:COL")
fmt.Println("-options")
fmt.Println("\nUse `micro -options` to see the full list of configuration options")
}
- optionFlags := make(map[string]*string)
+ optionFlags = make(map[string]*string)
- for k, v := range DefaultGlobalSettings() {
- optionFlags[k] = flag.String(k, "", fmt.Sprintf("The %s option. Default value: '%v'", k, v))
+ for k, v := range config.DefaultAllSettings() {
+ optionFlags[k] = flag.String(k, "", fmt.Sprintf("The %s option. Default value: '%v'.", k, v))
}
flag.Parse()
if *flagVersion {
// If -version was passed
- fmt.Println("Version:", Version)
- fmt.Println("Commit hash:", CommitHash)
- fmt.Println("Compiled on", CompileDate)
+ fmt.Println("Version:", util.Version)
+ fmt.Println("Commit hash:", util.CommitHash)
+ fmt.Println("Compiled on", util.CompileDate)
os.Exit(0)
}
if *flagOptions {
// If -options was passed
- for k, v := range DefaultGlobalSettings() {
+ var keys []string
+ m := config.DefaultAllSettings()
+ for k := range m {
+ keys = append(keys, k)
+ }
+ sort.Strings(keys)
+ for _, k := range keys {
+ v := m[k]
fmt.Printf("-%s value\n", k)
fmt.Printf(" \tDefault value: '%v'\n", v)
}
}
}
+// LoadInput determines which files should be loaded into buffers
+// based on the input stored in flag.Args()
+func LoadInput() []*buffer.Buffer {
+ // There are a number of ways micro should start given its input
+
+ // 1. If it is given a files in flag.Args(), it should open those
+
+ // 2. If there is no input file and the input is not a terminal, that means
+ // something is being piped in and the stdin should be opened in an
+ // empty buffer
+
+ // 3. If there is no input file and the input is a terminal, an empty buffer
+ // should be opened
+
+ var filename string
+ var input []byte
+ var err error
+ args := flag.Args()
+ buffers := make([]*buffer.Buffer, 0, len(args))
+
+ if len(args) > 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)
+ if err != nil {
+ screen.TermMessage(err)
+ continue
+ }
+ // If the file didn't exist, input will be empty, and we'll open an empty buffer
+ buffers = append(buffers, buf)
+ }
+ } 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)
+ if err != nil {
+ screen.TermMessage("Error reading from stdin: ", err)
+ input = []byte{}
+ }
+ buffers = append(buffers, buffer.NewBufferFromString(string(input), filename, buffer.BTDefault))
+ } else {
+ // Option 3, just open an empty buffer
+ buffers = append(buffers, buffer.NewBufferFromString(string(input), filename, buffer.BTDefault))
+ }
+
+ return buffers
+}
+
func main() {
+ defer os.Exit(0)
+
+ // runtime.SetCPUProfileRate(400)
+ // f, _ := os.Create("micro.prof")
+ // pprof.StartCPUProfile(f)
+ // defer pprof.StopCPUProfile()
+
var err error
InitLog()
+
InitFlags()
- InitConfigDir()
- InitRuntimeFiles()
- err = ReadSettings()
+
+ err = config.InitConfigDir(*flagConfigDir)
+ if err != nil {
+ screen.TermMessage(err)
+ }
+
+ config.InitRuntimeFiles()
+ err = config.ReadSettings()
+ if err != nil {
+ screen.TermMessage(err)
+ }
+ config.InitGlobalSettings()
+
+ // flag options
+ for k, v := range optionFlags {
+ if *v != "" {
+ nativeValue, err := config.GetNativeValue(k, config.DefaultAllSettings()[k], *v)
+ if err != nil {
+ screen.TermMessage(err)
+ continue
+ }
+ config.GlobalSettings[k] = nativeValue
+ }
+ }
+
+ action.InitBindings()
+ action.InitCommands()
+
+ err = config.InitColorscheme()
+ if err != nil {
+ screen.TermMessage(err)
+ }
+
+ err = config.LoadAllPlugins()
if err != nil {
- TermMessage(err)
+ screen.TermMessage(err)
}
- InitGlobalSettings()
- err = InitColorscheme()
+ err = config.RunPluginFn("init")
if err != nil {
- TermMessage(err)
+ screen.TermMessage(err)
}
- InitScreen()
+ 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.Fini()
+ screen.Screen.Fini()
fmt.Println("Micro encountered an error:", err)
+ // backup all open buffers
+ for _, b := range buffer.OpenBuffers {
+ b.Backup(false)
+ }
// Print the stack trace too
fmt.Print(errors.Wrap(err, 2).ErrorStack())
os.Exit(1)
}
}()
- b, err := NewBufferFromFile(os.Args[1])
-
- if err != nil {
- TermMessage(err)
- }
-
- width, height := screen.Size()
+ b := LoadInput()
+ action.InitTabs(b)
+ action.InitGlobals()
+
+ // Here is the event loop which runs in a separate thread
+ go func() {
+ events = make(chan tcell.Event)
+ for {
+ screen.Lock()
+ e := screen.Screen.PollEvent()
+ screen.Unlock()
+ if e != nil {
+ events <- e
+ }
+ }
+ }()
- w := NewWindow(0, 0, width/2, height/2, b)
+ 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()
+
+ var event tcell.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:
+ }
- for i := 0; i < 5; i++ {
- screen.Clear()
- w.DisplayBuffer()
- w.DisplayStatusLine()
- screen.Show()
- time.Sleep(200 * time.Millisecond)
- w.StartLine++
+ if action.InfoBar.HasPrompt {
+ action.InfoBar.HandleEvent(event)
+ } else {
+ action.Tabs.HandleEvent(event)
+ }
}
-
- // time.Sleep(2 * time.Second)
-
- screen.Fini()
}