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 3056A431FD0 for ; Fri, 27 May 2011 11:52:18 -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 CuOUTMaxUCPM for ; Fri, 27 May 2011 11:52:16 -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 0C2F0431FB6 for ; Fri, 27 May 2011 11:52:15 -0700 (PDT) Received: by bwg12 with SMTP id 12so1675023bwg.26 for ; Fri, 27 May 2011 11:52:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=gamma; h=domainkey-signature:from:to:subject:user-agent:date:message-id :mime-version:content-type; bh=2jUg/k42igPzmBrgHNr9gFNLuU5vVxShm85e/9S6QJc=; b=ctlDneiOx5CJYaHd/VcenMUeSuW8x5Ad7SEbhfkF/qOu0qod5Ys3w4N+nNKUHyjIiU tJVP4nhZrxttmejBAC0pDvcdz0Z2Nm5ZXPOEGP7EZ5mBV4hg7puF+HXpGJLhh509ezuE wiFYJ5cbEanwfj2set68LSEpB66/N5O4Ep5LU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=gamma; h=from:to:subject:user-agent:date:message-id:mime-version :content-type; b=a5p5T9f+GSI8hFrBsM1WsM3C2nwUTmd2d/68Pv1Hb73jjYss9PwBypk6lFAjaXHhHq ZvYtGvd4LuWhxZht+9OC/8j7XeR7z06pdSsEjTusir+i8iFyH0jbSDw5kikk04TvWTzC ncYu5QcYR4/8mSrbiyMHrUl9gkTaRW7oJNjCA= Received: by 10.204.26.215 with SMTP id f23mr2126391bkc.140.1306522334463; Fri, 27 May 2011 11:52:14 -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 ek1sm1478731bkb.21.2011.05.27.11.52.11 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 27 May 2011 11:52:12 -0700 (PDT) From: Daniel Schoepe To: notmuch@notmuchmail.org Subject: [PATCH] emacs: User-defined sections in notmuch-hello User-Agent: Notmuch/0.5-155-gd2550df (http://notmuchmail.org) Emacs/23.3.1 (x86_64-pc-linux-gnu) Date: Fri, 27 May 2011 20:52:01 +0200 Message-ID: <874o4g563y.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 18:52:18 -0000 --==-=-= Content-Type: multipart/mixed; boundary="=-=-=" --=-=-= Content-Type: text/plain Content-Transfer-Encoding: quoted-printable From=20the commit message: > 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. >=20=20 > The sections are (hopefully) fully editable using customize. >=20=20 > I have not yet rewritten the saved-searches portion using this > mechanism, to avoid breaking the configurations of many users. It contains a lot of code to make the whole thing play nice with customize, but hey, at least it's (somewhat) user-friendly. :) I'm also not sure if using plists to describe the sections was a particularly (e)lispy way to do this, so comments are appreciated. I'll also work on some tests for this functionality, (if no one has big, structural complaints about the code). Cheers, Daniel --=-=-= 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) iQIcBAEBAgAGBQJN3/LSAAoJEIaTAtce+Z+JTIoQAIY4zu4NmM3sIFPQiifDkdgj 96/YKzqksFv5dSNDYS7l9PH5Jbp60Smq0MV+7GkFNop7FqgYcViJyuncBZx0Llh0 pd28a4Zfg3t/uROGvkccIxDxcByIM58BxbnRMXfrV+oPf4wx3ZmGcZQMcjp/ZM1D pq+8rcszIAp2ApyefF+VgTSS4hv6v9YI2ztuPT1f07Ox86t4WLEvtRg/t3TLiTeu pJtfEgb8blNtIkMH6YNOR97M67DFZ6hinnAy/TtOY5A9cvkbcPg1N6oKuupjfuWF EUDtikkNcy7ceLPBItmcTpcLLMzrLMc1xc1030FAQU5vFi7OLKILJNcVhffjA7+M iDXvzmF2qxKl7fGDmYQhbDNuXeaAldoh3iYOGS3FZxbLQUuV8K1UscgDPtw/AVxk /vYNgTQSB3aUflV/krRdtTrVoYJzQ8cO1HIPzxHnZTaLudVkvzZMztyLLo7sTSY3 dEU+7GNeQmdSWxPrGWKOF+BehGuCaDXSpbG2CuiZAXzbK0owFJyHK3JKWKFQYZgq JpOebXUZHKgEvw1H5KPs5B1brro/y7b0h8ihcGx8dqwaTdRAfX8dVLWFWywDt/0d G8Yy1FmONNHxGCNazeltGeDbrnwSU/Aiaw2sVAGpdBhnY0nyix19hKrP4a1uwFJW L8RLs6eFCk9nPuP9iFO+ =yy4u -----END PGP SIGNATURE----- --==-=-=--