From f4b774146387f147d6e693d789889bfdd817a290 Mon Sep 17 00:00:00 2001 From: Daniel Bridges Date: Thu, 1 Aug 2019 11:29:21 -0700 Subject: [PATCH] Add cc and bcc commands --- commands/compose/cc-bcc.go | 39 +++++++++++++++++++++++++ doc/aerc.1.scd | 4 +++ widgets/compose.go | 58 +++++++++++++++++++++++++++++--------- 3 files changed, 87 insertions(+), 14 deletions(-) create mode 100644 commands/compose/cc-bcc.go diff --git a/commands/compose/cc-bcc.go b/commands/compose/cc-bcc.go new file mode 100644 index 0000000..7bc1f5d --- /dev/null +++ b/commands/compose/cc-bcc.go @@ -0,0 +1,39 @@ +package compose + +import ( + "fmt" + "strings" + + "git.sr.ht/~sircmpwn/aerc/widgets" +) + +type CC struct{} + +func init() { + register(CC{}) +} + +func (_ CC) Aliases() []string { + return []string{"cc", "bcc"} +} + +func (_ CC) Complete(aerc *widgets.Aerc, args []string) []string { + return nil +} + +func (_ CC) Execute(aerc *widgets.Aerc, args []string) error { + if len(args) < 2 { + return fmt.Errorf("Usage: %s ", args[0]) + } + addrs := strings.Join(args[1:], " ") + composer, _ := aerc.SelectedTab().(*widgets.Composer) + + switch args[0] { + case "cc": + composer.AddEditor("Cc", addrs) + case "bcc": + composer.AddEditor("Bcc", addrs) + } + + return nil +} diff --git a/doc/aerc.1.scd b/doc/aerc.1.scd index c158422..725549e 100644 --- a/doc/aerc.1.scd +++ b/doc/aerc.1.scd @@ -216,6 +216,10 @@ message list, the message in the message viewer, etc). Detaches the file with the given path from the composed email. If no path is specified, detaches the first attachment instead. +*cc* , *bcc* + Sets the Cc or Bcc header to the given addresses. If an editor for the header + is not currently visible in the compose window, a new one will be added. + *edit* (Re-) opens your text editor to edit the message in progress. diff --git a/widgets/compose.go b/widgets/compose.go index a0a5259..e2615ed 100644 --- a/widgets/compose.go +++ b/widgets/compose.go @@ -34,9 +34,11 @@ type Composer struct { email *os.File attachments []string grid *ui.Grid + header *ui.Grid review *reviewMessage worker *types.Worker + layout HeaderLayout focusable []ui.DrawableInteractive focused int } @@ -54,38 +56,26 @@ func NewComposer(conf *config.AercConfig, layout, editors, focusable := buildComposeHeader( conf.Compose.HeaderLayout, defaults) - header, headerHeight := layout.grid( - func(header string) ui.Drawable { return editors[header] }, - ) - - grid := ui.NewGrid().Rows([]ui.GridSpec{ - {ui.SIZE_EXACT, headerHeight}, - {ui.SIZE_WEIGHT, 1}, - }).Columns([]ui.GridSpec{ - {ui.SIZE_WEIGHT, 1}, - }) - email, err := ioutil.TempFile("", "aerc-compose-*.eml") if err != nil { // TODO: handle this better return nil } - grid.AddChild(header).At(0, 0) - c := &Composer{ editors: editors, acct: acct, config: conf, defaults: defaults, email: email, - grid: grid, worker: worker, + layout: layout, // You have to backtab to get to "From", since you usually don't edit it focused: 1, focusable: focusable, } + c.updateGrid() c.ShowTerminal() return c @@ -518,6 +508,46 @@ func (c *Composer) NextField() { c.focusable[c.focused].Focus(true) } +// AddEditor appends a new header editor to the compose window. +func (c *Composer) AddEditor(header string, value string) { + if _, ok := c.editors[header]; ok { + c.editors[header].input.Set(value) + return + } + e := newHeaderEditor(header, value) + c.editors[header] = e + c.layout = append(c.layout, []string{header}) + // Insert focus of new editor before terminal editor + c.focusable = append( + c.focusable[:len(c.focusable)-1], + e, + c.focusable[len(c.focusable)-1], + ) + c.updateGrid() +} + +// updateGrid should be called when the underlying header layout is changed. +func (c *Composer) updateGrid() { + header, height := c.layout.grid( + func(h string) ui.Drawable { return c.editors[h] }, + ) + + if c.grid == nil { + c.grid = ui.NewGrid().Columns([]ui.GridSpec{{ui.SIZE_WEIGHT, 1}}) + } + + c.grid.Rows([]ui.GridSpec{ + {ui.SIZE_EXACT, height}, + {ui.SIZE_WEIGHT, 1}, + }) + + if c.header != nil { + c.grid.RemoveChild(c.header) + } + c.header = header + c.grid.AddChild(c.header).At(0, 0) +} + func (c *Composer) reloadEmail() error { name := c.email.Name() c.email.Close()