Accidentally attached the old file again, sorry for the noise.
From cafc93e3364955cab70885fec740fee87bc3248e Mon Sep 17 00:00:00 2001
From: Daniel Schoepe <daniel.scho...@googlemail.com>
Date: Thu, 26 May 2011 23:03:22 +0200
Subject: [PATCH] emacs: User-defined sections in notmuch-hello

This patch allows the user to define various sections that will
be displayed in notmuch-hello. I tried to keep the section
description flexible, so it allows different queries for the
tag buttons and the counts next to them, as well as hiding items
with zero results.

The sections are (hopefully) fully editable using customize.

I have not yet rewritten the saved-searches portion using this
mechanism, to avoid breaking the configurations of many users.
---
 emacs/notmuch-hello.el                             |  333 ++++++++++++++++----
 emacs/notmuch-lib.el                               |   22 +-
 test/emacs.expected-output/notmuch-hello           |    2 +-
 .../notmuch-hello-no-saved-searches                |    2 +-
 .../emacs.expected-output/notmuch-hello-with-empty |    2 +-
 5 files changed, 282 insertions(+), 79 deletions(-)

diff --git a/emacs/notmuch-hello.el b/emacs/notmuch-hello.el
index 916cda1..f014357 100644
--- a/emacs/notmuch-hello.el
+++ b/emacs/notmuch-hello.el
@@ -55,24 +55,115 @@
   :type 'boolean
   :group 'notmuch)
 
-(defcustom notmuch-hello-tag-list-make-query nil
-  "Function or string to generate queries for the all tags list.
-
-This variable controls which query results are shown for each tag
-in the \"all tags\" list. If nil, it will use all messages with
-that tag. If this is set to a string, it is used as a filter for
-messages having that tag (equivalent to \"tag:TAG and (THIS-VARIABLE)\").
-Finally this can be a function that will be called for each tag and
-should return a filter for that tag, or nil to hide the tag."
-  :type '(choice (const :tag "All messages" nil)
-		 (const :tag "Unread messages" "tag:unread")
-		 (const :tag "Custom filter" string)
-		 (const :tag "Custom filter function" function))
-  :group 'notmuch)
-
-(defcustom notmuch-hello-hide-tags nil
-  "List of tags to be hidden in the \"all tags\"-section."
-  :type '(repeat string)
+(defvar notmuch-hello-section-all-tags
+  (list :title "All tags:"
+	:type 'eachtag)
+  "Default section definition for the \"all tags\" section.")
+
+(defvar notmuch-hello-section-unread-tags
+  (list :title "All tags, unread only:"
+	:type 'eachtag
+	:make-query "tag:unread")
+  "Show and count only unread messages for each tag.")
+
+(defvar notmuch-hello-hidden-sections nil
+  "Sections that are hidden in notmuch-hello")
+
+(defvar notmuch-hello-first-run t
+  "Internal variable for hiding sections with :hidden-on-startup")
+
+(define-widget 'notmuch-section-query-type 'lazy
+  "Customize-type for query items"
+  :tag "Query item"
+  :type '(list (string :tag "Search title")
+	       (string :tag "Query to use")
+	       (choice :tag "Query for message count"
+		       (const :tag "Use previous query" nil)
+		       (string :tag "Different query"))))
+
+(define-widget 'notmuch-section-make-query-type 'lazy
+  "Customize-type for query functions"
+  :tag "Query function"
+  :type '(choice (const :tag "All messages with tag" nil)
+		 (const :tag "Only unread messages" "tag:unread")
+		 (string :tag "Custom filter string")
+		 (function :tag "Custom filter function")))
+
+(define-widget 'notmuch-section-type 'lazy
+  "Customize-type for sections"
+  :tag "Custom section"
+  :type
+  '(plist :options (((const :tag "Title for this section" :title)
+		     string)
+		    ((const :tag "Type of this section" :type)
+		     (choice (const :tag "One item for each tag"
+				    eachtag)
+			     (const :tag "Custom list of searches"
+				    query-list)))
+		    ((const :tag "Function to generate a query, ignored if custom list"
+			    :make-query)
+		     notmuch-section-make-query)
+		    ((const :tag "Function to generate counts, ignored if custom list"
+			    :make-count)
+		     notmuch-section-make-query)
+		    ((const :tag "Function to create titles for tag entries, ignored if custom list"
+			    :make-title)
+		     (choice (const :tag "The tag itself" nil)
+			     (function :tag "Custom function")))
+		    ((const :tag "List of tags to hide, ignored if custom list"
+			    :hide-tags)
+		     (repeat (string :tag "tag")))
+		    ((const :tag "Search queries, only used if custom list"
+			    :items)
+		     (repeat notmuch-section-query))
+		    ((const :tag "Hide if there are no results" :hide-empty)
+		     boolean)
+		    ((const :tag "Where should this be positioned" :position)
+		     (choice (const :tag "Before the search input" before)
+			     (const :tag "After the search input" after))))))
+
+(defcustom notmuch-hello-sections (list notmuch-hello-section-all-tags)
+  "Sections to be displayed in notmuch-hello.
+
+This variable does not include the saved-searches section, which
+is handled separately.
+This variable should be a list of plists with the following
+possible properties:
+
+:title - The title of the section
+:type - Can be 'eachtag or 'query-list, If 'eachtag, generate one
+        item for each tag, otherwise use a fixed set of items.
+The following options are only used when type is 'eachtag:
+:make-query - This can be a query string that is used as a filter for all
+              messages that contain this tag. This can also be a function
+              that is given is tag and should return a filter query. If
+              it returns nil, the tag will be hidden. If nil, all
+              messages having the tag will be used.
+:make-count - Query used to generate message counts next to the labels,
+              same type as :make-query. If this is nil, the same query as
+              above will be used.
+:make-title - Function to generate an alternative title for each tag item.
+              Can be nil.
+:hide-tags - List of tags that will be hidden.
+
+If :type is 'query-list, the following entry must be set:
+:items - List of cons-cells of a title and a query for that item,
+         and/or of lists of the form (TITLE QUERY COUNT), where COUNT is
+         used to generate the message count instead of QUERY. (To make
+         the customize-interface nicer, COUNT is also allowed to be nil).
+
+The following properties are valid for both :type's:
+:hide-empty - If non-nil, hide items that have no matching messages
+:position - If 'before, it will be displayed before the search input form, otherwise
+            after the recent searches.
+:hidden-on-startup - If non-nil, this section will be hidden when notmuch-hello
+                     is first run."
+  :type `(repeat
+	  (choice (const :tag "Show all messages for each tag"
+			 ,notmuch-hello-section-all-tags)
+		  (const :tag "Only unread messages for each tag"
+			 ,notmuch-hello-section-unread-tags)
+		  notmuch-section-type))
   :group 'notmuch)
 
 (defface notmuch-hello-logo-background
@@ -238,12 +329,32 @@ should be. Returns a cons cell `(tags-per-line width)'."
 				   (* tags-per-line (+ 9 1))))
 			   tags-per-line))))
 
