[PATCH 2/2] emacs: possibility to customize the rendering of tags
authorDamien Cassou <damien.cassou@gmail.com>
Wed, 6 Feb 2013 14:53:55 +0000 (15:53 +0100)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:53:30 +0000 (09:53 -0800)
68/915aeb04afc4e208608cf4e7025b0e28d4acd3 [new file with mode: 0644]

diff --git a/68/915aeb04afc4e208608cf4e7025b0e28d4acd3 b/68/915aeb04afc4e208608cf4e7025b0e28d4acd3
new file mode 100644 (file)
index 0000000..f233b1b
--- /dev/null
@@ -0,0 +1,370 @@
+Return-Path: <damien.cassou@gmail.com>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 56FB4431FB6\r
+       for <notmuch@notmuchmail.org>; Wed,  6 Feb 2013 06:54:10 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.799\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5\r
+       tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+       FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id eT2flK3b42Lq for <notmuch@notmuchmail.org>;\r
+       Wed,  6 Feb 2013 06:54:08 -0800 (PST)\r
+Received: from mail-wg0-f43.google.com (mail-wg0-f43.google.com\r
+ [74.125.82.43])       (using TLSv1 with cipher RC4-SHA (128/128 bits))        (No client\r
+ certificate requested)        by olra.theworths.org (Postfix) with ESMTPS id\r
+ 9E5EB431FAF   for <notmuch@notmuchmail.org>; Wed,  6 Feb 2013 06:54:07 -0800\r
+ (PST)\r
+Received: by mail-wg0-f43.google.com with SMTP id e12so1144316wge.10\r
+       for <notmuch@notmuchmail.org>; Wed, 06 Feb 2013 06:54:06 -0800 (PST)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+       h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to\r
+       :references:mime-version:content-type:content-transfer-encoding;\r
+       bh=xa8ypnoW75jJBtOX0s0Oxv09Zy5HLrnHMMHZSHv05e8=;\r
+       b=Nl9DSiYV30gcVK2e+blUVaeAw25C8NI45a1j7erFUDb98DoVyCDzZ0NSfFCTFgQTT5\r
+       KFWibnK8JS2pX6CLENw2Z376HegCki4c2/KrJeAUSeeeNxem1DIiEaIBey4NvIvGpj7u\r
+       c31FCUWdcZGKkbmhnCIiqnqt+QIpBVAc988UklRRC2+x5icBncKMyxNd5jYimGcg8YUq\r
+       3WjTu8BKMTiiZPUO9yIN3aWQjtesfMVayMfXpORM1F2o/c2XfClQf7DR0L7EBr+htqnh\r
+       AS94zqNidLzEywk8HUwuQUoliUzPElNZVXoouHya25XnmxVteIjnN7iONzhg82Op+Akx\r
+       ej7A==\r
+X-Received: by 10.180.85.8 with SMTP id d8mr5643285wiz.4.1360162446497;\r
+       Wed, 06 Feb 2013 06:54:06 -0800 (PST)\r
+Received: from luz3.lille.inria.fr ([193.51.236.44])\r
+       by mx.google.com with ESMTPS id df2sm3441008wib.0.2013.02.06.06.54.05\r
+       (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);\r
+       Wed, 06 Feb 2013 06:54:05 -0800 (PST)\r
+From: Damien Cassou <damien.cassou@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 2/2] emacs: possibility to customize the rendering of tags\r
+Date: Wed,  6 Feb 2013 15:53:55 +0100\r
+Message-Id: <1360162435-29506-3-git-send-email-damien.cassou@gmail.com>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <1360162435-29506-1-git-send-email-damien.cassou@gmail.com>\r
+References: <1360162435-29506-1-git-send-email-damien.cassou@gmail.com>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=UTF-8\r
+Content-Transfer-Encoding: 8bit\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Wed, 06 Feb 2013 14:54:10 -0000\r
+\r
+This patch extracts the rendering of tags in notmuch-show to\r
+the notmuch-tag file.\r
+\r
+This file introduces a `notmuch-tag-formats' variable that associates\r
+each tag to a particular format. This variable can be customized\r
+thanks to the work of Austin Clements. For example,\r
+\r
+  '(("unread" (propertize tag 'face '(:foreground "red")))\r
+    ("flagged" (notmuch-tag-format-image tag "star.svg")))\r
+\r
+associates a red foreground to the "unread" tag and a star picture to\r
+the "flagged" tag.\r
+\r
+Signed-off-by: Damien Cassou <damien.cassou@gmail.com>\r
+---\r
+ emacs/notmuch-show.el |    6 +-\r
+ emacs/notmuch-tag.el  |  221 ++++++++++++++++++++++++++++++++++++++++++++++++-\r
+ emacs/notmuch.el      |    5 +-\r
+ 3 files changed, 224 insertions(+), 8 deletions(-)\r
+\r
+diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el\r
+index 1864dd1..bb4bd92 100644\r
+--- a/emacs/notmuch-show.el\r
++++ b/emacs/notmuch-show.el\r
+@@ -362,8 +362,7 @@ operation on the contents of the current buffer."\r
+     (if (re-search-forward "(\\([^()]*\\))$" (line-end-position) t)\r
+       (let ((inhibit-read-only t))\r
+         (replace-match (concat "("\r
+-                               (propertize (mapconcat 'identity tags " ")\r
+-                                           'face 'notmuch-tag-face)\r
++                               (notmuch-tag-format-tags tags)\r
+                                ")"))))))\r
+ \r
+ (defun notmuch-clean-address (address)\r
+@@ -441,8 +440,7 @@ message at DEPTH in the current thread."\r
+           " ("\r
+           date\r
+           ") ("\r
+-          (propertize (mapconcat 'identity tags " ")\r
+-                      'face 'notmuch-tag-face)\r
++          (notmuch-tag-format-tags tags)\r
+           ")\n")\r
+     (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face)))\r
+ \r
+diff --git a/emacs/notmuch-tag.el b/emacs/notmuch-tag.el\r
+index 4fce3a9..2a64d48 100644\r
+--- a/emacs/notmuch-tag.el\r
++++ b/emacs/notmuch-tag.el\r
+@@ -1,5 +1,6 @@\r
+ ;; notmuch-tag.el --- tag messages within emacs\r
+ ;;\r
++;; Copyright © Damien Cassou\r
+ ;; Copyright © Carl Worth\r
+ ;;\r
+ ;; This file is part of Notmuch.\r
+@@ -18,11 +19,229 @@\r
+ ;; along with Notmuch.  If not, see <http://www.gnu.org/licenses/>.\r
+ ;;\r
+ ;; Authors: Carl Worth <cworth@cworth.org>\r
++;;          Damien Cassou <damien.cassou@gmail.com>\r
++;;\r
++;;; Code:\r
++;;\r
+ \r
+-(eval-when-compile (require 'cl))\r
++(require 'cl)\r
+ (require 'crm)\r
+ (require 'notmuch-lib)\r
+ \r
++(defcustom notmuch-tag-formats\r
++  '(("unread" (propertize tag 'face '(:foreground "red")))\r
++    ("flagged" (notmuch-tag-format-image-data tag (notmuch-tag-star-icon))))\r
++  "Custom formats for individual tags.\r
++\r
++This gives a list that maps from tag names to lists of formatting\r
++expressions.  The car of each element gives a tag name and the\r
++cdr gives a list of Elisp expressions that modify the tag.  If\r
++the list is empty, the tag will simply be hidden.  Otherwise,\r
++each expression will be evaluated in order: for the first\r
++expression, the variable `tag' will be bound to the tag name; for\r
++each later expression, the variable `tag' will be bound to the\r
++result of the previous expression.  In this way, each expression\r
++can build on the formatting performed by the previous expression.\r
++The result of the last expression will displayed in place of the\r
++tag.\r
++\r
++For example, to replace a tag with another string, simply use\r
++that string as a formatting expression.  To change the foreground\r
++of a tag to red, use the expression\r
++  (propertize tag 'face '(:foreground \"red\"))\r
++\r
++See also `notmuch-tag-format-image', which can help replace tags\r
++with images."\r
++\r
++  :group 'notmuch-search\r
++  :group 'notmuch-show\r
++  :type '(alist :key-type (string :tag "Tag")\r
++              :extra-offset -3\r
++              :value-type\r
++              (radio :format "%v"\r
++                     (const :tag "Hidden" nil)\r
++                     (set :tag "Modified"\r
++                          (string :tag "Display as")\r
++                          (list :tag "Face" :extra-offset -4\r
++                                (const :format "" :inline t\r
++                                       (propertize tag 'face))\r
++                                (list :format "%v"\r
++                                      (const :format "" quote)\r
++                                      custom-face-edit))\r
++                          (list :format "%v" :extra-offset -4\r
++                                (const :format "" :inline t\r
++                                       (notmuch-tag-format-image-data tag))\r
++                                (choice :tag "Image"\r
++                                        (const :tag "Star"\r
++                                               (notmuch-tag-star-icon))\r
++                                        (const :tag "Empty star"\r
++                                               (notmuch-tag-star-empty-icon))\r
++                                        (const :tag "Tag"\r
++                                               (notmuch-tag-tag-icon))\r
++                                        (string :tag "Custom")))\r
++                          (sexp :tag "Custom")))))\r
++\r
++(defun notmuch-tag-format-image-data (tag data)\r
++  "Replace TAG with image DATA, if available.\r
++\r
++This function returns a propertized string that will display image\r
++DATA in place of TAG.This is designed for use in\r
++`notmuch-tag-formats'.\r
++\r
++DATA is the content of an SVG picture (e.g., as returned by\r
++`notmuch-tag-star-icon')."\r
++  (propertize tag 'display\r
++            `(image :type svg\r
++                    :data ,data\r
++                    :ascent center\r
++                    :mask heuristic)))\r
++\r
++(defun notmuch-tag-star-icon ()\r
++  "Return SVG data representing a star icon.\r
++This can be used with `notmuch-tag-format-image-data'."\r
++  "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r
++<svg\r
++   xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\r
++   xmlns:cc=\"http://creativecommons.org/ns#\"\r
++   xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\r
++   xmlns:svg=\"http://www.w3.org/2000/svg\"\r
++   xmlns=\"http://www.w3.org/2000/svg\"\r
++   version=\"1.1\"\r
++   width=\"16\"\r
++   height=\"16\"\r
++   id=\"svg2\">\r
++  <defs\r
++     id=\"defs4\" />\r
++  <metadata\r
++     id=\"metadata7\">\r
++    <rdf:RDF>\r
++      <cc:Work\r
++         rdf:about=\"\">\r
++        <dc:format>image/svg+xml</dc:format>\r
++        <dc:type\r
++           rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\" />\r
++        <dc:title></dc:title>\r
++      </cc:Work>\r
++    </rdf:RDF>\r
++  </metadata>\r
++  <g\r
++     transform=\"translate(-242.81601,-315.59635)\"\r
++     id=\"layer1\">\r
++    <path\r
++       d=\"m 290.25762,334.31206 -17.64143,-11.77975 -19.70508,7.85447 5.75171,-20.41814 -13.55925,-16.31348 21.19618,-0.83936 11.325,-17.93675 7.34825,19.89939 20.55849,5.22795 -16.65471,13.13786 z\"\r
++       transform=\"matrix(0.2484147,-0.02623394,0.02623394,0.2484147,174.63605,255.37691)\"\r
++       id=\"path2985\"\r
++       style=\"fill:#ffff00;fill-rule:evenodd;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1\" />\r
++  </g>\r
++</svg>")\r
++\r
++(defun notmuch-tag-star-empty-icon ()\r
++  "Return SVG data representing an empty star icon.\r
++This can be used with `notmuch-tag-format-image-data'."\r
++  "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r
++<!-- Created with Inkscape (http://www.inkscape.org/) -->\r
++\r
++<svg\r
++   xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\r
++   xmlns:cc=\"http://creativecommons.org/ns#\"\r
++   xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\r
++   xmlns:svg=\"http://www.w3.org/2000/svg\"\r
++   xmlns=\"http://www.w3.org/2000/svg\"\r
++   version=\"1.1\"\r
++   width=\"16\"\r
++   height=\"16\"\r
++   id=\"svg2\">\r
++  <defs\r
++     id=\"defs4\" />\r
++  <metadata\r
++     id=\"metadata7\">\r
++    <rdf:RDF>\r
++      <cc:Work\r
++         rdf:about=\"\">\r
++        <dc:format>image/svg+xml</dc:format>\r
++        <dc:type\r
++           rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\" />\r
++        <dc:title></dc:title>\r
++      </cc:Work>\r
++    </rdf:RDF>\r
++  </metadata>\r
++  <g\r
++     transform=\"translate(-242.81601,-315.59635)\"\r
++     id=\"layer1\">\r
++    <path\r
++       d=\"m 290.25762,334.31206 -17.64143,-11.77975 -19.70508,7.85447 5.75171,-20.41814 -13.55925,-16.31348 21.19618,-0.83936 11.325,-17.93675 7.34825,19.89939 20.55849,5.22795 -16.65471,13.13786 z\"\r
++       transform=\"matrix(0.2484147,-0.02623394,0.02623394,0.2484147,174.63605,255.37691)\"\r
++       id=\"path2985\"\r
++       style=\"fill:#d6d6d1;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1\" />\r
++  </g>\r
++</svg>")\r
++\r
++(defun notmuch-tag-tag-icon ()\r
++  "Return SVG data representing a tag icon.\r
++This can be used with `notmuch-tag-format-image-data'."\r
++  "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r
++<!-- Created with Inkscape (http://www.inkscape.org/) -->\r
++\r
++<svg\r
++   xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\r
++   xmlns:cc=\"http://creativecommons.org/ns#\"\r
++   xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\r
++   xmlns:svg=\"http://www.w3.org/2000/svg\"\r
++   xmlns=\"http://www.w3.org/2000/svg\"\r
++   version=\"1.1\"\r
++   width=\"16\"\r
++   height=\"16\"\r
++   id=\"svg3805\">\r
++  <defs\r
++     id=\"defs3807\" />\r
++  <metadata\r
++     id=\"metadata3810\">\r
++    <rdf:RDF>\r
++      <cc:Work\r
++         rdf:about=\"\">\r
++        <dc:format>image/svg+xml</dc:format>\r
++        <dc:type\r
++           rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\" />\r
++        <dc:title></dc:title>\r
++      </cc:Work>\r
++    </rdf:RDF>\r
++  </metadata>\r
++  <g\r
++     transform=\"translate(0,-1036.3622)\"\r
++     id=\"layer1\">\r
++    <path\r
++       d=\"m 0.44642857,1040.9336 12.50000043,0 2.700893,3.6161 -2.700893,3.616 -12.50000043,0 z\"\r
++       id=\"rect4321\"\r
++       style=\"fill:#ffff00;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:0.25;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1\" />\r
++  </g>\r
++</svg>")\r
++\r
++(defun notmuch-tag-get-format (tag)\r
++  "Return the format for TAG in `notmuch-tag-formats'."\r
++  (let ((formats (assoc tag notmuch-tag-formats)))\r
++    (cond\r
++     ((null formats)          ;; - Tag not in `notmuch-tag-formats',\r
++      tag)                    ;; the format is then the tag itself.\r
++     ((null (cdr formats))    ;; - Tag was deliberately hidden,\r
++      nil)                    ;; no format must be returned\r
++     (t                               ;; - Tag was found and has formats,\r
++      (let ((tag tag))                ;; we must apply all the formats.\r
++      (dolist (format (cdr formats) tag)\r
++        (setq tag (eval format))))))))\r
++\r
++(defun notmuch-tag-list-get-format (tags)\r
++  (mapconcat #'identity\r
++           ;; nil indicated that the tag was deliberately hidden\r
++           (delq nil (mapcar #'notmuch-tag-get-format tags))\r
++           " "))\r
++\r
++(defun notmuch-tag-format-tags (tags)\r
++  "Return a string representing TAGS with their formats."\r
++  (notmuch-combine-face-text-property-string\r
++   (notmuch-tag-list-get-format tags)\r
++   'notmuch-tag-face\r
++   t))\r
++\r
+ (defcustom notmuch-before-tag-hook nil\r
+   "Hooks that are run before tags of a message are modified.\r
+ \r
+diff --git a/emacs/notmuch.el b/emacs/notmuch.el\r
+index c98a4fe..e58c51d 100644\r
+--- a/emacs/notmuch.el\r
++++ b/emacs/notmuch.el\r
+@@ -797,9 +797,8 @@ non-authors is found, assume that all of the authors match."\r
+     (notmuch-search-insert-authors format-string (plist-get result :authors)))\r
+ \r
+    ((string-equal field "tags")\r
+-    (let ((tags-str (mapconcat 'identity (plist-get result :tags) " ")))\r
+-      (insert (propertize (format format-string tags-str)\r
+-                        'face 'notmuch-tag-face))))))\r
++    (let ((tags (plist-get result :tags)))\r
++      (insert (format format-string (notmuch-tag-format-tags tags)))))))\r
+ \r
+ (defun notmuch-search-show-result (result &optional pos)\r
+   "Insert RESULT at POS or the end of the buffer if POS is null."\r
+-- \r
+1.7.10.4\r
+\r