[PATCH v2 6/7] cli: allow search mode to include msg-ids with JSON output
authormarkwalters1009 <markwalters1009@gmail.com>
Sat, 24 Nov 2012 13:20:55 +0000 (13:20 +0000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:50:50 +0000 (09:50 -0800)
40/83d8704b89f6de996db3300ecd4617418b3cae [new file with mode: 0644]

diff --git a/40/83d8704b89f6de996db3300ecd4617418b3cae b/40/83d8704b89f6de996db3300ecd4617418b3cae
new file mode 100644 (file)
index 0000000..465ca39
--- /dev/null
@@ -0,0 +1,256 @@
+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 6BB2B431FBD\r
+       for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 05:21:18 -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 lUhXcg7esj4v for <notmuch@notmuchmail.org>;\r
+       Sat, 24 Nov 2012 05:21:16 -0800 (PST)\r
+Received: from mail-wg0-f45.google.com (mail-wg0-f45.google.com\r
+ [74.125.82.45])       (using TLSv1 with cipher RC4-SHA (128/128 bits))        (No client\r
+ certificate requested)        by olra.theworths.org (Postfix) with ESMTPS id\r
+ 0F303431FAF   for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 05:21:15 -0800\r
+ (PST)\r
+Received: by mail-wg0-f45.google.com with SMTP id dq11so3973532wgb.2\r
+       for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 05:21:15 -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:x-mailer:in-reply-to:references;\r
+       bh=fVtRUudxpPp9qg10ROX/1HSFdBG9fBa+e0lHY46xMUE=;\r
+       b=AsCAuZGpCYh73jZdYWopFkZJFACI7uEC5+bsbtaEIlKKXQ1uBsWY3oTLknxVWCxqz5\r
+       IftB7iE54FOmIOwAdgZk/FTiuejizmGN71588qeYjeY02fNvUXfPHvYl79W7TgRG9I4b\r
+       ASO2fPn33M2/OLoRBh7OSkGbAqlsVhcV5ZtX2Os28wM9lbErIznBMPSeWdcoCmIDkJPd\r
+       V5K9qm0zBxHsPkDYc7fyKibynFIE3LZwdTIYE+lf0EaFvQYYF9HjJ/EpzoLM81NLvS2R\r
+       yIgNNC9ZkyNSK0hP1LQ++w2m/notz0qMBRlpMIUoux+Q70OeOc9pMwtKH6rYb8aRzVp4\r
+       VSSg==\r
+Received: by 10.180.90.70 with SMTP id bu6mr13655680wib.20.1353763275642;\r
+       Sat, 24 Nov 2012 05:21:15 -0800 (PST)\r
+Received: from localhost (93-97-24-31.zone5.bethere.co.uk. [93.97.24.31])\r
+       by mx.google.com with ESMTPS id i2sm12683344wiw.3.2012.11.24.05.21.14\r
+       (version=TLSv1/SSLv3 cipher=OTHER);\r
+       Sat, 24 Nov 2012 05:21:14 -0800 (PST)\r
+From: markwalters1009 <markwalters1009@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 6/7] cli: allow search mode to include msg-ids with JSON\r
+       output\r
+Date: Sat, 24 Nov 2012 13:20:55 +0000\r
+Message-Id: <1353763256-32336-7-git-send-email-markwalters1009@gmail.com>\r
+X-Mailer: git-send-email 1.7.9.1\r
+In-Reply-To: <1353763256-32336-1-git-send-email-markwalters1009@gmail.com>\r
+References: <1353763256-32336-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: Sat, 24 Nov 2012 13:21:18 -0000\r
+\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+\r
+This adds a --queries=true option which modifies the summary output of\r
+notmuch search by including two extra query strings with each result:\r
+one query string specifies all matching messages and one query string\r
+all non-matching messages. Currently these are just lists of message\r
+ids joined with " or " but that could change in future.\r
+\r
+Currently this is not implemented for text format.\r
+---\r
+ notmuch-search.c |   95 ++++++++++++++++++++++++++++++++++++++++++++++++++---\r
+ 1 files changed, 89 insertions(+), 6 deletions(-)\r
+\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index 830c4e4..c8fc9a6 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -26,7 +26,8 @@ typedef enum {\r
+     OUTPUT_THREADS,\r
+     OUTPUT_MESSAGES,\r
+     OUTPUT_FILES,\r
+-    OUTPUT_TAGS\r
++    OUTPUT_TAGS,\r
++    OUTPUT_SUMMARY_WITH_QUERIES\r
+ } output_t;\r
+ \r
+ static char *\r
+@@ -46,6 +47,57 @@ sanitize_string (const void *ctx, const char *str)\r
+     return out;\r
+ }\r
+ \r
++/* This function takes a message id and returns an escaped string\r
++ * which can be used as a Xapian query. This involves prefixing with\r
++ * `id:', putting the id inside double quotes, and doubling any\r
++ * occurence of a double quote in the message id itself.*/\r
++static char *\r
++xapian_escape_id (const void *ctx,\r
++         const char *msg_id)\r
++{\r
++    const char *c;\r
++    char *escaped_msg_id;\r
++    escaped_msg_id = talloc_strdup (ctx, "id:\"");\r
++    for (c=msg_id; *c; c++)\r
++      if (*c == '"')\r
++          escaped_msg_id = talloc_asprintf_append (escaped_msg_id, "\"\"");\r
++      else\r
++          escaped_msg_id = talloc_asprintf_append (escaped_msg_id, "%c", *c);\r
++    escaped_msg_id = talloc_asprintf_append (escaped_msg_id, "\"");\r
++    return escaped_msg_id;\r
++}\r
++\r
++static char *\r
++output_msg_query (const void *ctx,\r
++              sprinter_t *format,\r
++              notmuch_bool_t matching,\r
++              notmuch_bool_t first,\r
++              notmuch_messages_t *messages)\r
++{\r
++    notmuch_message_t *message;\r
++    char *query, *escaped_msg_id;\r
++    query = talloc_strdup (ctx, "");\r
++    for (;\r
++       notmuch_messages_valid (messages);\r
++       notmuch_messages_move_to_next (messages))\r
++    {\r
++      message = notmuch_messages_get (messages);\r
++      if (notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) == matching) {\r
++          escaped_msg_id = xapian_escape_id (ctx, notmuch_message_get_message_id (message));\r
++          if (first) {\r
++              query = talloc_asprintf_append (query, "%s", escaped_msg_id);\r
++              first = FALSE;\r
++          }\r
++          else\r
++              query = talloc_asprintf_append (query, " or %s", escaped_msg_id);\r
++          talloc_free (escaped_msg_id);\r
++      }\r
++      /* output_msg_query already starts with an ` or' */\r
++      query = talloc_asprintf_append (query, "%s", output_msg_query (ctx, format, matching, first, notmuch_message_get_replies (message)));\r
++    }\r
++    return query;\r
++}\r
++\r
+ static int\r
+ do_search_threads (sprinter_t *format,\r
+                  notmuch_query_t *query,\r
+@@ -88,7 +140,7 @@ do_search_threads (sprinter_t *format,\r
+           format->string (format,\r
+                           notmuch_thread_get_thread_id (thread));\r
+           format->separator (format);\r
+-      } else { /* output == OUTPUT_SUMMARY */\r
++      } else { /* output == OUTPUT_SUMMARY or OUTPUT_SUMMARY_WITH_QUERIES */\r
+           void *ctx_quote = talloc_new (thread);\r
+           const char *authors = notmuch_thread_get_authors (thread);\r
+           const char *subject = notmuch_thread_get_subject (thread);\r
+@@ -108,7 +160,7 @@ do_search_threads (sprinter_t *format,\r
+           relative_date = notmuch_time_relative_date (ctx_quote, date);\r
+ \r
+           if (format->is_text_printer) {\r
+-                /* Special case for the text formatter */\r
++               /* Special case for the text formatter */\r
+               printf ("thread:%s %12s [%d/%d] %s; %s (",\r
+                       thread_id,\r
+                       relative_date,\r
+@@ -133,8 +185,6 @@ do_search_threads (sprinter_t *format,\r
+               format->string (format, subject);\r
+           }\r
+ \r
+-          talloc_free (ctx_quote);\r
+-\r
+           format->map_key (format, "tags");\r
+           format->begin_list (format);\r
+ \r
+@@ -145,7 +195,7 @@ do_search_threads (sprinter_t *format,\r
+               const char *tag = notmuch_tags_get (tags);\r
+ \r
+               if (format->is_text_printer) {\r
+-                  /* Special case for the text formatter */\r
++                  /* Special case for the text formatter */\r
+                   if (first_tag)\r
+                       first_tag = FALSE;\r
+                   else\r
+@@ -160,8 +210,25 @@ do_search_threads (sprinter_t *format,\r
+               printf (")");\r
+ \r
+           format->end (format);\r
++\r
++          if (output == OUTPUT_SUMMARY_WITH_QUERIES) {\r
++              char *query;\r
++              query = output_msg_query (ctx_quote, format, TRUE, TRUE, notmuch_thread_get_toplevel_messages (thread));\r
++              if (strlen (query)) {\r
++                  format->map_key (format, "matching_msg_query");\r
++                  format->string (format, query);\r
++              }\r
++              query = output_msg_query (ctx_quote, format, FALSE, TRUE, notmuch_thread_get_toplevel_messages (thread));\r
++              if (strlen (query)) {\r
++                  format->map_key (format, "nonmatching_msg_query");\r
++                  format->string (format, query);\r
++              }\r
++          }\r
++\r
+           format->end (format);\r
+           format->separator (format);\r
++\r
++          talloc_free (ctx_quote);\r
+       }\r
+ \r
+       notmuch_thread_destroy (thread);\r
+@@ -303,6 +370,7 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
+     int offset = 0;\r
+     int limit = -1; /* unlimited */\r
+     int exclude = EXCLUDE_TRUE;\r
++    notmuch_bool_t with_queries = FALSE;\r
+     unsigned int i;\r
+ \r
+     enum { NOTMUCH_FORMAT_JSON, NOTMUCH_FORMAT_TEXT }\r
+@@ -323,12 +391,14 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
+                                 { "messages", OUTPUT_MESSAGES },\r
+                                 { "files", OUTPUT_FILES },\r
+                                 { "tags", OUTPUT_TAGS },\r
++                                { "with-queries", OUTPUT_SUMMARY_WITH_QUERIES },\r
+                                 { 0, 0 } } },\r
+         { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',\r
+           (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },\r
+                                   { "false", EXCLUDE_FALSE },\r
+                                   { "flag", EXCLUDE_FLAG },\r
+                                   { 0, 0 } } },\r
++        { NOTMUCH_OPT_BOOLEAN, &with_queries, "queries", 'b', 0 },\r
+       { NOTMUCH_OPT_INT, &offset, "offset", 'O', 0 },\r
+       { NOTMUCH_OPT_INT, &limit, "limit", 'L', 0  },\r
+       { 0, 0, 0, 0, 0 }\r
+@@ -398,6 +468,19 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
+           notmuch_query_set_omit_excluded (query, FALSE);\r
+     }\r
+ \r
++    if (with_queries) {\r
++      if (format_sel == NOTMUCH_FORMAT_TEXT) {\r
++          fprintf (stderr, "Warning: --queries=true not implemented for text format.\n");\r
++          with_queries = FALSE;\r
++      }\r
++      if (output != OUTPUT_SUMMARY) {\r
++          fprintf (stderr, "Warning: --queries=true only implemented for --output=summary.\n");\r
++          with_queries = FALSE;\r
++      }\r
++    }\r
++\r
++    if (with_queries) output = OUTPUT_SUMMARY_WITH_QUERIES;\r
++\r
+     switch (output) {\r
+     default:\r
+     case OUTPUT_SUMMARY:\r
+-- \r
+1.7.9.1\r
+\r