-
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathkeybinds.go
More file actions
162 lines (147 loc) · 5.11 KB
/
Copy pathkeybinds.go
File metadata and controls
162 lines (147 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package config
import (
_ "embed"
"encoding/json"
keybind "github.com/floatpane/go-keybind"
)
const keyDelete = "delete" // used in ValidateKeybinds action map keys
//go:embed default_keybinds.json
var defaultKeybindsJSON []byte
// Keybinds is the active keybind configuration. Initialized to defaults at
// package init; overwritten by LoadKeybindsFromDir when config is loaded.
var Keybinds = defaultKeybinds()
// KeybindsConfig holds all configurable key bindings organized by area.
type KeybindsConfig struct {
Global GlobalKeys `json:"global"`
Inbox InboxKeys `json:"inbox"`
Email EmailKeys `json:"email"`
Composer ComposerKeys `json:"composer"`
Folder FolderKeys `json:"folder"`
Drafts DraftsKeys `json:"drafts"`
}
type GlobalKeys struct {
Quit string `json:"quit"`
Cancel string `json:"cancel"`
NavUp string `json:"nav_up"`
NavDown string `json:"nav_down"`
}
type InboxKeys struct {
VisualMode string `json:"visual_mode"`
ToggleThreaded string `json:"toggle_threaded"`
Delete string `json:"delete"`
Archive string `json:"archive"`
Refresh string `json:"refresh"`
Search string `json:"search"`
Filter string `json:"filter"`
Open string `json:"open"`
NextTab string `json:"next_tab"`
PrevTab string `json:"prev_tab"`
}
type EmailKeys struct {
Reply string `json:"reply"`
Forward string `json:"forward"`
Delete string `json:"delete"`
Archive string `json:"archive"`
ToggleImages string `json:"toggle_images"`
RsvpAccept string `json:"rsvp_accept"`
RsvpDecline string `json:"rsvp_decline"`
RsvpTentative string `json:"rsvp_tentative"`
FocusAttachments string `json:"focus_attachments"`
OpenHTMLBrowser string `json:"open_html_browser"`
}
type ComposerKeys struct {
ExternalEditor string `json:"external_editor"`
NextField string `json:"next_field"`
PrevField string `json:"prev_field"`
Delete string `json:"delete"`
SpellNext string `json:"spell_next"`
SpellPrev string `json:"spell_prev"`
SpellAccept string `json:"spell_accept"`
SpellDismiss string `json:"spell_dismiss"`
UndoSend string `json:"undo_send"`
}
type FolderKeys struct {
NextFolder string `json:"next_folder"`
PrevFolder string `json:"prev_folder"`
Move string `json:"move"`
FocusPreview string `json:"focus_preview"`
FocusInbox string `json:"focus_inbox"`
}
type DraftsKeys struct {
Open string `json:"open"`
Delete string `json:"delete"`
}
func defaultKeybinds() KeybindsConfig {
var kb KeybindsConfig
if err := json.Unmarshal(defaultKeybindsJSON, &kb); err != nil {
panic("matcha: malformed default_keybinds.json: " + err.Error())
}
return kb
}
// LoadKeybindsFromDir reads keybinds.json from cfgDir, writing defaults if
// the file does not exist, then updates the package-level Keybinds var.
func LoadKeybindsFromDir(cfgDir string) error {
kb, err := keybind.Load(cfgDir, "keybinds.json", defaultKeybinds())
if err != nil {
return err
}
Keybinds = kb
return nil
}
// ValidateKeybinds returns a list of conflict descriptions where two different
// actions within the same area are mapped to the same key. Cross-area
// duplicates are intentional (e.g. "d" = delete in both inbox and email view).
func ValidateKeybinds(kb KeybindsConfig) []string {
return keybind.Validate(map[string]map[string]string{
"global": {
"quit": kb.Global.Quit,
"cancel": kb.Global.Cancel,
"nav_up": kb.Global.NavUp,
"nav_down": kb.Global.NavDown,
},
"inbox": {
"visual_mode": kb.Inbox.VisualMode,
"toggle_threaded": kb.Inbox.ToggleThreaded,
keyDelete: kb.Inbox.Delete,
"archive": kb.Inbox.Archive,
"refresh": kb.Inbox.Refresh,
"search": kb.Inbox.Search,
"filter": kb.Inbox.Filter,
"open": kb.Inbox.Open,
"next_tab": kb.Inbox.NextTab,
"prev_tab": kb.Inbox.PrevTab,
},
"email": {
"reply": kb.Email.Reply,
"forward": kb.Email.Forward,
keyDelete: kb.Email.Delete,
"archive": kb.Email.Archive,
"toggle_images": kb.Email.ToggleImages,
"rsvp_accept": kb.Email.RsvpAccept,
"rsvp_decline": kb.Email.RsvpDecline,
"rsvp_tentative": kb.Email.RsvpTentative,
"focus_attachments": kb.Email.FocusAttachments,
},
"composer": {
"undo_send": kb.Composer.UndoSend,
"external_editor": kb.Composer.ExternalEditor,
"next_field": kb.Composer.NextField,
"prev_field": kb.Composer.PrevField,
keyDelete: kb.Composer.Delete,
// spell_* bindings intentionally excluded — spell_accept reusing
// "tab" with next_field and spell_dismiss reusing "esc" with cancel
// are deliberate: the spellcheck popup intercepts before those handlers.
},
"folder": {
"next_folder": kb.Folder.NextFolder,
"prev_folder": kb.Folder.PrevFolder,
"move": kb.Folder.Move,
"focus_preview": kb.Folder.FocusPreview,
"focus_inbox": kb.Folder.FocusInbox,
},
"drafts": {
"open": kb.Drafts.Open,
keyDelete: kb.Drafts.Delete,
},
})
}