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