~sircmpwn/aerc

add close command at global level v2 PROPOSED

Can you add an alias for "abort" which only works when composing a
message? I think the "abort" verb makes more sense than "close" in that
context, and I don't want to needlessly break people's keybindings.
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/aerc/%3C20190809040700.7132-1-adityasri%40ocf.berkeley.edu%3E/mbox | git am -3
Learn more about email & git

[PATCH v2] add close command at global level Export this patch

---
 commands/close.go          | 59 ++++++++++++++++++++++++++++++++++++++
 commands/compose/abort.go  | 33 ---------------------
 commands/msgview/close.go  | 30 -------------------
 commands/terminal/close.go | 30 -------------------
 doc/aerc.1.scd             | 13 +++------
 widgets/aerc.go            |  8 ++++++
 6 files changed, 71 insertions(+), 102 deletions(-)
 create mode 100644 commands/close.go
 delete mode 100644 commands/compose/abort.go
 delete mode 100644 commands/msgview/close.go
 delete mode 100644 commands/terminal/close.go

diff --git a/commands/close.go b/commands/close.go
new file mode 100644
index 0000000..738cb5a
--- /dev/null
+++ b/commands/close.go
@@ -0,0 +1,59 @@
+package commands
+
+import (
+	"errors"
+	"fmt"
+
+	"git.sr.ht/~sircmpwn/aerc/widgets"
+)
+
+type Close struct{}
+
+func init() {
+	register(Close{})
+}
+
+func (_ Close) Aliases() []string {
+	return []string{"close"}
+}
+
+func (_ Close) Complete(aerc *widgets.Aerc, args []string) []string {
+	return nil
+}
+
+func (_ Close) Execute(aerc *widgets.Aerc, args []string) error {
+	if len(args) > 2 {
+		return errors.New("Usage: close [tab name]")
+	} else if len(args) == 1 {
+		return CloseTab(aerc, aerc.SelectedTabName())
+	} else {
+		tabname := args[1]
+		for _, tab := range aerc.TabNames() {
+			if tab == tabname {
+				return CloseTab(aerc, tabname)
+			}
+		}
+		return errors.New(fmt.Sprintf("Tab %s not found", tabname))
+	}
+	return nil
+}
+
+func CloseTab(aerc *widgets.Aerc, tabname string) error {
+	curTabIndex := aerc.SelectedTabIndex()
+	aerc.SelectTab(tabname)
+	switch tab := aerc.SelectedTab().(type) {
+	default:
+		aerc.RemoveTab(tab)
+		return nil
+	case *widgets.Terminal:
+		tab.Close(nil)
+		return nil
+	case *widgets.Composer:
+		aerc.RemoveTab(tab)
+		tab.Close()
+		return nil
+	case *widgets.AccountView:
+		aerc.SelectTabIndex(curTabIndex)
+		return errors.New("Cannot close account tab")
+	}
+}
diff --git a/commands/compose/abort.go b/commands/compose/abort.go
deleted file mode 100644
index 4c121d7..0000000
--- a/commands/compose/abort.go
@@ -1,33 +0,0 @@
-package compose
-
-import (
-	"errors"
-
-	"git.sr.ht/~sircmpwn/aerc/widgets"
-)
-
-type Abort struct{}
-
-func init() {
-	register(Abort{})
-}
-
-func (_ Abort) Aliases() []string {
-	return []string{"abort"}
-}
-
-func (_ Abort) Complete(aerc *widgets.Aerc, args []string) []string {
-	return nil
-}
-
-func (_ Abort) Execute(aerc *widgets.Aerc, args []string) error {
-	if len(args) != 1 {
-		return errors.New("Usage: abort")
-	}
-	composer, _ := aerc.SelectedTab().(*widgets.Composer)
-
-	aerc.RemoveTab(composer)
-	composer.Close()
-
-	return nil
-}
diff --git a/commands/msgview/close.go b/commands/msgview/close.go
deleted file mode 100644
index 6a7eb0a..0000000
--- a/commands/msgview/close.go
@@ -1,30 +0,0 @@
-package msgview
-
-import (
-	"errors"
-
-	"git.sr.ht/~sircmpwn/aerc/widgets"
-)
-
-type Close struct{}
-
-func init() {
-	register(Close{})
-}
-
-func (_ Close) Aliases() []string {
-	return []string{"close"}
-}
-
-func (_ Close) Complete(aerc *widgets.Aerc, args []string) []string {
-	return nil
-}
-
-func (_ Close) Execute(aerc *widgets.Aerc, args []string) error {
-	if len(args) != 1 {
-		return errors.New("Usage: close")
-	}
-	mv, _ := aerc.SelectedTab().(*widgets.MessageViewer)
-	aerc.RemoveTab(mv)
-	return nil
-}
diff --git a/commands/terminal/close.go b/commands/terminal/close.go
deleted file mode 100644
index 8256e67..0000000
--- a/commands/terminal/close.go
@@ -1,30 +0,0 @@
-package terminal
-
-import (
-	"errors"
-
-	"git.sr.ht/~sircmpwn/aerc/widgets"
-)
-
-type Close struct{}
-
-func init() {
-	register(Close{})
-}
-
-func (_ Close) Aliases() []string {
-	return []string{"close"}
-}
-
-func (_ Close) Complete(aerc *widgets.Aerc, args []string) []string {
-	return nil
-}
-
-func (_ Close) Execute(aerc *widgets.Aerc, args []string) error {
-	if len(args) != 1 {
-		return errors.New("Usage: close")
-	}
-	term, _ := aerc.SelectedTab().(*widgets.Terminal)
-	term.Close(nil)
-	return nil
-}
diff --git a/doc/aerc.1.scd b/doc/aerc.1.scd
index 10e6485..bc80b4e 100644
--- a/doc/aerc.1.scd
+++ b/doc/aerc.1.scd
@@ -41,6 +41,10 @@ These commands work in any context.
 	it's treated as an index. If + or - is specified, the number is interpreted
 	as a delta from the selected tab.
 
