~sircmpwn/aerc

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
2 2

[PATCH v3] Add Templates with Parsing

Details
Message ID
<20191025163457.34590-1-sri@vathsan.com>
DKIM signature
missing
Download raw message
Patch: +378 -142
+ Changes NewComposer to return error.
+ Add lib to handle templates using "text/template".
+ Add -T option to following commands
    - compose.
    - reply
    - forward
+ Quoted replies using templates.
+ Forwards as body using templates
+ Added functions to format data avilable to templates.
+ Default templates are installed similar to filters.
+ Templates Config in aerc.conf.
    - Required templates are parsed while loading config.
---
Getting close to completion on this one.
Flag for using templates is added to compose, reply and forward.
Quoting original message in reply and forward use templates.

TODO:
+ Add documentation describing how to write templates.
+ Add documentation for aerc.conf

 Makefile                    |   4 +-
 commands/account/compose.go |  24 +++---
 commands/msg/forward.go     | 156 ++++++++++++++++++------------------
 commands/msg/reply.go       |  72 ++++++++---------
 commands/msg/unsubscribe.go |   6 +-
 config/aerc.conf.in         |  19 +++++
 config/config.go            |  42 +++++++---
 lib/templates/template.go   | 149 ++++++++++++++++++++++++++++++++++
 templates/forward_as_body   |   2 +
 templates/quoted_reply      |   2 +
 widgets/aerc.go             |   7 +-
 widgets/compose.go          |  37 ++++++++-
 12 files changed, 378 insertions(+), 142 deletions(-)
 create mode 100644 lib/templates/template.go
 create mode 100644 templates/forward_as_body
 create mode 100644 templates/quoted_reply

diff --git a/Makefile b/Makefile
index c2e6d1ba4a97..aa6063f94de0 100644
--- a/Makefile
+++ b/Makefile
@@ -58,7 +58,7 @@ clean:

install: all
	mkdir -p $(BINDIR) $(MANDIR)/man1 $(MANDIR)/man5 $(MANDIR)/man7 \
		$(SHAREDIR) $(SHAREDIR)/filters
		$(SHAREDIR) $(SHAREDIR)/filters $(SHAREDIR)/templates
	install -m755 aerc $(BINDIR)/aerc
	install -m644 aerc.1 $(MANDIR)/man1/aerc.1
	install -m644 aerc-search.1 $(MANDIR)/man1/aerc-search.1
@@ -75,6 +75,8 @@ install: all
	install -m755 filters/hldiff $(SHAREDIR)/filters/hldiff
	install -m755 filters/html $(SHAREDIR)/filters/html
	install -m755 filters/plaintext $(SHAREDIR)/filters/plaintext
	install -m644 templates/quote_reply $(SHAREDIR)/templates/quote_reply
	install -m644 templates/forward_as_body $(SHAREDIR)/templates/forward_as_body

RMDIR_IF_EMPTY:=sh -c '\
if test -d $$0 && ! ls -1qA $$0 | grep -q . ; then \
diff --git a/commands/account/compose.go b/commands/account/compose.go
index 039eb92363c6..24e460b318b1 100644
--- a/commands/account/compose.go
+++ b/commands/account/compose.go
@@ -24,13 +24,17 @@ func (Compose) Complete(aerc *widgets.Aerc, args []string) []string {
}

