diff options
author | Moritz Poldrack <git@moritz.sh> | 2023-09-01 13:40:56 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-09-18 21:15:34 +0200 |
commit | ad159d5e9bd40dd42ce2bdbcbab30d954a3c4c0a (patch) | |
tree | f67ae66a7aec3fd6c400a31f133e716a2a1c934b /widgets | |
parent | 389d89a9362e2e782f17074331bf85bb579d7466 (diff) | |
download | aerc-ad159d5e9bd40dd42ce2bdbcbab30d954a3c4c0a.tar.gz |
compose: add option for LF-only editors
Some editors only support LF line endings. For these, standard compliant
eml files are not an option.
Add an option compose.lf-editor to translate the eml file to lf
lineendings.
Signed-off-by: Moritz Poldrack <git@moritz.sh>
Acked-by: Robin Jarry <robin@jarry.cc>
Tested-by: Matěj Cepl <mcepl@cepl.eu>
Diffstat (limited to 'widgets')
-rw-r--r-- | widgets/compose.go | 40 |
1 files changed, 31 insertions, 9 deletions
diff --git a/widgets/compose.go b/widgets/compose.go index f7bce6d4..1c0aaab5 100644 --- a/widgets/compose.go +++ b/widgets/compose.go @@ -462,11 +462,18 @@ func (c *Composer) updateCrypto() error { return nil } -func (c *Composer) writeCRLF(reader io.Reader) error { - // .eml files must always use '\r\n' line endings +func (c *Composer) writeEml(reader io.Reader) error { + // .eml files must always use '\r\n' line endings, but some editors + // don't support these, so if they are using one of those, the + // line-endings are transformed + lineEnding := "\r\n" + if config.Compose.LFEditor { + lineEnding = "\n" + } + scanner := bufio.NewScanner(reader) for scanner.Scan() { - _, err := c.email.WriteString(scanner.Text() + "\r\n") + _, err := c.email.WriteString(scanner.Text() + lineEnding) if err != nil { return err } @@ -488,6 +495,11 @@ func (c *Composer) setContents(reader io.Reader) error { if err != nil { return err } + lineEnding := "\r\n" + if config.Compose.LFEditor { + lineEnding = "\n" + } + if c.editHeaders { for _, h := range c.headerOrder() { var value string @@ -506,7 +518,7 @@ func (c *Composer) setContents(reader io.Reader) error { for _, a := range addresses { addr = append(addr, format.AddressForHumans(a)) } - value = strings.Join(addr, ",\r\n\t") + value = strings.Join(addr, ","+lineEnding+"\t") } default: value, err = c.header.Text(h) @@ -516,17 +528,17 @@ func (c *Composer) setContents(reader io.Reader) error { } } key := textproto.CanonicalMIMEHeaderKey(h) - _, err = fmt.Fprintf(c.email, "%s: %s\r\n", key, value) + _, err = fmt.Fprintf(c.email, "%s: %s"+lineEnding, key, value) if err != nil { return err } } - _, err = c.email.WriteString("\r\n") + _, err = c.email.WriteString(lineEnding) if err != nil { return err } } - return c.writeCRLF(reader) + return c.writeEml(reader) } func (c *Composer) appendContents(reader io.Reader) error { @@ -534,7 +546,7 @@ func (c *Composer) appendContents(reader io.Reader) error { if err != nil { return err } - return c.writeCRLF(reader) + return c.writeEml(reader) } func (c *Composer) AppendPart(mimetype string, params map[string]string, body io.Reader) error { @@ -904,7 +916,17 @@ func (c *Composer) parseEmbeddedHeader() (*mail.Header, error) { if err != nil { return nil, errors.Wrap(err, "Seek") } - msg, err := mail.CreateReader(c.email) + + buf := bytes.NewBuffer([]byte{}) + _, err = io.Copy(buf, c.email) + if err != nil { + return nil, fmt.Errorf("mail.ReadMessageCopy: %w", err) + } + if config.Compose.LFEditor { + bytes.ReplaceAll(buf.Bytes(), []byte{'\n'}, []byte{'\r', '\n'}) + } + + msg, err := mail.CreateReader(buf) if errors.Is(err, io.EOF) { // completely empty h := mail.HeaderFromMap(make(map[string][]string)) return &h, nil |