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