func (Compose) Execute(aerc *widgets.Aerc, args []string) error {
	body, err := buildBody(args)
	body, template, err := buildBody(args)
	if err != nil {
		return err
	}
	acct := aerc.SelectedAccount()
	composer := widgets.NewComposer(aerc,
		aerc.Config(), acct.AccountConfig(), acct.Worker(), nil)

	composer, err := widgets.NewComposer(aerc,
		aerc.Config(), acct.AccountConfig(), acct.Worker(), template, nil)
	if err != nil {
		return err
	}
	tab := aerc.NewTab(composer, "New email")
	composer.OnHeaderChange("Subject", func(subject string) {
		if subject == "" {
@@ -44,11 +48,11 @@ func (Compose) Execute(aerc *widgets.Aerc, args []string) error {
	return nil
}

func buildBody(args []string) (string, error) {
	var body, headers string
	opts, optind, err := getopt.Getopts(args, "H:")
func buildBody(args []string) (string, string, error) {
	var body, template, headers string
	opts, optind, err := getopt.Getopts(args, "H:T:")
	if err != nil {
		return "", err
		return "", "", err
	}
	for _, opt := range opts {
		switch opt.Option {
@@ -60,11 +64,13 @@ func buildBody(args []string) (string, error) {
			} else {
				headers += opt.Value + ":\n"
			}
		case 'T':
			template = opt.Value
		}
	}
	posargs := args[optind:]
	if len(posargs) > 1 {
		return "", errors.New("Usage: compose [-H] [body]")
		return "", template, errors.New("Usage: compose [-H] [body]")
	}
	if len(posargs) == 1 {
		body = posargs[0]
@@ -76,5 +82,5 @@ func buildBody(args []string) (string, error) {
			body = headers + "\n\n"
		}
	}
	return body, nil
	return body, template, nil
}
diff --git a/commands/msg/forward.go b/commands/msg/forward.go
index 494072d07d04..b2c38ae8e1eb 100644
--- a/commands/msg/forward.go
+++ b/commands/msg/forward.go
@@ -1,20 +1,21 @@
package msg

import (
	"bufio"
	"bytes"
	"errors"
	"fmt"
	"git.sr.ht/~sircmpwn/aerc/lib"
	"git.sr.ht/~sircmpwn/aerc/models"
	"git.sr.ht/~sircmpwn/aerc/widgets"
	"git.sr.ht/~sircmpwn/getopt"
	"github.com/emersion/go-message"
	"github.com/emersion/go-message/mail"
	"io"
	"io/ioutil"
	"os"
	"path"
	"strings"

	"github.com/emersion/go-message"
	"github.com/emersion/go-message/mail"

	"git.sr.ht/~sircmpwn/aerc/models"
	"git.sr.ht/~sircmpwn/aerc/widgets"
	"git.sr.ht/~sircmpwn/getopt"
)

type forward struct{}
@@ -32,15 +33,18 @@ func (forward) Complete(aerc *widgets.Aerc, args []string) []string {
}

func (forward) Execute(aerc *widgets.Aerc, args []string) error {
	opts, optind, err := getopt.Getopts(args, "A")
	opts, optind, err := getopt.Getopts(args, "AT:")
	if err != nil {
		return err
	}
	attach := false
	template := ""
	for _, opt := range opts {
		switch opt.Option {
		case 'A':
			attach = true
		case 'T':
			template = opt.Value
		}
	}

@@ -69,10 +73,19 @@ func (forward) Execute(aerc *widgets.Aerc, args []string) error {
		"To":      to,
		"Subject": subject,
	}
	composer := widgets.NewComposer(aerc, aerc.Config(), acct.AccountConfig(),
		acct.Worker(), defaults)

	addTab := func() {
	addTab := func() (*widgets.Composer, error) {
		if template != "" {
			defaults["OriginalFrom"] = models.FormatAddresses(msg.Envelope.From)
			defaults["OriginalDate"] = msg.Envelope.Date.Format("Mon Jan 2, 2006 at 3:04 PM")
		}

		composer, err := widgets.NewComposer(aerc, aerc.Config(), acct.AccountConfig(),
		acct.Worker(), template, defaults)
		if err != nil {
			return nil, err
		}

		tab := aerc.NewTab(composer, subject)
		if to == "" {
			composer.FocusRecipient()
@@ -87,83 +100,68 @@ func (forward) Execute(aerc *widgets.Aerc, args []string) error {
			}
			tab.Content.Invalidate()
		})
		return composer, nil
	}

	if attach {
		forwardAttach(store, composer, msg, addTab)
	} else {
		forwardBodyPart(store, composer, msg, addTab)
	}
	return nil
}

func forwardAttach(store *lib.MessageStore, composer *widgets.Composer,
	msg *models.MessageInfo, addTab func()) {

	store.FetchFull([]uint32{msg.Uid}, func(reader io.Reader) {
		tmpDir, err := ioutil.TempDir("", "aerc-tmp-attachment")
		if err != nil {
			// TODO: Do something with the error
			addTab()
			return
			return err
		}
		tmpFileName := path.Join(tmpDir,
			strings.ReplaceAll(fmt.Sprintf("%s.eml", msg.Envelope.Subject), "/", "-"))
		tmpFile, err := os.Create(tmpFileName)
		if err != nil {
			println(err)
			// TODO: Do something with the error
			addTab()
			return
		}
		strings.ReplaceAll(fmt.Sprintf("%s.eml", msg.Envelope.Subject), "/", "-"))
		store.FetchFull([]uint32{msg.Uid}, func(reader io.Reader) {
			tmpFile, err := os.Create(tmpFileName)
			if err != nil {
				println(err)
				// TODO: Do something with the error
				addTab()
				return
			}

		defer tmpFile.Close()
		io.Copy(tmpFile, reader)
		composer.AddAttachment(tmpFileName)
		composer.OnClose(func(composer *widgets.Composer) {
			os.RemoveAll(tmpDir)
			defer tmpFile.Close()
			io.Copy(tmpFile, reader)
			composer, err := addTab()
			if err != nil {
				return
			}
			composer.AddAttachment(tmpFileName)
			composer.OnClose(func(composer *widgets.Composer) {
				os.RemoveAll(tmpDir)
			})
		})
		addTab()
	})
}

func forwardBodyPart(store *lib.MessageStore, composer *widgets.Composer,
	msg *models.MessageInfo, addTab func()) {
	// TODO: something more intelligent than fetching the 1st part
	// TODO: add attachments!
	store.FetchBodyPart(msg.Uid, []int{1}, func(reader io.Reader) {
		header := message.Header{}
		header.SetText(
			"Content-Transfer-Encoding", msg.BodyStructure.Encoding)
		header.SetContentType(
			msg.BodyStructure.MIMEType, msg.BodyStructure.Params)
		header.SetText("Content-Description", msg.BodyStructure.Description)
		entity, err := message.New(header, reader)
		if err != nil {
			// TODO: Do something with the error
			addTab()
			return
		}
		mreader := mail.NewReader(entity)
		part, err := mreader.NextPart()
		if err != nil {
			// TODO: Do something with the error
			addTab()
			return
	} else {
		if template == "" {
			template = aerc.Config().Templates.Forwards
		}

		pipeout, pipein := io.Pipe()
		scanner := bufio.NewScanner(part.Body)
		go composer.PrependContents(pipeout)
		// TODO: Let user customize the date format used here
		io.WriteString(pipein, fmt.Sprintf("Forwarded message from %s on %s:\n\n",
			msg.Envelope.From[0].Name,
			msg.Envelope.Date.Format("Mon Jan 2, 2006 at 3:04 PM")))
		for scanner.Scan() {
			io.WriteString(pipein, fmt.Sprintf("%s\n", scanner.Text()))
		}
		pipein.Close()
		pipeout.Close()
		addTab()
	})
		// TODO: something more intelligent than fetching the 1st part
		// TODO: add attachments!
		store.FetchBodyPart(msg.Uid, []int{1}, func(reader io.Reader) {
			header := message.Header{}
			header.SetText(
				"Content-Transfer-Encoding", msg.BodyStructure.Encoding)
			header.SetContentType(
				msg.BodyStructure.MIMEType, msg.BodyStructure.Params)
				header.SetText("Content-Description", msg.BodyStructure.Description)
				entity, err := message.New(header, reader)
				if err != nil {
					// TODO: Do something with the error
					addTab()
					return
				}
				mreader := mail.NewReader(entity)
				part, err := mreader.NextPart()
				if err != nil {
					// TODO: Do something with the error
					addTab()
					return
				}
				buf := new(bytes.Buffer)
				buf.ReadFrom(part.Body)
				defaults["Original"] = buf.String()
				addTab()
			})
	}
	return nil
}
diff --git a/commands/msg/reply.go b/commands/msg/reply.go
index 9ef7a3b82206..305ffd645caa 100644
--- a/commands/msg/reply.go
+++ b/commands/msg/reply.go
@@ -1,7 +1,7 @@
package msg

import (
	"bufio"
	"bytes"
	"errors"
	"fmt"
	"io"
@@ -32,16 +32,17 @@ func (reply) Complete(aerc *widgets.Aerc, args []string) []string {
}

func (reply) Execute(aerc *widgets.Aerc, args []string) error {
	opts, optind, err := getopt.Getopts(args, "aq")
	opts, optind, err := getopt.Getopts(args, "aqT:")
	if err != nil {
		return err
	}
	if optind != len(args) {
		return errors.New("Usage: reply [-aq]")
		return errors.New("Usage: reply [-aq -T <template>]")
	}
	var (
		quote    bool
		replyAll bool
		template string
	)
	for _, opt := range opts {
		switch opt.Option {
@@ -49,11 +50,14 @@ func (reply) Execute(aerc *widgets.Aerc, args []string) error {
			replyAll = true
		case 'q':
			quote = true
		case 'T':
			template = opt.Value
		}
	}

	widget := aerc.SelectedTab().(widgets.ProvidesMessage)
	acct := widget.SelectedAccount()

	if acct == nil {
		return errors.New("No account selected")
	}
@@ -116,14 +120,22 @@ func (reply) Execute(aerc *widgets.Aerc, args []string) error {
		"In-Reply-To": msg.Envelope.MessageId,
	}

	composer := widgets.NewComposer(aerc, aerc.Config(),
		acct.AccountConfig(), acct.Worker(), defaults)
	addTab := func() error {
		if template != "" {
			defaults["OriginalFrom"] = models.FormatAddresses(msg.Envelope.From)
			defaults["OriginalDate"] = msg.Envelope.Date.Format("Mon Jan 2, 2006 at 3:04 PM")
		}

	if args[0] == "reply" {
		composer.FocusTerminal()
	}
		composer, err := widgets.NewComposer(aerc, aerc.Config(),
			acct.AccountConfig(), acct.Worker(), template, defaults)
		if err != nil {
			return err
		}

		if args[0] == "reply" {
			composer.FocusTerminal()
		}

	addTab := func() {
		tab := aerc.NewTab(composer, subject)
		composer.OnHeaderChange("Subject", func(subject string) {
			if subject == "" {
@@ -133,27 +145,21 @@ func (reply) Execute(aerc *widgets.Aerc, args []string) error {
			}
			tab.Content.Invalidate()
		})

		return nil
	}

	if quote {
		var (
			path []int
			part *models.BodyStructure
		)
		if len(msg.BodyStructure.Parts) != 0 {
			part, path = findPlaintext(msg.BodyStructure, path)
		}
		if part == nil {
			part = msg.BodyStructure
			path = []int{1}
		if template == "" {
			template = aerc.Config().Templates.QuotedReply
		}

		store.FetchBodyPart(msg.Uid, path, func(reader io.Reader) {
		store.FetchBodyPart(msg.Uid, []int{1}, func(reader io.Reader) {
			header := message.Header{}
			header.SetText(
				"Content-Transfer-Encoding", part.Encoding)
			header.SetContentType(part.MIMEType, part.Params)
			header.SetText("Content-Description", part.Description)
				"Content-Transfer-Encoding", msg.BodyStructure.Encoding)
			header.SetContentType(msg.BodyStructure.MIMEType, msg.BodyStructure.Params)
			header.SetText("Content-Description", msg.BodyStructure.Description)
			entity, err := message.New(header, reader)
			if err != nil {
				// TODO: Do something with the error
@@ -168,25 +174,15 @@ func (reply) Execute(aerc *widgets.Aerc, args []string) error {
				return
			}

			pipeout, pipein := io.Pipe()
			scanner := bufio.NewScanner(part.Body)
			go composer.PrependContents(pipeout)
			// TODO: Let user customize the date format used here
			io.WriteString(pipein, fmt.Sprintf("On %s %s wrote:\n",
				msg.Envelope.Date.Format("Mon Jan 2, 2006 at 3:04 PM"),
				msg.Envelope.From[0].Name))
			for scanner.Scan() {
				io.WriteString(pipein, fmt.Sprintf("> %s\n", scanner.Text()))
			}
			pipein.Close()
			pipeout.Close()
			buf := new(bytes.Buffer)
			buf.ReadFrom(part.Body)
			defaults["Original"] = buf.String()
			addTab()
		})
		return nil
	} else {
		addTab()
		return addTab()
	}

	return nil
}

func findPlaintext(bs *models.BodyStructure,
diff --git a/commands/msg/unsubscribe.go b/commands/msg/unsubscribe.go
index 15a9411c9c8f..5ffec4652056 100644
--- a/commands/msg/unsubscribe.go
+++ b/commands/msg/unsubscribe.go
@@ -87,13 +87,17 @@ func unsubscribeMailto(aerc *widgets.Aerc, u *url.URL) error {
		"To":      u.Opaque,
		"Subject": u.Query().Get("subject"),
	}
	composer := widgets.NewComposer(
	composer, err := widgets.NewComposer(
		aerc,
		aerc.Config(),
		acct.AccountConfig(),
		acct.Worker(),
		"",
		defaults,
	)
	if err != nil {
		return err
	}
	composer.SetContents(strings.NewReader(u.Query().Get("body")))
	tab := aerc.NewTab(composer, "unsubscribe")
	composer.OnHeaderChange("Subject", func(subject string) {
diff --git a/config/aerc.conf.in b/config/aerc.conf.in
index ec89ff7e9d26..a077365eb88c 100644
--- a/config/aerc.conf.in
+++ b/config/aerc.conf.in
@@ -139,3 +139,22 @@ text/*=awk -f @SHAREDIR@/filters/plaintext
#
# Executed when a new email arrives in the selected folder
new-email=

[templates]
# Templates are used to populate email bodies automatically.
#

# The directory where the template are stored.
#
# default: @SHAREDIR@/templates/
template-dir=@SHAREDIR@/templates/

# The template to be used for quoted replies.
#
# default: quoted_reply
quoted-reply=quoted_reply

# The template to be used for forward as body.
#
# default: forward_as_body
forwards=forward_as_body
diff --git a/config/config.go b/config/config.go
index 133a7f4ed5ca..adbf743d227f 100644
--- a/config/config.go
+++ b/config/config.go
@@ -16,6 +16,8 @@ import (
	"github.com/gdamore/tcell"
	"github.com/go-ini/ini"
	"github.com/kyoh86/xdg"

	"git.sr.ht/~sircmpwn/aerc/lib/templates"
)

type GeneralConfig struct {
@@ -98,16 +100,23 @@ type TriggersConfig struct {
	ExecuteCommand func(command []string) error
}

type TemplateConfig struct {
	TemplateDir string `ini:"template-dir"`
	QuotedReply string `ini:"quoted-reply"`
	Forwards    string `ini:"forwards"`
}

type AercConfig struct {
	Bindings BindingConfig
	Compose  ComposeConfig
	Ini      *ini.File       `ini:"-"`
	Accounts []AccountConfig `ini:"-"`
	Filters  []FilterConfig  `ini:"-"`
	Viewer   ViewerConfig    `ini:"-"`
	Triggers TriggersConfig  `ini:"-"`
	Ui       UIConfig
	General  GeneralConfig
	Bindings  BindingConfig
	Compose   ComposeConfig
	Ini       *ini.File       `ini:"-"`
	Accounts  []AccountConfig `ini:"-"`
	Filters   []FilterConfig  `ini:"-"`
	Viewer    ViewerConfig    `ini:"-"`
	Triggers  TriggersConfig  `ini:"-"`
	Ui        UIConfig
	General   GeneralConfig
	Templates TemplateConfig
}

// Input: TimestampFormat
@@ -305,6 +314,21 @@ func (config *AercConfig) LoadConfig(file *ini.File) error {
			return err
		}
	}
	if templatesSec, err := file.GetSection("templates"); err == nil {
		if err := templatesSec.MapTo(&config.Templates); err != nil {
			return err
		}
		for key, val := range templatesSec.KeysHash() {
			if key == "template-dir" {
				continue
			}
			_, err := templates.ParseTemplateFromFile(
				val, config.Templates.TemplateDir, templates.TestTemplateData())
			if err != nil {
				return err
			}
		}
	}
	return nil
}

diff --git a/lib/templates/template.go b/lib/templates/template.go
new file mode 100644
index 000000000000..412ffc14139d
--- /dev/null
+++ b/lib/templates/template.go
@@ -0,0 +1,149 @@
package templates

import (
	"bytes"
	"errors"
	"net/mail"
	"path"
	"strings"
	"text/template"
	"time"

	"github.com/mitchellh/go-homedir"
)

type TemplateData struct {
	To              []*mail.Address
	Cc              []*mail.Address
	Bcc             []*mail.Address
	From            []*mail.Address
	Date            time.Time
	Subject         string
	// Only available when replying with a quote
	OriginalText    string
	OriginalFrom    []*mail.Address
	OriginalDate    time.Time
}

func TestTemplateData() TemplateData{
	defaults := map[string]string {
		"To": "John Doe <john@example.com>",
		"Cc": "Josh Doe <josh@example.com>",
		"From": "Jane Smith <jane@example.com>",
		"Subject": "This is only a test",
		"OriginalText": "This is only a test text",
		"OriginalFrom": "John Doe <john@example.com>",
		"OriginalDate": time.Now().Format("Mon Jan 2, 2006 at 3:04 PM"),
	}

	return ParseTemplateData(defaults)
}

func ParseTemplateData(defaults map[string]string) TemplateData {
	originalDate, _ := time.Parse("Mon Jan 2, 2006 at 3:04 PM", defaults["OriginalDate"])
	td := TemplateData {
		To: parseAddressList(defaults["To"]),
		Cc: parseAddressList(defaults["Cc"]),
		Bcc: parseAddressList(defaults["Bcc"]),
		From: parseAddressList(defaults["From"]),
		Date: time.Now(),
		Subject: defaults["Subject"],
		OriginalText: defaults["Original"],
		OriginalFrom: parseAddressList(defaults["OriginalFrom"]),
		OriginalDate: originalDate,
	}
	return td
}

func parseAddressList(list string) []*mail.Address {
	addrs, err := mail.ParseAddressList(list)
	if err != nil {
		return nil
	}

	return addrs
}

func wrapLine(text string, lineWidth int) string {
	words := strings.Fields(text)
	if len(words) == 0 {
		return text
	}
	wrapped := words[0]
	spaceLeft := lineWidth - len(wrapped)
	for _, word := range words[1:] {
		if len(word)+1 > spaceLeft {
			wrapped += "\n" + word
			spaceLeft = lineWidth - len(word)
		} else {
			wrapped += " " + word
			spaceLeft -= 1 + len(word)
		}
	}

	return wrapped
}

func wrapText(text string, lineWidth int) string {
	text = strings.Replace(text, "\r\n", "\n", -1)
	lines := strings.Split(text, "\n")
	var wrapped string

	for _, line := range lines {
		wrapped += wrapLine(line, lineWidth) + "\n"
	}
	return wrapped
}

// Wraping lines at 70 so that with the "> " of the quote it is under 72
func quote(text string) string {
	text = strings.Replace(text, "\r\n", "\n", -1)
	lines := strings.Split(text, "\n")
	var quoted string

	for _, line := range lines {
		quoted += "> " + wrapLine(line, 70) + "\n"
	}
	return quoted
}

var templateFuncs = template.FuncMap{
	"quote": quote,
	"wrapText": wrapText,
	"dateFormat": time.Time.Format,
}

func ParseTemplateFromFile(templateName string, templateDir string, data interface{})  ([]byte, error) {
	if templateDir == "" {
		return nil, errors.New("Template Directory is not set.")
	}
	templateFile, err := homedir.Expand(path.Join(templateDir, templateName))
	if err != nil {
		return nil, err
	}
	emailTemplate, err :=
		template.New(templateName).Funcs(templateFuncs).ParseFiles(templateFile)
	if err != nil {
		return nil, err
	}

	var outString bytes.Buffer
	if err := emailTemplate.Execute(&outString, data); err != nil {
		return nil, err
	}
	return outString.Bytes(), nil
}

func ParseTemplate(templateText string, data interface{})  ([]byte, error) {
	emailTemplate, err :=
		template.New("email_template").Funcs(templateFuncs).Parse(templateText)
	if err != nil {
		return nil, err
	}

	var outString bytes.Buffer
	if err := emailTemplate.Execute(&outString, data); err != nil {
		return nil, err
	}
	return outString.Bytes(), nil
}
diff --git a/templates/forward_as_body b/templates/forward_as_body
new file mode 100644
index 000000000000..a487224ff3c0
--- /dev/null
+++ b/templates/forward_as_body
@@ -0,0 +1,2 @@
Forwarded message from {{(index .OriginalFrom 0).Name}} on {{dateFormat .OriginalDate "Mon Jan 2, 2006 at 3:04 PM"}}:
{{wrapText .OriginalText 72}}
diff --git a/templates/quoted_reply b/templates/quoted_reply
new file mode 100644
index 000000000000..ee4e1f7ef5f5
--- /dev/null
+++ b/templates/quoted_reply
@@ -0,0 +1,2 @@
on {{dateFormat .OriginalDate "Mon Jan 2, 2006 at 3:04 PM"}}, {{(index .OriginalFrom 0).Name}} wrote:
{{quote .OriginalText}}
diff --git a/widgets/aerc.go b/widgets/aerc.go
index af51a0f4d3db..d324908b58a5 100644
--- a/widgets/aerc.go
+++ b/widgets/aerc.go
@@ -431,8 +431,11 @@ func (aerc *Aerc) Mailto(addr *url.URL) error {
			defaults[header] = strings.Join(vals, ",")
		}
	}
	composer := NewComposer(aerc, aerc.Config(),
		acct.AccountConfig(), acct.Worker(), defaults)
	composer, err := NewComposer(aerc, aerc.Config(),
		acct.AccountConfig(), acct.Worker(), "", defaults)
	if err != nil {
		return nil
	}
	composer.FocusSubject()
	title := "New email"
	if subj, ok := defaults["Subject"]; ok {
diff --git a/widgets/compose.go b/widgets/compose.go
index 22c58da52ca1..f7825ce777af 100644
--- a/widgets/compose.go
+++ b/widgets/compose.go
@@ -23,6 +23,7 @@ import (
	"github.com/pkg/errors"

	"git.sr.ht/~sircmpwn/aerc/config"
	"git.sr.ht/~sircmpwn/aerc/lib/templates"
	"git.sr.ht/~sircmpwn/aerc/lib/ui"
	"git.sr.ht/~sircmpwn/aerc/worker/types"
)
@@ -53,7 +54,7 @@ type Composer struct {
}

func NewComposer(aerc *Aerc, conf *config.AercConfig,
	acct *config.AccountConfig, worker *types.Worker, defaults map[string]string) *Composer {
	acct *config.AccountConfig, worker *types.Worker, template string, defaults map[string]string) (*Composer, error) {

	if defaults == nil {
		defaults = make(map[string]string)
@@ -62,13 +63,14 @@ func NewComposer(aerc *Aerc, conf *config.AercConfig,
		defaults["From"] = acct.From
	}

	templateData := templates.ParseTemplateData(defaults)
	layout, editors, focusable := buildComposeHeader(
		conf.Compose.HeaderLayout, defaults)

	email, err := ioutil.TempFile("", "aerc-compose-*.eml")
	if err != nil {
		// TODO: handle this better
		return nil
		return nil, err
	}

	c := &Composer{
@@ -86,11 +88,14 @@ func NewComposer(aerc *Aerc, conf *config.AercConfig,
	}

	c.AddSignature()
	if err := c.AddTemplate(template, templateData); err != nil {
		return nil, err
	}

	c.updateGrid()
	c.ShowTerminal()

	return c
	return c, nil
}

func buildComposeHeader(layout HeaderLayout, defaults map[string]string) (
@@ -163,6 +168,32 @@ func (c *Composer) AppendContents(reader io.Reader) {
	c.email.Sync()
}

func (c *Composer) AddTemplate(template string, data interface{}) error {
	if template == "" {
		return nil
	}

	templateText, err := templates.ParseTemplateFromFile(template, c.config.Templates.TemplateDir, data)
	if err != nil {
		return err
	}
	c.PrependContents(bytes.NewReader(templateText))
	return nil
}

func (c *Composer) AddTemplateFromString(template string, data interface{}) error {
	if template == "" {
		return nil
	}

	templateText, err := templates.ParseTemplate(template, data)
	if err != nil {
		return err
	}
	c.PrependContents(bytes.NewReader(templateText))
	return nil
}

func (c *Composer) AddSignature() {
	var signature []byte
	if c.acct.SignatureCmd != "" {
-- 
2.23.0
Details
Message ID
<BY3S9BKLBM6C.1UVF7GQ1WL8FQ@homura>
In-Reply-To
<20191025163457.34590-1-sri@vathsan.com> (view parent)
DKIM signature
missing
Download raw message
This is looking really good, nice work.

The only change I'd like to see right now is making the template dir a
colon-separated list of dirs, rather than just a single dir - so that
the user can have overrides in their home dir but still use the
templates that come with the distribution for others.
Details
Message ID
<BY3SDQY7A5AS.2S32VFV7X9WQQ@enceladus>
In-Reply-To
<BY3S9BKLBM6C.1UVF7GQ1WL8FQ@homura> (view parent)
DKIM signature
missing
Download raw message
on Thu Oct 31, 2019 at 10:35 AM, Drew DeVault wrote:
> This is looking really good, nice work.
Thanks!

> The only change I'd like to see right now is making the template dir a
> colon-separated list of dirs, rather than just a single dir - so that
> the user can have overrides in their home dir but still use the
> templates that come with the distribution for others.
Not a problem. Will implement that soon.

PS: Using the templates for this reply ;)
--
Cheers,
Srivathsan Murali (sri)
Review patch Export thread (mbox)