--- /dev/null
+Return-Path: <amdragon@mit.edu>\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 B3109431FAF\r
+ for <notmuch@notmuchmail.org>; Fri, 26 Oct 2012 13:18:20 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+ tests=[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 EwTtc40udegB for <notmuch@notmuchmail.org>;\r
+ Fri, 26 Oct 2012 13:18:19 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-8.mit.edu (DMZ-MAILSEC-SCANNER-8.MIT.EDU\r
+ [18.7.68.37])\r
+ by olra.theworths.org (Postfix) with ESMTP id 366DF431FAE\r
+ for <notmuch@notmuchmail.org>; Fri, 26 Oct 2012 13:18:19 -0700 (PDT)\r
+X-AuditID: 12074425-b7fcc6d00000091f-7a-508af00ae45d\r
+Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
+ by dmz-mailsec-scanner-8.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id 56.D6.02335.A00FA805; Fri, 26 Oct 2012 16:18:18 -0400 (EDT)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+ by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id q9QKIIih006740; \r
+ Fri, 26 Oct 2012 16:18:18 -0400\r
+Received: from drake.dyndns.org (26-4-172.dynamic.csail.mit.edu [18.26.4.172])\r
+ (authenticated bits=0)\r
+ (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+ by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id q9QKIFCn024591\r
+ (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+ Fri, 26 Oct 2012 16:18:18 -0400 (EDT)\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
+ (envelope-from <amdragon@mit.edu>)\r
+ id 1TRqMB-0002zV-7G; Fri, 26 Oct 2012 16:18:15 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 1/3] emacs: Introduce generic boolean term escaping function\r
+Date: Fri, 26 Oct 2012 16:18:10 -0400\r
+Message-Id: <1351282692-11455-1-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.7.10\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFprJIsWRmVeSWpSXmKPExsUixG6nrsv1oSvAYNFjLYvrN2cyOzB6PFt1\r
+ izmAMYrLJiU1J7MstUjfLoErY9WcX6wFK3grnp98zd7AeIOri5GTQ0LARKJh+2R2CFtM4sK9\r
+ 9WxdjFwcQgL7GCXmdtxkBEkICWxglPgzWwQicYpJonlJEyOEM5dRouvbDBaQKjYBDYlt+5eD\r
+ dYgISEvsvDubtYuRg4NZQE3iT5cKiCks4CPxZ5Y+SAWLgKrExeu3wBbzCjhItP+exApxhLzE\r
+ 0/t9bBMYeRcwMqxilE3JrdLNTczMKU5N1i1OTszLSy3StdDLzSzRS00p3cQIDgEX1R2MEw4p\r
+ HWIU4GBU4uHd8aQrQIg1say4MvcQoyQHk5Io78LnQCG+pPyUyozE4oz4otKc1OJDjBIczEoi\r
+ vEXHgHK8KYmVValF+TApaQ4WJXHeGyk3/YUE0hNLUrNTUwtSi2CyMhwcShK8r98BNQoWpaan\r
+ VqRl5pQgpJk4OEGG8wANXwZSw1tckJhbnJkOkT/FqCglzjsfJCEAksgozYPrhcXoK0ZxoFeE\r
+ ed3eA1XxAOMbrvsV0GAmoMFirJ0gg0sSEVJSDYzHJC3us04qcO3a0WzuP2WSXs3Xct70awy3\r
+ 1l/JnDSh4/aC8MP79Zbd7S+x4H/6zXrNJP+CB5G+szOP2k4O2Nd+PGf7tcMydZarV+b0bPou\r
+ uNvZWL5ovr74tV13m4VzZsvH5P9Yd6NHOFfsNNdepfIVHK1CJmv/iCn+uPAvTHFrzVXvzhtn\r
+ mlcpsRRnJBpqMRcVJwIAlSGZQawCAAA=\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: Fri, 26 Oct 2012 20:18:20 -0000\r
+\r
+Currently, we only properly escape stashed id queries, but there are\r
+other places where the Emacs UI constructs queries for boolean terms.\r
+Since this escaping function is meant to be used in other places, it\r
+avoids escaping strings that don't need escaping.\r
+---\r
+ emacs/notmuch-lib.el | 16 +++++++++++++++-\r
+ test/emacs | 2 +-\r
+ 2 files changed, 16 insertions(+), 2 deletions(-)\r
+\r
+diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el\r
+index 69867ad..eeb005f 100644\r
+--- a/emacs/notmuch-lib.el\r
++++ b/emacs/notmuch-lib.el\r
+@@ -161,9 +161,23 @@ the user hasn't set this variable with the old or new value."\r
+ "[No Subject]"\r
+ subject)))\r
+ \r
++(defun notmuch-escape-boolean-term (term)\r
++ "Escape a boolean term for use in a query.\r
++\r
++The caller is responsible for prepending the term prefix and a\r
++colon. This performs minimal escaping in order to produce\r
++user-friendly queries."\r
++\r
++ (save-match-data\r
++ (if (or (equal term "")\r
++ (string-match "[ ()]\\|^\"" term))\r
++ ;; Requires escaping\r
++ (concat "\"" (replace-regexp-in-string "\"" "\"\"" term t t) "\"")\r
++ term)))\r
++\r
+ (defun notmuch-id-to-query (id)\r
+ "Return a query that matches the message with id ID."\r
+- (concat "id:\"" (replace-regexp-in-string "\"" "\"\"" id t t) "\""))\r
++ (concat "id:" (notmuch-escape-boolean-term id)))\r
+ \r
+ ;;\r
+ \r
+diff --git a/test/emacs b/test/emacs\r
+index 21f1d16..44f641e 100755\r
+--- a/test/emacs\r
++++ b/test/emacs\r
+@@ -667,7 +667,7 @@ Some One <someone@somewhere.org>\r
+ Some One Else <notsomeone@somewhere.org>\r
+ Notmuch <notmuch@notmuchmail.org>\r
+ Stash my stashables\r
+-id:"bought"\r
++id:bought\r
+ bought\r
+ inbox,stashtest\r
+ ${gen_msg_filename}\r
+-- \r
+1.7.10\r
+\r