github.com/hoop33/elvish@v0.0.0-20160801152013-6d25485beab4/edit/module.go (about)

     1  package edit
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/elves/elvish/eval"
     7  	"github.com/elves/elvish/util"
     8  )
     9  
    10  // Interface between the editor and elvish script. Implements the le: module.
    11  
    12  // makeModule builds a module from an Editor.
    13  func makeModule(ed *Editor) eval.Namespace {
    14  	ns := eval.Namespace{}
    15  	// Populate builtins.
    16  	for _, b := range builtins {
    17  		ns[eval.FnPrefix+b.name] = eval.NewPtrVariable(&BuiltinAsFnValue{b, ed})
    18  	}
    19  	// Populate binding tables in the variable $binding.
    20  	// TODO Make binding specific to the Editor.
    21  	binding := &eval.Struct{
    22  		[]string{"insert", "command", "completion", "navigation", "history"},
    23  		[]eval.Variable{
    24  			eval.NewRoVariable(BindingTable{keyBindings[modeInsert]}),
    25  			eval.NewRoVariable(BindingTable{keyBindings[modeCommand]}),
    26  			eval.NewRoVariable(BindingTable{keyBindings[modeCompletion]}),
    27  			eval.NewRoVariable(BindingTable{keyBindings[modeNavigation]}),
    28  			eval.NewRoVariable(BindingTable{keyBindings[modeHistory]}),
    29  		},
    30  	}
    31  
    32  	ns["binding"] = eval.NewRoVariable(binding)
    33  	ns["completer"] = eval.NewRoVariable(CompleterTable(argCompleter))
    34  	ns[eval.FnPrefix+"complete-getopt"] = eval.NewRoVariable(
    35  		// XXX Repr is "&le:complete-getopt" instead of "le:&complete-getopt"
    36  		&eval.BuiltinFn{"le:complete-getopt", eval.WrapFn(complGetopt)})
    37  
    38  	ns["prompt"] = PromptVariable{&ed.ps1}
    39  	ns["rprompt"] = PromptVariable{&ed.rps1}
    40  
    41  	ns["abbr"] = eval.NewRoVariable(eval.MapStringString(ed.abbreviations))
    42  
    43  	return ns
    44  }
    45  
    46  func throw(e error) {
    47  	util.Throw(e)
    48  }
    49  
    50  func maybeThrow(e error) {
    51  	if e != nil {
    52  		util.Throw(e)
    53  	}
    54  }
    55  
    56  func throwf(format string, args ...interface{}) {
    57  	util.Throw(fmt.Errorf(format, args...))
    58  }