]> git.lizzy.rs Git - micro.git/blob - cmd/micro/micro.go
Code optimisation (#1117)
[micro.git] / cmd / micro / micro.go
1 package main
2
3 import (
4         "flag"
5         "fmt"
6         "io/ioutil"
7         "os"
8         "path/filepath"
9         "runtime"
10         "strings"
11         "time"
12
13         "github.com/go-errors/errors"
14         "github.com/mattn/go-isatty"
15         "github.com/mitchellh/go-homedir"
16         "github.com/yuin/gopher-lua"
17         "github.com/zyedidia/clipboard"
18         "github.com/zyedidia/micro/cmd/micro/terminfo"
19         "github.com/zyedidia/tcell"
20         "github.com/zyedidia/tcell/encoding"
21         "layeh.com/gopher-luar"
22 )
23
24 const (
25         doubleClickThreshold = 400 // How many milliseconds to wait before a second click is not a double click
26         undoThreshold        = 500 // If two events are less than n milliseconds apart, undo both of them
27         autosaveTime         = 8   // Number of seconds to wait before autosaving
28 )
29
30 var (
31         // The main screen
32         screen tcell.Screen
33
34         // Object to send messages and prompts to the user
35         messenger *Messenger
36
37         // The default highlighting style
38         // This simply defines the default foreground and background colors
39         defStyle tcell.Style
40
41         // Where the user's configuration is
42         // This should be $XDG_CONFIG_HOME/micro
43         // If $XDG_CONFIG_HOME is not set, it is ~/.config/micro
44         configDir string
45
46         // Version is the version number or commit hash
47         // These variables should be set by the linker when compiling
48         Version = "0.0.0-unknown"
49         // CommitHash is the commit this version was built on
50         CommitHash = "Unknown"
51         // CompileDate is the date this binary was compiled on
52         CompileDate = "Unknown"
53
54         // The list of views
55         tabs []*Tab
56         // This is the currently open tab
57         // It's just an index to the tab in the tabs array
58         curTab int
59
60         // Channel of jobs running in the background
61         jobs chan JobFunction
62
63         // Event channel
64         events   chan tcell.Event
65         autosave chan bool
66
67         // Channels for the terminal emulator
68         updateterm chan bool
69         closeterm  chan int
70
71         // How many redraws have happened
72         numRedraw uint
73 )
74
75 // LoadInput determines which files should be loaded into buffers
76 // based on the input stored in flag.Args()
77 func LoadInput() []*Buffer {
78         // There are a number of ways micro should start given its input
79
80         // 1. If it is given a files in flag.Args(), it should open those
81
82         // 2. If there is no input file and the input is not a terminal, that means
83         // something is being piped in and the stdin should be opened in an
84         // empty buffer
85
86         // 3. If there is no input file and the input is a terminal, an empty buffer
87         // should be opened
88
89         var filename string
90         var input []byte
91         var err error
92         args := flag.Args()
93         buffers := make([]*Buffer, 0, len(args))
94
95         if len(args) > 0 {
96                 // Option 1
97                 // We go through each file and load it
98                 for i := 0; i < len(args); i++ {
99                         if strings.HasPrefix(args[i], "+") {
100                                 if strings.Contains(args[i], ":") {
101                                         split := strings.Split(args[i], ":")
102                                         *flagStartPos = split[0][1:] + "," + split[1]
103                                 } else {
104                                         *flagStartPos = args[i][1:] + ",0"
105                                 }
106                                 continue
107                         }
108
109                         buf, err := NewBufferFromFile(args[i])
110                         if err != nil {
111                                 TermMessage(err)
112                                 continue
113                         }
114                         // If the file didn't exist, input will be empty, and we'll open an empty buffer
115                         buffers = append(buffers, buf)
116                 }
117         } else if !isatty.IsTerminal(os.Stdin.Fd()) {
118                 // Option 2
119                 // The input is not a terminal, so something is being piped in
120                 // and we should read from stdin
121                 input, err = ioutil.ReadAll(os.Stdin)
122                 if err != nil {
123                         TermMessage("Error reading from stdin: ", err)
124                         input = []byte{}
125                 }
126                 buffers = append(buffers, NewBufferFromString(string(input), filename))
127         } else {
128                 // Option 3, just open an empty buffer
129                 buffers = append(buffers, NewBufferFromString(string(input), filename))
130         }
131
132         return buffers
133 }
134
135 // InitConfigDir finds the configuration directory for micro according to the XDG spec.
136 // If no directory is found, it creates one.
137 func InitConfigDir() {
138         xdgHome := os.Getenv("XDG_CONFIG_HOME")
139         if xdgHome == "" {
140                 // The user has not set $XDG_CONFIG_HOME so we should act like it was set to ~/.config
141                 home, err := homedir.Dir()
142                 if err != nil {
143                         TermMessage("Error finding your home directory\nCan't load config files")
144                         return
145                 }
146                 xdgHome = home + "/.config"
147         }
148         configDir = xdgHome + "/micro"
149
150         if len(*flagConfigDir) > 0 {
151                 if _, err := os.Stat(*flagConfigDir); os.IsNotExist(err) {
152                         TermMessage("Error: " + *flagConfigDir + " does not exist. Defaulting to " + configDir + ".")
153                 } else {
154                         configDir = *flagConfigDir
155                         return
156                 }
157         }
158
159         if _, err := os.Stat(xdgHome); os.IsNotExist(err) {
160                 // If the xdgHome doesn't exist we should create it
161                 err = os.Mkdir(xdgHome, os.ModePerm)
162                 if err != nil {
163                         TermMessage("Error creating XDG_CONFIG_HOME directory: " + err.Error())
164                 }
165         }
166
167         if _, err := os.Stat(configDir); os.IsNotExist(err) {
168                 // If the micro specific config directory doesn't exist we should create that too
169                 err = os.Mkdir(configDir, os.ModePerm)
170                 if err != nil {
171                         TermMessage("Error creating configuration directory: " + err.Error())
172                 }
173         }
174 }
175
176 // InitScreen creates and initializes the tcell screen
177 func InitScreen() {
178         // Should we enable true color?
179         truecolor := os.Getenv("MICRO_TRUECOLOR") == "1"
180
181         tcelldb := os.Getenv("TCELLDB")
182         os.Setenv("TCELLDB", configDir+"/.tcelldb")
183
184         // In order to enable true color, we have to set the TERM to `xterm-truecolor` when
185         // initializing tcell, but after that, we can set the TERM back to whatever it was
186         oldTerm := os.Getenv("TERM")
187         if truecolor {
188                 os.Setenv("TERM", "xterm-truecolor")
189         }
190
191         // Initilize tcell
192         var err error
193         screen, err = tcell.NewScreen()
194         if err != nil {
195                 if err == tcell.ErrTermNotFound {
196                         terminfo.WriteDB(configDir + "/.tcelldb")
197                         screen, err = tcell.NewScreen()
198                         if err != nil {
199                                 fmt.Println(err)
200                                 fmt.Println("Fatal: Micro could not initialize a screen.")
201                                 os.Exit(1)
202                         }
203                 } else {
204                         fmt.Println(err)
205                         fmt.Println("Fatal: Micro could not initialize a screen.")
206                         os.Exit(1)
207                 }
208         }
209         if err = screen.Init(); err != nil {
210                 fmt.Println(err)
211                 os.Exit(1)
212         }
213
214         // Now we can put the TERM back to what it was before
215         if truecolor {
216                 os.Setenv("TERM", oldTerm)
217         }
218
219         if GetGlobalOption("mouse").(bool) {
220                 screen.EnableMouse()
221         }
222
223         os.Setenv("TCELLDB", tcelldb)
224
225         // screen.SetStyle(defStyle)
226 }
227
228 // RedrawAll redraws everything -- all the views and the messenger
229 func RedrawAll() {
230         messenger.Clear()
231
232         w, h := screen.Size()
233         for x := 0; x < w; x++ {
234                 for y := 0; y < h; y++ {
235                         screen.SetContent(x, y, ' ', nil, defStyle)
236                 }
237         }
238
239         for _, v := range tabs[curTab].Views {
240                 v.Display()
241         }
242         DisplayTabs()
243         messenger.Display()
244         if globalSettings["keymenu"].(bool) {
245                 DisplayKeyMenu()
246         }
247         screen.Show()
248
249         if numRedraw%50 == 0 {
250                 runtime.GC()
251         }
252         numRedraw++
253 }
254
255 func LoadAll() {
256         // Find the user's configuration directory (probably $XDG_CONFIG_HOME/micro)
257         InitConfigDir()
258
259         // Build a list of available Extensions (Syntax, Colorscheme etc.)
260         InitRuntimeFiles()
261
262         // Load the user's settings
263         InitGlobalSettings()
264
265         InitCommands()
266         InitBindings()
267
268         InitColorscheme()
269
270         for _, tab := range tabs {
271                 for _, v := range tab.Views {
272                         v.Buf.UpdateRules()
273                 }
274         }
275 }
276
277 // Command line flags
278 var flagVersion = flag.Bool("version", false, "Show the version number and information")
279 var flagStartPos = flag.String("startpos", "", "LINE,COL to start the cursor at when opening a buffer.")
280 var flagConfigDir = flag.String("config-dir", "", "Specify a custom location for the configuration directory")
281 var flagOptions = flag.Bool("options", false, "Show all option help")
282
283 func main() {
284         flag.Usage = func() {
285                 fmt.Println("Usage: micro [OPTIONS] [FILE]...")
286                 fmt.Println("-config-dir dir")
287                 fmt.Println("    \tSpecify a custom location for the configuration directory")
288                 fmt.Println("-startpos LINE,COL")
289                 fmt.Println("+LINE:COL")
290                 fmt.Println("    \tSpecify a line and column to start the cursor at when opening a buffer")
291                 fmt.Println("    \tThis can also be done by opening file:LINE:COL")
292                 fmt.Println("-options")
293                 fmt.Println("    \tShow all option help")
294                 fmt.Println("-version")
295                 fmt.Println("    \tShow the version number and information")
296
297                 fmt.Print("\nMicro's options can also be set via command line arguments for quick\nadjustments. For real configuration, please use the settings.json\nfile (see 'help options').\n\n")
298                 fmt.Println("-option value")
299                 fmt.Println("    \tSet `option` to `value` for this session")
300                 fmt.Println("    \tFor example: `micro -syntax off file.c`")
301                 fmt.Println("\nUse `micro -options` to see the full list of configuration options")
302         }
303
304         optionFlags := make(map[string]*string)
305
306         for k, v := range DefaultGlobalSettings() {
307                 optionFlags[k] = flag.String(k, "", fmt.Sprintf("The %s option. Default value: '%v'", k, v))
308         }
309
310         flag.Parse()
311
312         if *flagVersion {
313                 // If -version was passed
314                 fmt.Println("Version:", Version)
315                 fmt.Println("Commit hash:", CommitHash)
316                 fmt.Println("Compiled on", CompileDate)
317                 os.Exit(0)
318         }
319
320         if *flagOptions {
321                 // If -options was passed
322                 for k, v := range DefaultGlobalSettings() {
323                         fmt.Printf("-%s value\n", k)
324                         fmt.Printf("    \tThe %s option. Default value: '%v'\n", k, v)
325                 }
326                 os.Exit(0)
327         }
328
329         // Start the Lua VM for running plugins
330         L = lua.NewState()
331         defer L.Close()
332
333         // Some encoding stuff in case the user isn't using UTF-8
334         encoding.Register()
335         tcell.SetEncodingFallback(tcell.EncodingFallbackASCII)
336
337         // Find the user's configuration directory (probably $XDG_CONFIG_HOME/micro)
338         InitConfigDir()
339
340         // Build a list of available Extensions (Syntax, Colorscheme etc.)
341         InitRuntimeFiles()
342
343         // Load the user's settings
344         InitGlobalSettings()
345
346         InitCommands()
347         InitBindings()
348
349         // Start the screen
350         InitScreen()
351
352         // This is just so if we have an error, we can exit cleanly and not completely
353         // mess up the terminal being worked in
354         // In other words we need to shut down tcell before the program crashes
355         defer func() {
356                 if err := recover(); err != nil {
357                         screen.Fini()
358                         fmt.Println("Micro encountered an error:", err)
359                         // Print the stack trace too
360                         fmt.Print(errors.Wrap(err, 2).ErrorStack())
361                         os.Exit(1)
362                 }
363         }()
364
365         // Create a new messenger
366         // This is used for sending the user messages in the bottom of the editor
367         messenger = new(Messenger)
368         messenger.LoadHistory()
369
370         // Now we load the input
371         buffers := LoadInput()
372         if len(buffers) == 0 {
373                 screen.Fini()
374                 os.Exit(1)
375         }
376
377         for _, buf := range buffers {
378                 // For each buffer we create a new tab and place the view in that tab
379                 tab := NewTabFromView(NewView(buf))
380                 tab.SetNum(len(tabs))
381                 tabs = append(tabs, tab)
382                 for _, t := range tabs {
383                         for _, v := range t.Views {
384                                 v.Center(false)
385                         }
386
387                         t.Resize()
388                 }
389         }
390
391         for k, v := range optionFlags {
392                 if *v != "" {
393                         SetOption(k, *v)
394                 }
395         }
396
397         // Load all the plugin stuff
398         // We give plugins access to a bunch of variables here which could be useful to them
399         L.SetGlobal("OS", luar.New(L, runtime.GOOS))
400         L.SetGlobal("tabs", luar.New(L, tabs))
401         L.SetGlobal("curTab", luar.New(L, curTab))
402         L.SetGlobal("messenger", luar.New(L, messenger))
403         L.SetGlobal("GetOption", luar.New(L, GetOption))
404         L.SetGlobal("AddOption", luar.New(L, AddOption))
405         L.SetGlobal("SetOption", luar.New(L, SetOption))
406         L.SetGlobal("SetLocalOption", luar.New(L, SetLocalOption))
407         L.SetGlobal("BindKey", luar.New(L, BindKey))
408         L.SetGlobal("MakeCommand", luar.New(L, MakeCommand))
409         L.SetGlobal("CurView", luar.New(L, CurView))
410         L.SetGlobal("IsWordChar", luar.New(L, IsWordChar))
411         L.SetGlobal("HandleCommand", luar.New(L, HandleCommand))
412         L.SetGlobal("HandleShellCommand", luar.New(L, HandleShellCommand))
413         L.SetGlobal("ExecCommand", luar.New(L, ExecCommand))
414         L.SetGlobal("RunShellCommand", luar.New(L, RunShellCommand))
415         L.SetGlobal("RunBackgroundShell", luar.New(L, RunBackgroundShell))
416         L.SetGlobal("RunInteractiveShell", luar.New(L, RunInteractiveShell))
417         L.SetGlobal("TermEmuSupported", luar.New(L, TermEmuSupported))
418         L.SetGlobal("RunTermEmulator", luar.New(L, RunTermEmulator))
419         L.SetGlobal("GetLeadingWhitespace", luar.New(L, GetLeadingWhitespace))
420         L.SetGlobal("MakeCompletion", luar.New(L, MakeCompletion))
421         L.SetGlobal("NewBuffer", luar.New(L, NewBufferFromString))
422         L.SetGlobal("NewBufferFromFile", luar.New(L, NewBufferFromFile))
423         L.SetGlobal("RuneStr", luar.New(L, func(r rune) string {
424                 return string(r)
425         }))
426         L.SetGlobal("Loc", luar.New(L, func(x, y int) Loc {
427                 return Loc{x, y}
428         }))
429         L.SetGlobal("WorkingDirectory", luar.New(L, os.Getwd))
430         L.SetGlobal("JoinPaths", luar.New(L, filepath.Join))
431         L.SetGlobal("DirectoryName", luar.New(L, filepath.Dir))
432         L.SetGlobal("configDir", luar.New(L, configDir))
433         L.SetGlobal("Reload", luar.New(L, LoadAll))
434         L.SetGlobal("ByteOffset", luar.New(L, ByteOffset))
435         L.SetGlobal("ToCharPos", luar.New(L, ToCharPos))
436
437         // Used for asynchronous jobs
438         L.SetGlobal("JobStart", luar.New(L, JobStart))
439         L.SetGlobal("JobSpawn", luar.New(L, JobSpawn))
440         L.SetGlobal("JobSend", luar.New(L, JobSend))
441         L.SetGlobal("JobStop", luar.New(L, JobStop))
442
443         // Extension Files
444         L.SetGlobal("ReadRuntimeFile", luar.New(L, PluginReadRuntimeFile))
445         L.SetGlobal("ListRuntimeFiles", luar.New(L, PluginListRuntimeFiles))
446         L.SetGlobal("AddRuntimeFile", luar.New(L, PluginAddRuntimeFile))
447         L.SetGlobal("AddRuntimeFilesFromDirectory", luar.New(L, PluginAddRuntimeFilesFromDirectory))
448         L.SetGlobal("AddRuntimeFileFromMemory", luar.New(L, PluginAddRuntimeFileFromMemory))
449
450         // Access to Go stdlib
451         L.SetGlobal("import", luar.New(L, Import))
452
453         jobs = make(chan JobFunction, 100)
454         events = make(chan tcell.Event, 100)
455         autosave = make(chan bool)
456         updateterm = make(chan bool)
457         closeterm = make(chan int)
458
459         LoadPlugins()
460
461         for _, t := range tabs {
462                 for _, v := range t.Views {
463                         GlobalPluginCall("onViewOpen", v)
464                         GlobalPluginCall("onBufferOpen", v.Buf)
465                 }
466         }
467
468         InitColorscheme()
469         messenger.style = defStyle
470
471         // Here is the event loop which runs in a separate thread
472         go func() {
473                 for {
474                         if screen != nil {
475                                 events <- screen.PollEvent()
476                         }
477                 }
478         }()
479
480         go func() {
481                 for {
482                         time.Sleep(autosaveTime * time.Second)
483                         if globalSettings["autosave"].(bool) {
484                                 autosave <- true
485                         }
486                 }
487         }()
488
489         for {
490                 // Display everything
491                 RedrawAll()
492
493                 var event tcell.Event
494
495                 // Check for new events
496                 select {
497                 case f := <-jobs:
498                         // If a new job has finished while running in the background we should execute the callback
499                         f.function(f.output, f.args...)
500                         continue
501                 case <-autosave:
502                         if CurView().Buf.Path != "" {
503                                 CurView().Save(true)
504                         }
505                 case <-updateterm:
506                         continue
507                 case vnum := <-closeterm:
508                         tabs[curTab].Views[vnum].CloseTerminal()
509                 case event = <-events:
510                 }
511
512                 for event != nil {
513                         didAction := false
514
515                         switch e := event.(type) {
516                         case *tcell.EventResize:
517                                 for _, t := range tabs {
518                                         t.Resize()
519                                 }
520                         case *tcell.EventMouse:
521                                 if !searching {
522                                         if e.Buttons() == tcell.Button1 {
523                                                 // If the user left clicked we check a couple things
524                                                 _, h := screen.Size()
525                                                 x, y := e.Position()
526                                                 if y == h-1 && messenger.message != "" && globalSettings["infobar"].(bool) {
527                                                         // If the user clicked in the bottom bar, and there is a message down there
528                                                         // we copy it to the clipboard.
529                                                         // Often error messages are displayed down there so it can be useful to easily
530                                                         // copy the message
531                                                         clipboard.WriteAll(messenger.message, "primary")
532                                                         break
533                                                 }
534
535                                                 if CurView().mouseReleased {
536                                                         // We loop through each view in the current tab and make sure the current view
537                                                         // is the one being clicked in
538                                                         for _, v := range tabs[curTab].Views {
539                                                                 if x >= v.x && x < v.x+v.Width && y >= v.y && y < v.y+v.Height {
540                                                                         tabs[curTab].CurView = v.Num
541                                                                 }
542                                                         }
543                                                 }
544                                         } else if e.Buttons() == tcell.WheelUp || e.Buttons() == tcell.WheelDown {
545                                                 var view *View
546                                                 x, y := e.Position()
547                                                 for _, v := range tabs[curTab].Views {
548                                                         if x >= v.x && x < v.x+v.Width && y >= v.y && y < v.y+v.Height {
549                                                                 view = tabs[curTab].Views[v.Num]
550                                                         }
551                                                 }
552                                                 if view != nil {
553                                                         view.HandleEvent(e)
554                                                         didAction = true
555                                                 }
556                                         }
557                                 }
558                         }
559
560                         if !didAction {
561                                 // This function checks the mouse event for the possibility of changing the current tab
562                                 // If the tab was changed it returns true
563                                 if TabbarHandleMouseEvent(event) {
564                                         break
565                                 }
566
567                                 if searching {
568                                         // Since searching is done in real time, we need to redraw every time
569                                         // there is a new event in the search bar so we need a special function
570                                         // to run instead of the standard HandleEvent.
571                                         HandleSearchEvent(event, CurView())
572                                 } else {
573                                         // Send it to the view
574                                         CurView().HandleEvent(event)
575                                 }
576                         }
577
578                         select {
579                         case event = <-events:
580                         default:
581                                 event = nil
582                         }
583                 }
584         }
585 }