Control of alternatives

64 views
Skip to first unread message

Magnus Therning

unread,
May 10, 2017, 3:12:56 AM5/10/17
to mu-discuss
I receive quite a lot of email in multipart/alternative format.
Currently mu4e prefers to show the HTML part (when available), which
results in the following questions:

1. How can I configure what part mu4e prefers? (I've so far had no luck
with 'mm-discouraged-alternatives and 'mm-automatic-display, so I'm
guessing mu4e has some other way of configuring this.)

2. How can I switch between parts? (Some emails are sent with dodgy
clients and the text part doesn't reflect the contents of the HTML part
so I need to be able to switch between them.)

/M

--
Magnus Therning OpenPGP: 0x927912051716CE39
email: mag...@therning.org jabber: mag...@therning.org
twitter: magthe http://therning.org/magnus

There's nothing in computing that can't be broken by another level of
indirection.
— Rob Pike
signature.asc

Christophe Troestler

unread,
May 10, 2017, 4:10:25 AM5/10/17
to mu-di...@googlegroups.com

On 2017-05-10, Magnus Therning wrote:
>
> I receive quite a lot of email in multipart/alternative format.
> Currently mu4e prefers to show the HTML part (when available), which
> results in the following questions:
>
> 1. How can I configure what part mu4e prefers? (I've so far had no
> luck with 'mm-discouraged-alternatives and 'mm-automatic-display, so
> I'm guessing mu4e has some other way of configuring this.)

(setq mu4e-view-prefer-html t)
(setq mu4e-view-prefer-html nil)

> 2. How can I switch between parts? (Some emails are sent with dodgy
> clients and the text part doesn't reflect the contents of the HTML
> part so I need to be able to switch between them.)

Usually, the HTML part is more complete. I use shr to display the HTML
part which is slow but the rendering is good (whatever the color
background). I use the following code:

(setq mu4e-html2text-command #'my/shr-mu4e-html2text-command)

(defadvice shr-colorize-region (before default-bg-color compile
activate)
"Use the default background color."
(setq bg nil))

(defun my/shr-mu4e-html2text-command (msg)
;; Use the (private) variable `mu4e~view-msg' for `msg'.
(let ((shr-color-visible-luminance-min 65)
;(shr-use-fonts nil)
(shr-blocked-images "https?://.*"); No web access
(shr-inhibit-images nil)
(shr-width nil)
(html (or (mu4e-message-field msg :body-html) ""))
(attachments (remove-if (lambda (part)
(or (null (plist-get
part :attachment))
(null (plist-get part :cid))))
(mu4e-message-field msg :parts)))
shr-content-function)
(when (and (display-images-p) mu4e-view-show-images)
(defun my/shr-content-function (cid)
(let* ((a (find cid attachments
:test (lambda(c a) (string= c (plist-get
a :cid)))))
(fname
(if (plist-get a :temp)
(plist-get a :temp)
(let ((tmp-attachment-name
(concat temporary-file-directory
(plist-get a :name))))
(if (plist-get a :index)
(progn
(mu4e~proc-extract 'save
(mu4e-message-field
msg :docid)
(plist-get a :index)
mu4e-decryption-policy
tmp-attachment-name)
(mu4e-remove-file-later tmp-attachment-name)
tmp-attachment-name)
(mu4e-message "cid %s does not exist" (mu4e~escape
cid))
))))
(ty (plist-get a :mime-type))
(img (ignore-errors
(with-temp-buffer
(mm-disable-multibyte)
(if fname
(insert-file-contents-literally fname))
(buffer-substring-no-properties
(point-min) (point-max))))))
(list img ty)))
(setq shr-content-function #'my/shr-content-function))
(let ((max-specpdl-size 3000))
;; FIXME: have a fallback when an error occurs
(with-temp-buffer
(insert html)
(shr-render-region (point-min) (point-max))
;; (mu4e-rewrite-cid-to-file-links msg)
;; (w3m-region (point-min) (point-max))
;; (w3m-toggle-inline-image t)
;;(whitespace-cleanup)
(delete-trailing-whitespace)
;;(shr-remove-trailing-whitespace (point-min) (point-max))
(or (buffer-string) "")))))

(when (eq mu4e-html2text-command 'my/shr-mu4e-html2text-command)
;;(define-key mu4e-view-mode-map [down-mouse-1] 'shr-browse-url)
(define-key mu4e-view-mode-map [down-mouse-1]
'mu4e~view-browse-url-from-binding)
(define-key mu4e-view-mode-map [mouse-1] nil)
(define-key mu4e-view-clickable-urls-keymap [mouse-1] nil))

Hope it helps,
C.

Magnus Therning

unread,
May 10, 2017, 8:03:02 AM5/10/17
to mu-di...@googlegroups.com

Christophe Troestler <christophe...@gmail.com> writes:

> On 2017-05-10, Magnus Therning wrote:
>>
>> I receive quite a lot of email in multipart/alternative format. Currently mu4e
>> prefers to show the HTML part (when available), which results in the following
>> questions:
>>
>> 1. How can I configure what part mu4e prefers? (I've so far had no luck with
>> 'mm-discouraged-alternatives and 'mm-automatic-display, so I'm guessing mu4e
>> has some other way of configuring this.)
>
> (setq mu4e-view-prefer-html t)
> (setq mu4e-view-prefer-html nil)
>
>> 2. How can I switch between parts? (Some emails are sent with dodgy clients
>> and the text part doesn't reflect the contents of the HTML part so I need to
>> be able to switch between them.)
>
> Usually, the HTML part is more complete. I use shr to display the HTML part
> which is slow but the rendering is good (whatever the color background). I use
> the following code:

Yes, it's sometimes more complete, but it's often far less readable due
to colour choices in the HTML. I'd prefer using the text part, and only
switch to the HTML when I absolutely have to.

Also, I find that I'm very often surprised when seeing the quoted
message when replying to an HTML email. There's frequently a bit of
fixing of line breaks and such needed to make my reply look like it's
written by a (somewhat) sane person.

/M

--
Magnus Therning OpenPGP: 0x927912051716CE39
email: mag...@therning.org jabber: mag...@therning.org
twitter: magthe http://therning.org/magnus

Some people, wanting an escape from their full-time job, think
“I know, I'll contribute to open source.” Now they have two full-time
jobs.
signature.asc

Yuri D'Elia

unread,
May 10, 2017, 8:06:41 AM5/10/17
to mu-di...@googlegroups.com
On Wed, May 10 2017, Christophe Troestler wrote:
> Usually, the HTML part is more complete. I use shr to display the HTML part
> which is slow but the rendering is good (whatever the color background). I use
> the following code:
<snip>

Ah! Helpful!

I actually disabled font coloring entirely via 'shr-use-colors because
very often outlook templates set a white background, and/or use some
version of black for the text, which doesn't respect the current emacs
theme.

Like you said, outlook was also generally so bad at generating the text
part, to the point that setting prefer-html to t with shr generates a
much more readable part anyway.

Magnus Therning

unread,
May 10, 2017, 8:22:12 AM5/10/17
to mu-di...@googlegroups.com
What's 'shr-use-colors? It doesn't turn up as a documented variable in
my emacs setup (spacemacs), and it does sound like it'd be a good thing
to turn off...

/M

--
Magnus Therning OpenPGP: 0x927912051716CE39
email: mag...@therning.org jabber: mag...@therning.org
twitter: magthe http://therning.org/magnus

Some people, when confronted with a problem, think,
“I know, I’ll use MongoDB.” Now their problems are Web Scale.
signature.asc

Christophe Troestler

unread,
May 10, 2017, 9:20:53 AM5/10/17
to mu-di...@googlegroups.com

On 2017-05-10, Magnus Therning wrote:
>
> Yes, it's sometimes more complete, but it's often far less readable
> due to colour choices in the HTML. I'd prefer using the text part, and
> only switch to the HTML when I absolutely have to.

Not the case with the code sent — shr alters the colors if needed so
they stay readable against the background.

> Also, I find that I'm very often surprised when seeing the quoted
> message when replying to an HTML email. There's frequently a bit of
> fixing of line breaks and such needed to make my reply look like it's
> written by a (somewhat) sane person.

M-q is your friend.

Best,
C.

Stig Brautaset

unread,
May 10, 2017, 9:50:13 AM5/10/17
to mu-di...@googlegroups.com

Magnus Therning <mag...@therning.org> writes:

[...]

> What's 'shr-use-colors? It doesn't turn up as a documented variable in
> my emacs setup (spacemacs), and it does sound like it'd be a good thing
> to turn off...

Ditto. I'm wondering if it's available in devel version of Emacs?

Stig

--
; GNU Emacs 25.2.1, Mu4e 0.9.19

Yuri D'Elia

unread,
May 11, 2017, 1:23:19 PM5/11/17
to mu-di...@googlegroups.com
On Wed, May 10 2017, Magnus Therning wrote:
> What's 'shr-use-colors? It doesn't turn up as a documented variable in
> my emacs setup (spacemacs), and it does sound like it'd be a good thing
> to turn off...

This was introduced in emacs 26 at some point:

(defcustom shr-use-colors t
"If non-nil, respect color specifications in the HTML."
:version "26.1"
:group 'shr
:type 'boolean)

I actually started using this in emacs 25 a while ago: just fetch shr.el
from a development snapshot. There are some minor tweaks to make it run
on an older version of emacs (unfortunately I no longer have the
modified version at hand), but nothing difficult.

Armando Sano

unread,
Jan 15, 2018, 7:32:18 AM1/15/18
to mu-discuss
On Wednesday, May 10, 2017 at 6:10:25 PM UTC+10, Christophe T wrote:
I use shr to display the HTML
part which is slow but the rendering is good (whatever the color
background).  I use the following code:
<snip>

Hope it helps,
C.
 
Thanks for sharing! This is the only way I found to have images rendering inline at their point of insertion. Seems surprisingly difficult to achieve, when this has become fairly standard in most email browsers. I still experience two problems which I would love to solve:

1) the image renders at its full width irrespective of the values of shr-width and shr-max-image-proportion (and mu4e-view-image-max-width but this is probably expected). I can modify the displayed width after the image is rendered by hitting '-' and '+' when point is on it (image-decrease-size and image-increase-size), but that's not ideal. Why is this? Isn't there a way to control the max width within the custom function my/shr-content-function or my/shr-mu4e-html2text-
command?

2) inline images are still repeated at the end of the email (they are displayed a second time at the end of the email body - there, mu4e-view-image-max-width works as expected). This may be related to the pull request https://github.com/djcb/mu/pull/976. Is there any update on this?

Thanks for your insights!
A
Reply all
Reply to author
Forward
0 new messages