-(defun notmuch-hello-insert-tags (tag-alist widest target)
+(defun notmuch-hello-get-count-query (query-item)
+  (if (consp (cdr query-item))
+      (or (third query-item) (second query-item))
+    (cdr query-item)))
+
+(defun notmuch-hello-insert-tags (tag-alist widest target &optional show-empty)
   (let* ((tags-and-width (notmuch-hello-tags-per-line widest))
 	 (tags-per-line (car tags-and-width))
+	 (count-alist
+	  (mapcar
+	   (lambda (tag-entry)
+	     (cons (car tag-entry)
+		   (string-to-number (notmuch-saved-search-count
+				      (notmuch-hello-get-count-query tag-entry)))))
+	   tag-alist))
+	 (tag-alist-filtered
+	  (if show-empty
+	      tag-alist
+	    (notmuch-remove-if-not
+	     (lambda (tag-entry)
+	       ;; filter out entries that have 0 results
+	       (and (not (eq 0 (cdr (assoc (car tag-entry) count-alist)))) tag-entry))
+	     tag-alist)))
 	 (widest (cdr tags-and-width))
 	 (count 0)
-	 (reordered-list (notmuch-hello-reflect tag-alist tags-per-line))
+	 (reordered-list (notmuch-hello-reflect tag-alist-filtered tags-per-line))
 	 ;; Hack the display of the buttons used.
 	 (widget-push-button-prefix "")
 	 (widget-push-button-suffix "")
@@ -254,11 +365,14 @@ should be. Returns a cons cell `(tags-per-line width)'."
 	    ;; (not elem) indicates an empty slot in the matrix.
 	    (when elem
 	      (let* ((name (car elem))
-		     (query (cdr elem))
+		     (query (if (consp (cdr elem))
+				(second elem)
+			      (cdr elem)))
+		     (count-query (notmuch-hello-get-count-query elem))
 		     (formatted-name (format "%s " name)))
 		(widget-insert (format "%8s "
 				       (notmuch-hello-nice-number
-					(string-to-number (notmuch-saved-search-count query)))))
+					(cdr (assoc name count-alist)))))
 		(if (string= formatted-name target)
 		    (setq found-target-pos (point-marker)))
 		(widget-create 'push-button
@@ -338,24 +452,94 @@ Complete list of currently available key bindings:
  ;;(setq buffer-read-only t)
 )
 
-(defun notmuch-hello-generate-tag-alist ()
-  "Return an alist from tags to queries to display in the all-tags section."
-  (notmuch-remove-if-not
-   #'cdr
-   (mapcar (lambda (tag)
-	     (cons tag
-		   (cond
-		    ((functionp notmuch-hello-tag-list-make-query)
-		     (concat "tag:" tag " and ("
-			     (funcall notmuch-hello-tag-list-make-query tag) ")"))
-		    ((stringp notmuch-hello-tag-list-make-query)
-		     (concat "tag:" tag " and ("
-			     notmuch-hello-tag-list-make-query ")"))
-		    (t (concat "tag:" tag)))))
-	   (notmuch-remove-if-not
-	    (lambda (tag)
-	      (not (member tag notmuch-hello-hide-tags)))
-	    (process-lines notmuch-command "search-tags")))))
+(defun notmuch-hello-section-tag-to-title (section tag)
+  (let ((make-title (plist-get section :make-title)))
+    (if make-title
+	(funcall make-title tag)
+      tag)))
+
+(defun notmuch-hello-section-get-widest (section)
+  "Return widest title string in SECTION."
+  (if (member (plist-get section :title) notmuch-hello-hidden-sections)
+      0
+    (case (plist-get section :type)
+      ('eachtag (apply #'max
+		       (mapcar (lambda (tag)
+				 (length (notmuch-hello-section-tag-to-title section tag)))
+			       (notmuch-all-tags))))
+      ('query-list (apply #'max
+			  (mapcar (lambda (entry) (length (first entry)))
+				  (plist-get section :items))))
+      (t (message (concat "Unknown section type: " 
+			  (prin1-to-string (plist-get section :type))))))))
+  
+(defun notmuch-hello-generate-tag-alist (section)
+  "Generate an alist of tags as expected by notmuch-insert-tags for SECTION.
+
+This function handles the special case that section is of type 'eachtag"
+  (let ((make-tag (lambda (make-entry tag)
+		    (cond
+		     ((functionp make-entry)
+		      (let ((query (funcall make-entry tag)))
+			(and query
+			   (concat "tag:" tag " and (" query ")"))))
+		     ((stringp make-entry)
+		      (concat "tag:" tag " and (" make-entry ")"))
+		     (t (concat "tag:" tag))))))
+    ;; remove tags for which make-query returned nil
+    (delq nil
+	  (mapcar (lambda (tag)
+		    (let ((title
+			   (notmuch-hello-section-tag-to-title section tag))
+			  (query (funcall make-tag
+					  (plist-get section :make-query) tag))
+			  (count (plist-get section :make-count)))
+		      (and query
+			 (if count
+			     (list title query (funcall make-tag count tag))
+			   (cons title query)))))
+		  ;; remove tags from :hide-tags
+		  (notmuch-remove-if-not
+		   (lambda (tag)
+		     (not (member tag (plist-get section :hide-tags))))
+		   (notmuch-all-tags))))))
+
+(defun notmuch-hello-section-to-alist (section)
+  "Generate an alist of tags as expected by notmuch-insert-tags for SECTION."
+  (case (plist-get section :type)
+    ('eachtag (notmuch-hello-generate-tag-alist section))
+    ('query-list (plist-get section :items))
+    (t (message (concat "Unknown section type: "
+			(prin1-to-string (plist-get section :type)))))))
+	      
+
+(defun notmuch-hello-insert-section (section widest target)
+"Insert all UI elements for SECTION in the current buffer."
+  (let* ((tags-alist (notmuch-hello-section-to-alist section))
+	 (title (plist-get section :title))
+	 (hidden (member title notmuch-hello-hidden-sections))
+	 (hide-empty (plist-get section :hide-empty))
+	 target-pos)
+    (widget-insert (concat "\n" title " "))
+    (when hidden
+      (widget-create 'push-button
+		     :notify `(lambda (widget &rest ignore)
+				(setq notmuch-hello-hidden-sections
+				      (delq ,title notmuch-hello-hidden-sections))
+				(notmuch-hello-update))
+		     "show"))
+    (when (not hidden)
+      (widget-create 'push-button
+		     :notify `(lambda (widget &rest ignore)
+				(add-to-list 'notmuch-hello-hidden-sections ,title)
+				(notmuch-hello-update))
+		     "hide")
+      (widget-insert "\n\n")
+      (let ((start (point)))
+	(setq target-pos (notmuch-hello-insert-tags tags-alist widest target
+						    (not hide-empty)))
+	(indent-rigidly start (point) notmuch-hello-indent))
+      target-pos)))
 
 ;;;###autoload
 (defun notmuch-hello (&optional no-display)
@@ -426,7 +610,30 @@ Complete list of currently available key bindings:
       (widget-insert " messages.\n"))
 
     (let ((found-target-pos nil)
-	  (final-target-pos nil))
+	  (final-target-pos nil)
+	  (upper-sections (notmuch-remove-if-not
+			   (lambda (section) (eq (plist-get section :position)
+					    'before))
+			   notmuch-hello-sections))
+	  (lower-sections (notmuch-remove-if-not
+			   (lambda (section) (not (eq (plist-get section :position)
+					       'before)))
+			   notmuch-hello-sections)))
+
+      ;; handle sections that should be hidden initially
+      (when notmuch-hello-first-run
+	(mapc (lambda (section)
+		(if (plist-get section :hidden-on-startup)
+		    (add-to-list 'notmuch-hello-hidden-sections
+				 (plist-get section :title))))
+	      notmuch-hello-sections)
+	;; special case for notmuch-show-all-tags-list
+	(when (and (not notmuch-show-all-tags-list)
+		 (member notmuch-hello-section-all-tags notmuch-hello-sections))
+	  (add-to-list 'notmuch-hello-hidden-sections
+		       (plist-get notmuch-hello-section-all-tags :title)))
+	(setq notmuch-hello-first-run nil))
+
       (let* ((saved-alist
 	      ;; Filter out empty saved seaches if required.
 	      (if notmuch-show-empty-saved-searches
@@ -435,9 +642,9 @@ Complete list of currently available key bindings:
 		      if (> (string-to-number (notmuch-saved-search-count (cdr elem))) 0)
 		      collect elem)))
 	     (saved-widest (notmuch-hello-longest-label saved-alist))
-	     (alltags-alist (if notmuch-show-all-tags-list (notmuch-hello-generate-tag-alist)))
-	     (alltags-widest (notmuch-hello-longest-label alltags-alist))
-	     (widest (max saved-widest alltags-widest)))
+	     (sections-widest (apply #'max 0 (mapcar #'notmuch-hello-section-get-widest
+						     notmuch-hello-sections)))
+	     (widest (max saved-widest sections-widest)))
 
 	(when saved-alist
 	  (widget-insert "\nSaved searches: ")
@@ -448,11 +655,19 @@ Complete list of currently available key bindings:
 	  (widget-insert "\n\n")
 	  (setq final-target-pos (point-marker))
 	  (let ((start (point)))
-	    (setq found-target-pos (notmuch-hello-insert-tags saved-alist widest target))
+	    (setq found-target-pos (notmuch-hello-insert-tags saved-alist widest target
+							      notmuch-show-empty-saved-searches))
 	    (if found-target-pos
 		(setq final-target-pos found-target-pos))
 	    (indent-rigidly start (point) notmuch-hello-indent)))
 
+	(mapc (lambda (section)
+		(setq found-target-pos
+		      (notmuch-hello-insert-section section widest target))
+		(unless final-target-pos
+		    (setq final-target-pos found-target-pos)))
+	      upper-sections)
+
 	(widget-insert "\nSearch: ")
 	(setq notmuch-hello-search-bar-marker (point-marker))
 	(widget-create 'editable-field
@@ -507,28 +722,12 @@ Complete list of currently available key bindings:
 		  notmuch-hello-recent-searches)
 	    (indent-rigidly start (point) notmuch-hello-indent)))
 
-	(when alltags-alist
-	  (widget-insert "\nAll tags: ")
-	  (widget-create 'push-button
-			 :notify (lambda (widget &rest ignore)
-				   (setq notmuch-show-all-tags-list nil)
-				   (notmuch-hello-update))
-			 "hide")
-	  (widget-insert "\n\n")
-	  (let ((start (point)))
-	    (setq found-target-pos (notmuch-hello-insert-tags alltags-alist widest target))
-	    (if (not final-target-pos)
-		(setq final-target-pos found-target-pos))
-	    (indent-rigidly start (point) notmuch-hello-indent)))
-
-	(widget-insert "\n")
-
-	(if (not notmuch-show-all-tags-list)
-	    (widget-create 'push-button
-			   :notify (lambda (widget &rest ignore)
-				     (setq notmuch-show-all-tags-list t)
-				     (notmuch-hello-update))
-			   "Show all tags")))
+	(mapc (lambda (section)
+		(setq found-target-pos
+		      (notmuch-hello-insert-section section widest target))
+		(unless final-target-pos
+		    (setq final-target-pos found-target-pos)))
+	      lower-sections))
 
       (let ((start (point)))
 	(widget-insert "\n\n")
diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el
index d5ca0f4..615abad 100644
--- a/emacs/notmuch-lib.el
+++ b/emacs/notmuch-lib.el
@@ -103,6 +103,19 @@ the user hasn't set this variable with the old or new value."
   (kill-new text)
   (message "Stashed: %s" text))
 
+(defun notmuch-all-tags ()
+  "Return a list of all tags in the database"
+  (process-lines "notmuch" "search-tags"))
+
+(defun notmuch-remove-if-not (predicate list)
+  "Return a copy of LIST with all items not satisfying PREDICATE removed."
+  (let (out)
+    (while list
+      (when (funcall predicate (car list))
+        (push (car list) out))
+      (setq list (cdr list)))
+    (nreverse out)))
+
 ;;
 
 ;; XXX: This should be a generic function in emacs somewhere, not
@@ -120,15 +133,6 @@ within the current window."
       (or (memq prop buffer-invisibility-spec)
 	  (assq prop buffer-invisibility-spec)))))
 
-(defun notmuch-remove-if-not (predicate list)
-  "Return a copy of LIST with all items not satisfying PREDICATE removed."
-  (let (out)
-    (while list
-      (when (funcall predicate (car list))
-        (push (car list) out))
-      (setq list (cdr list)))
-    (nreverse out)))
-
 ; This lets us avoid compiling these replacement functions when emacs
 ; is sufficiently new enough to supply them alone. We do the macro
 ; treatment rather than just wrapping our defun calls in a when form
diff --git a/test/emacs.expected-output/notmuch-hello b/test/emacs.expected-output/notmuch-hello
index 64b7e42..f98f132 100644
--- a/test/emacs.expected-output/notmuch-hello
+++ b/test/emacs.expected-output/notmuch-hello
@@ -6,7 +6,7 @@ Saved searches: [edit]
 
 Search:                                                                     
 
-[Show all tags]
+All tags: [show]
 
 	 Type a search query and hit RET to view matching threads.
 		Edit saved searches with the `edit' button.
diff --git a/test/emacs.expected-output/notmuch-hello-no-saved-searches b/test/emacs.expected-output/notmuch-hello-no-saved-searches
index 7f8206a..46172ee 100644
--- a/test/emacs.expected-output/notmuch-hello-no-saved-searches
+++ b/test/emacs.expected-output/notmuch-hello-no-saved-searches
@@ -2,7 +2,7 @@
 
 Search:                                                                     
 
-[Show all tags]
+All tags: [show]
 
 	 Type a search query and hit RET to view matching threads.
 		Edit saved searches with the `edit' button.
diff --git a/test/emacs.expected-output/notmuch-hello-with-empty b/test/emacs.expected-output/notmuch-hello-with-empty
index a9ed630..eb563cc 100644
--- a/test/emacs.expected-output/notmuch-hello-with-empty
+++ b/test/emacs.expected-output/notmuch-hello-with-empty
@@ -6,7 +6,7 @@ Saved searches: [edit]
 
 Search:                                                                     
 
-[Show all tags]
+All tags: [show]
 
 	 Type a search query and hit RET to view matching threads.
 		Edit saved searches with the `edit' button.
-- 
1.7.5.1

Attachment: pgpylMlsF0Ci2.pgp
Description: PGP signature

_______________________________________________
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch

Reply via email to