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

Fernando Carrijo fcarrijo at yahoo.com.br
Wed Jan 13 04:44:09 PST 2010


Hi Ali,

On Wed, 13 Jan 2010 12:24:22 +0200, Ali Polatel <alip at exherbo.org> wrote:

> 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

I almost certainly should not be speaking in the name of Carl, but a
few weeks ago he replied with the following words to a message which
somehow raised the discussion of copyright holding in notmuch source
code:

  "Please feel free to retain your own copyright. I certainly don't
   deserve anything being assigned to me."

The message-id of his reply is: 873a34tf8b.fsf at yoom.home.cworth.org,
just in the case you want to check the context in which it occurred.

> + *
> + * 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");

Could we break this line so that it did not go beyond the column limit
set by the coding conventions? Maybe we should go one step further and
import the file CODING_STYLE from cairo into notmuch's repository.

> +	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);

It is not something of major importance, but I realized that the
function notmuch_config_close is only being called when notmuch is
executed with no arguments. As we know, the kernel shall close all
open file descriptors upon program termination, but wouldn't it be
better if we did it explicitly? Or did I overlook something?

Kind regards,
Fernando Carrijo.

> +
> +    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
> 
> _______________________________________________
> notmuch mailing list
> notmuch at notmuchmail.org
> http://notmuchmail.org/mailman/listinfo/notmuch



More information about the notmuch mailing list