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