aboutsummaryrefslogtreecommitdiffstats
path: root/config/binds.go
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2022-12-12 15:03:30 +0100
committerRobin Jarry <robin@jarry.cc>2022-12-14 11:22:58 +0100
commitc05c2ffe0424b048b10e7dd1aca59ae9cf631f12 (patch)
tree13a3a84eb74fdea77996161f01bec5596f67f39f /config/binds.go
parent9d0297e9d913a92b2d7ae02692e83f0f4093a766 (diff)
downloadaerc-c05c2ffe0424b048b10e7dd1aca59ae9cf631f12.tar.gz
config: make various sections accessible via global vars
There is only one instance of AercConfig which is associated to the Aerc widget. Everywhere we need to access configuration options, we need somehow to get a reference either to the Aerc widget or to a pointer to the AercConfig instance. This makes the code cluttered. Remove the AercConfig structure and every place where it is referenced. Instead, declare global variables for every configuration section and access them directly from the `config` module. Since bindings and ui sections can be "contextual" (i.e. per account, per folder or per subject), leave most local references intact. Replacing them with config.{Ui,Binds}.For{Account,Folder,Subject} would make this patch even more unreadable. This is something that may be addressed in the future. Signed-off-by: Robin Jarry <robin@jarry.cc> Acked-by: Tim Culverhouse <tim@timculverhouse.com>
Diffstat (limited to 'config/binds.go')
-rw-r--r--config/binds.go62
1 files changed, 31 insertions, 31 deletions
diff --git a/config/binds.go b/config/binds.go
index abaff78c..60e3a854 100644
--- a/config/binds.go
+++ b/config/binds.go
@@ -77,14 +77,28 @@ const (
type BindingSearchResult int
-func (config *AercConfig) parseBinds(root string) error {
+func defaultBindsConfig() *BindingConfig {
// These bindings are not configurable
- config.Bindings.AccountWizard.ExKey = KeyStroke{
- Key: tcell.KeyCtrlE,
- }
+ wizard := NewKeyBindings()
+ wizard.ExKey = KeyStroke{Key: tcell.KeyCtrlE}
quit, _ := ParseBinding("<C-q>", ":quit<Enter>")
- config.Bindings.AccountWizard.Add(quit)
+ wizard.Add(quit)
+ return &BindingConfig{
+ Global: NewKeyBindings(),
+ AccountWizard: wizard,
+ Compose: NewKeyBindings(),
+ ComposeEditor: NewKeyBindings(),
+ ComposeReview: NewKeyBindings(),
+ MessageList: NewKeyBindings(),
+ MessageView: NewKeyBindings(),
+ MessageViewPassthrough: NewKeyBindings(),
+ Terminal: NewKeyBindings(),
+ }
+}
+
+var Binds = defaultBindsConfig()
+func parseBinds(root string) error {
filename := path.Join(root, "binds.conf")
if _, err := os.Stat(filename); errors.Is(err, os.ErrNotExist) {
fmt.Printf("%s not found, installing the system default", filename)
@@ -99,14 +113,14 @@ func (config *AercConfig) parseBinds(root string) error {
}
baseGroups := map[string]**KeyBindings{
- "default": &config.Bindings.Global,
- "compose": &config.Bindings.Compose,
- "messages": &config.Bindings.MessageList,
- "terminal": &config.Bindings.Terminal,
- "view": &config.Bindings.MessageView,
- "view::passthrough": &config.Bindings.MessageViewPassthrough,
- "compose::editor": &config.Bindings.ComposeEditor,
- "compose::review": &config.Bindings.ComposeReview,
+ "default": &Binds.Global,
+ "compose": &Binds.Compose,
+ "messages": &Binds.MessageList,
+ "terminal": &Binds.Terminal,
+ "view": &Binds.MessageView,
+ "view::passthrough": &Binds.MessageViewPassthrough,
+ "compose::editor": &Binds.ComposeEditor,
+ "compose::review": &Binds.ComposeReview,
}
// Base Bindings
@@ -123,14 +137,14 @@ func (config *AercConfig) parseBinds(root string) error {
}
if baseOnly {
- err = config.LoadBinds(binds, baseSectionName, group)
+ err = LoadBinds(binds, baseSectionName, group)
if err != nil {
return err
}
}
}
- log.Debugf("binds.conf: %#v", config.Bindings)
+ log.Debugf("binds.conf: %#v", Binds)
return nil
}
@@ -167,7 +181,7 @@ func LoadBindingSection(sec *ini.Section) (*KeyBindings, error) {
return bindings, nil
}
-func (config *AercConfig) LoadBinds(binds *ini.File, baseName string, baseGroup **KeyBindings) error {
+func LoadBinds(binds *ini.File, baseName string, baseGroup **KeyBindings) error {
if sec, err := binds.GetSection(baseName); err == nil {
binds, err := LoadBindingSection(sec)
if err != nil {
@@ -221,7 +235,7 @@ func (config *AercConfig) LoadBinds(binds *ini.File, baseName string, baseGroup
case "account":
acctName := sectionName[index+1:]
valid := false
- for _, acctConf := range config.Accounts {
+ for _, acctConf := range Accounts {
matches := contextualBind.Regex.FindString(acctConf.Name)
if matches != "" {
valid = true
@@ -246,20 +260,6 @@ func (config *AercConfig) LoadBinds(binds *ini.File, baseName string, baseGroup
return nil
}
-func defaultBindsConfig() BindingConfig {
- return BindingConfig{
- Global: NewKeyBindings(),
- AccountWizard: NewKeyBindings(),
- Compose: NewKeyBindings(),
- ComposeEditor: NewKeyBindings(),
- ComposeReview: NewKeyBindings(),
- MessageList: NewKeyBindings(),
- MessageView: NewKeyBindings(),
- MessageViewPassthrough: NewKeyBindings(),
- Terminal: NewKeyBindings(),
- }
-}
-
func NewKeyBindings() *KeyBindings {
return &KeyBindings{
ExKey: KeyStroke{tcell.ModNone, tcell.KeyRune, ':'},