[notmuch] [PATCH 2/2] Add a new "notmuch search-messages" command.

Carl Worth cworth at cworth.org
Sat Nov 21 08:24:19 PST 2009


This allows for searching for individual messages, rather than threads
as "notmuch search" does. Currently just prints out the message id for
each message.
---
 Makefile.local            |   29 +++++-----
 notmuch-client.h          |    3 +
 notmuch-search-messages.c |  138 +++++++++++++++++++++++++++++++++++++++++++++
 notmuch.c                 |   29 ++++++++-
 4 files changed, 181 insertions(+), 18 deletions(-)
 create mode 100644 notmuch-search-messages.c

diff --git a/Makefile.local b/Makefile.local
index 3c99624..288d5e9 100644
--- a/Makefile.local
+++ b/Makefile.local
@@ -2,20 +2,21 @@ all: notmuch notmuch.1.gz
 
 emacs: notmuch.elc
 
-notmuch_client_srcs =		\
-	notmuch.c		\
-	notmuch-config.c	\
-	notmuch-dump.c		\
-	notmuch-new.c		\
-	notmuch-reply.c		\
-	notmuch-restore.c	\
-	notmuch-search.c	\
-	notmuch-setup.c		\
-	notmuch-show.c		\
-	notmuch-tag.c		\
-	notmuch-time.c		\
-	gmime-filter-reply.c	\
-	query-string.c		\
+notmuch_client_srcs =			\
+	notmuch.c			\
+	notmuch-config.c		\
+	notmuch-dump.c			\
+	notmuch-new.c			\
+	notmuch-reply.c			\
+	notmuch-restore.c		\
+	notmuch-search.c		\
+	notmuch-search-messages.c	\
+	notmuch-setup.c			\
+	notmuch-show.c			\
+	notmuch-tag.c			\
+	notmuch-time.c			\
+	gmime-filter-reply.c		\
+	query-string.c			\
 	show-message.c
 
 notmuch_client_modules = $(notmuch_client_srcs:.c=.o)
diff --git a/notmuch-client.h b/notmuch-client.h
index b65aa77..c4ca687 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -106,6 +106,9 @@ int
 notmuch_search_command (void *ctx, int argc, char *argv[]);
 
 int
+notmuch_search_messages_command (void *ctx, int argc, char *argv[]);
+
+int
 notmuch_setup_command (void *ctx, int argc, char *argv[]);
 
 int
