RFC: "notmuch search-messages"

classic Classic list List threaded Threaded
3 messages Options
Carl Worth-2 Carl Worth-2
Reply | Threaded
Open this post in threaded view
|

RFC: "notmuch search-messages"

Here's a quick proposal for a new "notmuch search-messages" command,
from which one can generate a list of Message IDs matching a search
string.

Beyond this, I'd like to add some --format options to both "search"
and "search-messages" so that users can get out the information they
want, (such as thread-id only from a thread search, filenames only
from a message search, etc.).

Any thoughts from others on this kind of thing?

I know people have been talking about doing thing like modifying
message files based on tags, and this seems like the beginnings of
support for doing scripted operations like that.

-Carl


Carl Worth-2 Carl Worth-2
Reply | Threaded
Open this post in threaded view
|

[PATCH 1/2] notmuch search: Rename the --max-threads option to --max.

The idea is to make this a little more general so that different
commands can use identical option names.
---
 notmuch-search.c |   22 +++++++++++-----------
 notmuch.c        |    2 +-
 2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/notmuch-search.c b/notmuch-search.c
index 2b1c0fe..845321d 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -29,7 +29,7 @@ static void
 do_search_threads (const void *ctx,
    notmuch_query_t *query,
    notmuch_sort_t sort,
-   int first, int max_threads)
+   int first, int max)
 {
     notmuch_thread_t *thread;
     notmuch_threads_t *threads;
@@ -37,7 +37,7 @@ do_search_threads (const void *ctx,
     time_t date;
     const char *relative_date;
 
-    for (threads = notmuch_query_search_threads (query, first, max_threads);
+    for (threads = notmuch_query_search_threads (query, first, max);
  notmuch_threads_has_more (threads);
  notmuch_threads_advance (threads))
     {
@@ -83,7 +83,7 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
     notmuch_database_t *notmuch;
     notmuch_query_t *query;
     char *query_str;
-    int i, first = 0, max_threads = -1;
+    int i, first = 0, max = -1;
     char *opt, *end;
     notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
 
@@ -99,11 +99,11 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
  fprintf (stderr, "Invalid value for --first: %s\n", opt);
  return 1;
     }
- } else if (STRNCMP_LITERAL (argv[i], "--max-threads=") == 0) {
-    opt = argv[i] + sizeof ("--max-threads=") - 1;
-    max_threads = strtoul (opt, &end, 10);
+ } 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-threads: %s\n", opt);
+ fprintf (stderr, "Invalid value for --max: %s\n", opt);
  return 1;
     }
  } else if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) {
@@ -151,17 +151,17 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
 
     notmuch_query_set_sort (query, sort);
 
-    if (max_threads < 0 || max_threads > NOTMUCH_SHOW_INITIAL_BURST)
+    if (max < 0 || max > NOTMUCH_SHOW_INITIAL_BURST)
     {
  do_search_threads (ctx, query, sort,
    first, NOTMUCH_SHOW_INITIAL_BURST);
 
  first += NOTMUCH_SHOW_INITIAL_BURST;
- if (max_threads > 0)
-    max_threads -= NOTMUCH_SHOW_INITIAL_BURST;
+ if (max > 0)
+    max -= NOTMUCH_SHOW_INITIAL_BURST;
     }
 
-    do_search_threads (ctx, query, sort, first, max_threads);
+    do_search_threads (ctx, query, sort, first, max);
 
     notmuch_query_destroy (query);
     notmuch_database_close (notmuch);
diff --git a/notmuch.c b/notmuch.c
index 5cc8e4c..14d4865 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -139,7 +139,7 @@ command_t commands[] = {
       "\n"
       "\t\tSupported options for search include:\n"
       "\n"
-      "\t\t--max-threads=<value>\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"
--
1.6.5.2


Carl Worth-2 Carl Worth-2
Reply | Threaded
Open this post in threaded view
|

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

In reply to this post by Carl Worth-2
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 <[hidden email]>
+ */
+
+#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