Skip to content

Commit

Permalink
$edit:current-command and $edit:-dot
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaq committed Oct 8, 2024
1 parent 9a049d4 commit 8532574
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
17 changes: 16 additions & 1 deletion pkg/etkedit/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,22 @@ func (ed *Editor) Ns() *eval.Ns {
"command-abbr": makeEditVar(ed, &ed.commandAbbr),
"small-word-abbr": makeEditVar(ed, &ed.smallWordAbbr),

"command-duration": vars.FromInit(1.0),
"command-duration": vars.FromInit(0.0),

"-dot": bufferVar[int]{
ed,
func(buf comps.TextBuffer) int { return buf.Dot },
func(buf comps.TextBuffer, dot int) comps.TextBuffer {
return comps.TextBuffer{Content: buf.Content, Dot: dot}
},
},
"current-command": bufferVar[string]{
ed,
func(buf comps.TextBuffer) string { return buf.Content },
func(_ comps.TextBuffer, content string) comps.TextBuffer {
return comps.TextBuffer{Content: content, Dot: len(content)}
},
},
}).
AddNs("insert", eval.BuildNsNamed("edit:insert").
AddVar("binding", makeEditVar(ed, &ed.insertBinding))).
Expand Down
40 changes: 40 additions & 0 deletions pkg/etkedit/old_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"src.elv.sh/pkg/etk"
"src.elv.sh/pkg/etk/comps"
"src.elv.sh/pkg/eval/vals"
)

var errEditorNotActive = errors.New("editor is not active")
Expand Down Expand Up @@ -34,6 +35,45 @@ func (ed *Editor) codeBufferBuiltins() map[string]any {
return m
}

type bufferVar[T any] struct {
ed *Editor
get func(comps.TextBuffer) T
set func(comps.TextBuffer, T) comps.TextBuffer
}

func (bv bufferVar[T]) Get() any {
bufferVar, ok := getBufferVar(bv.ed)
if !ok {
var zero T
return zero
}
return bv.get(bufferVar.Get())
}

func (bv bufferVar[T]) Set(anyVal any) error {
bufferVar, ok := getBufferVar(bv.ed)
if !ok {
return errEditorNotActive
}
var val T
err := vals.ScanToGo(anyVal, &val)
if err != nil {
return err
}
bufferVar.Swap(func(buf comps.TextBuffer) comps.TextBuffer {
return bv.set(buf, val)
})
return nil
}

func getBufferVar(ed *Editor) (etk.StateVar[comps.TextBuffer], bool) {
ctxPtr := getField(ed, &ed.etkCtx)
if ctxPtr == nil {
return etk.StateVar[comps.TextBuffer]{}, false
}
return etk.BindState(*ctxPtr, "code/buffer", comps.TextBuffer{}), true
}

/*
func (ed *Editor) wrapBufferFn(f func(comps.TextBuffer) comps.TextBuffer) func() error {
return ed.wrapCtxFn(wrapBufferFn(f))
Expand Down

0 comments on commit 8532574

Please sign in to comment.