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