@@ -4,26 +4,40 @@ import (
44 "log"
55 "os"
66 "os/exec"
7+ "path/filepath"
8+ "strings"
79
810 "github.com/neovim/go-client/nvim"
911)
1012
1113func main () {
12- log .SetFlags (0 )
13-
1414 const version string = "v0.2.1"
15+
16+ log .SetFlags (log .Ldate | log .Ltime )
17+ log .SetPrefix (version + " " )
18+
19+ logfile := os .Getenv ("FLATNVIM_LOGFILE" )
20+ if logfile != "" {
21+ if f , err := os .OpenFile (logfile , os .O_RDWR | os .O_APPEND | os .O_CREATE , 0o664 ); err == nil {
22+ defer f .Close ()
23+ log .SetOutput (f )
24+ } else {
25+ log .Printf ("FLATNVIM_LOGFILE is set to %v but cannot be opened: %v\n " , logfile , err )
26+ }
27+ }
28+
1529 err := os .Setenv ("FLATNVIM_VERSION" , version )
1630 if err != nil {
17- log .Fatalf ( "%v: unable to set FLATNVIM_VERSION environment variable\n " , version )
31+ log .Printf ( " unable to set FLATNVIM_VERSION environment variable: %v \n " , err )
1832 }
1933
20- addr := os .Getenv ("NVIM_LISTEN_ADDRESS " )
34+ addr := os .Getenv ("NVIM " )
2135 if addr == "" {
2236 editor := os .Getenv ("FLATNVIM_EDITOR" )
2337
2438 path , err := exec .LookPath (editor )
2539 if err != nil {
26- log .Fatalf ( "%v: command '%v' from FLATNVIM_EDITOR is not in $PATH\n " , version , editor )
40+ log .Panicf ( " command '%v' from FLATNVIM_EDITOR is not in $PATH: %v \n " , editor , err )
2741 }
2842
2943 cmd := exec .Command (path , os .Args [1 :]... )
@@ -32,35 +46,81 @@ func main() {
3246 cmd .Stderr = os .Stderr
3347
3448 if err := cmd .Run (); err != nil {
35- log .Fatalf ( "%v: %v \n " , version , err )
49+ log .Panicln ( err )
3650 }
37- os .Exit (0 )
51+
52+ return
3853 }
3954
4055 files := os .Args [1 :]
4156 if len (files ) == 0 {
42- log .Fatalf ( "%v: no arguments given\n " , version )
57+ log .Panicln ( " no arguments given" )
4358 }
4459
4560 v , err := nvim .Dial (addr )
4661 if err != nil {
47- log .Fatalf ( "%v : %v\n ", version , err )
62+ log .Panicf ( "unable to connect to parent nvim instance : %v\n " , err )
4863 }
4964 defer v .Close ()
5065
5166 b := v .NewBatch ()
67+ b .Command ("let flatnvim_buf=bufname()" )
68+
69+ var curDir string
70+ if dir , err := os .Getwd (); err == nil {
71+ curDir = dir + string (os .PathSeparator )
72+ } else {
73+ log .Printf ("could not get current directory: %v\n " , err )
74+ }
75+
76+ var vimDir string
77+ if dir , err := v .Exec ("pwd" , true ); err == nil {
78+ vimDir = dir + string (os .PathSeparator )
79+ } else {
80+ log .Printf ("could not get directory of nvim: %v\n " , err )
81+ }
82+
83+ homeDir , err := os .UserHomeDir ()
84+ if err != nil {
85+ log .Printf ("unable to get user home directory: %v\n " , err )
86+ }
87+
5288 for _ , file := range files {
53- b .Command (":e " + file )
89+ if file == "--" {
90+ continue
91+ }
92+
93+ path := trimPath (file , curDir , vimDir , homeDir )
94+ b .Command ("e " + path )
5495 }
55- b . Command ( ":b term://" )
56- b .Command (":bw! " )
96+
97+ b .Command ("exe 'bd! '.flatnvim_buf " )
5798
5899 extraCmd := os .Getenv ("FLATNVIM_EXTRA_COMMAND" )
59100 if extraCmd != "" {
60101 b .Command (extraCmd )
61102 }
62103
63104 if err := b .Execute (); err != nil {
64- log .Fatalf ("%v: %v\n " , version , err )
105+ log .Panicf ("unable to execute nvim commands: %v\n " , err )
106+ }
107+ }
108+
109+ func trimPath (path , curDir , vimDir , homeDir string ) string {
110+ if len (curDir ) > 0 && ! filepath .IsAbs (path ) {
111+ path = curDir + path
65112 }
113+
114+ if len (vimDir ) > 0 {
115+ path = strings .TrimPrefix (path , vimDir )
116+ }
117+
118+ if len (homeDir ) > 0 && strings .HasPrefix (path , homeDir ) {
119+ path = strings .Replace (path , homeDir , "~" , 1 )
120+ }
121+
122+ path = strings .ReplaceAll (path , "/./" , "/" )
123+ path = strings .TrimPrefix (path , "./" )
124+
125+ return path
66126}
0 commit comments