Debian Codegraph @

editor/editor.go

raw
1 // Package editor provides functionality for opening files in external editors.
2 package editor
3
4 import (
5 "context"
6 "fmt"
7 "os"
8 "os/exec"
9 "path/filepath"
10 "slices"
11 "strings"
12 )
13
14 const defaultEditor = "nano"
15
16 // Option defines an editor option.
17 //
18 // An Option may act differently in some editors, or not be supported in
19 // some of them.
20 type Option func(editor, filename string) (args []string, pathInArgs bool)
21
22 // OpenAtLine opens the file at the given line number in supported editors.
23 //
24 // Deprecated: use LineNumber instead.
25 func OpenAtLine(n int) Option { return LineNumber(n) }
26
27 // LineNumber opens the file at the given line number in supported editors. If
28 // [number] is less than line 1, the file will be opened at line 1.
29 func LineNumber(number int) Option {
30 if number < 1 {
31 number = 1
32 }
33 plusLineEditors := []string{"vi", "vim", "nvim", "nano", "emacs", "kak", "gedit"}
34 return func(editor, filename string) ([]string, bool) {
35 if slices.Contains(plusLineEditors, editor) {
36 return []string{fmt.Sprintf("+%d", number)}, false
37 }
38 if editor == "code" {
39 return []string{
40 "--goto",
41 fmt.Sprintf("%s:%d", filename, number),
42 }, true
43 }
44 return nil, false
45 }
46 }
47
48 // EndOfLine opens the file at the end of the line in supported editors.
49 func EndOfLine() Option {
50 return func(editor, _ string) (args []string, pathInArgs bool) {
51 switch editor {
52 case "vim", "nvim":
53 return []string{"+norm! $"}, false
54 }
55 return nil, false
56 }
57 }
58
59 // Cmd returns a *exec.Cmd editing the given path with $EDITOR or nano if no
60 // $EDITOR is set.
61 // Deprecated: use Command or CommandContext instead.
62 func Cmd(app, path string, options ...Option) (*exec.Cmd, error) {
63 return CommandContext(context.Background(), app, path, options...)
64 }
65
66 // Command returns a *exec.Cmd editing the given path with $EDITOR or nano if
67 // no $EDITOR is set.
68 func Command(app, path string, options ...Option) (*exec.Cmd, error) {
69 return CommandContext(context.Background(), app, path, options...)
70 }
71
72 // CommandContext returns a *exec.Cmd editing the given path with $EDITOR or nano
73 // if no $EDITOR is set.
74 func CommandContext(ctx context.Context, app, path string, options ...Option) (*exec.Cmd, error) {
75 if os.Getenv("SNAP_REVISION") != "" {
76 return nil, fmt.Errorf("did you install with Snap? %[1]s is sandboxed and unable to open an editor. Please install %[1]s with Go or another package manager to enable editing", app)
77 }
78
79 editor, args := getEditor()
80 editorName := filepath.Base(editor)
81
82 needsToAppendPath := true
83 for _, opt := range options {
84 optArgs, pathInArgs := opt(editorName, path)
85 if pathInArgs {
86 needsToAppendPath = false
87 }
88 args = append(args, optArgs...)
89 }
90 if needsToAppendPath {
91 args = append(args, path)
92 }
93
94 return exec.CommandContext(ctx, editor, args...), nil
95 }
96
97 func getEditor() (string, []string) {
98 editor := strings.Fields(os.Getenv("EDITOR"))
99 if len(editor) > 1 {
100 return editor[0], editor[1:]
101 }
102 if len(editor) == 1 {
103 return editor[0], []string{}
104 }
105 return defaultEditor, []string{}
106 }