[PATCH] emacs: Show cleaner addresses during message display.

David Edmondson dme at dme.org
Thu Nov 11 23:58:24 PST 2010


Simplify the display of addresses by setting
`notmuch-show-address-simplication' to:

- 'full: Only the name component of the address, if present, is
  shown,
- 'partial: Addresses are stripped of redundant information (the
  default),
- 'none: Addresses are shown as-is.

`mail-header-parse-address' fails for some addresses, in particular
"undisclosed-recipients:;". Accommodate this by returning the original
address if the parser fails.

Adjust the test results accordingly.
---

Default is now 'partial (most similar to the old behaviour). Allow for
un-parseable addresses. Don't add unnecessary quotes. Oh, and, fix the
test suite.

Carl: You can ignore the notmuch-lkml.el chunk, as you don't have that
yet.

 emacs/notmuch-lkml.el                              |    4 +-
 emacs/notmuch-show.el                              |   65 +++++++++++++++++++-
 .../notmuch-show-thread-maildir-storage            |    6 +-
 3 files changed, 68 insertions(+), 7 deletions(-)

diff --git a/emacs/notmuch-lkml.el b/emacs/notmuch-lkml.el
index 750370b..fc04be6 100644
--- a/emacs/notmuch-lkml.el
+++ b/emacs/notmuch-lkml.el
@@ -29,7 +29,7 @@
 (declare-function notmuch-call-notmuch-process "notmuch" (&rest args))
 (declare-function notmuch-show "notmuch-show" (&rest args))
 (declare-function notmuch-show-strip-re "notmuch-show" (subject))
