mu4e: improve mu4e-contact-rewrite-function

Allow mu4e-contact-rewrite-function to return nil to remove a
contact. Improve documentation.
This commit is contained in:
djcb 2015-12-26 13:26:26 +02:00
parent 08540628e0
commit 6fa9556aa8
3 changed files with 53 additions and 32 deletions

View File

@ -626,6 +626,17 @@ process."
(t (format "\"%s\"" ph)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defsubst mu4e~process-contact (contact)
"Process CONTACT, possibly rewriting it, or return nil if
should be removed."
(when mu4e-contact-rewrite-function
(setq contact (funcall mu4e-contact-rewrite-function contact)))
(when contact
(let ((name (plist-get contact :name))
(mail (plist-get contact :mail)))
(unless (and mail (string-match mu4e-compose-complete-ignore-address-regexp mail))
(if name (format "%s <%s>" (mu4e~rfc822-quoteit name) mail) mail)))))
;; start and stopping
(defun mu4e~fill-contacts (contacts)
"We receive a list of contacts, which each contact of the form
@ -634,9 +645,14 @@ and fill the list `mu4e~contacts-for-completion' with it, with
each element looking like
name <email>
This is used by the completion function in mu4e-compose."
(setq mu4e~contact-list contacts)
(setq
mu4e~contact-list contacts
mu4e~contacts-for-completion nil)
(let ((lst)
;; sort by the frequency (descending), then timestamp (descending)
;; sort by the frequency (ascending), then timestamp (ascending)
;; note -- this the opposite order we'd want, but the
;; `push' below reverses the order
;; FIXME: sadly, the emacs completion subsystem re-sorts the list
;; before showing candidates, so this doesn't do anything useful yet.
(contacts (sort contacts
@ -649,18 +665,8 @@ This is used by the completion function in mu4e-compose."
(< tstamp1 tstamp2)
(< freq1 freq2)))))))
(dolist (contact contacts)
(let* ((contact
(if mu4e-contact-rewrite-function
(funcall mu4e-contact-rewrite-function contact) contact))
(name (plist-get contact :name))
(mail (plist-get contact :mail)))
(when mail
(unless ;; ignore some address ('noreply' etc.)
(and mu4e-compose-complete-ignore-address-regexp
(string-match mu4e-compose-complete-ignore-address-regexp mail))
(add-to-list 'lst
(if name (format "%s <%s>" (mu4e~rfc822-quoteit name) mail) mail))))))
(setq mu4e~contacts-for-completion lst)
(let ((contact (mu4e~process-contact contact)))
(when contact (push contact mu4e~contacts-for-completion))))
(mu4e-index-message "Contacts received: %d"
(length mu4e~contacts-for-completion))))

View File

@ -1,6 +1,6 @@
;;; mu4e-vars.el -- part of mu4e, the mu mail user agent
;;
;; Copyright (C) 2011-2013 Dirk-Jan C. Binnema
;; Copyright (C) 2011-2015 Dirk-Jan C. Binnema
;; Author: Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
;; Maintainer: Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
@ -305,10 +305,17 @@ their canonical counterpart; useful as an example."
(list :name name :mail mail)))
(defcustom mu4e-contact-rewrite-function nil
"Function to be used for when processing contacts and rewrite
them, for example you may use this for correcting typo's, changed
names and adapting addresses or names to company policies. As an
example of this, see `mu4e-contact-identity'."
"Either nil or a function to be used for when processing
contacts and rewrite them or remove them altogether.
If the function receives the contact as a list of the form
(:name NAME :mail EMAIL)
(other properties may be there as well)
The function should return either:
- nil: remove this contact
- a possible rewritten cell (:name NAME :mail EMAIL), or simply return
the functions parameter."
:type 'function
:group 'mu4e-compose)

View File

@ -2791,23 +2791,31 @@ point. Requires the 'formail' tool from procmail."
@node Contact functions
@section Contact functions
It can be useful to rewrite the contact information that @t{mu4e}
provides, for example to convert them to some standardized format, or
to fix spelling errors.
It can sometimes be useful to rewrite the contact information that
@t{mu4e} provides, for example to convert them to some standardized
format, or to fix spelling errors. And sometimes, you may want to remove
certain contacts altogether.
You can do this by setting @code{mu4e-contact-rewrite-function} to
your function, for example:
For this, @t{mu4e} provides @code{mu4e-contact-rewrite-function}, which
passes each contact to a user-provided function, which is expected to
return either the possibly rewritten contact or @code{nil} to remove the
contact from the list - note that the latter can also be achieved using
@code{mu4e-compose-complete-ignore-address-regexp}.
Let's look at an example.
@lisp
(defun my-rewrite-function (contact)
(let* ((name (plist-get contact :name))
(mail (plist-get contact :mail))
(actual-name
(cond
((string= name "jonh smiht") "John Smith")
;; other replacements
(t name))))
(list :name actual-name :mail mail)))
(let ((name (or (plist-get contact :name) ""))
(mail (plist-get contact :mail)))
(cond
;; jonh smiht --> John Smith
((string= "jonh smiht" name) (list :name "John Smith" :mail mail))
;; remove evilspammer from the contacts list
((string= "evilspammer@@example.com" mail) nil)
;; others stay as the are
(t contact))))
(setq mu4e-contact-rewrite-function 'my-rewrite-function)
@end lisp