unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Jani Nikula <jani@nikula.org>
To: Michal Sojka <sojkam1@fel.cvut.cz>
Cc: notmuch@notmuchmail.org
Subject: [RFC PATCH 2/2] cli: add notmuch address command
Date: Sat,  1 Nov 2014 13:30:48 +0200	[thread overview]
Message-ID: <1bc05ab764867004fd5b6c79266ffcd0249909cd.1414839970.git.jani@nikula.org> (raw)
In-Reply-To: <cover.1414839970.git.jani@nikula.org>
In-Reply-To: <cover.1414839970.git.jani@nikula.org>

---
 notmuch-client.h |   3 ++
 notmuch-search.c | 128 +++++++++++++++++++++++++++++++++++--------------------
 notmuch.c        |   2 +
 3 files changed, 87 insertions(+), 46 deletions(-)

diff --git a/notmuch-client.h b/notmuch-client.h
index e1efbe0c8252..5e0d47508c6a 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -199,6 +199,9 @@ int
 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[]);
 
 int
+notmuch_address_command (notmuch_config_t *config, int argc, char *argv[]);
+
+int
 notmuch_setup_command (notmuch_config_t *config, int argc, char *argv[]);
 
 int
diff --git a/notmuch-search.c b/notmuch-search.c
index 671fe4139981..07755f115776 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -35,6 +35,7 @@ typedef enum {
 #define OUTPUT_ADDRESS_FLAGS (OUTPUT_SENDER | OUTPUT_RECIPIENTS)
 
 typedef struct {
+    notmuch_bool_t address_command;
     sprinter_t *format;
     notmuch_query_t *query;
     notmuch_sort_t sort;
@@ -440,17 +441,11 @@ do_search_tags (notmuch_database_t *notmuch,
     return 0;
 }
 
-int
-notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
+static int
+_notmuch_search_command (notmuch_config_t *config, int argc, char *argv[],
+			 search_options_t *opt, notmuch_opt_desc_t *subopts)
 {
     notmuch_database_t *notmuch;
-    search_options_t opt = {
-	.sort = NOTMUCH_SORT_NEWEST_FIRST,
-	.output = 0,
-	.offset = 0,
-	.limit = -1, /* unlimited */
-	.dupe = -1,
-    };
     char *query_str;
     int opt_index, ret;
     notmuch_exclude_t exclude = NOTMUCH_EXCLUDE_TRUE;
@@ -464,7 +459,8 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
     } format_sel = NOTMUCH_FORMAT_TEXT;
 
     notmuch_opt_desc_t options[] = {
-	{ NOTMUCH_OPT_KEYWORD, &opt.sort, "sort", 's',
+	{ NOTMUCH_OPT_INHERIT, subopts, NULL, 0, 0 },
+	{ NOTMUCH_OPT_KEYWORD, &opt->sort, "sort", 's',
 	  (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
 				  { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
 				  { 0, 0 } } },
@@ -475,24 +471,15 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
 				  { "text0", NOTMUCH_FORMAT_TEXT0 },
 				  { 0, 0 } } },
 	{ NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 },
-	{ NOTMUCH_OPT_KEYWORD_FLAGS, &opt.output, "output", 'o',
-	  (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
-				  { "threads", OUTPUT_THREADS },
-				  { "messages", OUTPUT_MESSAGES },
-				  { "sender", OUTPUT_SENDER },
-				  { "recipients", OUTPUT_RECIPIENTS },
-				  { "files", OUTPUT_FILES },
-				  { "tags", OUTPUT_TAGS },
-				  { 0, 0 } } },
         { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
           (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
                                   { "false", NOTMUCH_EXCLUDE_FALSE },
                                   { "flag", NOTMUCH_EXCLUDE_FLAG },
                                   { "all", NOTMUCH_EXCLUDE_ALL },
                                   { 0, 0 } } },
-	{ NOTMUCH_OPT_INT, &opt.offset, "offset", 'O', 0 },
-	{ NOTMUCH_OPT_INT, &opt.limit, "limit", 'L', 0  },
-	{ NOTMUCH_OPT_INT, &opt.dupe, "duplicate", 'D', 0  },
+	{ NOTMUCH_OPT_INT, &opt->offset, "offset", 'O', 0 },
+	{ NOTMUCH_OPT_INT, &opt->limit, "limit", 'L', 0  },
+	{ NOTMUCH_OPT_INT, &opt->dupe, "duplicate", 'D', 0  },
 	{ 0, 0, 0, 0, 0 }
     };
 
@@ -500,25 +487,25 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
     if (opt_index < 0)
 	return EXIT_FAILURE;
 
-    if (! opt.output)
-	opt.output = OUTPUT_SUMMARY;
+    if (! opt->output)
+	opt->output = OUTPUT_SUMMARY;
 
     switch (format_sel) {
     case NOTMUCH_FORMAT_TEXT:
-	opt.format = sprinter_text_create (config, stdout);
+	opt->format = sprinter_text_create (config, stdout);
 	break;
     case NOTMUCH_FORMAT_TEXT0:
-	if (opt.output == OUTPUT_SUMMARY) {
+	if (opt->output == OUTPUT_SUMMARY) {
 	    fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
 	    return EXIT_FAILURE;
 	}
-	opt.format = sprinter_text0_create (config, stdout);
+	opt->format = sprinter_text0_create (config, stdout);
 	break;
     case NOTMUCH_FORMAT_JSON:
-	opt.format = sprinter_json_create (config, stdout);
+	opt->format = sprinter_json_create (config, stdout);
 	break;
     case NOTMUCH_FORMAT_SEXP:
-	opt.format = sprinter_sexp_create (config, stdout);
+	opt->format = sprinter_sexp_create (config, stdout);
 	break;
     default:
 	/* this should never happen */
@@ -541,15 +528,15 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
 	return EXIT_FAILURE;
     }
 
-    opt.query = notmuch_query_create (notmuch, query_str);
-    if (opt.query == NULL) {
+    opt->query = notmuch_query_create (notmuch, query_str);
+    if (opt->query == NULL) {
 	fprintf (stderr, "Out of memory\n");
 	return EXIT_FAILURE;
     }
 
-    notmuch_query_set_sort (opt.query, opt.sort);
+    notmuch_query_set_sort (opt->query, opt->sort);
 
-    if (exclude == NOTMUCH_EXCLUDE_FLAG && opt.output != OUTPUT_SUMMARY) {
+    if (exclude == NOTMUCH_EXCLUDE_FLAG && opt->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. */
@@ -564,28 +551,77 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
 	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 (opt.query, search_exclude_tags[i]);
-	notmuch_query_set_omit_excluded (opt.query, exclude);
+	    notmuch_query_add_tag_exclude (opt->query, search_exclude_tags[i]);
+	notmuch_query_set_omit_excluded (opt->query, exclude);
     }
 
-    if (opt.output == OUTPUT_SUMMARY ||
-	opt.output == OUTPUT_THREADS)
-	ret = do_search_threads (&opt);
-    else if (opt.output == OUTPUT_MESSAGES ||
-	     opt.output == OUTPUT_FILES ||
-	     (opt.output & OUTPUT_ADDRESS_FLAGS && !(opt.output & ~OUTPUT_ADDRESS_FLAGS)))
-	ret = do_search_messages (&opt);
-    else if (opt.output == OUTPUT_TAGS)
-	ret = do_search_tags (notmuch, &opt);
+    if (opt->output == OUTPUT_SUMMARY ||
+	opt->output == OUTPUT_THREADS)
+	ret = do_search_threads (opt);
+    else if (opt->output == OUTPUT_MESSAGES ||
+	     opt->output == OUTPUT_FILES ||
+	     (opt->output & OUTPUT_ADDRESS_FLAGS && !(opt->output & ~OUTPUT_ADDRESS_FLAGS)))
+	ret = do_search_messages (opt);
+    else if (opt->output == OUTPUT_TAGS)
+	ret = do_search_tags (notmuch, opt);
     else {
 	fprintf (stderr, "Error: the combination of outputs is not supported.\n");
 	ret = 1;
     }
 
-    notmuch_query_destroy (opt.query);
+    notmuch_query_destroy (opt->query);
     notmuch_database_destroy (notmuch);
 
-    talloc_free (opt.format);
+    talloc_free (opt->format);
 
     return ret ? EXIT_FAILURE : EXIT_SUCCESS;
 }
+
+int
+notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
+{
+    search_options_t opt = {
+	.address_command = FALSE,
+	.sort = NOTMUCH_SORT_NEWEST_FIRST,
+	.output = 0,
+	.offset = 0,
+	.limit = -1, /* unlimited */
+	.dupe = -1,
+    };
+
+    notmuch_opt_desc_t options[] = {
+	{ NOTMUCH_OPT_KEYWORD_FLAGS, &opt.output, "output", 'o',
+	  (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
+				  { "threads", OUTPUT_THREADS },
+				  { "messages", OUTPUT_MESSAGES },
+				  { "files", OUTPUT_FILES },
+				  { "tags", OUTPUT_TAGS },
+				  { 0, 0 } } },
+	{ 0, 0, 0, 0, 0 }
+    };
+
+    return _notmuch_search_command (config, argc, argv, &opt, options);
+}
+
+int
+notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])
+{
+    search_options_t opt = {
+	.address_command = TRUE,
+	.sort = NOTMUCH_SORT_NEWEST_FIRST,
+	.output = 0,
+	.offset = 0,
+	.limit = -1, /* unlimited */
+	.dupe = -1,
+    };
+
+    notmuch_opt_desc_t options[] = {
+	{ NOTMUCH_OPT_KEYWORD_FLAGS, &opt.output, "output", 'o',
+	  (notmuch_keyword_t []){ { "sender", OUTPUT_SENDER },
+				  { "recipients", OUTPUT_RECIPIENTS },
+				  { 0, 0 } } },
+	{ 0, 0, 0, 0, 0 }
+    };
+
+    return _notmuch_search_command (config, argc, argv, &opt, options);
+}
diff --git a/notmuch.c b/notmuch.c
index dcda0392a094..0fac0997865e 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -54,6 +54,8 @@ static command_t commands[] = {
       "Add a new message into the maildir and notmuch database." },
     { "search", notmuch_search_command, FALSE,
       "Search for messages matching the given search terms." },
+    { "address", notmuch_address_command, FALSE,
+      "Get addresses from messages matching the given search terms." },
     { "show", notmuch_show_command, FALSE,
       "Show all messages matching the search terms." },
     { "count", notmuch_count_command, FALSE,
-- 
2.1.1

  parent reply	other threads:[~2014-11-01 11:31 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-31 21:53 [PATCH v6 0/7] notmuch search --output=sender/recipients Michal Sojka
2014-10-31 21:53 ` [PATCH v6 1/7] cli: search: Refactor passing of command line options Michal Sojka
2014-10-31 21:53 ` [PATCH v6 2/7] cli: Add support for parsing keyword-flag arguments Michal Sojka
2014-10-31 21:53 ` [PATCH v6 3/7] cli: search: Convert --output to keyword-flag argument Michal Sojka
2014-10-31 21:53 ` [PATCH v6 4/7] cli: search: Add --output={sender,recipients} Michal Sojka
2014-11-01  7:14   ` David Bremner
2014-10-31 21:53 ` [PATCH v6 5/7] cli: search: Do not output duplicate addresses Michal Sojka
2014-10-31 21:54 ` [PATCH v6 6/7] cli: search: Add --output=count Michal Sojka
2014-11-01  1:16   ` Mark Walters
2014-11-02  0:53     ` Michal Sojka
2014-11-02  9:29       ` Mark Walters
2014-11-02  9:34         ` Michal Sojka
2014-11-03  9:44           ` Tomi Ollila
2014-11-03 21:39             ` Michal Sojka
2014-10-31 21:54 ` [PATCH v6 7/7] cli: search: Add --filter-by option to configure address filtering Michal Sojka
2014-11-01  8:55   ` Jani Nikula
2014-11-01  9:21     ` Michal Sojka
2014-11-01 11:30       ` [RFC PATCH 0/2] cli: split 'notmuch address' from 'notmuch search' Jani Nikula
2014-11-01 11:30         ` [RFC PATCH 1/2] cli: add support for hierarchical command line option arrays Jani Nikula
2014-11-01 11:30         ` Jani Nikula [this message]
2014-11-04  9:14         ` [RFC PATCH 0/2] cli: split 'notmuch address' from 'notmuch search' Mark Walters
2014-11-01  0:22 ` [PATCH v6 0/7] notmuch search --output=sender/recipients Mark Walters

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1bc05ab764867004fd5b6c79266ffcd0249909cd.1414839970.git.jani@nikula.org \
    --to=jani@nikula.org \
    --cc=notmuch@notmuchmail.org \
    --cc=sojkam1@fel.cvut.cz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).