mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-05 03:17:50 +08:00
Expose editor builtins via a le
module.
This commit is contained in:
parent
ae041b7dbf
commit
34fddcd100
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
14
eval/eval.go
14
eval/eval.go
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ type ptrVariable struct {
|
|||
valuePtr *Value
|
||||
}
|
||||
|
||||
func newPtrVariable(v Value) Variable {
|
||||
func NewPtrVariable(v Value) Variable {
|
||||
return ptrVariable{&v}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user