[PATCH] Display extra headers for emacs-mua - cmd line option

Johan Parin johanparin at gmail.com
Fri Nov 22 15:07:29 PST 2019


Add a new flag --extra-headers to notmuch show, in order to let the
user specify displayed headers using `notmuch-message-headers' in the
emacs mua.

The flag will impact which headers are output in
format_headers_sprinter.

The emacs mua will generate the --extra-headers flag based on
notmuch-message-headers.

See this bug report:

  https://notmuchmail.org/pipermail/notmuch/2017/026069.html
---
 doc/man1/notmuch-show.rst |  7 +++++++
 emacs/notmuch-lib.el      | 14 ++++++++++++++
 emacs/notmuch-query.el    |  9 ++++++++-
 emacs/notmuch-show.el     | 10 ----------
 emacs/notmuch-tree.el     | 16 +++++++++++-----
 notmuch-show.c            | 38 ++++++++++++++++++++++++++++++++++++++
 6 files changed, 78 insertions(+), 16 deletions(-)

diff --git a/doc/man1/notmuch-show.rst b/doc/man1/notmuch-show.rst
index becd3e79..9f61acce 100644
--- a/doc/man1/notmuch-show.rst
+++ b/doc/man1/notmuch-show.rst
@@ -190,6 +190,13 @@ Supported options for **show** include
     "text/html" parts, no part with content type "text/html" is included
     in the output.
 
+``--extra-headers=header[,header...]``
+    A comma separated list of extra headers that will be output by
+    **notmuch show** with ``--format=sexp``, if present in the
+    message.
+
+    Default: empty list.
+
 A common use of **notmuch show** is to display a single thread of email
 messages. For this, use a search term of "thread:<thread-id>" as can be
 seen in the first column of output from the **notmuch search** command.
diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el
index 8acad267..50dfb743 100644
--- a/emacs/notmuch-lib.el
+++ b/emacs/notmuch-lib.el
@@ -148,6 +148,20 @@ For example, if you wanted to remove an \"inbox\" tag and add an
   :group 'notmuch-search
   :group 'notmuch-show)
 
+(defconst notmuch-default-message-headers
+  '("Subject" "To" "Cc" "Date")
+  "Default set of headers to be displayed when showing a message")
+
+(defcustom notmuch-message-headers notmuch-default-message-headers
+  "Headers that should be shown in a message, in this order.
+
+For an open message, all of these headers will be made visible
+according to `notmuch-message-headers-visible' or can be toggled
+with `notmuch-show-toggle-visibility-headers'. For a closed message,
+only the first header in the list will be visible."
+  :type '(repeat string)
+  :group 'notmuch-show)
+
 (defvar notmuch-common-keymap
   (let ((map (make-sparse-keymap)))
     (define-key map "?" 'notmuch-help)
diff --git a/emacs/notmuch-query.el b/emacs/notmuch-query.el
index 563e4acf..c834a6d8 100644
--- a/emacs/notmuch-query.el
+++ b/emacs/notmuch-query.el
@@ -30,7 +30,14 @@ A thread is a forest or list of trees. A tree is a two element
 list where the first element is a message, and the second element
 is a possibly empty forest of replies.
 "
-  (let ((args '("show" "--format=sexp" "--format-version=4")))
+  ;; (let ((args '("show" "--format=sexp" "--format-version=4")))
+  (let ((extra-headers
+	  (cl-set-difference notmuch-message-headers notmuch-default-message-headers
+			     :test #'string=))
+	(args '("show" "--format=sexp" "--format-version=4")))
+    (when extra-headers
+      (nconc args (list (concat "--extra-headers="
+				(mapconcat #'identity extra-headers ",")))))
     (if notmuch-show-process-crypto
 	(setq args (append args '("--decrypt=true"))))
     (setq args (append args search-terms))
diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el
index e13ca3d7..069f0184 100644
--- a/emacs/notmuch-show.el
+++ b/emacs/notmuch-show.el
@@ -53,16 +53,6 @@
 (declare-function notmuch-read-query "notmuch" (prompt))
 (declare-function notmuch-draft-resume "notmuch-draft" (id))
 
-(defcustom notmuch-message-headers '("Subject" "To" "Cc" "Date")
-  "Headers that should be shown in a message, in this order.
-
-For an open message, all of these headers will be made visible
-according to `notmuch-message-headers-visible' or can be toggled
-with `notmuch-show-toggle-visibility-headers'. For a closed message,
-only the first header in the list will be visible."
-  :type '(repeat string)
-  :group 'notmuch-show)
-
 (defcustom notmuch-message-headers-visible t
   "Should the headers be visible by default?
 
diff --git a/emacs/notmuch-tree.el b/emacs/notmuch-tree.el
index c00315e8..4f97e632 100644
--- a/emacs/notmuch-tree.el
+++ b/emacs/notmuch-tree.el
@@ -915,14 +915,20 @@ the same as for the function notmuch-tree."
   (let* ((search-args (concat basic-query
 		       (if query-context (concat " and (" query-context ")"))
 		       ))
-	 (message-arg "--entire-thread"))
+	 (message-arg "--entire-thread")
+	 (extra-headers
+	  (cl-set-difference notmuch-message-headers notmuch-default-message-headers
+			     :test #'string=))
+	 (args '("notmuch-tree" (current-buffer) #'notmuch-tree-process-sentinel
+		  "show" "--body=false" "--format=sexp" "--format-version=4")))
+    (when extra-headers
+      (nconc args (list (concat "--extra-headers="
+				(mapconcat #'identity extra-headers ",")))))
+    (nconc args search-args)
     (if (equal (car (process-lines notmuch-command "count" search-args)) "0")
 	(setq search-args basic-query))
     (notmuch-tag-clear-cache)
-    (let ((proc (notmuch-start-notmuch
-		 "notmuch-tree" (current-buffer) #'notmuch-tree-process-sentinel
-		 "show" "--body=false" "--format=sexp" "--format-version=4"
-		 message-arg search-args))
+    (let ((proc (apply #'notmuch-start-notmuch args))
 	  ;; Use a scratch buffer to accumulate partial output.
 	  ;; This buffer will be killed by the sentinel, which
 	  ;; should be called no matter how the process dies.
diff --git a/notmuch-show.c b/notmuch-show.c
index 21792a57..1935c4ab 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -18,11 +18,20 @@
  * Author: Carl Worth <cworth at cworth.org>
  */
 
+#include <string.h>
+#include <stdlib.h>
+
 #include "notmuch-client.h"
 #include "gmime-filter-reply.h"
 #include "sprinter.h"
 #include "zlib-extra.h"
 
+
+/* Extra headers to be output by format_headers_sprinter, if present
+ * in message */
+static const char* extra_headers = NULL;
+
+
 static const char *
 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
 {
@@ -195,6 +204,30 @@ _is_from_line (const char *line)
 	return 0;
 }
 
+/* Output extra headers if specifoed by --extra-headers */
+void
+format_extra_headers_sprinter (sprinter_t *sp, GMimeMessage *message)
+{
+    GMimeHeaderList *header_list;
+    GMimeHeader *header;
+    char *str = strdup(extra_headers);
+    char *tofree = str;
+    char *header_name;
+
+    header_list  = g_mime_object_get_header_list (GMIME_OBJECT(message));
+
+    while ( (header_name = strsep(&str, ",")) != NULL) {
+
+	header = g_mime_header_list_get_header (header_list, header_name);
+	if (header == NULL)
+	    continue;
+
+	sp->map_key (sp, g_mime_header_get_name(header));
+	sp->string (sp, g_mime_header_get_value(header));
+    }
+    free (tofree);
+}
+
 void
 format_headers_sprinter (sprinter_t *sp, GMimeMessage *message,
 			 bool reply, const _notmuch_message_crypto_t *msg_crypto)
@@ -253,6 +286,10 @@ format_headers_sprinter (sprinter_t *sp, GMimeMessage *message,
     } else {
 	sp->map_key (sp, "Date");
 	sp->string (sp, g_mime_message_get_date_string (sp, message));
+
+	/* Output extra headers the user has configured in the database, if any */
+	if (extra_headers != NULL)
+	    format_extra_headers_sprinter (sp, message);
     }
 
     sp->end (sp);
@@ -1193,6 +1230,7 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
 	{ .opt_bool = &params.output_body, .name = "body" },
 	{ .opt_bool = &params.include_html, .name = "include-html" },
 	{ .opt_inherit = notmuch_shared_options },
+	{ .opt_string = &extra_headers, .name = "extra-headers" },
 	{ }
     };
 
-- 
2.21.0 (Apple Git-122)



More information about the notmuch mailing list