Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 5A86F431FB6 for ; Mon, 31 Oct 2011 14:18:27 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" X-Spam-Flag: NO X-Spam-Score: -0.7 X-Spam-Level: X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id BJm5avjV6kbW for ; Mon, 31 Oct 2011 14:18:26 -0700 (PDT) Received: from mail-bw0-f53.google.com (mail-bw0-f53.google.com [209.85.214.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 4E9A1429E29 for ; Mon, 31 Oct 2011 14:18:24 -0700 (PDT) Received: by bkbzs8 with SMTP id zs8so4069839bkb.26 for ; Mon, 31 Oct 2011 14:18:21 -0700 (PDT) Received: by 10.204.9.211 with SMTP id m19mr2654817bkm.92.1320095901236; Mon, 31 Oct 2011 14:18:21 -0700 (PDT) Received: from localhost (dsl-hkibrasgw4-fe5cdc00-23.dhcp.inet.fi. [80.220.92.23]) by mx.google.com with ESMTPS id k13sm16686618fah.0.2011.10.31.14.18.19 (version=SSLv3 cipher=OTHER); Mon, 31 Oct 2011 14:18:20 -0700 (PDT) From: Jani Nikula To: notmuch@notmuchmail.org Subject: [RFC PATCH v2 2/3] cli: add options --first and --maxitems to notmuch search Date: Mon, 31 Oct 2011 23:18:09 +0200 Message-Id: X-Mailer: git-send-email 1.7.5.4 In-Reply-To: References: In-Reply-To: References: Cc: amdragon@mit.edu X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 31 Oct 2011 21:18:27 -0000 Add options --first=[-]N and --maxitems=M to notmuch search to determine the first result and maximum number of results to display. Option --maxitems=M limits the maximum number of results to display to M. Option --first=[-]N skips the first N results; with the leading '-' skip until the Nth result from the end (showing a total of N results if within bounds of the total number of results and not limited with --maxitems). Note that --first with a negative N for thread or summary output requires counting the number of matching threads in advance, which is a heavy operation. Signed-off-by: Jani Nikula --- notmuch-search.c | 67 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 55 insertions(+), 12 deletions(-) diff --git a/notmuch-search.c b/notmuch-search.c index 6f04d9a..ceef6ac 100644 --- a/notmuch-search.c +++ b/notmuch-search.c @@ -194,13 +194,20 @@ static int do_search_threads (const search_format_t *format, notmuch_query_t *query, notmuch_sort_t sort, - output_t output) + output_t output, + int first, + unsigned int maxitems) { notmuch_thread_t *thread; notmuch_threads_t *threads; notmuch_tags_t *tags; time_t date; int first_thread = 1; + unsigned int i, start; + + if (first < 0) + first += notmuch_query_count_threads (query); + start = first < 0 ? 0 : first; threads = notmuch_query_search_threads (query); if (threads == NULL) @@ -208,17 +215,23 @@ do_search_threads (const search_format_t *format, fputs (format->results_start, stdout); - for (; - notmuch_threads_valid (threads); - notmuch_threads_move_to_next (threads)) + for (i = 0; + notmuch_threads_valid (threads) && + (!maxitems || i < start + maxitems); + notmuch_threads_move_to_next (threads), i++) { int first_tag = 1; + thread = notmuch_threads_get (threads); + + if (i < start) { + notmuch_thread_destroy (thread); + continue; + } + if (! first_thread) fputs (format->item_sep, stdout); - thread = notmuch_threads_get (threads); - if (output == OUTPUT_THREADS) { format->item_id (thread, "thread:", notmuch_thread_get_thread_id (thread)); @@ -271,12 +284,20 @@ do_search_threads (const search_format_t *format, static int do_search_messages (const search_format_t *format, notmuch_query_t *query, - output_t output) + output_t output, + int first, + unsigned int maxitems) { notmuch_message_t *message; notmuch_messages_t *messages; notmuch_filenames_t *filenames; int first_message = 1; + unsigned int i, start; + + if (first < 0) + first += notmuch_query_count_messages(query); + + start = first < 0 ? 0 : first; messages = notmuch_query_search_messages (query); if (messages == NULL) @@ -284,10 +305,14 @@ do_search_messages (const search_format_t *format, fputs (format->results_start, stdout); - for (; - notmuch_messages_valid (messages); - notmuch_messages_move_to_next (messages)) + for (i = 0; + notmuch_messages_valid (messages) && + (!maxitems || i < start + maxitems); + notmuch_messages_move_to_next (messages), i++) { + if (i < start) + continue; + message = notmuch_messages_get (messages); if (output == OUTPUT_FILES) { @@ -394,6 +419,8 @@ notmuch_search_command (void *ctx, int argc, char *argv[]) const search_format_t *format = &format_text; int i, ret; output_t output = OUTPUT_SUMMARY; + unsigned int maxitems = 0; + int first = 0; argc--; argv++; /* skip subcommand argument */ @@ -412,6 +439,22 @@ notmuch_search_command (void *ctx, int argc, char *argv[]) fprintf (stderr, "Invalid value for --sort: %s\n", opt); return 1; } + } else if (STRNCMP_LITERAL (argv[i], "--first=") == 0) { + char *p; + opt = argv[i] + sizeof ("--first=") - 1; + first = strtol(opt, &p, 10); + if (*opt == '\0' || p == opt || *p != '\0') { + fprintf (stderr, "Invalid value for --first: %s\n", opt); + return 1; + } + } else if (STRNCMP_LITERAL (argv[i], "--maxitems=") == 0) { + char *p; + opt = argv[i] + sizeof ("--maxitems=") - 1; + maxitems = strtoul(opt, &p, 10); + if (*opt == '\0' || p == opt || *p != '\0') { + fprintf (stderr, "Invalid value for --maxitems: %s\n", opt); + return 1; + } } else if (STRNCMP_LITERAL (argv[i], "--format=") == 0) { opt = argv[i] + sizeof ("--format=") - 1; if (strcmp (opt, "text") == 0) { @@ -478,11 +521,11 @@ notmuch_search_command (void *ctx, int argc, char *argv[]) default: case OUTPUT_SUMMARY: case OUTPUT_THREADS: - ret = do_search_threads (format, query, sort, output); + ret = do_search_threads (format, query, sort, output, first, maxitems); break; case OUTPUT_MESSAGES: case OUTPUT_FILES: - ret = do_search_messages (format, query, output); + ret = do_search_messages (format, query, output, first, maxitems); break; case OUTPUT_TAGS: ret = do_search_tags (notmuch, format, query); -- 1.7.5.4