-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
126 lines (99 loc) · 2.59 KB
/
Copy pathmain.go
File metadata and controls
126 lines (99 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/neovim/go-client/nvim"
)
func main() {
const version string = "v1.0.0"
log.SetFlags(log.Ldate | log.Ltime)
log.SetPrefix(version + " ")
logfile := os.Getenv("FLATNVIM_LOGFILE")
if logfile != "" {
if f, err := os.OpenFile(logfile, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0o664); err == nil {
defer f.Close()
log.SetOutput(f)
} else {
log.Printf("FLATNVIM_LOGFILE is set to %v but cannot be opened: %v\n", logfile, err)
}
}
err := os.Setenv("FLATNVIM_VERSION", version)
if err != nil {
log.Printf("unable to set FLATNVIM_VERSION environment variable: %v\n", err)
}
addr := os.Getenv("NVIM")
if addr == "" {
editor := os.Getenv("FLATNVIM_EDITOR")
path, err := exec.LookPath(editor)
if err != nil {
log.Panicf("command '%v' from FLATNVIM_EDITOR is not in $PATH: %v\n", editor, err)
}
cmd := exec.Command(path, os.Args[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Panicln(err)
}
return
}
files := os.Args[1:]
if len(files) == 0 {
log.Panicln("no arguments given")
}
v, err := nvim.Dial(addr)
if err != nil {
log.Panicf("unable to connect to parent nvim instance: %v\n", err)
}
defer v.Close()
b := v.NewBatch()
b.Command("let flatnvim_buf=bufname()")
var curDir string
if dir, err := os.Getwd(); err == nil {
curDir = dir + string(os.PathSeparator)
} else {
log.Printf("could not get current directory: %v\n", err)
}
var vimDir string
if dir, err := v.Exec("pwd", true); err == nil {
vimDir = dir + string(os.PathSeparator)
} else {
log.Printf("could not get directory of nvim: %v\n", err)
}
homeDir, err := os.UserHomeDir()
if err != nil {
log.Printf("unable to get user home directory: %v\n", err)
}
for _, file := range files {
if file == "--" {
continue
}
path := trimPath(file, curDir, vimDir, homeDir)
b.Command("e " + path)
}
b.Command("exe 'bd! '.flatnvim_buf")
extraCmd := os.Getenv("FLATNVIM_EXTRA_COMMAND")
if extraCmd != "" {
b.Command(extraCmd)
}
if err := b.Execute(); err != nil {
log.Panicf("unable to execute nvim commands: %v\n", err)
}
}
func trimPath(path, curDir, vimDir, homeDir string) string {
if len(curDir) > 0 && !filepath.IsAbs(path) {
path = curDir + path
}
if len(vimDir) > 0 {
path = strings.TrimPrefix(path, vimDir)
}
if len(homeDir) > 0 && strings.HasPrefix(path, homeDir) {
path = strings.Replace(path, homeDir, "~", 1)
}
path = strings.ReplaceAll(path, "/./", "/")
path = strings.TrimPrefix(path, "./")
return path
}