feat: add hilbish.editor interface

this is going to be a middle level interface
which brings functions for interacting
with the line editor used in hilbish

adds the following functions:
setVimRegister(reg, text) - sets text in a vim register
getVimRegister(reg) - gets text from a vim register
insert(text) - inserts text into the line
accessible via hilbish.editor
fg-job
TorchedSammy 2022-05-13 20:43:40 -04:00
parent 827c25fb57
commit dc933934eb
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
4 changed files with 94 additions and 8 deletions

5
api.go
View File

@ -164,11 +164,16 @@ Check out the {blue}{bold}guide{reset} command to get started.
util.Document(jobModule, "(Background) job interface.") util.Document(jobModule, "(Background) job interface.")
mod.Set(rt.StringValue("jobs"), rt.TableValue(jobModule)) mod.Set(rt.StringValue("jobs"), rt.TableValue(jobModule))
// hilbish.timers table
timers = newTimerHandler() timers = newTimerHandler()
timerModule := timers.loader(rtm) timerModule := timers.loader(rtm)
util.Document(timerModule, "Timer interface, for control of all intervals and timeouts.") util.Document(timerModule, "Timer interface, for control of all intervals and timeouts.")
mod.Set(rt.StringValue("timers"), rt.TableValue(timerModule)) mod.Set(rt.StringValue("timers"), rt.TableValue(timerModule))
editorModule := editorLoader(rtm)
util.Document(editorModule, "")
mod.Set(rt.StringValue("editor"), rt.TableValue(editorModule))
return rt.TableValue(fakeMod), nil return rt.TableValue(fakeMod), nil
} }

70
editor.go 100644
View File

@ -0,0 +1,70 @@
package main
import (
"hilbish/util"
rt "github.com/arnodel/golua/runtime"
)
func editorLoader(rtm *rt.Runtime) *rt.Table {
exports := map[string]util.LuaExport{
"insert": {editorInsert, 1, false},
"setVimRegister": {editorSetRegister, 1, false},
"getVimRegister": {editorGetRegister, 2, false},
}
mod := rt.NewTable()
util.SetExports(rtm, mod, exports)
return mod
}
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
}
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
}
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)
return c.PushingNext1(t.Runtime, rt.StringValue(string(bufma))), nil
}

View File

@ -107,6 +107,10 @@ func (rl *Instance) insert(r []rune) {
rl.updateHelpers() rl.updateHelpers()
} }
func (rl *Instance) Insert(t string) {
rl.insert([]rune(t))
}
func (rl *Instance) deleteX() { func (rl *Instance) deleteX() {
switch { switch {
case len(rl.line) == 0: case len(rl.line) == 0:

View File

@ -105,7 +105,10 @@ func (rl *Instance) saveToRegisterTokenize(tokeniser tokeniser, jumper func(toke
// let the caller pass directly this buffer, yet relying on the register system to // let the caller pass directly this buffer, yet relying on the register system to
// determine which register will store the buffer. // determine which register will store the buffer.
func (rl *Instance) saveBufToRegister(buffer []rune) { func (rl *Instance) saveBufToRegister(buffer []rune) {
rl.SetRegisterBuf(string(rl.registers.currentRegister), buffer)
}
func (rl *Instance) SetRegisterBuf(reg string, buffer []rune) {
// We must make an immutable version of the buffer first. // We must make an immutable version of the buffer first.
buf := string(buffer) buf := string(buffer)
@ -124,7 +127,7 @@ func (rl *Instance) saveBufToRegister(buffer []rune) {
// If there is an active register, directly give it the buffer. // If there is an active register, directly give it the buffer.
// Check if its a numbered or lettered register, and put it in. // Check if its a numbered or lettered register, and put it in.
if rl.registers.onRegister { if rl.registers.onRegister {
num, err := strconv.Atoi(string(rl.registers.currentRegister)) num, err := strconv.Atoi(reg)
if err == nil && num < 10 { if err == nil && num < 10 {
rl.registers.writeNumberedRegister(num, []rune(buf), false) rl.registers.writeNumberedRegister(num, []rune(buf), false)
} else if err != nil { } else if err != nil {
@ -149,8 +152,12 @@ func (rl *Instance) pasteFromRegister() (buffer []rune) {
} }
activeRegister := string(rl.registers.currentRegister) activeRegister := string(rl.registers.currentRegister)
// Else find the active register, and return its content. return rl.GetFromRegister(activeRegister)
num, err := strconv.Atoi(activeRegister) }
func (rl *Instance) GetFromRegister(reg string) []rune {
// Find the active register, and return its content.
num, err := strconv.Atoi(reg)
// Either from the numbered ones. // Either from the numbered ones.
if err == nil { if err == nil {
@ -158,20 +165,20 @@ func (rl *Instance) pasteFromRegister() (buffer []rune) {
if found { if found {
return buf return buf
} }
return return []rune{}
} }
// or the lettered ones // or the lettered ones
buf, found := rl.registers.alpha[activeRegister] buf, found := rl.registers.alpha[reg]
if found { if found {
return buf return buf
} }
// Or the read-only ones // Or the read-only ones
buf, found = rl.registers.ro[activeRegister] buf, found = rl.registers.ro[reg]
if found { if found {
return buf return buf
} }
return return []rune{}
} }
// setActiveRegister - The user has typed "<regiserID>, and we don't know yet // setActiveRegister - The user has typed "<regiserID>, and we don't know yet