Compare commits

..

6 Commits

Author SHA1 Message Date
TorchedSammy cb88db8cfc
feat: add hilbish.vim interface (closes #114) 2022-05-13 20:52:48 -04:00
TorchedSammy 960909b81d
fix: typo in editor interface 2022-05-13 20:52:30 -04:00
TorchedSammy dc933934eb
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
2022-05-13 20:43:40 -04:00
TorchedSammy 827c25fb57
feat: add hilbish.job.add function
this is mainly to accomodate for the employer
handler (#152)
2022-05-13 17:08:15 -04:00
TorchedSammy 3374dfd28f
fix: always start job in exec handler
makes sure that if any errors occur the job
will still throw the start hook
2022-05-13 16:59:16 -04:00
TorchedSammy 9a87d76969
fix: finish job on lua job stop function
makesdoc  sure that a job is finalized by throwing
the job.done hook and setting its running status
to false
2022-05-12 22:29:07 -04:00
8 changed files with 139 additions and 14 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(buf))), nil
}

View File

@ -362,12 +362,9 @@ func execHandle(bg bool) interp.ExecHandlerFunc {
if bg { if bg {
j = jobs.getLatest() j = jobs.getLatest()
j.setHandle(cmd.Process) j.setHandle(cmd.Process)
j.start(cmd.Process.Pid)
} }
if err == nil { if err == nil {
if bg {
j.start(cmd.Process.Pid)
}
if done := ctx.Done(); done != nil { if done := ctx.Done(); done != nil {
go func() { go func() {
<-done <-done

26
job.go
View File

@ -59,6 +59,7 @@ func (j *job) lua() rt.Value {
func (j *job) luaStop(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { func (j *job) luaStop(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if j.running { if j.running {
j.stop() j.stop()
j.finish()
} }
return c.Next(), nil return c.Next(), nil
@ -78,16 +79,19 @@ func newJobHandler() *jobHandler {
} }
} }
func (j *jobHandler) add(cmd string) { func (j *jobHandler) add(cmd string) *job {
j.mu.Lock() j.mu.Lock()
defer j.mu.Unlock() defer j.mu.Unlock()
j.latestID++ j.latestID++
j.jobs[j.latestID] = &job{ jb := &job{
cmd: cmd, cmd: cmd,
running: false, running: false,
id: j.latestID, id: j.latestID,
} }
j.jobs[j.latestID] = jb
return jb
} }
func (j *jobHandler) getLatest() *job { func (j *jobHandler) getLatest() *job {
@ -101,6 +105,7 @@ func (j *jobHandler) loader(rtm *rt.Runtime) *rt.Table {
jobFuncs := map[string]util.LuaExport{ jobFuncs := map[string]util.LuaExport{
"all": {j.luaAllJobs, 0, false}, "all": {j.luaAllJobs, 0, false},
"get": {j.luaGetJob, 1, false}, "get": {j.luaGetJob, 1, false},
"add": {j.luaAddJob, 1, false},
} }
luaJob := rt.NewTable() luaJob := rt.NewTable()
@ -129,6 +134,23 @@ func (j *jobHandler) luaGetJob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
return c.PushingNext1(t.Runtime, job.lua()), nil return c.PushingNext1(t.Runtime, job.lua()), nil
} }
func (j *jobHandler) luaAddJob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
j.mu.RLock()
defer j.mu.RUnlock()
if err := c.Check1Arg(); err != nil {
return nil, err
}
cmd, err := c.StringArg(0)
if err != nil {
return nil, err
}
jb := j.add(cmd)
return c.PushingNext1(t.Runtime, jb.lua()), nil
}
func (j *jobHandler) luaAllJobs(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { func (j *jobHandler) luaAllJobs(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
j.mu.RLock() j.mu.RLock()
defer j.mu.RUnlock() defer j.mu.RUnlock()

View File

@ -7,6 +7,7 @@ package.path = package.path .. ';' .. hilbish.dataDir .. '/?/init.lua'
require 'nature.commands' require 'nature.commands'
require 'nature.completions' require 'nature.completions'
require 'nature.opts' require 'nature.opts'
require 'nature.vim'
local shlvl = tonumber(os.getenv 'SHLVL') local shlvl = tonumber(os.getenv 'SHLVL')
if shlvl ~= nil then if shlvl ~= nil then

19
nature/vim.lua 100644
View File

@ -0,0 +1,19 @@
local vim = {}
hilbish.vim = {
registers = {}
}
setmetatable(hilbish.vim.registers, {
__newindex = function(_, k, v)
hilbish.editor.setVimRegister(k, v)
end,
__index = function(_, k)
return hilbish.editor.getVimRegister(k)
end
})
setmetatable(hilbish.vim, {
__index = function(_, k)
if k == 'mode' then return hilbish.vimMode end
end
})

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