예제 #1
0
파일: editor.go 프로젝트: elves/elvish
// NewEditor creates an Editor.
func NewEditor(file *os.File, sigs chan os.Signal, ev *eval.Evaler, st *store.Store) *Editor {
	seq := -1
	if st != nil {
		var err error
		seq, err = st.NextCmdSeq()
		if err != nil {
			// TODO(xiaq): Also report the error
			seq = -1
		}
	}

	prompt, rprompt := defaultPrompts()

	ed := &Editor{
		file:   file,
		writer: newWriter(file),
		reader: NewReader(file),
		sigs:   sigs,
		store:  st,
		evaler: ev,
		cmdSeq: seq,
		ps1:    eval.NewPtrVariableWithValidator(prompt, MustBeFn),
		rps1:   eval.NewPtrVariableWithValidator(rprompt, MustBeFn),

		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)
	return ed
}
예제 #2
0
func (hv History) IndexOne(idx eval.Value) eval.Value {
	hv.mutex.RLock()
	defer hv.mutex.RUnlock()

	slice, i, j := eval.ParseAndFixListIndex(eval.ToString(idx), hv.Len())
	if slice {
		cmds, err := hv.st.Cmds(i+1, j+1)
		maybeThrow(err)
		vs := make([]eval.Value, len(cmds))
		for i := range cmds {
			vs[i] = eval.String(cmds[i])
		}
		return eval.NewList(vs...)
	}
	s, err := hv.st.Cmd(i + 1)
	maybeThrow(err)
	return eval.String(s)
}