--- /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 2C28A431FD0\r
+ for <notmuch@notmuchmail.org>; Sun, 3 Jul 2011 10:17:58 -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 bwy0xnHscZbu for <notmuch@notmuchmail.org>;\r
+ Sun, 3 Jul 2011 10:17:56 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-4.mit.edu (DMZ-MAILSEC-SCANNER-4.MIT.EDU\r
+ [18.9.25.15])\r
+ by olra.theworths.org (Postfix) with ESMTP id F0BCF431FB6\r
+ for <notmuch@notmuchmail.org>; Sun, 3 Jul 2011 10:17:55 -0700 (PDT)\r
+X-AuditID: 1209190f-b7c82ae000000a20-4f-4e10a448440e\r
+Received: from mailhub-auth-2.mit.edu ( [18.7.62.36])\r
+ by dmz-mailsec-scanner-4.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id C7.34.02592.844A01E4; Sun, 3 Jul 2011 13:18:00 -0400 (EDT)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+ by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id p63HHsIx021268; \r
+ Sun, 3 Jul 2011 13:17:55 -0400\r
+Received: from awakening.csail.mit.edu (awakening.csail.mit.edu [18.26.4.91])\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 p63HHrnC027795\r
+ (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+ Sun, 3 Jul 2011 13:17:54 -0400 (EDT)\r
+Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.72)\r
+ (envelope-from <amdragon@mit.edu>)\r
+ id 1QdQIh-0001AG-Mb; Sun, 03 Jul 2011 13:17:43 -0400\r
+Date: Sun, 3 Jul 2011 13:17:43 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: Pieter Praet <pieter@praet.org>\r
+Subject: Re: [PATCH 2/2] [RFC] possible solution for "Race condition for\r
+ '*' command"\r
+Message-ID: <20110703171743.GL15901@mit.edu>\r
+References:\r
+ <CAH-f9WticM4EN8F1_ik_-mcBcBtrXwSpO+Drbtp7=UN7McECrg@mail.gmail.com>\r
+ <87zkkwydag.fsf@praet.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+Content-Disposition: inline\r
+In-Reply-To: <87zkkwydag.fsf@praet.org>\r
+User-Agent: Mutt/1.5.20 (2009-06-14)\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFprAKsWRmVeSWpSXmKPExsUixG6nouuxRMDPYM1VaYvrN2cyW/x+fYPZ\r
+ gcnj2apbzB4d+y6zBjBFcdmkpOZklqUW6dslcGXsO7uBpeCKccXMdUuZGxjXa3QxcnJICJhI\r
+ PP6/nRHCFpO4cG89WxcjF4eQwD5Gidm/JjJBOOsZJX7PbIByTjBJTOv6BuUsAcqsbGYF6WcR\r
+ UJF4ev0fG4jNJqAhsW3/crC5IgLKEqef/GQHsZkFpCW+/W5mArGFBcIlpn54BlbPK6Aj8erV\r
+ HrB6IYFciecf/rBDxAUlTs58wgLRqyVx499LoF4OsDnL/3GAhDkF1CWWf9oHVi4KdMK1/e1s\r
+ ExiFZiHpnoWkexZC9wJG5lWMsim5Vbq5iZk5xanJusXJiXl5qUW6Jnq5mSV6qSmlmxhBgc0p\r
+ yb+D8dtBpUOMAhyMSjy8DJME/IRYE8uKK3MPMUpyMCmJ8qotBgrxJeWnVGYkFmfEF5XmpBYf\r
+ YpTgYFYS4T1QBZTjTUmsrEotyodJSXOwKInzlnv/9xUSSE8sSc1OTS1ILYLJynBwKEnw5oIM\r
+ FSxKTU+tSMvMKUFIM3FwggznARq+AKSGt7ggMbc4Mx0if4pRl2PN2keHGYVY8vLzUqXEeZtA\r
+ igRAijJK8+DmwBLSK0ZxoLeEeWeBVPEAkxncpFdAS5iAlmTm84IsKUlESEk1MC52aDlooHEl\r
+ qKlW2bLy00W12DPypiUVzrd2iPr9O3BrdfPNJ6tj59T/KxZeWmX1wu7/pFiGre45Wyb8uSCg\r
+ Yemd8+uL9eKSTRek7yxvPzDfxeDVfMvpVQeYzn3LvGaz8vLR2ckpP5bNvXxEOi8+u8/RLLPw\r
+ xqa1u9a35Z/kT9G89DzRY7ols6MSS3FGoqEWc1FxIgBs/DhsIwMAAA==\r
+Cc: notmuch@notmuchmail.org\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: Sun, 03 Jul 2011 17:17:58 -0000\r
+\r
+Quoth Pieter Praet on Jul 02 at 4:20 pm:\r
+> On Fri, 1 Jul 2011 12:37:11 -0400, Austin Clements <amdragon@mit.edu> wrote:\r
+> Non-text part: multipart/alternative\r
+> > On Jul 1, 2011 10:55 AM, "Austin Clements" <amdragon@mit.edu> wrote:\r
+> > >\r
+> > > On Thu, Jun 30, 2011 at 3:38 PM, Pieter Praet <pieter@praet.org> wrote:\r
+> > > > Ok, even though my very first reply [1] may have created the impression\r
+> > > > that I understood the issue, I clearly didn't...\r
+> > > >\r
+> > > > The test [2] needs a more applicable commit message, and the subsequent\r
+> > > > patch [3] points more or less in the right direction, but the Message-Id\r
+> > > > list should be local to the *search buffer* rather than to the\r
+> > > > `notmuch-search-operate-all' function.\r
+> > > >\r
+> > > > `notmuch-search' could:\r
+> > > > - run "notmuch-command search" with the "--output=messages" option\r
+> > > > instead of a plain search,\r
+> > > > - maintain a buffer-local var with a list of returned Message-Id's,\r
+> > > > - ...and populate the buffer based on that list.\r
+> > > >\r
+> > > > As such we'd have -for each individual search buffer- a canonical list\r
+> > > > of Message-Id's (i.e. messages which actually *match* the query AND are\r
+> > > > currently visible in the search buffer), to be used by\r
+> > > > `notmuch-search-operate-all' et al.\r
+> > > >\r
+> > > >\r
+> > > > Peace\r
+> > > >\r
+> > > >\r
+> > > > [1] id:"87fwmuxxgd.fsf@praet.org"\r
+> > > > [2] id:"1309450108-2793-2-git-send-email-pieter@praet.org"\r
+> > > > [3] id:"1309450108-2793-1-git-send-email-pieter@praet.org"\r
+> > >\r
+> > > Ideally, this wouldn't be per-buffer, but per *line*. This race\r
+> > > equally affects adding and removing tags from individual results,\r
+> > > since that's done using a thread: query, whose results could have\r
+> > > changed since the original search.\r
+> > >\r
+> > > This almost certainly requires support from the notmuch core. The\r
+> > > good news is that the library already provides this information, so\r
+> > > there will be virtually no performance hit for outputting it.\r
+> > \r
+> > Actually, with a smidgeon of library support, you could even use document\r
+> > IDs for this, rather than message IDs, which would make the tagging\r
+> > operations (even '*') no more expensive than they are now. (Of course, it\r
+> > would be good to know just how much overhead going through message IDs\r
+> > actually introduces.)\r
+> Non-text part: text/html\r
+> \r
+> \r
+> That would be awesome!\r
+> \r
+> Though I'd rather leave the plumbing to someone sufficiently capable,\r
+> so, if leaving libnotmuch hacking out of the equation, how would one go\r
+> about doing this?\r
+> \r
+> I (ignorantly) assume we'd get `notmuch-search-process-filter'\r
+> to append each line with an invisible field containing a list\r
+> of matching Message-Id's, presumably obtained via\r
+> `notmuch_thread_get_matched_messages' (@ lib/thread.cc) ?\r
+\r
+Here's a super-hacky patch that adds an "id:x or id:y or ..." query\r
+string to the end of each search line, right after the list of tags.\r
+This is just the C side; care to prototype the elisp side?\r
+\r
+I also added a --stdin argument to notmuch tag that'll read the query\r
+string from stdin, since these queries could get very long (though\r
+ARG_MAX appears to be 2 megs these days, so maybe this wasn't\r
+necessary for prototyping).\r
+\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index faccaf7..65fe438 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -190,6 +190,23 @@ format_thread_json (const void *ctx,\r
+ talloc_free (ctx_quote);\r
+ }\r
+ \r
++static void\r
++show_message_ids (notmuch_messages_t *messages, notmuch_bool_t first)\r
++{\r
++ notmuch_message_t *message;\r
++\r
++ for (;\r
++ notmuch_messages_valid (messages);\r
++ notmuch_messages_move_to_next (messages)) {\r
++ if (!first)\r
++ fputs (" or ", stdout);\r
++ first = FALSE;\r
++ message = notmuch_messages_get (messages);\r
++ printf ("id:\"%s\"", notmuch_message_get_message_id (message));\r
++ show_message_ids (notmuch_message_get_replies (message), FALSE);\r
++ }\r
++}\r
++\r
+ static int\r
+ do_search_threads (const search_format_t *format,\r
+ notmuch_query_t *query,\r
+@@ -252,6 +269,12 @@ do_search_threads (const search_format_t *format,\r
+ \r
+ fputs (format->tag_end, stdout);\r
+ \r
++ if (format == &format_text) {\r
++ notmuch_messages_t *toplevel = notmuch_thread_get_toplevel_messages (thread);\r
++ fputs (" ", stdout);\r
++ show_message_ids (toplevel, TRUE);\r
++ }\r
++\r
+ fputs (format->item_end, stdout);\r
+ }\r
+ \r
+diff --git a/notmuch-tag.c b/notmuch-tag.c\r
+index 6204ae3..5609d02 100644\r
+--- a/notmuch-tag.c\r
++++ b/notmuch-tag.c\r
+@@ -30,6 +30,36 @@ handle_sigint (unused (int sig))\r
+ interrupted = 1;\r
+ }\r
+ \r
++static char*\r
++query_string_from_stdin (void *ctx)\r
++{\r
++ char *query_string = talloc_strdup (ctx, "");\r
++ char buf[4096];\r
++\r
++ if (query_string == NULL) {\r
++ fprintf (stderr, "Out of memory.\n");\r
++ return NULL;\r
++ }\r
++\r
++ while (1) {\r
++ ssize_t r = read(0, buf, sizeof (buf));\r
++ if (r < 0) {\r
++ fprintf (stderr, "Error reading from stdin: %s\n",\r
++ strerror (errno));\r
++ return NULL;\r
++ } else if (r == 0) {\r
++ break;\r
++ }\r
++ query_string = talloc_strndup_append (query_string, buf, r);\r
++ if (!query_string) {\r
++ fprintf (stderr, "Out of memory.\n");\r
++ return NULL;\r
++ }\r
++ }\r
++\r
++ return query_string;\r
++}\r
++\r
+ int\r
+ notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))\r
+ {\r
+@@ -44,6 +74,7 @@ notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))\r
+ notmuch_message_t *message;\r
+ struct sigaction action;\r
+ notmuch_bool_t synchronize_flags;\r
++ notmuch_bool_t use_stdin = FALSE;\r
+ int i;\r
+ \r
+ /* Setup our handler for SIGINT */\r
+@@ -70,7 +101,9 @@ notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))\r
+ i++;\r
+ break;\r
+ }\r
+- if (argv[i][0] == '+') {\r
++ if (STRNCMP_LITERAL (argv[i], "--stdin") == 0) {\r
++ use_stdin = TRUE;\r
++ } else if (argv[i][0] == '+') {\r
+ add_tags[add_tags_count++] = i;\r
+ } else if (argv[i][0] == '-') {\r
+ remove_tags[remove_tags_count++] = i;\r
+@@ -84,7 +117,13 @@ notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))\r
+ return 1;\r
+ }\r
+ \r
+- query_string = query_string_from_args (ctx, argc - i, &argv[i]);\r
++ if (use_stdin)\r
++ query_string = query_string_from_stdin (ctx);\r
++ else\r
++ query_string = query_string_from_args (ctx, argc - i, &argv[i]);\r
++\r
++ if (!query_string)\r
++ return 1;\r
+ \r
+ if (*query_string == '\0') {\r
+ fprintf (stderr, "Error: notmuch tag requires at least one search term.\n");\r
+\r