[notmuch] [PATCH 2/2] Add a new "notmuch search-messages" command.
authorCarl Worth <cworth@cworth.org>
Sat, 21 Nov 2009 16:24:19 +0000 (17:24 +0100)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:35:38 +0000 (09:35 -0800)
02/5d0905dac026908977a7051323a82e57240aca [new file with mode: 0644]

diff --git a/02/5d0905dac026908977a7051323a82e57240aca b/02/5d0905dac026908977a7051323a82e57240aca
new file mode 100644 (file)
index 0000000..35b1480
--- /dev/null
@@ -0,0 +1,287 @@
+Return-Path: <cworth@cworth.org>\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 3EE83431FC4\r
+       for <notmuch@notmuchmail.org>; Sat, 21 Nov 2009 08:25:15 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\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 CaTBf7YbKHof; Sat, 21 Nov 2009 08:25:14 -0800 (PST)\r
+Received: from localhost.localdomain (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 717B2431FAE;\r
+       Sat, 21 Nov 2009 08:25:13 -0800 (PST)\r
+From: Carl Worth <cworth@cworth.org>\r
+To: notmuch@notmuchmail.org\r
+Date: Sat, 21 Nov 2009 17:24:19 +0100\r
+Message-Id: <1258820659-24473-3-git-send-email-cworth@cworth.org>\r
+X-Mailer: git-send-email 1.6.5.2\r
+In-Reply-To: <1258820659-24473-1-git-send-email-cworth@cworth.org>\r
+References: <1258820659-24473-1-git-send-email-cworth@cworth.org>\r
+Subject: [notmuch] [PATCH 2/2] Add a new "notmuch search-messages" command.\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.12\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, 21 Nov 2009 16:25:15 -0000\r
+\r
+This allows for searching for individual messages, rather than threads\r
+as "notmuch search" does. Currently just prints out the message id for\r
+each message.\r
+---\r
+ Makefile.local            |   29 +++++-----\r
+ notmuch-client.h          |    3 +\r
+ notmuch-search-messages.c |  138 +++++++++++++++++++++++++++++++++++++++++++++\r
+ notmuch.c                 |   29 ++++++++-\r
+ 4 files changed, 181 insertions(+), 18 deletions(-)\r
+ create mode 100644 notmuch-search-messages.c\r
+\r
+diff --git a/Makefile.local b/Makefile.local\r
+index 3c99624..288d5e9 100644\r
+--- a/Makefile.local\r
++++ b/Makefile.local\r
+@@ -2,20 +2,21 @@ all: notmuch notmuch.1.gz\r
\r
+ emacs: notmuch.elc\r
\r
+-notmuch_client_srcs =         \\r
+-      notmuch.c               \\r
+-      notmuch-config.c        \\r
+-      notmuch-dump.c          \\r
+-      notmuch-new.c           \\r
+-      notmuch-reply.c         \\r
+-      notmuch-restore.c       \\r
+-      notmuch-search.c        \\r
+-      notmuch-setup.c         \\r
+-      notmuch-show.c          \\r
+-      notmuch-tag.c           \\r
+-      notmuch-time.c          \\r
+-      gmime-filter-reply.c    \\r
+-      query-string.c          \\r
++notmuch_client_srcs =                 \\r
++      notmuch.c                       \\r
++      notmuch-config.c                \\r
++      notmuch-dump.c                  \\r
++      notmuch-new.c                   \\r
++      notmuch-reply.c                 \\r
++      notmuch-restore.c               \\r
++      notmuch-search.c                \\r
++      notmuch-search-messages.c       \\r
++      notmuch-setup.c                 \\r
++      notmuch-show.c                  \\r
++      notmuch-tag.c                   \\r
++      notmuch-time.c                  \\r
++      gmime-filter-reply.c            \\r
++      query-string.c                  \\r
+       show-message.c\r
\r
+ notmuch_client_modules = $(notmuch_client_srcs:.c=.o)\r
+diff --git a/notmuch-client.h b/notmuch-client.h\r
+index b65aa77..c4ca687 100644\r
+--- a/notmuch-client.h\r
++++ b/notmuch-client.h\r
+@@ -106,6 +106,9 @@ int\r
+ notmuch_search_command (void *ctx, int argc, char *argv[]);\r
\r
+ int\r
++notmuch_search_messages_command (void *ctx, int argc, char *argv[]);\r
++\r
++int\r
+ notmuch_setup_command (void *ctx, int argc, char *argv[]);\r
\r
+ int\r
+diff --git a/notmuch-search-messages.c b/notmuch-search-messages.c\r
+new file mode 100644\r
+index 0000000..b01d566\r
+--- /dev/null\r
++++ b/notmuch-search-messages.c\r
+@@ -0,0 +1,138 @@\r
++/* notmuch - Not much of an email program, (just index and search)\r
++ *\r
++ * Copyright © 2009 Carl Worth\r
++ *\r
++ * This program is free software: you can redistribute it and/or modify\r
++ * it under the terms of the GNU General Public License as published by\r
++ * the Free Software Foundation, either version 3 of the License, or\r
++ * (at your option) any later version.\r
++ *\r
++ * This program is distributed in the hope that it will be useful,\r
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
++ * GNU General Public License for more details.\r
++ *\r
++ * You should have received a copy of the GNU General Public License\r
++ * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
++ *\r
++ * Author: Carl Worth <cworth@cworth.org>\r
++ */\r
++\r
++#include "notmuch-client.h"\r
++\r
++/* If the user asks for a *lot* of results, lets give some results as\r
++ * quickly as possible and let the user read those while we compute\r
++ * the remainder. */\r
++#define NOTMUCH_SHOW_INITIAL_BURST 100\r
++\r
++static void\r
++do_search_messages (notmuch_query_t *query,\r
++                  int first, int max)\r
++{\r
++    notmuch_messages_t *messages;\r
++    notmuch_message_t *message;\r
++\r
++    for (messages = notmuch_query_search_messages (query, first, max);\r
++       notmuch_messages_has_more (messages);\r
++       notmuch_messages_advance (messages))\r
++    {\r
++      message = notmuch_messages_get (messages);\r
++\r
++      printf ("id:%s\n", notmuch_message_get_message_id (message));\r
++\r
++      notmuch_message_destroy (message);\r
++    }\r
++}\r
++\r
++int\r
++notmuch_search_messages_command (void *ctx, int argc, char *argv[])\r
++{\r
++    notmuch_config_t *config;\r
++    notmuch_database_t *notmuch;\r
++    notmuch_query_t *query;\r
++    char *query_str;\r
++    int i, first = 0, max = -1;\r
++    char *opt, *end;\r
++    notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;\r
++\r
++    for (i = 0; i < argc && argv[i][0] == '-'; i++) {\r
++      if (strcmp (argv[i], "--") == 0) {\r
++          i++;\r
++          break;\r
++      }\r
++      if (STRNCMP_LITERAL (argv[i], "--first=") == 0) {\r
++          opt = argv[i] + sizeof ("--first=") - 1;\r
++          first = strtoul (opt, &end, 10);\r
++          if (*opt == '\0' || *end != '\0') {\r
++              fprintf (stderr, "Invalid value for --first: %s\n", opt);\r
++              return 1;\r
++          }\r
++      } else if (STRNCMP_LITERAL (argv[i], "--max=") == 0) {\r
++          opt = argv[i] + sizeof ("--max=") - 1;\r
++          max = strtoul (opt, &end, 10);\r
++          if (*opt == '\0' || *end != '\0') {\r
++              fprintf (stderr, "Invalid value for --max: %s\n", opt);\r
++              return 1;\r
++          }\r
++      } else if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) {\r
++          opt = argv[i] + sizeof ("--sort=") - 1;\r
++          if (strcmp (opt, "oldest-first") == 0) {\r
++              sort = NOTMUCH_SORT_OLDEST_FIRST;\r
++          } else if (strcmp (opt, "newest-first") == 0) {\r
++              sort = NOTMUCH_SORT_NEWEST_FIRST;\r
++          } else {\r
++              fprintf (stderr, "Invalid value for --sort: %s\n", opt);\r
++              return 1;\r
++          }\r
++      } else {\r
++          fprintf (stderr, "Unrecognized option: %s\n", argv[i]);\r
++          return 1;\r
++      }\r
++    }\r
++\r
++    argc -= i;\r
++    argv += i;\r
++\r
++    config = notmuch_config_open (ctx, NULL, NULL);\r
++    if (config == NULL)\r
++      return 1;\r
++\r
++    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));\r
++    if (notmuch == NULL)\r
++      return 1;\r
++\r
++    query_str = query_string_from_args (ctx, argc, argv);\r
++    if (query_str == NULL) {\r
++      fprintf (stderr, "Out of memory.\n");\r
++      return 1;\r
++    }\r
++    if (*query_str == '\0') {\r
++      fprintf (stderr, "Error: notmuch search requires at least one search term.\n");\r
++      return 1;\r
++    }\r
++\r
++    query = notmuch_query_create (notmuch, query_str);\r
++    if (query == NULL) {\r
++      fprintf (stderr, "Out of memory\n");\r
++      return 1;\r
++    }\r
++\r
++    notmuch_query_set_sort (query, sort);\r
++\r
++    if (max < 0 || max > NOTMUCH_SHOW_INITIAL_BURST)\r
++    {\r
++      do_search_messages (query,\r
++                         first, NOTMUCH_SHOW_INITIAL_BURST);\r
++\r
++      first += NOTMUCH_SHOW_INITIAL_BURST;\r
++      if (max > 0)\r
++          max -= NOTMUCH_SHOW_INITIAL_BURST;\r
++    }\r
++\r
++    do_search_messages (query, first, max);\r
++\r
++    notmuch_query_destroy (query);\r
++    notmuch_database_close (notmuch);\r
++\r
++    return 0;\r
++}\r
+diff --git a/notmuch.c b/notmuch.c\r
+index 14d4865..6440777 100644\r
+--- a/notmuch.c\r
++++ b/notmuch.c\r
+@@ -132,10 +132,31 @@ command_t commands[] = {\r
+       "\t\tnot previously been run." },\r
+     { "search", notmuch_search_command,\r
+       "[options...] <search-terms> [...]",\r
+-      "\t\tSearch for messages matching the given search terms.",\r
+-      "\t\tNote that the individual mail messages will be matched\n"\r
+-      "\t\tagainst the search terms, but the results will be the\n"\r
+-      "\t\tthreads (one per line) containing the matched messages.\n"\r
++      "\t\tSearch for threads with messages matching the search terms.",\r
++      "\n"\r
++      "\t\tSupported options for search include:\n"\r
++      "\n"\r
++      "\t\t--max=<value>\n"\r
++      "\n"\r
++      "\t\t\tRestricts displayed search results to a subset\n"\r
++      "\t\t\tof the results that would match the terms.\n"\r
++      "\n"\r
++      "\t\t--first=<value>\n"\r
++      "\n"\r
++      "\t\t\tOmits the first <value> threads from the search\n"\r
++      "\t\t\tresults that would otherwise be displayed.\n"\r
++      "\n"\r
++      "\t\t--sort=(newest-first|oldest-first)\n"\r
++      "\n"\r
++      "\t\t\tPresent results in either chronological order\n"\r
++      "\t\t\t(oldest-first) or reverse chronological order\n"\r
++      "\t\t\t(newest-first), which is the default.\n"\r
++      "\n"\r
++      "\t\tSee \"notmuch help search-terms\" for details of the search\n"\r
++      "\t\tterms syntax." },\r
++    { "search-messages", notmuch_search_messages_command,\r
++      "[options...] <search-terms> [...]",\r
++      "\t\tSearch for messages matching the search terms.",\r
+       "\n"\r
+       "\t\tSupported options for search include:\n"\r
+       "\n"\r
+-- \r
+1.6.5.2\r
+\r