eval: Add an AddBuiltinFns utility.

This commit is contained in:
Qi Xiao 2017-06-28 00:39:05 +01:00
parent cfc473fcfe
commit fb0fe70b8a
2 changed files with 16 additions and 4 deletions

View File

@ -121,7 +121,9 @@ func installModules(modules map[string]eval.Namespace, ed *Editor) {
}
// Utility functions.
ns[eval.FnPrefix+"styled"] = eval.NewRoVariable(&eval.BuiltinFn{"le:&styled", styled})
eval.AddBuiltinFns(ns,
&eval.BuiltinFn{"le:styled", styled},
)
modules["le"] = ns
// Install other modules.

View File

@ -2,6 +2,7 @@ package eval
import (
"strconv"
"strings"
"syscall"
"github.com/elves/elvish/daemon/api"
@ -16,8 +17,17 @@ func makeBuiltinNamespace(daemon *api.Client) Namespace {
"paths": &EnvPathList{envName: "PATH"},
"pwd": PwdVariable{daemon},
}
for _, b := range builtinFns {
ns[FnPrefix+b.Name] = NewRoVariable(b)
}
AddBuiltinFns(ns, builtinFns...)
return ns
}
// AddBuiltinFns adds builtin functions to a namespace.
func AddBuiltinFns(ns Namespace, fns ...*BuiltinFn) {
for _, b := range fns {
name := b.Name
if i := strings.IndexRune(b.Name, ':'); i != -1 {
name = b.Name[i+1:]
}
ns[FnPrefix+name] = NewRoVariable(b)
}
}