+*close* [tab name]
+    Closes current tab if no tab name provided, or tab with tab name if
+    argument is provided. Cannot close account tabs.
+
 *exec* <command...>
 	Executes an arbitrary command in the background.
 
@@ -180,9 +184,6 @@ message list, the message in the message viewer, etc).
 
 ## MESSAGE VIEW COMMANDS
 
-*close*
-	Closes the message viewer.
-
 *next* <n>[%], *prev* <n>[%]
 	Selects the next (or previous) message in the message list. If specified as
 	a percentage, the percentage is applied to the number of messages shown on
@@ -206,9 +207,6 @@ message list, the message in the message viewer, etc).
 
 ## MESSAGE COMPOSE COMMANDS
 
-*abort*
-	Close the composor without sending, discarding the message in progress.
-
 *attach* <path>
 	Attaches the file at the given path to the email.
 
@@ -242,9 +240,6 @@ message list, the message in the message viewer, etc).
 
 ## TERMINAL COMMANDS
 
-*close*
-	Closes the terminal.
-
 # LOGGING
 
 Aerc does not log by default, but collecting log output can be useful for
diff --git a/widgets/aerc.go b/widgets/aerc.go
index 5a7914a..2c783d4 100644
--- a/widgets/aerc.go
+++ b/widgets/aerc.go
@@ -244,6 +244,14 @@ func (aerc *Aerc) SelectedTab() ui.Drawable {
 	return aerc.tabs.Tabs[aerc.tabs.Selected].Content
 }
 
+func (aerc *Aerc) SelectedTabName() string {
+	return aerc.tabs.Tabs[aerc.tabs.Selected].Name
+}
+
+func (aerc *Aerc) SelectedTabIndex() int {
+	return aerc.tabs.Selected
+}
+
 func (aerc *Aerc) NewTab(drawable ui.Drawable, name string) *ui.Tab {
 	tab := aerc.tabs.Add(drawable, name)
 	aerc.tabs.Select(len(aerc.tabs.Tabs) - 1)
-- 
2.22.0
Can you add an alias for "abort" which only works when composing a
message? I think the "abort" verb makes more sense than "close" in that
context, and I don't want to needlessly break people's keybindings.
View this thread in the archives