Add eval.Editor; Evaler.store -> .Store.

This commit is contained in:
Qi Xiao 2016-09-11 19:17:08 +02:00
parent 4a3e3570e2
commit a3b02b0d42
3 changed files with 44 additions and 27 deletions

View File

@ -43,12 +43,14 @@ type Editor struct {
historyMutex sync.RWMutex
active bool
activeMutex *sync.Mutex
editorState
}
type editorState struct {
// States used during ReadLine. Reset at the beginning of ReadLine.
active bool
savedTermios *sys.Termios
notifications []string
@ -110,10 +112,23 @@ func NewEditor(file *os.File, sigs chan os.Signal, ev *eval.Evaler, st *store.St
beforeReadLine: eval.NewPtrVariableWithValidator(
eval.NewList(), eval.IsListOfFnValue),
}
ev.Editor = ed
ev.Modules["le"] = makeModule(ed)
return ed
}
func (ed *Editor) Active() bool {
return ed.active
}
func (ed *Editor) ActiveMutex() *sync.Mutex {
return ed.activeMutex
}
func (ed *Editor) Notify(s string) {
ed.notify(s)
}
func (ed *Editor) flash() {
// TODO implement fish-like flash effect
}
@ -220,6 +235,10 @@ func setupTerminal(file *os.File) (*sys.Termios, error) {
// startReadLine prepares the terminal for the editor.
func (ed *Editor) startReadLine() error {
ed.activeMutex.Lock()
defer ed.activeMutex.Unlock()
ed.active = true
savedTermios, err := setupTerminal(ed.file)
if err != nil {
return err
@ -253,6 +272,10 @@ func (ed *Editor) startReadLine() error {
// finishReadLine puts the terminal in a state suitable for other programs to
// use.
func (ed *Editor) finishReadLine(addError func(error)) {
ed.activeMutex.Lock()
defer ed.activeMutex.Unlock()
ed.active = false
ed.mode = &ed.insert
ed.tips = nil
ed.dot = len(ed.line)
@ -285,15 +308,6 @@ func (ed *Editor) finishReadLine(addError func(error)) {
// ReadLine reads a line interactively.
func (ed *Editor) ReadLine() (line string, err error) {
ed.editorState = editorState{active: true}
ed.mode = &ed.insert
isExternalCh := make(chan map[string]bool, 1)
go getIsExternal(ed.evaler, isExternalCh)
ed.writer.resetOldBuf()
go ed.reader.Run()
e := ed.startReadLine()
if e != nil {
return "", e
@ -304,6 +318,14 @@ func (ed *Editor) ReadLine() (line string, err error) {
}
})
ed.mode = &ed.insert
isExternalCh := make(chan map[string]bool, 1)
go getIsExternal(ed.evaler, isExternalCh)
ed.writer.resetOldBuf()
go ed.reader.Run()
fullRefresh := false
beforeReadLines := ed.beforeReadLine.Get().(eval.ListLike)

View File

@ -588,11 +588,11 @@ func cdInner(dir string, ec *EvalCtx) {
if err != nil {
throw(err)
}
if ec.store != nil {
if ec.Store != nil {
// XXX Error ignored.
pwd, err := os.Getwd()
if err == nil {
store := ec.store
store := ec.Store
go func() {
store.Waits.Add(1)
// XXX Error ignored.
@ -607,10 +607,10 @@ func cdInner(dir string, ec *EvalCtx) {
var dirFieldNames = []string{"path", "score"}
func dirs(ec *EvalCtx) {
if ec.store == nil {
if ec.Store == nil {
throw(ErrStoreNotConnected)
}
dirs, err := ec.store.ListDirs()
dirs, err := ec.Store.ListDirs()
if err != nil {
throw(errors.New("store error: " + err.Error()))
}
@ -624,11 +624,11 @@ func dirs(ec *EvalCtx) {
}
func history(ec *EvalCtx) {
if ec.store == nil {
if ec.Store == nil {
throw(ErrStoreNotConnected)
}
store := ec.store
store := ec.Store
seq, err := store.NextCmdSeq()
maybeThrow(err)
cmds, err := store.Cmds(0, seq)
@ -1011,7 +1011,7 @@ func exit(ec *EvalCtx, args ...int) {
}
func preExit(ec *EvalCtx) {
err := ec.store.Close()
err := ec.Store.Close()
if err != nil {
fmt.Fprintln(os.Stderr, err)
}

View File

@ -38,7 +38,8 @@ type Namespace map[string]Variable
type Evaler struct {
Global Namespace
Modules map[string]Namespace
store *store.Store
Store *store.Store
Editor Editor
Stub *stub.Stub
intCh <-chan struct{}
}
@ -67,13 +68,7 @@ func (ec *EvalCtx) evaling(begin, end int) {
// NewEvaler creates a new Evaler.
func NewEvaler(st *store.Store) *Evaler {
return &Evaler{Namespace{}, map[string]Namespace{}, st, nil, nil}
}
func (e *Evaler) hasEditor() bool {
// XXX Should guard against user supplying a "le.elv"
_, ok := e.Modules["le"]
return ok
return &Evaler{Namespace{}, map[string]Namespace{}, st, nil, nil, nil}
}
func (e *Evaler) searchPaths() []string {
@ -374,10 +369,10 @@ func (ec *EvalCtx) ResolveVar(ns, name string) Variable {
}
return envVariable{name}
case "shared":
if ec.store == nil {
if ec.Store == nil {
throw(ErrStoreUnconnected)
}
return sharedVariable{ec.store, name}
return sharedVariable{ec.Store, name}
default:
use(ec, ns, nil)
return ec.Modules[ns][name]