unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* [PATCH 1/2] emacs: Use pcase in notmuch-search-insert-field
@ 2021-01-07 17:25 David Edmondson
  2021-01-07 17:25 ` [PATCH 2/2] emacs: Allow functions in notmuch-search-result-format David Edmondson
  2021-02-15 21:16 ` [PATCH 1/2] emacs: Use pcase in notmuch-search-insert-field David Bremner
  0 siblings, 2 replies; 4+ messages in thread
From: David Edmondson @ 2021-01-07 17:25 UTC (permalink / raw)
  To: notmuch; +Cc: David Edmondson

Rather than lots of string-equal calls, use the pcase macro.
---
 emacs/notmuch.el | 44 +++++++++++++++++++++++---------------------
 1 file changed, 23 insertions(+), 21 deletions(-)

diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index 83bcee57..efbf5bd0 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -65,7 +65,9 @@
 
 ;;; Code:
 
-(eval-when-compile (require 'cl-lib))
+(eval-when-compile
+  (require 'cl-lib)
+  (require 'pcase))
 
 (require 'mm-view)
 (require 'message)
@@ -822,26 +824,26 @@ non-authors is found, assume that all of the authors match."
       (insert padding))))
 
 (defun notmuch-search-insert-field (field format-string result)
-  (cond
-   ((string-equal field "date")
-    (insert (propertize (format format-string (plist-get result :date_relative))
-			'face 'notmuch-search-date)))
-   ((string-equal field "count")
-    (insert (propertize (format format-string
-				(format "[%s/%s]" (plist-get result :matched)
-					(plist-get result :total)))
-			'face 'notmuch-search-count)))
-   ((string-equal field "subject")
-    (insert (propertize (format format-string
-				(notmuch-sanitize (plist-get result :subject)))
-			'face 'notmuch-search-subject)))
-   ((string-equal field "authors")
-    (notmuch-search-insert-authors
-     format-string (notmuch-sanitize (plist-get result :authors))))
-   ((string-equal field "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)))))))
+  (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

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] emacs: Allow functions in notmuch-search-result-format
  2021-01-07 17:25 [PATCH 1/2] emacs: Use pcase in notmuch-search-insert-field David Edmondson
@ 2021-01-07 17:25 ` David Edmondson
  2021-02-15 21:16 ` [PATCH 1/2] emacs: Use pcase in notmuch-search-insert-field David Bremner
  1 sibling, 0 replies; 4+ messages in thread
From: David Edmondson @ 2021-01-07 17:25 UTC (permalink / raw)
  To: notmuch; +Cc: David Edmondson

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

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] emacs: Use pcase in notmuch-search-insert-field
  2021-01-07 17:25 [PATCH 1/2] emacs: Use pcase in notmuch-search-insert-field David Edmondson
  2021-01-07 17:25 ` [PATCH 2/2] emacs: Allow functions in notmuch-search-result-format David Edmondson
@ 2021-02-15 21:16 ` David Bremner
  2021-02-16  0:24   ` Jonas Bernoulli
  1 sibling, 1 reply; 4+ messages in thread
From: David Bremner @ 2021-02-15 21:16 UTC (permalink / raw)
  To: David Edmondson, notmuch

David Edmondson <dme@dme.org> writes:

> Rather than lots of string-equal calls, use the pcase macro.

1. Alas, it doesn't apply anymore. Looks like too much cleanup from
   Jonas.

2. A glance at the pcase docs suggests the cond wrapping pcase can be
   avoided by using (pred functionp). Of course I've not actually tried
   it.

d

   

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] emacs: Use pcase in notmuch-search-insert-field
  2021-02-15 21:16 ` [PATCH 1/2] emacs: Use pcase in notmuch-search-insert-field David Bremner
@ 2021-02-16  0:24   ` Jonas Bernoulli
  0 siblings, 0 replies; 4+ messages in thread
From: Jonas Bernoulli @ 2021-02-16  0:24 UTC (permalink / raw)
  To: David Bremner, David Edmondson, notmuch

> David Edmondson <dme@dme.org> writes:
> 2. A glance at the pcase docs suggests the cond wrapping pcase can be
>    avoided by using (pred functionp). Of course I've not actually tried
>    it.

(pcase field
  ((pred functionp)
   (insert (funcall field format-string result)))
  ("date"
   ...))

works.  Tried it, because I haven't done this much yet.

     Jonas

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-02-16  0:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-01-07 17:25 [PATCH 1/2] emacs: Use pcase in notmuch-search-insert-field David Edmondson
2021-01-07 17:25 ` [PATCH 2/2] emacs: Allow functions in notmuch-search-result-format David Edmondson
2021-02-15 21:16 ` [PATCH 1/2] emacs: Use pcase in notmuch-search-insert-field David Bremner
2021-02-16  0:24   ` Jonas Bernoulli

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).