mirror of https://github.com/Hilbis/Hilbish
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.editorfg-job
parent
827c25fb57
commit
dc933934eb
5
api.go
5
api.go
|
@ -164,11 +164,16 @@ Check out the {blue}{bold}guide{reset} command to get started.
|
|||
util.Document(jobModule, "(Background) job interface.")
|
||||
mod.Set(rt.StringValue("jobs"), rt.TableValue(jobModule))
|
||||
|
||||
// hilbish.timers table
|
||||
timers = newTimerHandler()
|
||||
timerModule := timers.loader(rtm)
|
||||
util.Document(timerModule, "Timer interface, for control of all intervals and timeouts.")
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -107,6 +107,10 @@ func (rl *Instance) insert(r []rune) {
|
|||
rl.updateHelpers()
|
||||
}
|
||||
|
||||
func (rl *Instance) Insert(t string) {
|
||||
rl.insert([]rune(t))
|
||||
}
|
||||
|
||||
func (rl *Instance) deleteX() {
|
||||
switch {
|
||||
case len(rl.line) == 0:
|
||||
|
|
|
@ -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
|
||||
// determine which register will store the buffer.
|
||||
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.
|
||||
buf := string(buffer)
|
||||
|
||||
|
@ -124,7 +127,7 @@ func (rl *Instance) saveBufToRegister(buffer []rune) {
|
|||
// If there is an active register, directly give it the buffer.
|
||||
// Check if its a numbered or lettered register, and put it in.
|
||||
if rl.registers.onRegister {
|
||||
num, err := strconv.Atoi(string(rl.registers.currentRegister))
|
||||
num, err := strconv.Atoi(reg)
|
||||
if err == nil && num < 10 {
|
||||
rl.registers.writeNumberedRegister(num, []rune(buf), false)
|
||||
} else if err != nil {
|
||||
|
@ -149,8 +152,12 @@ func (rl *Instance) pasteFromRegister() (buffer []rune) {
|
|||
}
|
||||
activeRegister := string(rl.registers.currentRegister)
|
||||
|
||||
// Else find the active register, and return its content.
|
||||
num, err := strconv.Atoi(activeRegister)
|
||||
return rl.GetFromRegister(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.
|
||||
if err == nil {
|
||||
|
@ -158,20 +165,20 @@ func (rl *Instance) pasteFromRegister() (buffer []rune) {
|
|||
if found {
|
||||
return buf
|
||||
}
|
||||
return
|
||||
return []rune{}
|
||||
}
|
||||
// or the lettered ones
|
||||
buf, found := rl.registers.alpha[activeRegister]
|
||||
buf, found := rl.registers.alpha[reg]
|
||||
if found {
|
||||
return buf
|
||||
}
|
||||
// Or the read-only ones
|
||||
buf, found = rl.registers.ro[activeRegister]
|
||||
buf, found = rl.registers.ro[reg]
|
||||
if found {
|
||||
return buf
|
||||
}
|
||||
|
||||
return
|
||||
return []rune{}
|
||||
}
|
||||
|
||||
// setActiveRegister - The user has typed "<regiserID>, and we don't know yet
|
||||
|
|
Loading…
Reference in New Issue