aboutsummaryrefslogtreecommitdiffstats
path: root/commands
diff options
context:
space:
mode:
Diffstat (limited to 'commands')
-rw-r--r--commands/account/account.go16
-rw-r--r--commands/account/cf.go (renamed from commands/cf.go)4
-rw-r--r--commands/account/delete-message.go (renamed from commands/delete-message.go)4
-rw-r--r--commands/account/next-folder.go (renamed from commands/next-folder.go)6
-rw-r--r--commands/account/next-message.go (renamed from commands/next-message.go)6
-rw-r--r--commands/account/select-message.go (renamed from commands/select-message.go)4
-rw-r--r--commands/cd.go2
-rw-r--r--commands/commands.go36
-rw-r--r--commands/global.go12
-rw-r--r--commands/next-tab.go4
-rw-r--r--commands/quit.go4
-rw-r--r--commands/term-close.go2
-rw-r--r--commands/term.go2
13 files changed, 72 insertions, 30 deletions
diff --git a/commands/account/account.go b/commands/account/account.go
new file mode 100644
index 00000000..918d962b
--- /dev/null
+++ b/commands/account/account.go
@@ -0,0 +1,16 @@
+package account
+
+import (
+ "git.sr.ht/~sircmpwn/aerc2/commands"
+)
+
+var (
+ AccountCommands *commands.Commands
+)
+
+func register(name string, cmd commands.AercCommand) {
+ if AccountCommands == nil {
+ AccountCommands = commands.NewCommands()
+ }
+ AccountCommands.Register(name, cmd)
+}
diff --git a/commands/cf.go b/commands/account/cf.go
index d8b0508c..3d12b5c6 100644
--- a/commands/cf.go
+++ b/commands/account/cf.go
@@ -1,4 +1,4 @@
-package commands
+package account
import (
"errors"
@@ -12,7 +12,7 @@ var (
func init() {
history = make(map[string]string)
- Register("cf", ChangeFolder)
+ register("cf", ChangeFolder)
}
func ChangeFolder(aerc *widgets.Aerc, args []string) error {
diff --git a/commands/delete-message.go b/commands/account/delete-message.go
index be56dbbb..40e3bba5 100644
--- a/commands/delete-message.go
+++ b/commands/account/delete-message.go
@@ -1,4 +1,4 @@
-package commands
+package account
import (
"errors"
@@ -7,7 +7,7 @@ import (
)
func init() {
- Register("delete-message", DeleteMessage)
+ register("delete-message", DeleteMessage)
}
func DeleteMessage(aerc *widgets.Aerc, args []string) error {
diff --git a/commands/next-folder.go b/commands/account/next-folder.go
index f0332c6f..1b59af11 100644
--- a/commands/next-folder.go
+++ b/commands/account/next-folder.go
@@ -1,4 +1,4 @@
-package commands
+package account
import (
"errors"
@@ -9,8 +9,8 @@ import (
)
func init() {
- Register("next-folder", NextPrevFolder)
- Register("prev-folder", NextPrevFolder)
+ register("next-folder", NextPrevFolder)
+ register("prev-folder", NextPrevFolder)
}
func nextPrevFolderUsage(cmd string) error {
diff --git a/commands/next-message.go b/commands/account/next-message.go
index d1d188c9..d2c006fb 100644
--- a/commands/next-message.go
+++ b/commands/account/next-message.go
@@ -1,4 +1,4 @@
-package commands
+package account
import (
"errors"
@@ -10,8 +10,8 @@ import (
)
func init() {
- Register("next-message", NextPrevMessage)
- Register("prev-message", NextPrevMessage)
+ register("next-message", NextPrevMessage)
+ register("prev-message", NextPrevMessage)
}
func nextPrevMessageUsage(cmd string) error {
diff --git a/commands/select-message.go b/commands/account/select-message.go
index 728d31ba..54f48f2e 100644
--- a/commands/select-message.go
+++ b/commands/account/select-message.go
@@ -1,4 +1,4 @@
-package commands
+package account
import (
"errors"
@@ -8,7 +8,7 @@ import (
)
func init() {
- Register("select-message", SelectMessage)
+ register("select-message", SelectMessage)
}
func SelectMessage(aerc *widgets.Aerc, args []string) error {
diff --git a/commands/cd.go b/commands/cd.go
index 6e3da39a..bb06c235 100644
--- a/commands/cd.go
+++ b/commands/cd.go
@@ -13,7 +13,7 @@ var (
)
func init() {
- Register("cd", ChangeDirectory)
+ register("cd", ChangeDirectory)
}
func ChangeDirectory(aerc *widgets.Aerc, args []string) error {
diff --git a/commands/commands.go b/commands/commands.go
index 2890cdd4..a2589f87 100644
--- a/commands/commands.go
+++ b/commands/commands.go
@@ -10,18 +10,32 @@ import (
type AercCommand func(aerc *widgets.Aerc, args []string) error
-var (
- commands map[string]AercCommand
-)
+type Commands map[string]AercCommand
-func Register(name string, cmd AercCommand) {
- if commands == nil {
- commands = make(map[string]AercCommand)
- }
- commands[name] = cmd
+func NewCommands() *Commands {
+ cmds := Commands(make(map[string]AercCommand))
+ return &cmds
+}
+
+func (cmds *Commands) dict() map[string]AercCommand {
+ return map[string]AercCommand(*cmds)
+}
+
+func (cmds *Commands) Register(name string, cmd AercCommand) {
+ cmds.dict()[name] = cmd
+}
+
+type NoSuchCommand string
+
+func (err NoSuchCommand) Error() string {
+ return "Unknown command " + string(err)
+}
+
+type CommandSource interface {
+ Commands() *Commands
}
-func ExecuteCommand(aerc *widgets.Aerc, cmd string) error {
+func (cmds *Commands) ExecuteCommand(aerc *widgets.Aerc, cmd string) error {
args, err := shlex.Split(cmd)
if err != nil {
return err
@@ -29,8 +43,8 @@ func ExecuteCommand(aerc *widgets.Aerc, cmd string) error {
if len(args) == 0 {
return errors.New("Expected a command.")
}
- if fn, ok := commands[args[0]]; ok {
+ if fn, ok := cmds.dict()[args[0]]; ok {
return fn(aerc, args)
}
- return errors.New("Unknown command " + args[0])
+ return NoSuchCommand(args[0])
}
diff --git a/commands/global.go b/commands/global.go
new file mode 100644
index 00000000..c24869ae
--- /dev/null
+++ b/commands/global.go
@@ -0,0 +1,12 @@
+package commands
+
+var (
+ GlobalCommands *Commands
+)
+
+func register(name string, cmd AercCommand) {
+ if GlobalCommands == nil {
+ GlobalCommands = NewCommands()
+ }
+ GlobalCommands.Register(name, cmd)
+}
diff --git a/commands/next-tab.go b/commands/next-tab.go
index fee3fb21..a9a77c2a 100644
--- a/commands/next-tab.go
+++ b/commands/next-tab.go
@@ -9,8 +9,8 @@ import (
)
func init() {
- Register("next-tab", NextPrevTab)
- Register("prev-tab", NextPrevTab)
+ register("next-tab", NextPrevTab)
+ register("prev-tab", NextPrevTab)
}
func nextPrevTabUsage(cmd string) error {
diff --git a/commands/quit.go b/commands/quit.go
index 34358595..c0c387b5 100644
--- a/commands/quit.go
+++ b/commands/quit.go
@@ -7,7 +7,7 @@ import (
)
func init() {
- Register("quit", ChangeQuit)
+ register("quit", CommandQuit)
}
type ErrorExit int
@@ -16,7 +16,7 @@ func (err ErrorExit) Error() string {
return "exit"
}
-func ChangeQuit(aerc *widgets.Aerc, args []string) error {
+func CommandQuit(aerc *widgets.Aerc, args []string) error {
if len(args) != 1 {
return errors.New("Usage: quit")
}
diff --git a/commands/term-close.go b/commands/term-close.go
index ae2b55b8..7da4f426 100644
--- a/commands/term-close.go
+++ b/commands/term-close.go
@@ -8,7 +8,7 @@ import (
func init() {
// TODO: Move this command into a terminal-specific command set
- Register("close", TermClose)
+ register("close", TermClose)
}
func TermClose(aerc *widgets.Aerc, args []string) error {
diff --git a/commands/term.go b/commands/term.go
index 1981e62b..d122e78a 100644
--- a/commands/term.go
+++ b/commands/term.go
@@ -11,7 +11,7 @@ import (
)
func init() {
- Register("term", Term)
+ register("term", Term)
}
func Term(aerc *widgets.Aerc, args []string) error {