--- /dev/null
+Return-Path: <markwalters1009@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 A9D62431FD7\r
+ for <notmuch@notmuchmail.org>; Wed, 19 Feb 2014 12:20:44 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.201\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.201 tagged_above=-999 required=5\r
+ tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+ FREEMAIL_ENVFROM_END_DIGIT=1, FREEMAIL_FROM=0.001,\r
+ 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 znDtdC0MIKF7 for <notmuch@notmuchmail.org>;\r
+ Wed, 19 Feb 2014 12:20:39 -0800 (PST)\r
+Received: from mail-wg0-f47.google.com (mail-wg0-f47.google.com\r
+ [74.125.82.47]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client\r
+ certificate requested) by olra.theworths.org (Postfix) with ESMTPS id\r
+ B58FB431FBD for <notmuch@notmuchmail.org>; Wed, 19 Feb 2014 12:20:33 -0800\r
+ (PST)\r
+Received: by mail-wg0-f47.google.com with SMTP id k14so745581wgh.2\r
+ for <notmuch@notmuchmail.org>; Wed, 19 Feb 2014 12:20:31 -0800 (PST)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+ h=from:to:cc:subject:date:message-id:in-reply-to:references;\r
+ bh=400vgAVsKSjcDn7RluxqJMmbNbKI+Wu6OskJ/JDeyWU=;\r
+ b=qdbWOFe1ErxnSCGe95XI91SIl9e1xO/tgttvBO38CZ3fiOaUDDL2ohy9tpOgSKhjL7\r
+ J5Kcaw0S1Wt54w2yMpPI5DvmWWQOgw9SFuBr2CmJ4YWWbGz7Qjz671GiPSHw/RRjtbXj\r
+ +wP81HCu1U8x8TjtXBcHaMNw2kHDKbCOZyUHVp3feO+FA1ktnygvs+oty5yhu1zLECE2\r
+ hBF/C7nEvAzfQC2k0sjGuvkUI8Hrr6Ipkybit51fPl91cZCXaEYUiDVPBfibrbXajfpH\r
+ S5xv8mGCEIeYpjjMyejOB0I+x1vzPZc+FcO8oyOEY/tUaTDAeAeIP1/D7NfpC4wBETQW\r
+ MxOg==\r
+X-Received: by 10.194.110.41 with SMTP id hx9mr30734009wjb.28.1392841230963;\r
+ Wed, 19 Feb 2014 12:20:30 -0800 (PST)\r
+Received: from localhost (93-97-24-31.zone5.bethere.co.uk. [93.97.24.31])\r
+ by mx.google.com with ESMTPSA id fo6sm4686478wib.7.2014.02.19.12.20.29\r
+ for <multiple recipients>\r
+ (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+ Wed, 19 Feb 2014 12:20:30 -0800 (PST)\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 1/7] Make keys of notmuch-tag-formats regexps and use\r
+ caching\r
+Date: Wed, 19 Feb 2014 20:20:06 +0000\r
+Message-Id: <1392841212-8494-2-git-send-email-markwalters1009@gmail.com>\r
+X-Mailer: git-send-email 1.7.9.1\r
+In-Reply-To: <1392841212-8494-1-git-send-email-markwalters1009@gmail.com>\r
+References: <1392841212-8494-1-git-send-email-markwalters1009@gmail.com>\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, 19 Feb 2014 20:20:44 -0000\r
+\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+\r
+This patch switches notmuch-tag-formats to use regexps with caching\r
+for performance.\r
+\r
+We have to clear the cache somehow on changes to notmuch-tag-formats.\r
+This version takes the simplest approach: search/show/tree all clear\r
+the cache whenever they start loading.\r
+\r
+We cannot use assoc-default since there's no way to distinguish a\r
+missing key from a present key with a null cdr: thus, we use assoc*\r
+from cl instead.\r
+\r
+Performance-wise, the caching of regexp lookup makes this at least as\r
+fast as the previous code using assoc (see\r
+id:1392226351-31440-1-git-send-email-amdragon@mit.edu for timing\r
+details).\r
+---\r
+ emacs/notmuch-show.el | 1 +\r
+ emacs/notmuch-tag.el | 70 +++++++++++++++++++++++++++++++++---------------\r
+ emacs/notmuch-tree.el | 1 +\r
+ emacs/notmuch.el | 1 +\r
+ 4 files changed, 51 insertions(+), 22 deletions(-)\r
+\r
+diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el\r
+index 1ac80ca..4bddf6c 100644\r
+--- a/emacs/notmuch-show.el\r
++++ b/emacs/notmuch-show.el\r
+@@ -1145,6 +1145,7 @@ function is used."\r
+ ;; Don't track undo information for this buffer\r
+ (set 'buffer-undo-list t)\r
+ \r
++ (notmuch-tag-clear-cache)\r
+ (erase-buffer)\r
+ (goto-char (point-min))\r
+ (save-excursion\r
+diff --git a/emacs/notmuch-tag.el b/emacs/notmuch-tag.el\r
+index 908e7ad..47e0205 100644\r
+--- a/emacs/notmuch-tag.el\r
++++ b/emacs/notmuch-tag.el\r
+@@ -28,23 +28,39 @@\r
+ (require 'crm)\r
+ (require 'notmuch-lib)\r
+ \r
++;; (notmuch-tag-clear-cache will be called by the defcustom\r
++;; notmuch-tag-formats, so it has to be defined first.)\r
++\r
++(defvar notmuch-tag--format-cache (make-hash-table :test 'equal)\r
++ "Cache of tag format lookup. Internal to `notmuch-tag-format-tag'.")\r
++\r
++(defun notmuch-tag-clear-cache ()\r
++ "Clear the internal cache of tag formats.\r
++\r
++This must be called after changes to `notmuch-tag-formats'."\r
++ (clrhash notmuch-tag--format-cache))\r
++\r
+ (defcustom notmuch-tag-formats\r
+ '(("unread" (propertize tag 'face '(:foreground "red")))\r
+ ("flagged" (propertize tag 'face '(:foreground "blue"))\r
+ (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
++This is an association list that maps from tag name regexps to\r
++lists of formatting expressions. The first entry whose car\r
++regexp-matches a tag will be used to format that tag. The regexp\r
++is implicitly anchored, so to match a literal tag name, just use\r
++that tag name (if it contains special regexp characters like\r
++\".\" or \"*\", these have to be escaped). The cdr of the\r
++matching entry gives a list of Elisp expressions that modify the\r
++tag. If the list is empty, the tag will simply be hidden.\r
++Otherwise, each expression will be evaluated in order: for the\r
++first expression, the variable `tag' will be bound to the tag\r
++name; for each later expression, the variable `tag' will be bound\r
++to the result of the previous expression. In this way, each\r
++expression can build on the formatting performed by the previous\r
++expression. The result of the last expression will displayed in\r
++place of the 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
+@@ -56,7 +72,7 @@ with images."\r
+ \r
+ :group 'notmuch-search\r
+ :group 'notmuch-show\r
+- :type '(alist :key-type (string :tag "Tag")\r
++ :type '(alist :key-type (regexp :tag "Tag")\r
+ :extra-offset -3\r
+ :value-type\r
+ (radio :format "%v"\r
+@@ -137,16 +153,26 @@ This can be used with `notmuch-tag-format-image-data'."\r
+ \r
+ (defun notmuch-tag-format-tag (tag)\r
+ "Format TAG by looking into `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 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
++ (let ((formatted (gethash tag notmuch-tag--format-cache 'missing)))\r
++ (when (eq formatted 'missing)\r
++ (let* ((formats\r
++ (save-match-data\r
++ (assoc* tag notmuch-tag-formats\r
++ :test (lambda (tag key)\r
++ (and (eq (string-match key tag) 0)\r
++ (= (match-end 0) (length tag))))))))\r
++ (setq formatted\r
++ (cond\r
++ ((null formats) ;; - Tag not in `notmuch-tag-formats',\r
++ tag) ;; the format is 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
++ (puthash tag formatted notmuch-tag--format-cache)))\r
++ formatted))\r
+ \r
+ (defun notmuch-tag-format-tags (tags &optional face)\r
+ "Return a string representing formatted TAGS."\r
+diff --git a/emacs/notmuch-tree.el b/emacs/notmuch-tree.el\r
+index 4f2ac02..a106e09 100644\r
+--- a/emacs/notmuch-tree.el\r
++++ b/emacs/notmuch-tree.el\r
+@@ -881,6 +881,7 @@ the same as for the function notmuch-tree."\r
+ (message-arg "--entire-thread"))\r
+ (if (equal (car (process-lines notmuch-command "count" search-args)) "0")\r
+ (setq search-args basic-query))\r
++ (notmuch-tag-clear-cache)\r
+ (let ((proc (notmuch-start-notmuch\r
+ "notmuch-tree" (current-buffer) #'notmuch-tree-process-sentinel\r
+ "show" "--body=false" "--format=sexp"\r
+diff --git a/emacs/notmuch.el b/emacs/notmuch.el\r
+index 0471750..0c767f7 100644\r
+--- a/emacs/notmuch.el\r
++++ b/emacs/notmuch.el\r
+@@ -888,6 +888,7 @@ the configured default sort order."\r
+ (set 'notmuch-search-oldest-first oldest-first)\r
+ (set 'notmuch-search-target-thread target-thread)\r
+ (set 'notmuch-search-target-line target-line)\r
++ (notmuch-tag-clear-cache)\r
+ (let ((proc (get-buffer-process (current-buffer)))\r
+ (inhibit-read-only t))\r
+ (if proc\r
+-- \r
+1.7.9.1\r
+\r