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		"bind_key":          m.luaBindKey,
20		"http":              m.luaHTTP,
21	})
22
23	L.SetField(mod, "_VERSION", lua.LString("0.1.0"))
24}
25
26// matcha.on(event, callback) — register a hook callback.
27func (m *Manager) luaOn(L *lua.LState) int {
28	event := L.CheckString(1)
29	fn := L.CheckFunction(2)
30	m.registerHook(event, fn)
31	return 0
32}
33
34// matcha.log(msg) — log a message to stderr.
35func (m *Manager) luaLog(L *lua.LState) int {
36	msg := L.CheckString(1)
37	log.Printf("[plugin] %s", msg)
38	return 0
39}
40
41// matcha.set_status(area, text) — set a persistent status string for a view area.
42// Valid areas: "inbox", "composer", "email_view".
43func (m *Manager) luaSetStatus(L *lua.LState) int {
44	area := L.CheckString(1)
45	text := L.CheckString(2)
46	m.statuses[area] = text
47	return 0
48}
49
50// matcha.notify(msg [, seconds]) — show a temporary notification in the TUI.
51// The optional second argument sets the display duration in seconds (default 2).
52func (m *Manager) luaNotify(L *lua.LState) int {
53	m.pendingNotification = L.CheckString(1)
54	m.pendingDuration = float64(L.OptNumber(2, 2))
55	return 0
56}
57
58// matcha.bind_key(key, area, description, callback) — register a custom keyboard shortcut.
59// Valid areas: "inbox", "email_view", "composer".
60func (m *Manager) luaBindKey(L *lua.LState) int {
61	key := L.CheckString(1)
62	area := L.CheckString(2)
63	description := L.CheckString(3)
64	fn := L.CheckFunction(4)
65
66	switch area {
67	case "inbox", "email_view", "composer":
68		m.bindings = append(m.bindings, KeyBinding{
69			Key:         key,
70			Area:        area,
71			Description: description,
72			Fn:          fn,
73		})
74	default:
75		L.ArgError(2, "invalid area: must be \"inbox\", \"email_view\", or \"composer\"")
76	}
77	return 0
78}
79
80// matcha.set_compose_field(field, value) — set a compose field value.
81// Valid fields: "to", "cc", "bcc", "subject", "body".
82func (m *Manager) luaSetComposeField(L *lua.LState) int {
83	field := L.CheckString(1)
84	value := L.CheckString(2)
85
86	switch field {
87	case "to", "cc", "bcc", "subject", "body":
88		m.pendingFields[field] = value
89	default:
90		L.ArgError(1, "invalid field: must be \"to\", \"cc\", \"bcc\", \"subject\", or \"body\"")
91	}
92	return 0
93}