[notmuch] [RFC/PATCH] Add search-files command

Ali Polatel alip at exherbo.org
Wed Jan 13 02:24:22 PST 2010


This command can be used to integrate notmuch with other MUAs as a
searching client. The idea is simple, a simple script could get
search-terms as argument and create a "virtual" maildir which has
symbolic links to files output by search-files command. This is similar
to nmzmail.
---
 Makefile.local         |    1 +
 notmuch-client.h       |    3 +
 notmuch-search-files.c |  107 ++++++++++++++++++++++++++++++++++++++++++++++++
 notmuch.c              |   13 ++++++
 4 files changed, 124 insertions(+), 0 deletions(-)
 create mode 100644 notmuch-search-files.c

diff --git a/Makefile.local b/Makefile.local
index 933ff4c..78bc25d 100644
--- a/Makefile.local
+++ b/Makefile.local
@@ -12,6 +12,7 @@ notmuch_client_srcs =		\
 	notmuch-reply.c		\
 	notmuch-restore.c	\
 	notmuch-search.c	\
+	notmuch-search-files.c  \
 	notmuch-search-tags.c   \
 	notmuch-setup.c		\
 	notmuch-show.c		\
diff --git a/notmuch-client.h b/notmuch-client.h
index 77766de..d505d30 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -96,6 +96,9 @@ int
 notmuch_search_command (void *ctx, int argc, char *argv[]);
 
 int
+notmuch_search_files_command (void *ctx, int argc, char *argv[]);
+
+int
 notmuch_setup_command (void *ctx, int argc, char *argv[]);
 
 int
diff --git a/notmuch-search-files.c b/notmuch-search-files.c
new file mode 100644
index 0000000..b48783a
--- /dev/null
+++ b/notmuch-search-files.c
@@ -0,0 +1,107 @@
+/* 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: Ali Polatel <alip at exherbo.org>
+ */
+
+#include "notmuch-client.h"
+
+static void
+do_search_files (notmuch_query_t *query)
+{
+    notmuch_message_t *message;
+    notmuch_messages_t *messages;
+
+    for (messages = notmuch_query_search_messages (query);
+	 notmuch_messages_has_more (messages);
+	 notmuch_messages_advance (messages))
+    {
+	message = notmuch_messages_get (messages);
+	printf ("%s\n", notmuch_message_get_filename (message));
+	notmuch_message_destroy(message);
+    }
+}
+
+int
+notmuch_search_files_command (void *ctx, int argc, char *argv[])
+{
+    notmuch_config_t *config;
+    notmuch_database_t *notmuch;
+    notmuch_query_t *query;
+    char *query_str;
+    char *opt;
+    notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
+    int i;
+
+    for (i = 0; i < argc && argv[i][0] == '-'; i++) {
+	if (strcmp (argv[i], "--") == 0) {
+	    i++;
+	    break;
+	}
+        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),
+				     NOTMUCH_DATABASE_MODE_READ_ONLY);
+    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-files 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);
+
+    do_search_files (query);
+
+    notmuch_query_destroy (query);
+    notmuch_database_close (notmuch);
+
+    return 0;
+}
diff --git a/notmuch.c b/notmuch.c
index 87479f8..4907339 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -165,6 +165,19 @@ command_t commands[] = {
       "\n"
       "\t\tSee \"notmuch help search-terms\" for details of the search\n"
       "\t\tterms syntax." },
+    { "search-files", notmuch_search_files_command,
+      "[options...] <search-terms> [...]",
+      "\t\tSearch for filenames matching the given search terms.",
+      "\t\tSupported options for search-files include:\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." },
     { "show", notmuch_show_command,
       "<search-terms> [...]",
       "\t\tShow all messages matching the search terms.",
-- 
1.6.6



More information about the notmuch mailing list