unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: David Edmondson <dme@dme.org>
To: notmuch@notmuchmail.org
Cc: David Edmondson <dme@dme.org>
Subject: [PATCH v2 2/3] emacs: Allow functions in notmuch-search-result-format
Date: Tue, 12 Jan 2021 17:45:56 +0000	[thread overview]
Message-ID: <20210112174557.2229802-3-dme@dme.org> (raw)
In-Reply-To: <20210112174557.2229802-1-dme@dme.org>

If the car of an element in notmuch-search-result-format is a
function, insert the result of calling the function into the buffer.

This allows a user to generate custom fields in the output of a search
result. For example, with:

(defun -notmuch-result-flags (format-string result)
  (let ((tags-to-letters '(("flagged" . "!")
			   ("unread" . "u")
			   ("mine" . "m")
			   ("sent" . "s")
			   ("replied" . "r")))
	(tags (plist-get result :tags)))

    (format format-string
	    (mapconcat (lambda (t2l)
			 (if (member (car t2l) tags)
			     (cdr t2l)
			   " "))
		       tags-to-letters ""))))

(setq notmuch-search-result-format '((-notmuch-result-flags . "%s ")
				     ("date" . "%12s ")
				     ("count" . "%9s ")
				     ("authors" . "%-30s ")
				     ("subject" . "%s ")
				     ("tags" . "(%s)")))

The first few characters on each line of the search result are used to
show information about some significant tags associated with the
thread.
---
 emacs/notmuch.el | 45 +++++++++++++++++++++++++--------------------
 1 file changed, 25 insertions(+), 20 deletions(-)

diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index efbf5bd0..92cb5202 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -824,26 +824,31 @@ non-authors is found, assume that all of the authors match."
       (insert padding))))
 
 (defun notmuch-search-insert-field (field format-string result)
-  (pcase field
-    ("date"
-     (insert (propertize (format format-string (plist-get result :date_relative))
-			 'face 'notmuch-search-date)))
-    ("count"
-     (insert (propertize (format format-string
-				 (format "[%s/%s]" (plist-get result :matched)
-					 (plist-get result :total)))
-			 'face 'notmuch-search-count)))
-    ("subject"
-     (insert (propertize (format format-string
-				 (notmuch-sanitize (plist-get result :subject)))
-			 'face 'notmuch-search-subject)))
-    ("authors"
-     (notmuch-search-insert-authors format-string
-				    (notmuch-sanitize (plist-get result :authors))))
-    ("tags"
-     (let ((tags (plist-get result :tags))
-	   (orig-tags (plist-get result :orig-tags)))
-       (insert (format format-string (notmuch-tag-format-tags tags orig-tags)))))))
+  (cond
+   ((functionp field)
+    (insert (funcall field format-string result)))
+
+   ((stringp field)
+    (pcase field
+      ("date"
+       (insert (propertize (format format-string (plist-get result :date_relative))
+			   'face 'notmuch-search-date)))
+      ("count"
+       (insert (propertize (format format-string
+				   (format "[%s/%s]" (plist-get result :matched)
+					   (plist-get result :total)))
+			   'face 'notmuch-search-count)))
+      ("subject"
+       (insert (propertize (format format-string
+				   (notmuch-sanitize (plist-get result :subject)))
+			   'face 'notmuch-search-subject)))
+      ("authors"
+       (notmuch-search-insert-authors format-string
+				      (notmuch-sanitize (plist-get result :authors))))
+      ("tags"
+       (let ((tags (plist-get result :tags))
+	     (orig-tags (plist-get result :orig-tags)))
+	 (insert (format format-string (notmuch-tag-format-tags tags orig-tags)))))))))
 
 (defun notmuch-search-show-result (result pos)
   "Insert RESULT at POS."
-- 
2.29.2

  parent reply	other threads:[~2021-01-12 17:46 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-12 17:45 [PATCH v2 0/3] Allow functions in notmuch-*-result-format David Edmondson
2021-01-12 17:45 ` [PATCH v2 1/3] emacs: Use pcase in notmuch-search-insert-field David Edmondson
2021-01-12 17:45 ` David Edmondson [this message]
2021-01-12 17:45 ` [PATCH v2 3/3] emacs: Allow functions in notmuch-{tree,unthreaded}-result-format David Edmondson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210112174557.2229802-3-dme@dme.org \
    --to=dme@dme.org \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).