Re: [PATCH v2 6/7] cli: allow search mode to include msg-ids with JSON output
authorTomi Ollila <tomi.ollila@iki.fi>
Sat, 24 Nov 2012 22:30:43 +0000 (00:30 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:50:52 +0000 (09:50 -0800)
a5/8904ec46d679dcee64ebb15f7e76070ab44bc7 [new file with mode: 0644]

diff --git a/a5/8904ec46d679dcee64ebb15f7e76070ab44bc7 b/a5/8904ec46d679dcee64ebb15f7e76070ab44bc7
new file mode 100644 (file)
index 0000000..439b812
--- /dev/null
@@ -0,0 +1,281 @@
+Return-Path: <tomi.ollila@iki.fi>\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 E15FA431FAF\r
+       for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 14:30:56 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+       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 FiEkWJl2nTP2 for <notmuch@notmuchmail.org>;\r
+       Sat, 24 Nov 2012 14:30:47 -0800 (PST)\r
+Received: from guru.guru-group.fi (guru.guru-group.fi [46.183.73.34])\r
+       by olra.theworths.org (Postfix) with ESMTP id 0980C431FAE\r
+       for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 14:30:47 -0800 (PST)\r
+Received: from guru.guru-group.fi (localhost [IPv6:::1])\r
+       by guru.guru-group.fi (Postfix) with ESMTP id D3BF610010B;\r
+       Sun, 25 Nov 2012 00:30:43 +0200 (EET)\r
+From: Tomi Ollila <tomi.ollila@iki.fi>\r
+To: markwalters1009 <markwalters1009@gmail.com>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH v2 6/7] cli: allow search mode to include msg-ids with\r
+       JSON output\r
+In-Reply-To: <1353763256-32336-7-git-send-email-markwalters1009@gmail.com>\r
+References: <1353763256-32336-1-git-send-email-markwalters1009@gmail.com>\r
+       <1353763256-32336-7-git-send-email-markwalters1009@gmail.com>\r
+User-Agent: Notmuch/0.14+84~g8a199bf (http://notmuchmail.org) Emacs/24.2.1\r
+       (x86_64-unknown-linux-gnu)\r
+X-Face: HhBM'cA~<r"^Xv\KRN0P{vn'Y"Kd;zg_y3S[4)KSN~s?O\"QPoL\r
+       $[Xv_BD:i/F$WiEWax}R(MPS`^UaptOGD`*/=@\1lKoVa9tnrg0TW?"r7aRtgk[F\r
+       !)g;OY^,BjTbr)Np:%c_o'jj,Z\r
+Date: Sun, 25 Nov 2012 00:30:43 +0200\r
+Message-ID: <m2624uk50s.fsf@guru.guru-group.fi>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain\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 22:30:57 -0000\r
+\r
+On Sat, Nov 24 2012, markwalters1009 wrote:\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
+\r
+Please see (mostly formatting) comments inline below.\r
+\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
+second line indentation, not at '(' level as elsewhere\r
+\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
+\r
+spacing above\r
+\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
+If Austin sees fit he can comment the O(...) of the addition of msgids\r
+to query strings -- it would take quite an overhaul to the functionality\r
+if the escaped msgid's were directly written to query string...\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
+indentation level above\r
+\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
+\r
+irrelevant spacing change\r
+\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
+\r
+irrelevant spacing change\r
+\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
+\r
+this "with-queries" confuses me -- old version or undocumented feature ?\r
+\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
+newline before output = ...\r
+\r
+> +\r
+>      switch (output) {\r
+>      default:\r
+>      case OUTPUT_SUMMARY:\r
+> -- \r
+> 1.7.9.1\r
+>\r
+> _______________________________________________\r
+> notmuch mailing list\r
+> notmuch@notmuchmail.org\r
+> http://notmuchmail.org/mailman/listinfo/notmuch\r