diff --git a/api.go b/api.go index 5510717..dad3797 100644 --- a/api.go +++ b/api.go @@ -163,12 +163,17 @@ Check out the {blue}{bold}guide{reset} command to get started. jobModule := jobs.loader(rtm) 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 } diff --git a/editor.go b/editor.go new file mode 100644 index 0000000..0724fb5 --- /dev/null +++ b/editor.go @@ -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 +} diff --git a/readline/line.go b/readline/line.go index be5ef2c..974a34d 100644 --- a/readline/line.go +++ b/readline/line.go @@ -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: diff --git a/readline/register.go b/readline/register.go index 4372b89..2edf1fa 100644 --- a/readline/register.go +++ b/readline/register.go @@ -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 ", and we don't know yet