mirror of
https://github.com/Hilbis/Hilbish
synced 2025-07-01 16:52:03 +00:00
fix: setup functions to work with old calling method
This commit is contained in:
parent
2f79f4c56c
commit
96250f8227
@ -20,12 +20,18 @@ var rlMetaKey = rt.StringValue("__readline")
|
||||
func (rl *Instance) luaLoader(rtm *rt.Runtime) (rt.Value, func()) {
|
||||
rlMethods := rt.NewTable()
|
||||
rlMethodss := map[string]util.LuaExport{
|
||||
"read": {luaRead, 1, false},
|
||||
"deleteByAmount": {luaDeleteByAmount, 2, false},
|
||||
"getLine": {luaGetLine, 1, false},
|
||||
"getVimRegister": {luaGetRegister, 2, false},
|
||||
"insert": {luaInsert, 2, false},
|
||||
"read": {luaRead, 1, false},
|
||||
"readChar": {luaReadChar, 1, false},
|
||||
"setVimRegister": {luaSetRegister, 3, false},
|
||||
"log": {luaLog, 2, false},
|
||||
}
|
||||
util.SetExports(rtm, rlMethods, rlMethodss)
|
||||
|
||||
jobMeta := rt.NewTable()
|
||||
rlMeta := rt.NewTable()
|
||||
rlIndex := func(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
_, err := rlArg(c, 0)
|
||||
if err != nil {
|
||||
@ -38,8 +44,8 @@ func (rl *Instance) luaLoader(rtm *rt.Runtime) (rt.Value, func()) {
|
||||
return c.PushingNext1(t.Runtime, val), nil
|
||||
}
|
||||
|
||||
jobMeta.Set(rt.StringValue("__index"), rt.FunctionValue(rt.NewGoFunction(rlIndex, "__index", 2, false)))
|
||||
rtm.SetRegistry(rlMetaKey, rt.TableValue(jobMeta))
|
||||
rlMeta.Set(rt.StringValue("__index"), rt.FunctionValue(rt.NewGoFunction(rlIndex, "__index", 2, false)))
|
||||
rtm.SetRegistry(rlMetaKey, rt.TableValue(rlMeta))
|
||||
|
||||
rlFuncs := map[string]util.LuaExport{
|
||||
"new": {luaNew, 0, false},
|
||||
@ -99,6 +105,135 @@ func luaRead(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
return c.PushingNext1(t.Runtime, rt.StringValue(inp)), nil
|
||||
}
|
||||
|
||||
// setVimRegister(register, text)
|
||||
// Sets the vim register at `register` to hold the passed text.
|
||||
// #param register string
|
||||
// #param text string
|
||||
func luaSetRegister(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
if err := c.CheckNArgs(3); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rl, err := rlArg(c, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
register, err := c.StringArg(1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
text, err := c.StringArg(2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rl.SetRegisterBuf(register, []rune(text))
|
||||
|
||||
return c.Next(), nil
|
||||
}
|
||||
|
||||
// getVimRegister(register) -> string
|
||||
// Returns the text that is at the register.
|
||||
// #param register string
|
||||
func luaGetRegister(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
if err := c.CheckNArgs(2); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rl, err := rlArg(c, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
register, err := c.StringArg(1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
buf := rl.GetFromRegister(register)
|
||||
|
||||
return c.PushingNext1(t.Runtime, rt.StringValue(string(buf))), nil
|
||||
}
|
||||
|
||||
// getLine() -> string
|
||||
// Returns the current input line.
|
||||
// #returns string
|
||||
func luaGetLine(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
if err := c.Check1Arg(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rl, err := rlArg(c, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
buf := rl.GetLine()
|
||||
|
||||
return c.PushingNext1(t.Runtime, rt.StringValue(string(buf))), nil
|
||||
}
|
||||
|
||||
// getChar() -> string
|
||||
// Reads a keystroke from the user. This is in a format of something like Ctrl-L.
|
||||
func luaReadChar(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
if err := c.Check1Arg(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rl, err := rlArg(c, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
buf := rl.ReadChar()
|
||||
|
||||
return c.PushingNext1(t.Runtime, rt.StringValue(string(buf))), nil
|
||||
}
|
||||
|
||||
// deleteByAmount(amount)
|
||||
// Deletes characters in the line by the given amount.
|
||||
// #param amount number
|
||||
func luaDeleteByAmount(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
if err := c.CheckNArgs(2); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rl, err := rlArg(c, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
amount, err := c.IntArg(1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rl.DeleteByAmount(int(amount))
|
||||
|
||||
return c.Next(), nil
|
||||
}
|
||||
|
||||
func luaLog(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
if err := c.CheckNArgs(2); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rl, err := rlArg(c, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logText, err := c.StringArg(1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rl.RefreshPromptLog(logText)
|
||||
|
||||
return c.Next(), nil
|
||||
}
|
||||
|
||||
func rlArg(c *rt.GoCont, arg int) (*Instance, error) {
|
||||
j, ok := valueToRl(c.Arg(arg))
|
||||
if !ok {
|
||||
|
@ -1,3 +1,28 @@
|
||||
local readline = require 'readline'
|
||||
|
||||
hilbish.editor = readline.new()
|
||||
local editor = readline.new()
|
||||
local editorMt = {}
|
||||
|
||||
hilbish.editor = {}
|
||||
|
||||
local function contains(search, needle)
|
||||
for _, p in ipairs(search) do
|
||||
if p == needle then
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
function editorMt.__index(_, key)
|
||||
if contains({'deleteByAmount', 'getVimRegister', 'getLine', 'insert', 'readChar', 'setVimRegister'}, key) then
|
||||
--editor:log 'The calling method of this function has changed. Please use the colon to call this hilbish.editor function.'
|
||||
end
|
||||
|
||||
return function(...)
|
||||
return editor[key](editor, ...)
|
||||
end
|
||||
end
|
||||
|
||||
setmetatable(hilbish.editor, editorMt)
|
||||
|
Loading…
x
Reference in New Issue
Block a user