Dear all,

I am using Emacs as an external editor for composing e-mails for years
now.

A long time ago I opted for
[post-mode](https://github.com/zedinosaur/post-mode) and it kind of
worked for me.

"Kind of" because I ended up understanding recently that several
small but annoying breakages were actually due to post-mode.
I did some local fixing but then ran into something which I
though I won't fix. Given that post-mode is unmaintained for years
I thought it woud likely be more sustainable to finally depart
from it and choose something more vividly maintained.

I found Emacs' message mode, which I understood as being
state-of-the-art nowadays.

It's working now, but I had to struggle quite a bit to reach
this point and that's what I would like to share here.

Problem: Emacs needs to know whether it's in the header or body to
decide whether it should break lines automatically or not (it doesn't as
long as you are in the headers but does once you are in the body).

To know whether it's in the headers or in the body, Emacs inserts a
separator between headers and body which looks like this:

--text follows this line--

When the e-mails are forged by Emacs itself, it deals with adding this
line in the buffer before the edition and removing it just before the
message gets sent. When used as an external editor, though, things are a
bit different.

So to add the line I added this to my Emacs configuration:

```elisp
(defun my-message-add-header-separator ()
  (save-excursion
    (goto-char (point-min))
    (when (re-search-forward "^$" nil t)
      (forward-line 1)
      (unless (looking-at-p (regexp-quote mail-header-separator))
     (insert mail-header-separator "\n")))))

(add-hook 'message-mode-hook 'my-message-add-header-separator)
```

To remove it, I wrote ~/bin/maileditor which looks like this:

```bash
#!/bin/bash

file="$1"

emacs $file

# Remove header-body delimiter line added by Emacs
awk '!found && /^--text follows this line--$/ {found=1; next} {print}' "$file" 
> "$file.tmp" && mv "$file.tmp" "$file"
```

As a bonus, I also added the following line to my configuration, whose
effect is to place the point at the beginning of the message body
when Emacs is started, rather than on the first header, because
that was what post-mode was doing and I found it quite handy:

```elisp
(add-hook 'message-mode-hook 'message-goto-body)
```

I am sharing this for two reasons:

1. In case others might be interested and find this useful

2. To get feedback, please. Although I sort of can live with the fact
that the content of the Emacs variable mail-header-separator is
duplicated in a shell script, I find the solution a bit cumbersome. I
also assume I am not the first person to run into this and I wonder how
others have delt with the situation. Especially if you use Emacs as an
external editor and do not use message-mode, then I'd like to know which
mode you are using (if any beyond text-mode?) and how hard it
was to make things work as you wish.

Best wishes,

Seb.

Reply via email to