--- /dev/null
+Return-Path: <m.walters@qmul.ac.uk>\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 91F60431FAF\r
+ for <notmuch@notmuchmail.org>; Tue, 8 Oct 2013 09:39:04 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -1.098\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-1.098 tagged_above=-999 required=5\r
+ tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001,\r
+ NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_MED=-2.3] 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 Y7H6Qb12vx3K for <notmuch@notmuchmail.org>;\r
+ Tue, 8 Oct 2013 09:38:57 -0700 (PDT)\r
+Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6])\r
+ (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 16D53431FAE\r
+ for <notmuch@notmuchmail.org>; Tue, 8 Oct 2013 09:38:57 -0700 (PDT)\r
+Received: from smtp.qmul.ac.uk ([138.37.6.40])\r
+ by mail2.qmul.ac.uk with esmtp (Exim 4.71)\r
+ (envelope-from <m.walters@qmul.ac.uk>)\r
+ id 1VTaJ5-0001py-U6; Tue, 08 Oct 2013 17:38:53 +0100\r
+Received: from 188.28.146.25.threembb.co.uk ([188.28.146.25] helo=localhost)\r
+ by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.71)\r
+ (envelope-from <m.walters@qmul.ac.uk>)\r
+ id 1VTaIu-0008EX-T7; Tue, 08 Oct 2013 17:38:47 +0100\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: Austin Clements <amdragon@MIT.EDU>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH 08/11] search: Add stable queries to thread search results\r
+In-Reply-To: <1381185201-25197-9-git-send-email-amdragon@mit.edu>\r
+References: <1381185201-25197-1-git-send-email-amdragon@mit.edu>\r
+ <1381185201-25197-9-git-send-email-amdragon@mit.edu>\r
+User-Agent: Notmuch/0.15.2+334~gafd5c6a (http://notmuchmail.org) Emacs/23.4.1\r
+ (x86_64-pc-linux-gnu)\r
+Date: Tue, 08 Oct 2013 17:37:29 +0100\r
+Message-ID: <87wqlnafcm.fsf@qmul.ac.uk>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=utf-8\r
+Content-Transfer-Encoding: quoted-printable\r
+X-Sender-Host-Address: 188.28.146.25\r
+X-QM-SPAM-Info: Sender has good ham record. :)\r
+X-QM-Body-MD5: 68a047edf5c877238a745409d245a3ed (of first 20000 bytes)\r
+X-SpamAssassin-Score: 0.0\r
+X-SpamAssassin-SpamBar: /\r
+X-SpamAssassin-Report: The QM spam filters have analysed this message to\r
+ determine if it is\r
+ spam. We require at least 5.0 points to mark a message as spam.\r
+ This message scored 0.0 points. Summary of the scoring: \r
+ * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail\r
+ provider * (markwalters1009[at]gmail.com)\r
+X-QM-Scan-Virus: ClamAV says the message is clean\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: Tue, 08 Oct 2013 16:39:04 -0000\r
+\r
+\r
+On Mon, 07 Oct 2013, Austin Clements <amdragon@MIT.EDU> wrote:\r
+> These queries will match exactly the set of messages currently in the\r
+> thread, even if more messages later arrive. Two queries are provided:\r
+> one for matched messages and one for unmatched messages.\r
+>\r
+> This can be used to fix race conditions with tagging threads from\r
+> search results. While tagging based on a thread: query can affect\r
+> messages that arrived after the search, tagging based on stable\r
+> queries affects only the messages the user was shown in the search UI.\r
+>\r
+> Since we want clients to be able to depend on the presence of these\r
+> queries, this ushers in schema version 2.\r
+> ---\r
+> devel/schemata | 22 +++++++++++++++++--\r
+> notmuch-client.h | 2 +-\r
+> notmuch-search.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++=\r
+++++++\r
+> test/json | 2 ++\r
+> test/missing-headers | 6 ++++--\r
+> test/sexp | 4 ++--\r
+> 6 files changed, 89 insertions(+), 7 deletions(-)\r
+>\r
+> diff --git a/devel/schemata b/devel/schemata\r
+> index cdd0e43..41dc4a6 100644\r
+> --- a/devel/schemata\r
+> +++ b/devel/schemata\r
+> @@ -14,7 +14,17 @@ are interleaved. Keys are printed as keywords (symbols=\r
+ preceded by a\r
+> colon), e.g. (:id "123" :time 54321 :from "foobar"). Null is printed as\r
+> nil, true as t and false as nil.\r
+>=20=20\r
+> -This is version 1 of the structured output format.\r
+> +This is version 2 of the structured output format.\r
+> +\r
+> +Version history\r
+> +---------------\r
+> +\r
+> +v1\r
+> +- First versioned schema release.\r
+> +- Added part.content-length and part.content-transfer-encoding fields.\r
+> +\r
+> +v2\r
+> +- Added the thread_summary.query field.\r
+>=20=20\r
+> Common non-terminals\r
+> --------------------\r
+> @@ -145,7 +155,15 @@ thread_summary =3D {\r
+> authors: string, # comma-separated names with | between\r
+> # matched and unmatched\r
+> subject: string,\r
+> - tags: [string*]\r
+> + tags: [string*],\r
+> +\r
+> + # Two stable query strings identifying exactly the matched and\r
+> + # unmatched messages currently in this thread. The messages\r
+> + # matched by these queries will not change even if more messages\r
+> + # arrive in the thread. If there are no matched or unmatched\r
+> + # messages, the corresponding query will be null (there is no\r
+> + # query that matches nothing). (Added in schema version 2.)\r
+> + query: [string|null, string|null],\r
+> }\r
+>=20=20\r
+> notmuch reply schema\r
+> diff --git a/notmuch-client.h b/notmuch-client.h\r
+> index 8d986f4..1b14910 100644\r
+> --- a/notmuch-client.h\r
+> +++ b/notmuch-client.h\r
+> @@ -138,7 +138,7 @@ chomp_newline (char *str)\r
+> * this. New (required) map fields can be added without increasing\r
+> * this.\r
+> */\r
+> -#define NOTMUCH_FORMAT_CUR 1\r
+> +#define NOTMUCH_FORMAT_CUR 2\r
+> /* The minimum supported structured output format version. Requests\r
+> * for format versions below this will return an error. */\r
+> #define NOTMUCH_FORMAT_MIN 1\r
+> diff --git a/notmuch-search.c b/notmuch-search.c\r
+> index d9d39ec..1d14651 100644\r
+> --- a/notmuch-search.c\r
+> +++ b/notmuch-search.c\r
+> @@ -20,6 +20,7 @@\r
+>=20=20\r
+> #include "notmuch-client.h"\r
+> #include "sprinter.h"\r
+> +#include "string-util.h"\r
+>=20=20\r
+> typedef enum {\r
+> OUTPUT_SUMMARY,\r
+> @@ -46,6 +47,46 @@ sanitize_string (const void *ctx, const char *str)\r
+> return out;\r
+> }\r
+>=20=20\r
+> +/* Return two stable query strings that identify exactly the matched\r
+> + * and unmatched messages currently in thread. If there are no\r
+> + * matched or unmatched messages, the returned buffers will be\r
+> + * NULL. */\r
+> +static int\r
+> +get_thread_query (notmuch_thread_t *thread,\r
+> + char **matched_out, char **unmached_out)\r
+> +{\r
+> + notmuch_messages_t *messages;\r
+> + char *escaped =3D NULL;\r
+> + size_t escaped_len =3D 0;\r
+> +\r
+> + *matched_out =3D *unmached_out =3D NULL;\r
+\r
+A missing t in unmatched? (and once below where it is used)\r
+\r
+> +\r
+> + for (messages =3D notmuch_thread_get_messages (thread);\r
+> + notmuch_messages_valid (messages);\r
+> + notmuch_messages_move_to_next (messages))\r
+> + {\r
+> + notmuch_message_t *message =3D notmuch_messages_get (messages);\r
+> + const char *mid =3D notmuch_message_get_message_id (message);\r
+> + /* Determine which query buffer to extend */\r
+> + char **buf =3D notmuch_message_get_flag (\r
+> + message, NOTMUCH_MESSAGE_FLAG_MATCH) ? matched_out : unmached_out;\r
+> + /* Allocate the query buffer is this is the first message */\r
+> + if (!*buf && (*buf =3D talloc_strdup (thread, "")) =3D=3D NULL)\r
+> + return -1;\r
+> + /* Add this message's id: query. Since "id" is an exclusive\r
+> + * prefix, it is implicitly 'or'd together, so we only need to\r
+> + * join queries with a space. */\r
+> + if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)\r
+> + return -1;\r
+> + *buf =3D talloc_asprintf_append_buffer (\r
+> + *buf, "%s%s", **buf ? " " : "", escaped);\r
+> + if (!*buf)\r
+> + return -1;\r
+> + }\r
+> + talloc_free (escaped);\r
+> + return 0;\r
+> +}\r
+> +\r
+> static int\r
+> do_search_threads (sprinter_t *format,\r
+> notmuch_query_t *query,\r
+> @@ -131,6 +172,25 @@ do_search_threads (sprinter_t *format,\r
+> format->string (format, authors);\r
+> format->map_key (format, "subject");\r
+> format->string (format, subject);\r
+> + if (notmuch_format_version >=3D 2) {\r
+> + char *matched_query, *unmatched_query;\r
+> + if (get_thread_query (thread, &matched_query,\r
+> + &unmatched_query) < 0) {\r
+> + fprintf (stderr, "Out of memory\n");\r
+> + return 1;\r
+> + }\r
+> + format->map_key (format, "query");\r
+> + format->begin_list (format);\r
+> + if (matched_query)\r
+> + format->string (format, matched_query);\r
+> + else\r
+> + format->null (format);\r
+> + if (unmatched_query)\r
+> + format->string (format, unmatched_query);\r
+> + else\r
+> + format->null (format);\r
+> + format->end (format);\r
+> + }\r
+> }\r
+>=20=20\r
+> talloc_free (ctx_quote);\r
+> diff --git a/test/json b/test/json\r
+> index b87b7f6..e07a290 100755\r
+> --- a/test/json\r
+> +++ b/test/json\r
+> @@ -26,6 +26,7 @@ test_expect_equal_json "$output" "[{\"thread\": \"XXX\",\r
+> \"total\": 1,\r
+> \"authors\": \"Notmuch Test Suite\",\r
+> \"subject\": \"json-search-subject\",\r
+> + \"query\": [\"id:$gen_msg_id\", null],\r
+> \"tags\": [\"inbox\",\r
+> \"unread\"]}]"\r
+>=20=20\r
+> @@ -59,6 +60,7 @@ test_expect_equal_json "$output" "[{\"thread\": \"XXX\",\r
+> \"total\": 1,\r
+> \"authors\": \"Notmuch Test Suite\",\r
+> \"subject\": \"json-search-utf8-body-s=C3=BCbj=C3=A9ct\",\r
+> + \"query\": [\"id:$gen_msg_id\", null],\r
+> \"tags\": [\"inbox\",\r
+> \"unread\"]}]"\r
+>=20=20\r
+> diff --git a/test/missing-headers b/test/missing-headers\r
+> index f14b878..43e861b 100755\r
+> --- a/test/missing-headers\r
+> +++ b/test/missing-headers\r
+> @@ -43,7 +43,8 @@ test_expect_equal_json "$output" '\r
+> ],\r
+> "thread": "XXX",\r
+> "timestamp": 978709437,\r
+> - "total": 1\r
+> + "total": 1,\r
+> + "query": ["id:notmuch-sha1-7a6e4eac383ef958fcd3ebf2143db71b8ff01=\r
+161", null]\r
+> },\r
+> {\r
+> "authors": "Notmuch Test Suite",\r
+> @@ -56,7 +57,8 @@ test_expect_equal_json "$output" '\r
+> ],\r
+> "thread": "XXX",\r
+> "timestamp": 0,\r
+> - "total": 1\r
+> + "total": 1,\r
+> + "query": ["id:notmuch-sha1-ca55943aff7a72baf2ab21fa74fab3d632401=\r
+334", null]\r
+> }\r
+> ]'\r
+>=20=20\r
+> diff --git a/test/sexp b/test/sexp\r
+> index 492a82f..be815e1 100755\r
+> --- a/test/sexp\r
+> +++ b/test/sexp\r
+> @@ -19,7 +19,7 @@ test_expect_equal "$output" "((((:id \"${gen_msg_id}\" =\r
+:match t :excluded nil :f\r
+> test_begin_subtest "Search message: sexp"\r
+> add_message "[subject]=3D\"sexp-search-subject\"" "[date]=3D\"Sat, 01 Ja=\r
+n 2000 12:00:00 -0000\"" "[body]=3D\"sexp-search-message\""\r
+> output=3D$(notmuch search --format=3Dsexp "sexp-search-message" | notmuc=\r
+h_search_sanitize)\r
+> -test_expect_equal "$output" "((:thread \"0000000000000002\" :timestamp 9=\r
+46728000 :date_relative \"2000-01-01\" :matched 1 :total 1 :authors \"Notmu=\r
+ch Test Suite\" :subject \"sexp-search-subject\" :tags (\"inbox\" \"unread\=\r
+")))"\r
+> +test_expect_equal "$output" "((:thread \"0000000000000002\" :timestamp 9=\r
+46728000 :date_relative \"2000-01-01\" :matched 1 :total 1 :authors \"Notmu=\r
+ch Test Suite\" :subject \"sexp-search-subject\" :query (\"id:$gen_msg_id\"=\r
+ nil) :tags (\"inbox\" \"unread\")))"\r
+>=20=20\r
+> test_begin_subtest "Show message: sexp, utf-8"\r
+> add_message "[subject]=3D\"sexp-show-utf8-body-s=C3=BCbj=C3=A9ct\"" "[da=\r
+te]=3D\"Sat, 01 Jan 2000 12:00:00 -0000\"" "[body]=3D\"js=C3=B6n-show-m=C3=\r
+=A9ssage\""\r
+> @@ -44,7 +44,7 @@ test_expect_equal "$output" "((((:id \"$id\" :match t :=\r
+excluded nil :filename \"\r
+> test_begin_subtest "Search message: sexp, utf-8"\r
+> add_message "[subject]=3D\"sexp-search-utf8-body-s=C3=BCbj=C3=A9ct\"" "[=\r
+date]=3D\"Sat, 01 Jan 2000 12:00:00 -0000\"" "[body]=3D\"js=C3=B6n-search-m=\r
+=C3=A9ssage\""\r
+> output=3D$(notmuch search --format=3Dsexp "js=C3=B6n-search-m=C3=A9ssage=\r
+" | notmuch_search_sanitize)\r
+> -test_expect_equal "$output" "((:thread \"0000000000000005\" :timestamp 9=\r
+46728000 :date_relative \"2000-01-01\" :matched 1 :total 1 :authors \"Notmu=\r
+ch Test Suite\" :subject \"sexp-search-utf8-body-s=C3=BCbj=C3=A9ct\" :tags =\r
+(\"inbox\" \"unread\")))"\r
+> +test_expect_equal "$output" "((:thread \"0000000000000005\" :timestamp 9=\r
+46728000 :date_relative \"2000-01-01\" :matched 1 :total 1 :authors \"Notmu=\r
+ch Test Suite\" :subject \"sexp-search-utf8-body-s=C3=BCbj=C3=A9ct\" :query=\r
+ (\"id:$gen_msg_id\" nil) :tags (\"inbox\" \"unread\")))"\r
+\r
+Would it be worth adding an explicit test when unmatched is non-nil?\r
+\r
+Best wishes=20\r
+\r
+Mark\r
+\r
+\r
+>=20=20\r
+> test_done\r
+> --=20\r
+> 1.8.4.rc3\r
+>\r
+> _______________________________________________\r
+> notmuch mailing list\r
+> notmuch@notmuchmail.org\r
+> http://notmuchmail.org/mailman/listinfo/notmuch\r