Expose editor builtins via a le module.

This commit is contained in:
Qi Xiao 2016-02-12 00:43:41 +01:00
parent ae041b7dbf
commit 34fddcd100
9 changed files with 14 additions and 51 deletions

View File

@ -1,13 +1,11 @@
package edit
import (
"errors"
"fmt"
"strings"
"unicode"
"unicode/utf8"
"github.com/elves/elvish/eval"
"github.com/elves/elvish/strutil"
)
@ -90,28 +88,6 @@ var builtins = map[string]builtin{
"default-history": defaultHistory,
}
var (
ErrTakeNoArg = errors.New("editor builtins take no arguments")
ErrNoSuchBuiltin = errors.New("no such editor builtin")
ErrEditorInactive = errors.New("editor inactive")
)
// Call satisfies the eval.Foreign interface.
func (ed *Editor) Call(name string, args []eval.Value) error {
if len(args) > 0 {
return ErrTakeNoArg
}
f, ok := builtins[name]
if !ok {
return ErrNoSuchBuiltin
}
if !ed.active {
return ErrEditorInactive
}
f(ed)
return nil
}
func startInsert(ed *Editor) {
ed.mode = modeInsert
}

View File

@ -177,6 +177,7 @@ func NewEditor(file *os.File, sigs chan os.Signal, ev *eval.Evaler, st *store.St
cmdSeq: seq,
}
ev.Editor = ed
ev.AddModule("le", makeModule(ed))
return ed
}

View File

@ -65,7 +65,6 @@ func init() {
&BuiltinFn{"drop", wrapFn(drop)},
&BuiltinFn{"bind", wrapFn(bind)},
&BuiltinFn{"le", wrapFn(le)},
&BuiltinFn{"-sleep", wrapFn(_sleep)},
&BuiltinFn{"-stack", wrapFn(_stack)},
@ -510,13 +509,6 @@ func bind(ec *EvalCtx, key string, function Value) {
maybeThrow(ec.Editor.Bind(key, function))
}
func le(ec *EvalCtx, name string, args ...Value) {
if ec.Editor == nil {
throw(ErrNoEditor)
}
maybeThrow(ec.Editor.Call(name, args))
}
func _sleep(ec *EvalCtx, t float64) {
time.Sleep(time.Duration(t) * time.Second)
}

View File

@ -201,6 +201,6 @@ func compileFn(cp *compiler, fn *parse.Form) Op {
return func(ec *EvalCtx) {
closure := op(ec)[0].(*Closure)
closure.Op = makeFnOp(closure.Op)
ec.local[varName] = newPtrVariable(closure)
ec.local[varName] = NewPtrVariable(closure)
}
}

View File

@ -298,7 +298,7 @@ func (cp *compiler) indexingVar(n *parse.Indexing, msg string) VariableOp {
// New variable.
// XXX We depend on the fact that this variable will
// immeidately be set.
variable = newPtrVariable(nil)
variable = NewPtrVariable(nil)
ec.local[barename] = variable
}
return variable

View File

@ -61,20 +61,20 @@ func NewEvaler(st *store.Store) *Evaler {
paths := NewList()
paths.appendStrings(searchPaths)
global := Namespace{
"pid": newPtrVariable(pid),
"ok": newPtrVariable(OK),
"true": newPtrVariable(Bool(true)),
"false": newPtrVariable(Bool(false)),
"paths": newPtrVariable(paths),
"pid": NewPtrVariable(pid),
"ok": NewPtrVariable(OK),
"true": NewPtrVariable(Bool(true)),
"false": NewPtrVariable(Bool(false)),
"paths": NewPtrVariable(paths),
}
for _, b := range builtinFns {
global[FnPrefix+b.Name] = newPtrVariable(b)
global[FnPrefix+b.Name] = NewPtrVariable(b)
}
return &Evaler{global, map[string]Namespace{}, searchPaths, st, nil}
}
func (e *Evaler) addModule(name string, ns Namespace) {
func (e *Evaler) AddModule(name string, ns Namespace) {
e.modules[name] = ns
}

View File

@ -89,11 +89,11 @@ func (c *Closure) Call(ec *EvalCtx, args []Value) {
ec.local = make(map[string]Variable)
if !c.Variadic {
for i, name := range c.ArgNames {
ec.local[name] = newPtrVariable(args[i])
ec.local[name] = NewPtrVariable(args[i])
}
}
ec.local["args"] = newPtrVariable(List{&args})
ec.local["kwargs"] = newPtrVariable(Map{&map[Value]Value{}})
ec.local["args"] = NewPtrVariable(List{&args})
ec.local["kwargs"] = NewPtrVariable(Map{&map[Value]Value{}})
// TODO(xiaq): Also change ec.name and ec.text since the closure being
// called can come from another source.

View File

@ -1,12 +1,6 @@
package eval
// Foreign represents a foreign command provider.
type Foreign interface {
Call(name string, args []Value) error
}
// Editor is the interface through which the Evaler calls the line editor.
type Editor interface {
Foreign
Bind(key string, function Value) error
}

View File

@ -12,7 +12,7 @@ type ptrVariable struct {
valuePtr *Value
}
func newPtrVariable(v Value) Variable {
func NewPtrVariable(v Value) Variable {
return ptrVariable{&v}
}