Skip to content

Commit

Permalink
Add $le:after-readline.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaq committed Oct 26, 2016
1 parent 3627187 commit df1d282
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
20 changes: 20 additions & 0 deletions edit/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Editor struct {

rpromptPersistent bool
beforeReadLine eval.Variable
afterReadLine eval.Variable

historyMutex sync.RWMutex

Expand Down Expand Up @@ -113,6 +114,8 @@ func NewEditor(file *os.File, sigs chan os.Signal, ev *eval.Evaler, st *store.St
abbreviations: make(map[string]string),
beforeReadLine: eval.NewPtrVariableWithValidator(
eval.NewList(), eval.IsListOfFnValue),
afterReadLine: eval.NewPtrVariableWithValidator(
eval.NewList(), eval.IsListOfFnValue),
}
ev.Editor = ed
ev.Modules["le"] = makeModule(ed)
Expand Down Expand Up @@ -303,7 +306,24 @@ func (ed *Editor) finishReadLine(addError func(error)) {
addError(fmt.Errorf("can't restore terminal attribute: %s", err))
}
ed.savedTermios = nil
line := ed.line
ed.editorState = editorState{}

afterReadLine := ed.afterReadLine.Get().(eval.ListLike)
if afterReadLine.Len() > 0 {
opfunc := func(ec *eval.EvalCtx) {
afterReadLine.Iterate(func(v eval.Value) bool {
fn := v.(eval.FnValue)
ex := ec.PCall(fn, []eval.Value{eval.String(line)}, eval.NoOpts)
// TODO Pretty print.
if ex != nil {
fmt.Fprintln(os.Stderr, "function error: %s", ex.Error())
}
return true
})
}
ed.evaler.Eval(eval.Op{opfunc, -1, -1}, "[after-readline]", "no source")
}
}

// ReadLine reads a line interactively.
Expand Down
1 change: 1 addition & 0 deletions edit/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func makeModule(ed *Editor) eval.Namespace {
ns["abbr"] = eval.NewRoVariable(eval.MapStringString(ed.abbreviations))

ns["before-readline"] = ed.beforeReadLine
ns["after-readline"] = ed.afterReadLine

ns[eval.FnPrefix+"styled"] = eval.NewRoVariable(&eval.BuiltinFn{"le:styled", eval.WrapFn(styledBuiltin)})

Expand Down

0 comments on commit df1d282

Please sign in to comment.