]> git.cworth.org Git - notmuch/blobdiff - notmuch-search.c
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / notmuch-search.c
index fd0b58c5d32fd7b3aed6777eb443a1606758ad5c..327e144564de48e0b339036528505d5a227bc40a 100644 (file)
@@ -52,9 +52,11 @@ typedef enum {
 
 typedef struct {
     notmuch_database_t *notmuch;
+    void *talloc_ctx;
     int format_sel;
     sprinter_t *format;
     int exclude;
+    int query_syntax;
     notmuch_query_t *query;
     int sort;
     int output;
@@ -90,9 +92,13 @@ get_thread_query (notmuch_thread_t *thread,
         notmuch_messages_move_to_next (messages)) {
        notmuch_message_t *message = notmuch_messages_get (messages);
        const char *mid = notmuch_message_get_message_id (message);
+       notmuch_bool_t is_set;
+       char **buf;
+
+       if (notmuch_message_get_flag_st (message, NOTMUCH_MESSAGE_FLAG_MATCH, &is_set))
+           return -1;
        /* Determine which query buffer to extend */
-       char **buf = notmuch_message_get_flag (
-           message, NOTMUCH_MESSAGE_FLAG_MATCH) ? matched_out : unmatched_out;
+       buf = is_set ? matched_out : unmatched_out;
        /* Add this message's id: query.  Since "id" is an exclusive
         * prefix, it is implicitly 'or'd together, so we only need to
         * join queries with a space. */
@@ -673,28 +679,29 @@ do_search_tags (const search_context_t *ctx)
 }
 
 static int
-_notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int argc, char *argv[])
+_notmuch_search_prepare (search_context_t *ctx, int argc, char *argv[])
 {
     char *query_str;
-    unsigned int i;
-    char *status_string = NULL;
+
+    if (! ctx->talloc_ctx)
+       ctx->talloc_ctx = talloc_new (NULL);
 
     switch (ctx->format_sel) {
     case NOTMUCH_FORMAT_TEXT:
-       ctx->format = sprinter_text_create (config, stdout);
+       ctx->format = sprinter_text_create (ctx->talloc_ctx, stdout);
        break;
     case NOTMUCH_FORMAT_TEXT0:
        if (ctx->output == OUTPUT_SUMMARY) {
            fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
            return EXIT_FAILURE;
        }
-       ctx->format = sprinter_text0_create (config, stdout);
+       ctx->format = sprinter_text0_create (ctx->talloc_ctx, stdout);
        break;
     case NOTMUCH_FORMAT_JSON:
-       ctx->format = sprinter_json_create (config, stdout);
+       ctx->format = sprinter_json_create (ctx->talloc_ctx, stdout);
        break;
     case NOTMUCH_FORMAT_SEXP:
-       ctx->format = sprinter_sexp_create (config, stdout);
+       ctx->format = sprinter_sexp_create (ctx->talloc_ctx, stdout);
        break;
     default:
        /* this should never happen */
@@ -703,20 +710,6 @@ _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int ar
 
     notmuch_exit_if_unsupported_format ();
 
-    if (notmuch_database_open_verbose (
-           notmuch_config_get_database_path (config),
-           NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch, &status_string)) {
-
-       if (status_string) {
-           fputs (status_string, stderr);
-           free (status_string);
-       }
-
-       return EXIT_FAILURE;
-    }
-
-    notmuch_exit_if_unmatched_db_uuid (ctx->notmuch);
-
     query_str = query_string_from_args (ctx->notmuch, argc, argv);
     if (query_str == NULL) {
        fprintf (stderr, "Out of memory.\n");
@@ -727,11 +720,11 @@ _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int ar
        return EXIT_FAILURE;
     }
 
-    ctx->query = notmuch_query_create (ctx->notmuch, query_str);
-    if (ctx->query == NULL) {
-       fprintf (stderr, "Out of memory\n");
+    if (print_status_database ("notmuch search", ctx->notmuch,
+                              notmuch_query_create_with_syntax (ctx->notmuch, query_str,
+                                                                shared_option_query_syntax (),
+                                                                &ctx->query)))
        return EXIT_FAILURE;
-    }
 
     notmuch_query_set_sort (ctx->query, ctx->sort);
 
@@ -744,21 +737,20 @@ _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int ar
     }
 
     if (ctx->exclude != NOTMUCH_EXCLUDE_FALSE) {
-       const char **search_exclude_tags;
-       size_t search_exclude_tags_length;
+       notmuch_config_values_t *exclude_tags;
        notmuch_status_t status;
 
-       search_exclude_tags = notmuch_config_get_search_exclude_tags (
-           config, &search_exclude_tags_length);
+       for (exclude_tags = notmuch_config_get_values (ctx->notmuch, NOTMUCH_CONFIG_EXCLUDE_TAGS);
+            notmuch_config_values_valid (exclude_tags);
+            notmuch_config_values_move_to_next (exclude_tags)) {
 
-       for (i = 0; i < search_exclude_tags_length; i++) {
-           status = notmuch_query_add_tag_exclude (ctx->query, search_exclude_tags[i]);
+           status = notmuch_query_add_tag_exclude (ctx->query,
+                                                   notmuch_config_values_get (exclude_tags));
            if (status && status != NOTMUCH_STATUS_IGNORED) {
                print_status_query ("notmuch search", ctx->query, status);
                return EXIT_FAILURE;
            }
        }
-
        notmuch_query_set_omit_excluded (ctx->query, ctx->exclude);
     }
 
@@ -778,6 +770,7 @@ static search_context_t search_context = {
     .format_sel = NOTMUCH_FORMAT_TEXT,
     .exclude = NOTMUCH_EXCLUDE_TRUE,
     .sort = NOTMUCH_SORT_NEWEST_FIRST,
+    .query_syntax = NOTMUCH_QUERY_SYNTAX_XAPIAN,
     .output = 0,
     .offset = 0,
     .limit = -1, /* unlimited */
@@ -801,7 +794,7 @@ static const notmuch_opt_desc_t common_options[] = {
 };
 
 int
-notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
+notmuch_search_command (notmuch_database_t *notmuch, int argc, char *argv[])
 {
     search_context_t *ctx = &search_context;
     int opt_index, ret;
@@ -828,21 +821,22 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
        { }
     };
 
+    ctx->notmuch = notmuch;
     ctx->output = OUTPUT_SUMMARY;
     opt_index = parse_arguments (argc, argv, options, 1);
     if (opt_index < 0)
        return EXIT_FAILURE;
 
-    notmuch_process_shared_options (argv[0]);
+    notmuch_process_shared_options (notmuch, argv[0]);
 
     if (ctx->output != OUTPUT_FILES && ctx->output != OUTPUT_MESSAGES &&
        ctx->dupe != -1) {
-       fprintf (stderr, "Error: --duplicate=N is only supported with --output=files and --output=messages.\n");
+       fprintf (stderr,
+                "Error: --duplicate=N is only supported with --output=files and --output=messages.\n");
        return EXIT_FAILURE;
     }
 
-    if (_notmuch_search_prepare (ctx, config,
-                                argc - opt_index, argv + opt_index))
+    if (_notmuch_search_prepare (ctx, argc - opt_index, argv + opt_index))
        return EXIT_FAILURE;
 
     switch (ctx->output) {
@@ -867,7 +861,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
 }
 
 int
-notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])
+notmuch_address_command (notmuch_database_t *notmuch, int argc, char *argv[])
 {
     search_context_t *ctx = &search_context;
     int opt_index, ret;
@@ -893,11 +887,13 @@ notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])
        { }
     };
 
+    ctx->notmuch = notmuch;
+
     opt_index = parse_arguments (argc, argv, options, 1);
     if (opt_index < 0)
        return EXIT_FAILURE;
 
-    notmuch_process_shared_options (argv[0]);
+    notmuch_process_shared_options (notmuch, argv[0]);
 
     if (! (ctx->output & (OUTPUT_SENDER | OUTPUT_RECIPIENTS)))
        ctx->output |= OUTPUT_SENDER;
@@ -907,8 +903,7 @@ notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])
        return EXIT_FAILURE;
     }
 
-    if (_notmuch_search_prepare (ctx, config,
-                                argc - opt_index, argv + opt_index))
+    if (_notmuch_search_prepare (ctx, argc - opt_index, argv + opt_index))
        return EXIT_FAILURE;
 
     ctx->addresses = g_hash_table_new_full (strcase_hash, strcase_equal,