elvish/edit/stylists.go

57 lines
1.2 KiB
Go
Raw Normal View History

2016-01-26 06:09:47 +08:00
package edit
import (
2016-02-24 20:14:02 +08:00
"os"
2016-01-26 06:09:47 +08:00
"github.com/elves/elvish/eval"
"github.com/elves/elvish/parse"
"github.com/elves/elvish/util"
2016-01-26 06:09:47 +08:00
)
2016-02-12 20:31:24 +08:00
// stylist takes a Node and Editor, and returns a style string. The Node is
// always a leaf in the parsed AST.
// NOTE: Not all stylings are now done with stylists.
type stylist func(parse.Node, *Editor) string
2016-01-26 06:09:47 +08:00
2016-02-12 20:31:24 +08:00
var stylists = []stylist{
2016-01-26 06:09:47 +08:00
colorFormHead,
}
func colorFormHead(n parse.Node, ed *Editor) string {
2016-01-26 06:09:47 +08:00
// BUG doesn't work when the form head is compound
n, head := formHead(n)
if n == nil {
return ""
}
if goodFormHead(head, ed) {
return styleForGoodCommand
}
return styleForBadCommand
}
func goodFormHead(head string, ed *Editor) bool {
2016-01-27 00:35:43 +08:00
if isBuiltinSpecial[head] {
return true
} else if util.DontSearch(head) {
// XXX don't stat twice
return util.IsExecutable(head) || isDir(head)
} else {
return ed.isExternal[head] ||
ed.evaler.Builtin()[eval.FnPrefix+head] != nil ||
ed.evaler.Global()[eval.FnPrefix+head] != nil
}
2016-01-26 06:09:47 +08:00
}
2016-01-27 00:35:43 +08:00
var isBuiltinSpecial = map[string]bool{}
func init() {
for _, name := range eval.BuiltinSpecialNames {
isBuiltinSpecial[name] = true
}
}
2016-02-24 20:14:02 +08:00
func isDir(fname string) bool {
stat, err := os.Stat(fname)
return err == nil && stat.IsDir()
}