1package config
2
3import (
4 _ "embed"
5 "encoding/json"
6 "fmt"
7 "os"
8 "path/filepath"
9)
10
11//go:embed default_keybinds.json
12var defaultKeybindsJSON []byte
13
14// Keybinds is the active keybind configuration. Initialized to defaults at
15// package init; overwritten by LoadKeybindsFromDir when config is loaded.
16var Keybinds = defaultKeybinds()
17
18// KeybindsConfig holds all configurable key bindings organized by area.
19type KeybindsConfig struct {
20 Global GlobalKeys `json:"global"`
21 Inbox InboxKeys `json:"inbox"`
22 Email EmailKeys `json:"email"`
23 Composer ComposerKeys `json:"composer"`
24 Folder FolderKeys `json:"folder"`
25 Drafts DraftsKeys `json:"drafts"`
26}
27
28type GlobalKeys struct {
29 Quit string `json:"quit"`
30 Cancel string `json:"cancel"`
31 NavUp string `json:"nav_up"`
32 NavDown string `json:"nav_down"`
33}
34
35type InboxKeys struct {
36 VisualMode string `json:"visual_mode"`
37 ToggleThreaded string `json:"toggle_threaded"`
38 Delete string `json:"delete"`
39 Archive string `json:"archive"`
40 Refresh string `json:"refresh"`
41 Search string `json:"search"`
42 Filter string `json:"filter"`
43 Open string `json:"open"`
44 NextTab string `json:"next_tab"`
45 PrevTab string `json:"prev_tab"`
46}
47
48type EmailKeys struct {
49 Reply string `json:"reply"`
50 Forward string `json:"forward"`
51 Delete string `json:"delete"`
52 Archive string `json:"archive"`
53 ToggleImages string `json:"toggle_images"`
54 RsvpAccept string `json:"rsvp_accept"`
55 RsvpDecline string `json:"rsvp_decline"`
56 RsvpTentative string `json:"rsvp_tentative"`
57 FocusAttachments string `json:"focus_attachments"`
58}
59
60type ComposerKeys struct {
61 ExternalEditor string `json:"external_editor"`
62 NextField string `json:"next_field"`
63 PrevField string `json:"prev_field"`
64}
65
66type FolderKeys struct {
67 NextFolder string `json:"next_folder"`
68 PrevFolder string `json:"prev_folder"`
69 Move string `json:"move"`
70 FocusPreview string `json:"focus_preview"`
71 FocusInbox string `json:"focus_inbox"`
72}
73
74type DraftsKeys struct {
75 Open string `json:"open"`
76 Delete string `json:"delete"`
77}
78
79func defaultKeybinds() KeybindsConfig {
80 var kb KeybindsConfig
81 if err := json.Unmarshal(defaultKeybindsJSON, &kb); err != nil {
82 panic("matcha: malformed default_keybinds.json: " + err.Error())
83 }
84 return kb
85}
86
87// LoadKeybindsFromDir reads keybinds.json from cfgDir, writing defaults if
88// the file does not exist, then updates the package-level Keybinds var.
89func LoadKeybindsFromDir(cfgDir string) error {
90 path := filepath.Join(cfgDir, "keybinds.json")
91
92 data, err := os.ReadFile(path)
93 if err != nil {
94 if !os.IsNotExist(err) {
95 return fmt.Errorf("keybinds: read %s: %w", path, err)
96 }
97 // File missing — write defaults.
98 if err := os.MkdirAll(cfgDir, 0700); err != nil {
99 return fmt.Errorf("keybinds: mkdir %s: %w", cfgDir, err)
100 }
101 if err := os.WriteFile(path, defaultKeybindsJSON, 0600); err != nil {
102 return fmt.Errorf("keybinds: write defaults to %s: %w", path, err)
103 }
104 Keybinds = defaultKeybinds()
105 return nil
106 }
107
108 var kb KeybindsConfig
109 if err := json.Unmarshal(data, &kb); err != nil {
110 return fmt.Errorf("keybinds: parse %s: %w", path, err)
111 }
112 Keybinds = kb
113 return nil
114}
115
116// ValidateKeybinds returns a list of conflict descriptions where two different
117// actions within the same area are mapped to the same key. Cross-area
118// duplicates are intentional (e.g. "d" = delete in both inbox and email view).
119func ValidateKeybinds(kb KeybindsConfig) []string {
120 var conflicts []string
121
122 check := func(area string, bindings map[string]string) {
123 seen := make(map[string]string) // key → action name
124 for action, key := range bindings {
125 if key == "" {
126 continue
127 }
128 if prev, ok := seen[key]; ok {
129 conflicts = append(conflicts,
130 fmt.Sprintf("conflict in %s: key %q used for both %q and %q", area, key, prev, action))
131 } else {
132 seen[key] = action
133 }
134 }
135 }
136
137 check("global", map[string]string{
138 "quit": kb.Global.Quit,
139 "cancel": kb.Global.Cancel,
140 "nav_up": kb.Global.NavUp,
141 "nav_down": kb.Global.NavDown,
142 })
143 check("inbox", map[string]string{
144 "visual_mode": kb.Inbox.VisualMode,
145 "toggle_threaded": kb.Inbox.ToggleThreaded,
146 "delete": kb.Inbox.Delete,
147 "archive": kb.Inbox.Archive,
148 "refresh": kb.Inbox.Refresh,
149 "search": kb.Inbox.Search,
150 "filter": kb.Inbox.Filter,
151 "open": kb.Inbox.Open,
152 "next_tab": kb.Inbox.NextTab,
153 "prev_tab": kb.Inbox.PrevTab,
154 })
155 check("email", map[string]string{
156 "reply": kb.Email.Reply,
157 "forward": kb.Email.Forward,
158 "delete": kb.Email.Delete,
159 "archive": kb.Email.Archive,
160 "toggle_images": kb.Email.ToggleImages,
161 "rsvp_accept": kb.Email.RsvpAccept,
162 "rsvp_decline": kb.Email.RsvpDecline,
163 "rsvp_tentative": kb.Email.RsvpTentative,
164 "focus_attachments": kb.Email.FocusAttachments,
165 })
166 check("composer", map[string]string{
167 "external_editor": kb.Composer.ExternalEditor,
168 "next_field": kb.Composer.NextField,
169 "prev_field": kb.Composer.PrevField,
170 })
171 check("folder", map[string]string{
172 "next_folder": kb.Folder.NextFolder,
173 "prev_folder": kb.Folder.PrevFolder,
174 "move": kb.Folder.Move,
175 "focus_preview": kb.Folder.FocusPreview,
176 "focus_inbox": kb.Folder.FocusInbox,
177 })
178 check("drafts", map[string]string{
179 "open": kb.Drafts.Open,
180 "delete": kb.Drafts.Delete,
181 })
182
183 return conflicts
184}