diff --git a/notmuch-search-messages.c b/notmuch-search-messages.c
new file mode 100644
index 0000000..b01d566
--- /dev/null
+++ b/notmuch-search-messages.c
@@ -0,0 +1,138 @@
+/* notmuch - Not much of an email program, (just index and search)
+ *
+ * Copyright © 2009 Carl Worth
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see http://www.gnu.org/licenses/ .
+ *
+ * Author: Carl Worth <cworth at cworth.org>
+ */
+
+#include "notmuch-client.h"
+
+/* If the user asks for a *lot* of results, lets give some results as
+ * quickly as possible and let the user read those while we compute
+ * the remainder. */
+#define NOTMUCH_SHOW_INITIAL_BURST 100
+
+static void
+do_search_messages (notmuch_query_t *query,
+		    int first, int max)
+{
+    notmuch_messages_t *messages;
+    notmuch_message_t *message;
+
+    for (messages = notmuch_query_search_messages (query, first, max);
+	 notmuch_messages_has_more (messages);
+	 notmuch_messages_advance (messages))
+    {
+	message = notmuch_messages_get (messages);
+
+	printf ("id:%s\n", notmuch_message_get_message_id (message));
+
+	notmuch_message_destroy (message);
+    }
+}
+
+int
+notmuch_search_messages_command (void *ctx, int argc, char *argv[])
+{
+    notmuch_config_t *config;
+    notmuch_database_t *notmuch;
+    notmuch_query_t *query;
+    char *query_str;
+    int i, first = 0, max = -1;
+    char *opt, *end;
+    notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
+
+    for (i = 0; i < argc && argv[i][0] == '-'; i++) {
+	if (strcmp (argv[i], "--") == 0) {
+	    i++;
+	    break;
+	}
+	if (STRNCMP_LITERAL (argv[i], "--first=") == 0) {
+	    opt = argv[i] + sizeof ("--first=") - 1;
+	    first = strtoul (opt, &end, 10);
+	    if (*opt == '\0' || *end != '\0') {
+		fprintf (stderr, "Invalid value for --first: %s\n", opt);
+		return 1;
+	    }
+	} else if (STRNCMP_LITERAL (argv[i], "--max=") == 0) {
+	    opt = argv[i] + sizeof ("--max=") - 1;
+	    max = strtoul (opt, &end, 10);
+	    if (*opt == '\0' || *end != '\0') {
+		fprintf (stderr, "Invalid value for --max: %s\n", opt);
+		return 1;
+	    }
+	} else if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) {
+	    opt = argv[i] + sizeof ("--sort=") - 1;
+	    if (strcmp (opt, "oldest-first") == 0) {
+		sort = NOTMUCH_SORT_OLDEST_FIRST;
+	    } else if (strcmp (opt, "newest-first") == 0) {
+		sort = NOTMUCH_SORT_NEWEST_FIRST;
+	    } else {
+		fprintf (stderr, "Invalid value for --sort: %s\n", opt);
+		return 1;
+	    }
+	} else {
+	    fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
+	    return 1;
+	}
+    }
+
+    argc -= i;
+    argv += i;
+
+    config = notmuch_config_open (ctx, NULL, NULL);
+    if (config == NULL)
+	return 1;
+
+    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
+    if (notmuch == NULL)
+	return 1;
+
+    query_str = query_string_from_args (ctx, argc, argv);
+    if (query_str == NULL) {
+	fprintf (stderr, "Out of memory.\n");
+	return 1;
+    }
+    if (*query_str == '\0') {
+	fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
+	return 1;
+    }
+
+    query = notmuch_query_create (notmuch, query_str);
+    if (query == NULL) {
+	fprintf (stderr, "Out of memory\n");
+	return 1;
+    }
+
+    notmuch_query_set_sort (query, sort);
+
+    if (max < 0 || max > NOTMUCH_SHOW_INITIAL_BURST)
+    {
+	do_search_messages (query,
+			   first, NOTMUCH_SHOW_INITIAL_BURST);
+
+	first += NOTMUCH_SHOW_INITIAL_BURST;
+	if (max > 0)
+	    max -= NOTMUCH_SHOW_INITIAL_BURST;
+    }
+
+    do_search_messages (query, first, max);
+
+    notmuch_query_destroy (query);
+    notmuch_database_close (notmuch);
+
+    return 0;
+}
diff --git a/notmuch.c b/notmuch.c
index 14d4865..6440777 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -132,10 +132,31 @@ command_t commands[] = {
       "\t\tnot previously been run." },
     { "search", notmuch_search_command,
       "[options...] <search-terms> [...]",
-      "\t\tSearch for messages matching the given search terms.",
-      "\t\tNote that the individual mail messages will be matched\n"
-      "\t\tagainst the search terms, but the results will be the\n"
-      "\t\tthreads (one per line) containing the matched messages.\n"
+      "\t\tSearch for threads with messages matching the search terms.",
+      "\n"
+      "\t\tSupported options for search include:\n"
+      "\n"
+      "\t\t--max=<value>\n"
+      "\n"
+      "\t\t\tRestricts displayed search results to a subset\n"
+      "\t\t\tof the results that would match the terms.\n"
+      "\n"
+      "\t\t--first=<value>\n"
+      "\n"
+      "\t\t\tOmits the first <value> threads from the search\n"
+      "\t\t\tresults that would otherwise be displayed.\n"
+      "\n"
+      "\t\t--sort=(newest-first|oldest-first)\n"
+      "\n"
+      "\t\t\tPresent results in either chronological order\n"
+      "\t\t\t(oldest-first) or reverse chronological order\n"
+      "\t\t\t(newest-first), which is the default.\n"
+      "\n"
+      "\t\tSee \"notmuch help search-terms\" for details of the search\n"
+      "\t\tterms syntax." },
+    { "search-messages", notmuch_search_messages_command,
+      "[options...] <search-terms> [...]",
+      "\t\tSearch for messages matching the search terms.",
       "\n"
       "\t\tSupported options for search include:\n"
       "\n"
-- 
1.6.5.2



More information about the notmuch mailing list