api.go

 1package plugin
 2
 3import (
 4	"log"
 5
 6	lua "github.com/yuin/gopher-lua"
 7)
 8
 9// registerAPI registers the "matcha" module into the Lua VM.
10func (m *Manager) registerAPI() {
11	L := m.state
12
13	mod := L.RegisterModule("matcha", map[string]lua.LGFunction{
14		"on":                m.luaOn,
15		"log":               m.luaLog,
16		"notify":            m.luaNotify,
17		"set_status":        m.luaSetStatus,
18		"set_compose_field": m.luaSetComposeField,
19	})
20
21	L.SetField(mod, "_VERSION", lua.LString("0.1.0"))
22}
23
24// matcha.on(event, callback) — register a hook callback.
25func (m *Manager) luaOn(L *lua.LState) int {
26	event := L.CheckString(1)
27	fn := L.CheckFunction(2)
28	m.registerHook(event, fn)
29	return 0
30}
31
32// matcha.log(msg) — log a message to stderr.
33func (m *Manager) luaLog(L *lua.LState) int {
34	msg := L.CheckString(1)
35	log.Printf("[plugin] %s", msg)
36	return 0
37}
38
39// matcha.set_status(area, text) — set a persistent status string for a view area.
40// Valid areas: "inbox", "composer", "email_view".
41func (m *Manager) luaSetStatus(L *lua.LState) int {
42	area := L.CheckString(1)
43	text := L.CheckString(2)
44	m.statuses[area] = text
45	return 0
46}
47
48// matcha.notify(msg [, seconds]) — show a temporary notification in the TUI.
49// The optional second argument sets the display duration in seconds (default 2).
50func (m *Manager) luaNotify(L *lua.LState) int {
51	m.pendingNotification = L.CheckString(1)
52	m.pendingDuration = float64(L.OptNumber(2, 2))
53	return 0
54}
55
56// matcha.set_compose_field(field, value) — set a compose field value.
57// Valid fields: "to", "cc", "bcc", "subject", "body".
58func (m *Manager) luaSetComposeField(L *lua.LState) int {
59	field := L.CheckString(1)
60	value := L.CheckString(2)
61
62	switch field {
63	case "to", "cc", "bcc", "subject", "body":
64		m.pendingFields[field] = value
65	default:
66		L.ArgError(1, "invalid field: must be \"to\", \"cc\", \"bcc\", \"subject\", or \"body\"")
67	}
68	return 0
69}