示例#1
0
文件: main.go 项目: mhilton/acmedot
func main() {
	if os.Getenv("winid") == "" {
		error("no winid, not acme window")
	}
	id, err := strconv.Atoi(os.Getenv("winid"))
	if err != nil {
		error("cannot parse winid %q: %s", os.Getenv("winid"), err)
	}
	w, err := acme.Open(id, nil)
	if err != nil {
		error("cannot open window %d: %s", id, err)
	}
	// Need the address file open to get it correctly
	if _, _, err := w.ReadAddr(); err != nil {
		error("cannot read addr: %s", err)
	}
	if err := w.Ctl("addr=dot"); err != nil {
		error("cannot write ctl: %s", err)
	}
	start, end, err := w.ReadAddr()
	if err != nil {
		error("cannot read addr: %s", err)
	}
	if len(os.Args) > 2 && start == end {
		fmt.Printf(os.Args[2], start)
	} else if len(os.Args) > 1 {
		fmt.Printf(os.Args[1], start, end)
	} else {
		fmt.Printf("%d,%d\n", start, end)
	}
}
示例#2
0
文件: main.go 项目: uluyol/tools
func openWin() (*acme.Win, error) {
	id, err := strconv.Atoi(os.Getenv("winid"))
	if err != nil {
		return nil, err
	}
	return acme.Open(id, nil)
}
示例#3
0
文件: check.go 项目: uluyol/tools
func main() {
	log.SetFlags(0)
	log.SetPrefix("")
	wid, err := strconv.Atoi(os.Getenv("winid"))
	if err != nil {
		log.Fatal("unable to find window")
	}
	win, err := acme.Open(wid, nil)
	if err != nil {
		log.Fatalf("unable to open window: %v", err)
	}
	wis, _ := acme.Windows()
	var name string
	for _, wi := range wis {
		if wi.ID == wid {
			name = wi.Name
			break
		}
	}
	cmd := exec.Command("aspell", "-a")
	cmd.Stdin = prefixer.New(bodyReader{win}, "^")
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		log.Fatalf("unable to create pipe")
	}
	cmd.Start()
	p := printer{name}
	go p.scan(stdout)
	if err := cmd.Wait(); err != nil && err != io.EOF {
		log.Fatalf("error running \"aspell -a\": %v", err)
	}
}
示例#4
0
文件: main.go 项目: aoeu/acme
func open(filepath string) (w *acme.Win, err error) {
	if id, ok := existingWindows[filepath]; ok {
		if w, err = acme.Open(id, nil); err != nil {
			return w, err
		}
	} else if w, err = acme.New(); err == nil {
		w.Name(filepath)
		w.Write("tag", []byte("Put "))
		w.Ctl("get")
	}
	return w, err
}
示例#5
0
文件: dot.go 项目: wdblair/9paredit
//taken from godef
func acmeCurrentWin() (*acme.Win, error) {
	winid := os.Getenv("winid")
	if winid == "" {
		return nil, fmt.Errorf("$winid not set - not running inside acme?")
	}
	id, err := strconv.Atoi(winid)
	if err != nil {
		return nil, fmt.Errorf("invalid $winid %q", winid)
	}
	win, err := acme.Open(id, nil)
	if err != nil {
		return nil, fmt.Errorf("cannot open acme window: %v", err)
	}
	return win, nil
}
示例#6
0
文件: main.go 项目: aoeu/acme
func reformat(id int, name string) {
	w, err := acme.Open(id, nil)
	if err != nil {
		log.Print(err)
		return
	}
	defer w.CloseFiles()

	old, err := ioutil.ReadFile(name)
	if err != nil {
		//log.Print(err)
		return
	}
	new, err := exec.Command("goimports", name).CombinedOutput()
	if err != nil {
		// Probably a syntax error, use the compiler for better message.
		// For now use 'go build file.go' and strip the package header.
		// We run it in /var/run so that paths do not get shortened
		// (assuming /var/run exists and no one is editing go files under that path).
		// A better fix to both would be to use go tool 6g, but we don't know
		// whether 6g is the right architecture. Could parse 'go env' output.
		// Or maybe the go command should have 'go tool compile' and 'go tool link'.
		cmd := exec.Command("go", "build", name)
		cmd.Dir = "/var/run"
		out, _ := cmd.CombinedOutput()
		start := []byte("# command-line-arguments\n")
		if !bytes.HasPrefix(out, start) {
			fmt.Fprintf(os.Stderr, "goimports %s: %v\n%s", name, err, new)
			return
		}
		fmt.Fprintf(os.Stderr, "%s", out)
		return
	}

	if bytes.Equal(old, new) {
		return
	}

	if !*gofmt {
		oldTop, err := readImports(bytes.NewReader(old), true)
		if err != nil {
			//log.Print(err)
			return
		}
		newTop, err := readImports(bytes.NewReader(new), true)
		if err != nil {
			//log.Print(err)
			return
		}
		if bytes.Equal(oldTop, newTop) {
			return
		}
		w.Addr("0,#%d", utf8.RuneCount(oldTop))
		w.Write("data", newTop)
		return
	}

	f, err := ioutil.TempFile("", "Imports")
	if err != nil {
		log.Print(err)
		return
	}
	if _, err := f.Write(new); err != nil {
		log.Print(err)
		return
	}
	tmp := f.Name()
	f.Close()
	defer os.Remove(tmp)

	diff, _ := exec.Command("9", "diff", name, tmp).CombinedOutput()

	w.Write("ctl", []byte("mark"))
	w.Write("ctl", []byte("nomark"))
	diffLines := strings.Split(string(diff), "\n")
	for i := len(diffLines) - 1; i >= 0; i-- {
		line := diffLines[i]
		if line == "" {
			continue
		}
		if line[0] == '<' || line[0] == '-' || line[0] == '>' {
			continue
		}
		j := 0
		for j < len(line) && line[j] != 'a' && line[j] != 'c' && line[j] != 'd' {
			j++
		}
		if j >= len(line) {
			log.Printf("cannot parse diff line: %q", line)
			break
		}
		oldStart, oldEnd := parseSpan(line[:j])
		newStart, newEnd := parseSpan(line[j+1:])
		if oldStart == 0 || newStart == 0 {
			continue
		}
		switch line[j] {
		case 'a':
			err := w.Addr("%d+#0", oldStart)
			if err != nil {
				log.Print(err)
				break
			}
			w.Write("data", findLines(new, newStart, newEnd))
		case 'c':
			err := w.Addr("%d,%d", oldStart, oldEnd)
			if err != nil {
				log.Print(err)
				break
			}
			w.Write("data", findLines(new, newStart, newEnd))
		case 'd':
			err := w.Addr("%d,%d", oldStart, oldEnd)
			if err != nil {
				log.Print(err)
				break
			}
			w.Write("data", nil)
		}
	}
}