From 6fa9556aa80c1c8658b5af348edecc879353a3ad Mon Sep 17 00:00:00 2001 From: djcb Date: Sat, 26 Dec 2015 13:26:26 +0200 Subject: [PATCH] mu4e: improve mu4e-contact-rewrite-function Allow mu4e-contact-rewrite-function to return nil to remove a contact. Improve documentation. --- mu4e/mu4e-utils.el | 34 ++++++++++++++++++++-------------- mu4e/mu4e-vars.el | 17 ++++++++++++----- mu4e/mu4e.texi | 34 +++++++++++++++++++++------------- 3 files changed, 53 insertions(+), 32 deletions(-) diff --git a/mu4e/mu4e-utils.el b/mu4e/mu4e-utils.el index 4005acb8..866cab05 100644 --- a/mu4e/mu4e-utils.el +++ b/mu4e/mu4e-utils.el @@ -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 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)))) diff --git a/mu4e/mu4e-vars.el b/mu4e/mu4e-vars.el index 676b2985..d42a8d32 100644 --- a/mu4e/mu4e-vars.el +++ b/mu4e/mu4e-vars.el @@ -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 ;; Maintainer: Dirk-Jan C. Binnema @@ -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) diff --git a/mu4e/mu4e.texi b/mu4e/mu4e.texi index 673abb79..0b0a1151 100644 --- a/mu4e/mu4e.texi +++ b/mu4e/mu4e.texi @@ -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