Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 735FF431FD0 for ; Fri, 27 May 2011 14:41:52 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.799 X-Spam-Level: X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id BOQ8cff8Hsal for ; Fri, 27 May 2011 14:41:50 -0700 (PDT) Received: from mail-bw0-f53.google.com (mail-bw0-f53.google.com [209.85.214.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 59F3B431FB6 for ; Fri, 27 May 2011 14:41:50 -0700 (PDT) Received: by bwg12 with SMTP id 12so1785299bwg.26 for ; Fri, 27 May 2011 14:41:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=gamma; h=domainkey-signature:from:to:subject:in-reply-to:references :user-agent:date:message-id:mime-version:content-type; bh=CNfvavToSxNWzwzeeSEe0BZk0nhTkdt2XV/UKgbnSyk=; b=HlkrYTrFm2jqttA3P2dNaps7cdHy11nPL+8f2cEw2c96TLIWoxqrkS/wd5LWMs8ei0 jkGyPlf5W0EIjy4WvhW3ffnDf0RLHtWDyue6PkuiZSSAwUVm6g0IKSbXxKteIKCtD+YB ON5NCeiYtiBxbqsuDMt8BcRSyn0gMc5HgHZTU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=gamma; h=from:to:subject:in-reply-to:references:user-agent:date:message-id :mime-version:content-type; b=qsdCBFLJrps16aSgADm53y8CfxA7LrWGuTLvKRV9UA1ELik1DnVUU0GP35tTQrE4pI hbHidSv3zQh610F52pimkvsWcyPIW4ukaZZrf/L3gJUibgJXARqTtrIQ9FwAa/C3AMnJ /+hCQSLySXFcATtizsFaJUHGbqqP5HFUMZXC4= Received: by 10.204.138.136 with SMTP id a8mr2267382bku.106.1306532508896; Fri, 27 May 2011 14:41:48 -0700 (PDT) Received: from localhost (dslb-178-004-027-138.pools.arcor-ip.net [178.4.27.138]) by mx.google.com with ESMTPS id z1sm1569944bka.8.2011.05.27.14.41.46 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 27 May 2011 14:41:47 -0700 (PDT) From: Daniel Schoepe To: notmuch@notmuchmail.org Subject: Re: [PATCH] emacs: User-defined sections in notmuch-hello In-Reply-To: <874o4g563y.fsf@gilead.invalid> References: <874o4g563y.fsf@gilead.invalid> User-Agent: Notmuch/0.5-170-g1441ce1 (http://notmuchmail.org) Emacs/23.3.1 (x86_64-pc-linux-gnu) Date: Fri, 27 May 2011 23:41:38 +0200 Message-ID: <87tycf3jot.fsf@gilead.invalid> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="==-=-="; micalg=pgp-sha1; protocol="application/pgp-signature" X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 27 May 2011 21:41:52 -0000 --==-=-= Content-Type: multipart/mixed; boundary="=-=-=" --=-=-= Content-Type: text/plain I forgot to run the test suite with the previous patch, which revealed some minor formatting problems, updated patch in the attachment. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-emacs-User-defined-sections-in-notmuch-hello.patch Content-Transfer-Encoding: quoted-printable From=20bca5a58d7910b6d46a782db787dfe07e2fcf21e1 Mon Sep 17 00:00:00 2001 From: Daniel Schoepe 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. =2D-- emacs/notmuch-hello.el | 328 ++++++++++++++++++++++++++++++++++++++------= ---- emacs/notmuch-lib.el | 22 ++-- 2 files changed, 275 insertions(+), 75 deletions(-) diff --git a/emacs/notmuch-hello.el b/emacs/notmuch-hello.el index 916cda1..40333ae 100644 =2D-- a/emacs/notmuch-hello.el +++ b/emacs/notmuch-hello.el @@ -55,24 +55,115 @@ :type 'boolean :group 'notmuch) =20 =2D(defcustom notmuch-hello-tag-list-make-query nil =2D "Function or string to generate queries for the all tags list. =2D =2DThis variable controls which query results are shown for each tag =2Din the \"all tags\" list. If nil, it will use all messages with =2Dthat tag. If this is set to a string, it is used as a filter for =2Dmessages having that tag (equivalent to \"tag:TAG and (THIS-VARIABLE)\"). =2DFinally this can be a function that will be called for each tag and =2Dshould return a filter for that tag, or nil to hide the tag." =2D :type '(choice (const :tag "All messages" nil) =2D (const :tag "Unread messages" "tag:unread") =2D (const :tag "Custom filter" string) =2D (const :tag "Custom filter function" function)) =2D :group 'notmuch) =2D =2D(defcustom notmuch-hello-hide-tags nil =2D "List of tags to be hidden in the \"all tags\"-section." =2D :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) =20 (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)))) =20 =2D(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-e= mpty) (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-en= try)) + tag-alist))) (widest (cdr tags-and-width)) (count 0) =2D (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)) =2D (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 =2D (string-to-number (notmuch-saved-search-count query))))) + (cdr (assoc name count-alist))))) (if (string=3D formatted-name target) (setq found-target-pos (point-marker))) (widget-create 'push-button @@ -338,24 +452,92 @@ Complete list of currently available key bindings: ;;(setq buffer-read-only t) ) =20 =2D(defun notmuch-hello-generate-tag-alist () =2D "Return an alist from tags to queries to display in the all-tags secti= on." =2D (notmuch-remove-if-not =2D #'cdr =2D (mapcar (lambda (tag) =2D (cons tag =2D (cond =2D ((functionp notmuch-hello-tag-list-make-query) =2D (concat "tag:" tag " and (" =2D (funcall notmuch-hello-tag-list-make-query tag) ")")) =2D ((stringp notmuch-hello-tag-list-make-query) =2D (concat "tag:" tag " and (" =2D notmuch-hello-tag-list-make-query ")")) =2D (t (concat "tag:" tag))))) =2D (notmuch-remove-if-not =2D (lambda (tag) =2D (not (member tag notmuch-hello-hide-tags))) =2D (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." + (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: "=20 + (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 SECTIO= N. + +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 SECTIO= N." + (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))))))) +=09=20=20=20=20=20=20 + +(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))) =20 ;;;###autoload (defun notmuch-hello (&optional no-display) @@ -426,7 +608,30 @@ Complete list of currently available key bindings: (widget-insert " messages.\n")) =20 (let ((found-target-pos nil) =2D (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 +640,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)) =2D (alltags-alist (if notmuch-show-all-tags-list (notmuch-hello-gener= ate-tag-alist))) =2D (alltags-widest (notmuch-hello-longest-label alltags-alist)) =2D (widest (max saved-widest alltags-widest))) + (sections-widest (apply #'max (mapcar #'notmuch-hello-section-get-wi= dest + notmuch-hello-sections))) + (widest (max saved-widest sections-widest))) =20 (when saved-alist (widget-insert "\nSaved searches: ") @@ -453,6 +658,13 @@ Complete list of currently available key bindings: (setq final-target-pos found-target-pos)) (indent-rigidly start (point) notmuch-hello-indent))) =20 + (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 +719,12 @@ Complete list of currently available key bindings: notmuch-hello-recent-searches) (indent-rigidly start (point) notmuch-hello-indent))) =20 =2D (when alltags-alist =2D (widget-insert "\nAll tags: ") =2D (widget-create 'push-button =2D :notify (lambda (widget &rest ignore) =2D (setq notmuch-show-all-tags-list nil) =2D (notmuch-hello-update)) =2D "hide") =2D (widget-insert "\n\n") =2D (let ((start (point))) =2D (setq found-target-pos (notmuch-hello-insert-tags alltags-alist wid= est target)) =2D (if (not final-target-pos) =2D (setq final-target-pos found-target-pos)) =2D (indent-rigidly start (point) notmuch-hello-indent))) =2D =2D (widget-insert "\n") =2D =2D (if (not notmuch-show-all-tags-list) =2D (widget-create 'push-button =2D :notify (lambda (widget &rest ignore) =2D (setq notmuch-show-all-tags-list t) =2D (notmuch-hello-update)) =2D "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)) =20 (let ((start (point))) (widget-insert "\n\n") diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el index d5ca0f4..615abad 100644 =2D-- 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)) =20 +(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))) + ;; =20 ;; 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))))) =20 =2D(defun notmuch-remove-if-not (predicate list) =2D "Return a copy of LIST with all items not satisfying PREDICATE removed= ." =2D (let (out) =2D (while list =2D (when (funcall predicate (car list)) =2D (push (car list) out)) =2D (setq list (cdr list))) =2D (nreverse out))) =2D ; 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 =2D-=20 1.7.5.1 --=-=-=-- --==-=-= Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iQIcBAEBAgAGBQJN4BqSAAoJEIaTAtce+Z+JFAcQAJWyoiJR6hNxbejtfvW9mRox zwLEjuMEQBe1pkumNRANh1apo3Ig8oPX5CFcephRNj/PiRkhlf/+D0TQRP0oYXxZ 122NKhI37EMHBF7X6GLGe51FhGGff2DBgFD8HPbcOGuoDTlIlhgt5a2SFdjPP0kf SWpoJF12aujXyto4tfJuBaC/awFRT+zpcIQmms8u86hyMp5bgKwDskonUqSkqOhv jSyGyehbySqdXwtLjyjb1ba854afBLax/wQsbPEf50ueOD3A+poFVrKjOUh+88Qq EL/iicJNaRIKa/0lWR/CZ755QfKuIKBGVINECMv/azj6Nj6+BsN4WoPcRxjSGgnp BK3QKQ/pOgJYnkPgYi8tctiN5g8dDE2btNM4saaU+f37jDsCz2xIsqe3YBi3sz3C TpTmxvTxA/rKsjn347pluBjisFT0V07B5lXm1j/SnorXt6vdWNc1x8gOyiNaNdtH SJSa/CAZhkLa5BQf/2TGWFV2RQcNU1v2+rauo35C776Pa42Zd66w1F4W5Ys9bwxK 7dDgkE7Af2tU+hxCCOpWrMqrIlRZPmDBmtNFux+kLJOsiuE093TAUSY95zeae/fs QFikNiSstd6TvdRwD3/uxEgXJbDouJP5MDxDbaL77kPfcTMB0yw2J5HcxsOBpm7t +9Azicx+8+iQJCOVMXh0 =05Aj -----END PGP SIGNATURE----- --==-=-=--