2019-12-18 18:38:21 +08:00
|
|
|
package edit
|
2019-11-03 09:22:56 +08:00
|
|
|
|
|
|
|
import (
|
2019-12-24 04:00:59 +08:00
|
|
|
"github.com/elves/elvish/pkg/cli"
|
|
|
|
"github.com/elves/elvish/pkg/cli/el/codearea"
|
|
|
|
"github.com/elves/elvish/pkg/eval"
|
|
|
|
"github.com/elves/elvish/pkg/eval/vals"
|
|
|
|
"github.com/elves/elvish/pkg/eval/vars"
|
2019-11-03 09:22:56 +08:00
|
|
|
)
|
|
|
|
|
2019-11-04 07:27:16 +08:00
|
|
|
//elvdoc:fn insert-at-dot
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// edit:insert-at-dot $text
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Inserts the given text at the dot, moving the dot after the newly
|
|
|
|
// inserted text.
|
|
|
|
|
|
|
|
func insertAtDot(app cli.App, text string) {
|
|
|
|
app.CodeArea().MutateState(func(s *codearea.State) {
|
|
|
|
s.Buffer.InsertAtDot(text)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
//elvdoc:fn replace-input
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// edit:replace-input $text
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Equivalent to assigning `$text` to `$edit:current-command`.
|
|
|
|
|
|
|
|
func replaceInput(app cli.App, text string) {
|
2019-11-29 08:01:27 +08:00
|
|
|
cli.SetCodeBuffer(app, codearea.Buffer{Content: text, Dot: len(text)})
|
2019-11-04 07:27:16 +08:00
|
|
|
}
|
|
|
|
|
2019-11-03 09:22:56 +08:00
|
|
|
//elvdoc:var -dot
|
|
|
|
//
|
|
|
|
// Contains the current position of the cursor, as a byte position within
|
|
|
|
// `$edit:current-command`.
|
|
|
|
|
|
|
|
//elvdoc:var current-command
|
|
|
|
//
|
|
|
|
// Contains the content of the current input. Setting the variable will
|
|
|
|
// cause the cursor to move to the very end, as if `edit-dot = (count
|
|
|
|
// $edit:current-command)` has been invoked.
|
|
|
|
//
|
|
|
|
// This API is subject to change.
|
|
|
|
|
|
|
|
func initStateAPI(app cli.App, ns eval.Ns) {
|
2019-11-04 07:27:16 +08:00
|
|
|
ns.AddGoFns("<edit>", map[string]interface{}{
|
|
|
|
"insert-at-dot": func(s string) { insertAtDot(app, s) },
|
|
|
|
"replace-input": func(s string) { replaceInput(app, s) },
|
|
|
|
})
|
|
|
|
|
2019-11-03 09:22:56 +08:00
|
|
|
setDot := func(v interface{}) error {
|
|
|
|
var dot int
|
|
|
|
err := vals.ScanToGo(v, &dot)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
app.CodeArea().MutateState(func(s *codearea.State) {
|
2019-11-04 07:22:36 +08:00
|
|
|
s.Buffer.Dot = dot
|
2019-11-03 09:22:56 +08:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
getDot := func() interface{} {
|
2019-11-04 07:22:36 +08:00
|
|
|
return vals.FromGo(app.CodeArea().CopyState().Buffer.Dot)
|
2019-11-03 09:22:56 +08:00
|
|
|
}
|
|
|
|
ns.Add("-dot", vars.FromSetGet(setDot, getDot))
|
|
|
|
|
2019-11-04 07:27:16 +08:00
|
|
|
setCurrentCommand := func(v interface{}) error {
|
2019-11-03 09:22:56 +08:00
|
|
|
var content string
|
|
|
|
err := vals.ScanToGo(v, &content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-04 07:27:16 +08:00
|
|
|
replaceInput(app, content)
|
2019-11-03 09:22:56 +08:00
|
|
|
return nil
|
|
|
|
}
|
2019-11-04 07:27:16 +08:00
|
|
|
getCurrentCommand := func() interface{} {
|
2019-11-29 08:01:27 +08:00
|
|
|
return vals.FromGo(cli.CodeBuffer(app).Content)
|
2019-11-03 09:22:56 +08:00
|
|
|
}
|
2019-11-04 07:27:16 +08:00
|
|
|
ns.Add("current-command", vars.FromSetGet(setCurrentCommand, getCurrentCommand))
|
2019-11-03 09:22:56 +08:00
|
|
|
}
|