mu4e: allow for message body rewriting

Add `mu4e-message-body-rewrite-functions', a list of functions to
transform the message body text. Add a small example to the
documentation.
This commit is contained in:
djcb 2017-01-29 13:21:53 +02:00
parent 67b499f9ed
commit 08f2bb2ef4
2 changed files with 64 additions and 27 deletions

View File

@ -1,6 +1,6 @@
;;; mu4e-message.el -- part of mu4e, the mu mail user agent
;;
;; Copyright (C) 2012-2016 Dirk-Jan C. Binnema
;; Copyright (C) 2012-2017 Dirk-Jan C. Binnema
;; Author: Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
;; Maintainer: Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
@ -76,6 +76,12 @@ it (always show the text version) by using
:type 'integer
:group 'mu4e-view)
(defvar mu4e-message-body-rewrite-functions '(mu4e-message-outlook-cleanup)
"List of functions to transform the message body text. The functions
take two parameters, MSG and TXT, which are the message-plist
and the text, which is the plain-text version, possibly
converted from html and/or transformed by earlier rewrite
functions. ")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defsubst mu4e-message-field-raw (msg field)
@ -164,7 +170,6 @@ This is equivalent to:
(defvar mu4e~message-body-html nil
"Whether the body text uses HTML.")
(defun mu4e~message-use-html-p (msg prefer-html)
"Determine whether we want to use html or text; this is based
on PREFER-HTML and whether the message supports the given
@ -194,33 +199,39 @@ unless PREFER-HTML is non-nil."
(setq mu4e~message-body-html (mu4e~message-use-html-p msg prefer-html))
(let ((body
(if mu4e~message-body-html
(progn
;; use an HTML body
(cond
((stringp mu4e-html2text-command)
(mu4e-html2text-shell msg mu4e-html2text-command))
((functionp mu4e-html2text-command)
(if (help-function-arglist mu4e-html2text-command)
(funcall mu4e-html2text-command msg)
;; oldskool parameterless mu4e-html2text-command
(mu4e~html2text-wrapper mu4e-html2text-command msg)))
(t (mu4e-error "Invalid `mu4e-html2text-command'"))
(setq mu4e~message-body-html t)))
;; use an htmml body
(cond
((stringp mu4e-html2text-command)
(mu4e-html2text-shell msg mu4e-html2text-command))
((functionp mu4e-html2text-command)
(if (help-function-arglist mu4e-html2text-command)
(funcall mu4e-html2text-command msg)
;; oldskool parameterless mu4e-html2text-command
(mu4e~html2text-wrapper mu4e-html2text-command msg)))
(t (mu4e-error "Invalid `mu4e-html2text-command'")))
;; use a text body
(or (mu4e-message-field msg :body-txt) ""))))
;; and finally, remove some crap from the remaining string; it seems
;; esp. outlook lies about its encoding (ie., it says 'iso-8859-1' but
;; really it's 'windows-1252'), thus giving us these funky chars. here, we
;; either remove them, or replace with 'what-was-meant' (heuristically)
(with-temp-buffer
(insert body)
(goto-char (point-min))
(while (re-search-forward "[  ]" nil t)
(replace-match
(cond
((string= (match-string 0) "") "'")
(t ""))))
(buffer-string))))
(dolist (func mu4e-message-body-rewrite-functions)
(setq body (funcall func msg body)))
body))
(defun mu4e-message-outlook-cleanup (msg txt)
"Remove some crap from the remaining string; it seems
esp. outlook lies about its encoding (ie., it says
'iso-8859-1' but really it's 'windows-1252'), thus giving us
these funky chars. here, we either remove them, or replace
with 'what-was-meant' (heuristically)."
(with-temp-buffer
(insert body)
(goto-char (point-min))
(while (re-search-forward "[  ]" nil t)
(replace-match
(cond
((string= (match-string 0) "") "'")
(t ""))))
(buffer-string)))
(defun mu4e-message-contact-field-matches (msg cfield rx)
"Checks whether any of the of the contacts in field

View File

@ -2949,6 +2949,7 @@ Note that:
(mu4e-message-field some-msg :to)
;; => (("Jack" . "jack@@example.com") (nil . "foo@@example.com"))
@end lisp
If you are only looking for a match in this list (e.g., ``Is Jack one of the
recipients of the message?''), there is a convenience function
@code{mu4e-message-contact-field-matches} to make this easy.
@ -2974,6 +2975,31 @@ point. Requires the 'formail' tool from procmail."
(shell-quote-argument (mu4e-message-field-at-point :path))))))
@end lisp
@node Message rewriting
@subsection Rewriting the message body
Message body rewriting allows you to modify the message text that is
presented in the message view. This can be useful if the message needs
special processing, for instance for special filling or cleaning up
encoding artifacts (this is what @t{mu4e} uses this for internally).
To enable this, you can append your rewrite-function to
@code{mu4e-message-body-rewrite-functions}; your function is expected to
take two parameters @code{MSG} and @code{TXT}, which are the
message-plist and the body-text, which could be the result of earlier
transformations, including html->text conversion as per
@code{mu4e-html2-text-command}. The function is expected to return the
transformed text.
As a fairly useless example, suppose we insist on reading @t{mu4e} as
@t{MU4E}:
@lisp
(defun mu4e-to-MU4E-rewrite (msg txt)
(replace-regexp-in-string "mu4e" "MU4E" txt))
(add-to-list 'mu4e-message-body-rewrite-functions 'mu4e-to-MU4E-rewrite t)
@end lisp
@node Contact functions
@section Contact functions