summary refs log tree commit diff stats
path: root/commands
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2019-03-21 16:30:23 -0400
committerDrew DeVault <sir@cmpwn.com>2019-03-21 16:30:23 -0400
commit8126d82956636a2525263e2d0d985d721fdb8074 (patch)
tree2c5f5c2e2fb2e5ecfb4c12eb8ef6bc575edc39f8 /commands
parentfe79a9a5879936a7f5b16cc6a8be1d93ec1bfae7 (diff)
downloadaerc-8126d82956636a2525263e2d0d985d721fdb8074.tar.gz
Add context-specific commands
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 0000000..918d962
--- /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 d8b0508..3d12b5c 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 be56dbb..40e3bba 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 f0332c6..1b59af1 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 d1d188c..d2c006f 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 728d31b..54f48f2 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 6e3da39..bb06c23 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 2890cdd..a2589f8 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 0000000..c24869a
--- /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 fee3fb2..a9a77c2 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 3435859..c0c387b 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 ae2b55b..7da4f42 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 1981e62..d122e78 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 {