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