acme/editinacme/editinacme.go (view raw)
1// Copyright 2015 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// Editinacme can be used as $EDITOR in a Unix environment.
6//
7// Usage:
8//
9// editinacme <file>
10//
11// Editinacme uses the plumber to ask acme to open the file,
12// waits until the file's acme window is deleted, and exits.
13
14package main
15
16import (
17 "flag"
18 "fmt"
19 "log"
20 "os"
21 "os/exec"
22 "path/filepath"
23
24 "9fans.net/go/acme"
25)
26
27func main() {
28 log.SetFlags(0)
29 log.SetPrefix("editinacme: ")
30 flag.Usage = func() {
31 fmt.Fprintf(os.Stderr, "usage: editinacme file\n")
32 os.Exit(2)
33 }
34 flag.Parse()
35 if flag.NArg() != 1 {
36 flag.Usage()
37 }
38
39 file := flag.Arg(0)
40
41 fullpath, err := filepath.Abs(file)
42 if err != nil {
43 log.Fatal(err)
44 }
45 file = fullpath
46
47 r, err := acme.Log()
48 if err != nil {
49 log.Fatal(err)
50 }
51
52 log.Printf("editing %s", file)
53
54 out, err := exec.Command("plumb", "-d", "edit", file).CombinedOutput()
55 if err != nil {
56 log.Fatalf("executing plumb: %v\n%s", err, out)
57 }
58
59 for {
60 ev, err := r.Read()
61 if err != nil {
62 log.Fatalf("reading acme log: %v", err)
63 }
64 if ev.Op == "del" && ev.Name == file {
65 break
66 }
67 }
68}