2022-05-14 00:43:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"hilbish/util"
|
|
|
|
|
|
|
|
rt "github.com/arnodel/golua/runtime"
|
|
|
|
)
|
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
// #interface editor
|
|
|
|
// interactions for Hilbish's line reader
|
|
|
|
// The hilbish.editor interface provides functions to
|
|
|
|
// directly interact with the line editor in use.
|
2022-05-14 00:43:40 +00:00
|
|
|
func editorLoader(rtm *rt.Runtime) *rt.Table {
|
|
|
|
exports := map[string]util.LuaExport{
|
|
|
|
"insert": {editorInsert, 1, false},
|
|
|
|
"setVimRegister": {editorSetRegister, 1, false},
|
|
|
|
"getVimRegister": {editorGetRegister, 2, false},
|
2022-07-19 21:55:03 +00:00
|
|
|
"getLine": {editorGetLine, 0, false},
|
2023-10-25 04:41:53 +00:00
|
|
|
"readChar": {editorReadChar, 0, false},
|
2022-05-14 00:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mod := rt.NewTable()
|
|
|
|
util.SetExports(rtm, mod, exports)
|
|
|
|
|
|
|
|
return mod
|
|
|
|
}
|
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
// #interface editor
|
|
|
|
// insert(text)
|
2023-12-04 00:54:00 +00:00
|
|
|
// Inserts text into the Hilbish command line.
|
|
|
|
// #param text string
|
2022-05-14 00:43:40 +00:00
|
|
|
func editorInsert(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
text, err := c.StringArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lr.rl.Insert(text)
|
|
|
|
|
|
|
|
return c.Next(), nil
|
|
|
|
}
|
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
// #interface editor
|
|
|
|
// setVimRegister(register, text)
|
|
|
|
// Sets the vim register at `register` to hold the passed text.
|
2023-12-04 00:54:00 +00:00
|
|
|
// #aram register string
|
|
|
|
// #param text string
|
2022-05-14 00:43:40 +00:00
|
|
|
func editorSetRegister(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
register, err := c.StringArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
text, err := c.StringArg(1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lr.rl.SetRegisterBuf(register, []rune(text))
|
|
|
|
|
|
|
|
return c.Next(), nil
|
|
|
|
}
|
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
// #interface editor
|
2023-02-07 22:18:03 +00:00
|
|
|
// getVimRegister(register) -> string
|
2022-12-15 04:00:54 +00:00
|
|
|
// Returns the text that is at the register.
|
2023-12-04 00:54:00 +00:00
|
|
|
// #param register string
|
2022-05-14 00:43:40 +00:00
|
|
|
func editorGetRegister(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
register, err := c.StringArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := lr.rl.GetFromRegister(register)
|
|
|
|
|
2022-05-14 00:52:30 +00:00
|
|
|
return c.PushingNext1(t.Runtime, rt.StringValue(string(buf))), nil
|
2022-05-14 00:43:40 +00:00
|
|
|
}
|
2022-07-19 21:55:03 +00:00
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
// #interface editor
|
2023-02-07 22:18:03 +00:00
|
|
|
// getLine() -> string
|
2022-12-15 04:00:54 +00:00
|
|
|
// Returns the current input line.
|
2023-12-04 00:54:00 +00:00
|
|
|
// #returns string
|
2022-07-19 21:55:03 +00:00
|
|
|
func editorGetLine(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
buf := lr.rl.GetLine()
|
|
|
|
|
|
|
|
return c.PushingNext1(t.Runtime, rt.StringValue(string(buf))), nil
|
|
|
|
}
|
2023-10-25 04:41:53 +00:00
|
|
|
|
|
|
|
// #interface editor
|
|
|
|
// getChar() -> string
|
2023-12-04 00:54:00 +00:00
|
|
|
// Reads a keystroke from the user. This is in a format of something like Ctrl-L.
|
2023-10-25 04:41:53 +00:00
|
|
|
func editorReadChar(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
buf := lr.rl.ReadChar()
|
|
|
|
|
|
|
|
return c.PushingNext1(t.Runtime, rt.StringValue(string(buf))), nil
|
|
|
|
}
|