-
Notifications
You must be signed in to change notification settings - Fork 7
/
vim.go
66 lines (60 loc) · 1.42 KB
/
vim.go
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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
)
func isNeovimPath(vimpath string) bool {
suff := strings.TrimSuffix(vimpath, ".exe")
if strings.HasSuffix(suff, "nvim") {
return true
}
if strings.HasSuffix(suff, "lvim") {
return true
}
return false
}
func runVim(vimpath string, extra []string, args ...string) error {
a := make([]string, 0, len(extra)+3+len(args))
a = append(a, extra...)
if isNeovimPath(vimpath) {
a = append(a, "--headless")
} else {
a = append(a, "--not-a-term")
}
if runtime.GOOS == "windows" {
if p, err := exec.LookPath(vimpath); err == nil {
if filepath.Base(p) == "vim.exe" {
a = append(a, "-e")
}
}
}
a = append(a, "-c", "qall!")
a = append(a, args...)
cmd := exec.Command(vimpath, a...)
out, err := cmd.CombinedOutput()
if err != nil {
for i, b := range out {
if b == '\n' || b == '\r' {
out[i] = ' '
}
}
return fmt.Errorf("failed to run %q with args %v: %w. Output: %s", vimpath, a, err, string(out))
}
return nil
}
func runVimStartuptime(vimpath, tmpdir string, id int, extra []string) (*os.File, error) {
outfile := filepath.Join(tmpdir, strconv.Itoa(id))
if err := runVim(vimpath, extra, "--startuptime", outfile); err != nil {
return nil, err
}
f, err := os.Open(outfile)
if err != nil {
return nil, fmt.Errorf("could not open --startuptime result file '%s': %w", outfile, err)
}
return f, nil
}