-(declare-function notmuch-show-clean-address "notmuch-show" (parsed-address))
+(declare-function notmuch-show-clean-address "notmuch-show" (address))
 (declare-function notmuch-show-spaces-n "notmuch-show" (n))
 
 (defcustom notmuch-lkml-author-width 30
@@ -71,7 +71,7 @@
 	     (notmuch-lkml-string-width 
 	      (concat (notmuch-show-spaces-n depth)
 		      (notmuch-show-clean-address
-		       (mail-header-parse-address (plist-get headers :From))))
+		       (plist-get headers :From)))
 	      notmuch-lkml-author-width)
 	     " "
 	     (if (string= notmuch-lkml-previous-subject
diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el
index d8773e6..aa89cfd 100644
--- a/emacs/notmuch-show.el
+++ b/emacs/notmuch-show.el
@@ -26,6 +26,7 @@
 (require 'message)
 (require 'mm-decode)
 (require 'mailcap)
+(require 'mail-parse)
 
 (require 'notmuch-lib)
 (require 'notmuch-query)
@@ -82,6 +83,21 @@ any given message."
 	     notmuch-wash-elide-blank-lines
 	     notmuch-wash-excerpt-citations))
 
+(defcustom notmuch-show-address-simplification 'partial
+  "How should addresses be displayed?
+
+Set `notmuch-show-address-simplication' to:
+
+- 'full: Only the name component of the address, if present, is
+  shown,
+- 'partial: Addresses are stripped of redundant information,
+- 'none: Addresses are shown as-is."
+  :group 'notmuch
+  :type '(choice
+	  (const :tag "Full simplification" full)
+	  (const :tag "Partial simplification" partial)
+	  (const :tag "No simplification" none)))
+
 (defmacro with-current-notmuch-show-message (&rest body)
   "Evaluate body with current buffer set to the text of current message"
   `(save-excursion
@@ -198,12 +214,46 @@ any given message."
 					     'face 'notmuch-tag-face)
 				 ")"))))))
 
+(defun notmuch-show-clean-address (original-address)
+  "Prepare a single email address for display."
+  (let* ((parsed-address (mail-header-parse-address original-address))
+	 (address (car parsed-address))
+	 (name (cdr parsed-address))
+	 (displayed-name name))
+
+    ;; If the parser failed, use the original string.
+    (if (not parsed-address)
+	original-address
+
+      ;; If the address is 'foo at bar.com <foo at bar.com>' then show just
+      ;; 'foo at bar.com'.
+      (when (string= displayed-name address)
+	(setq displayed-name nil))
+
+      (cond
+       ((eq notmuch-show-address-simplification 'full)
+	(if displayed-name
+	    (propertize displayed-name 'help-echo address)
+	  address))
+
+       ((eq notmuch-show-address-simplification 'partial)
+	(if displayed-name
+	    ;; `mail-header-make-address' is enthusiastic about
+	    ;; quoting the displayed name if it contains spaces (which
+	    ;; is visually unappealing) so generate the displayed
+	    ;; string directly here.
+	    (concat displayed-name " <" address ">")
+	  address))
+
+       (t ;; All other settings, but mostly 'none.
+	original-address)))))
+
 (defun notmuch-show-insert-headerline (headers date tags depth)
   "Insert a notmuch style headerline based on HEADERS for a
 message at DEPTH in the current thread."
   (let ((start (point)))
     (insert (notmuch-show-spaces-n depth)
-	    (plist-get headers :From)
+	    (notmuch-show-clean-address (plist-get headers :From))
 	    " ("
 	    date
 	    ") ("
@@ -214,7 +264,18 @@ message at DEPTH in the current thread."
 
 (defun notmuch-show-insert-header (header header-value)
   "Insert a single header."
-  (insert header ": " header-value "\n"))
+  (insert header ": "
+	  (cond
+	   ((or (string= "To" header)
+		(string= "Cc" header)
+		(string= "Bcc" header)
+		(string= "From" header))
+	    (mapconcat 'notmuch-show-clean-address
+		       (mail-header-parse-addresses header-value t)
+		       ", "))
+	   (t
+	    header-value))
+	  "\n"))
 
 (defun notmuch-show-insert-headers (headers)
   "Insert the headers of the current message."
diff --git a/test/emacs.expected-output/notmuch-show-thread-maildir-storage b/test/emacs.expected-output/notmuch-show-thread-maildir-storage
index 086f874..2f18924 100644
--- a/test/emacs.expected-output/notmuch-show-thread-maildir-storage
+++ b/test/emacs.expected-output/notmuch-show-thread-maildir-storage
@@ -92,7 +92,7 @@ http://notmuchmail.org/mailman/listinfo/notmuch
   notmuch mailing list
   notmuch at notmuchmail.org
   http://notmuchmail.org/mailman/listinfo/notmuch
-   "Mikhail Gusarov" <dottedmag at dottedmag.net> (2009-11-17) (inbox unread)
+   Mikhail Gusarov <dottedmag at dottedmag.net> (2009-11-17) (inbox unread)
    Subject: [notmuch] Working with Maildir storage?
    To: notmuch at notmuchmail.org
    Date: Wed, 18 Nov 2009 02:50:48 +0600
@@ -120,7 +120,7 @@ http://notmuchmail.org/mailman/listinfo/notmuch
    Desc: not available
    URL: <http://notmuchmail.org/pipermail/notmuch/attachments/20091118/0e33d964/attachment.pgp>
 
-   "Keith Packard" <keithp at keithp.com> (2009-11-17) (inbox unread)
+   Keith Packard <keithp at keithp.com> (2009-11-17) (inbox unread)
    Subject: [notmuch] Working with Maildir storage?
    To: notmuch at notmuchmail.org
    Date: Tue, 17 Nov 2009 13:24:13 -0800
@@ -167,7 +167,7 @@ http://notmuchmail.org/mailman/listinfo/notmuch
     notmuch mailing list
     notmuch at notmuchmail.org
     http://notmuchmail.org/mailman/listinfo/notmuch
- "Carl Worth" <cworth at cworth.org> (2009-11-18) (inbox unread)
+ Carl Worth <cworth at cworth.org> (2009-11-18) (inbox unread)
  Subject: [notmuch] Working with Maildir storage?
  To: notmuch at notmuchmail.org
  Date: Wed, 18 Nov 2009 02:08:10 -0800
-- 
1.7.2.3



More information about the notmuch mailing list