]> git.notmuchmail.org Git - notmuch/blobdiff - notmuch-search.c
Use notmuch_database_destroy instead of notmuch_database_close
[notmuch] / notmuch-search.c
index d504051c618ae9f2444365615c24d620b919a659..7dfd270e2019c651d6738a04e2cca7766a92bff7 100644 (file)
@@ -90,6 +90,9 @@ format_thread_json (const void *ctx,
                    const int total,
                    const char *authors,
                    const char *subject);
+
+/* Any changes to the JSON format should be reflected in the file
+ * devel/schemata. */
 static const search_format_t format_json = {
     "[",
        "{",
@@ -368,6 +371,9 @@ do_search_tags (notmuch_database_t *notmuch,
     const char *tag;
     int first_tag = 1;
 
+    /* should the following only special case if no excluded terms
+     * specified? */
+
     /* Special-case query of "*" for better performance. */
     if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
        tags = notmuch_database_get_all_tags (notmuch);
@@ -410,6 +416,12 @@ do_search_tags (notmuch_database_t *notmuch,
     return 0;
 }
 
+enum {
+    EXCLUDE_TRUE,
+    EXCLUDE_FALSE,
+    EXCLUDE_FLAG,
+};
+
 int
 notmuch_search_command (void *ctx, int argc, char *argv[])
 {
@@ -423,8 +435,7 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
     output_t output = OUTPUT_SUMMARY;
     int offset = 0;
     int limit = -1; /* unlimited */
-    const char **search_exclude_tags;
-    size_t search_exclude_tags_length;
+    int exclude = EXCLUDE_TRUE;
     unsigned int i;
 
     enum { NOTMUCH_FORMAT_JSON, NOTMUCH_FORMAT_TEXT }
@@ -446,6 +457,11 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
                                  { "files", OUTPUT_FILES },
                                  { "tags", OUTPUT_TAGS },
                                  { 0, 0 } } },
+        { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
+          (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
+                                  { "false", EXCLUDE_FALSE },
+                                  { "flag", EXCLUDE_FLAG },
+                                  { 0, 0 } } },
        { NOTMUCH_OPT_INT, &offset, "offset", 'O', 0 },
        { NOTMUCH_OPT_INT, &limit, "limit", 'L', 0  },
        { 0, 0, 0, 0, 0 }
@@ -493,10 +509,25 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
 
     notmuch_query_set_sort (query, sort);
 
-    search_exclude_tags = notmuch_config_get_search_exclude_tags
-       (config, &search_exclude_tags_length);
-    for (i = 0; i < search_exclude_tags_length; i++)
-       notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
+    if (exclude == EXCLUDE_FLAG && output != OUTPUT_SUMMARY) {
+       /* If we are not doing summary output there is nowhere to
+        * print the excluded flag so fall back on including the
+        * excluded messages. */
+       fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
+       exclude = EXCLUDE_FALSE;
+    }
+
+    if (exclude == EXCLUDE_TRUE || exclude == EXCLUDE_FLAG) {
+       const char **search_exclude_tags;
+       size_t search_exclude_tags_length;
+
+       search_exclude_tags = notmuch_config_get_search_exclude_tags
+           (config, &search_exclude_tags_length);
+       for (i = 0; i < search_exclude_tags_length; i++)
+           notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
+       if (exclude == EXCLUDE_FLAG)
+           notmuch_query_set_omit_excluded (query, FALSE);
+    }
 
     switch (output) {
     default:
@@ -514,7 +545,7 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
     }
 
     notmuch_query_destroy (query);
-    notmuch_database_close (notmuch);
+    notmuch_database_destroy (notmuch);
 
     return ret;
 }