1 Return-Path: <jani@nikula.org>
\r
2 X-Original-To: notmuch@notmuchmail.org
\r
3 Delivered-To: notmuch@notmuchmail.org
\r
4 Received: from localhost (localhost [127.0.0.1])
\r
5 by olra.theworths.org (Postfix) with ESMTP id 5A86F431FB6
\r
6 for <notmuch@notmuchmail.org>; Mon, 31 Oct 2011 14:18:27 -0700 (PDT)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"
\r
12 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5
\r
13 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled
\r
14 Received: from olra.theworths.org ([127.0.0.1])
\r
15 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
16 with ESMTP id BJm5avjV6kbW for <notmuch@notmuchmail.org>;
\r
17 Mon, 31 Oct 2011 14:18:26 -0700 (PDT)
\r
18 Received: from mail-bw0-f53.google.com (mail-bw0-f53.google.com
\r
19 [209.85.214.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))
\r
20 (No client certificate requested)
\r
21 by olra.theworths.org (Postfix) with ESMTPS id 4E9A1429E29
\r
22 for <notmuch@notmuchmail.org>; Mon, 31 Oct 2011 14:18:24 -0700 (PDT)
\r
23 Received: by bkbzs8 with SMTP id zs8so4069839bkb.26
\r
24 for <notmuch@notmuchmail.org>; Mon, 31 Oct 2011 14:18:21 -0700 (PDT)
\r
25 Received: by 10.204.9.211 with SMTP id m19mr2654817bkm.92.1320095901236;
\r
26 Mon, 31 Oct 2011 14:18:21 -0700 (PDT)
\r
27 Received: from localhost (dsl-hkibrasgw4-fe5cdc00-23.dhcp.inet.fi.
\r
29 by mx.google.com with ESMTPS id k13sm16686618fah.0.2011.10.31.14.18.19
\r
30 (version=SSLv3 cipher=OTHER); Mon, 31 Oct 2011 14:18:20 -0700 (PDT)
\r
31 From: Jani Nikula <jani@nikula.org>
\r
32 To: notmuch@notmuchmail.org
\r
33 Subject: [RFC PATCH v2 2/3] cli: add options --first and --maxitems to notmuch
\r
35 Date: Mon, 31 Oct 2011 23:18:09 +0200
\r
37 <e3440dc1ad040137074b1db5ff3882c987234b0e.1320093940.git.jani@nikula.org>
\r
38 X-Mailer: git-send-email 1.7.5.4
\r
39 In-Reply-To: <cover.1320093940.git.jani@nikula.org>
\r
40 References: <cover.1320093940.git.jani@nikula.org>
\r
41 In-Reply-To: <cover.1320093940.git.jani@nikula.org>
\r
42 References: <cover.1320093940.git.jani@nikula.org>
\r
43 Cc: amdragon@mit.edu
\r
44 X-BeenThere: notmuch@notmuchmail.org
\r
45 X-Mailman-Version: 2.1.13
\r
47 List-Id: "Use and development of the notmuch mail system."
\r
48 <notmuch.notmuchmail.org>
\r
49 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
50 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
51 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
52 List-Post: <mailto:notmuch@notmuchmail.org>
\r
53 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
54 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
55 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
56 X-List-Received-Date: Mon, 31 Oct 2011 21:18:27 -0000
\r
58 Add options --first=[-]N and --maxitems=M to notmuch search to determine
\r
59 the first result and maximum number of results to display.
\r
61 Option --maxitems=M limits the maximum number of results to display to M.
\r
63 Option --first=[-]N skips the first N results; with the leading '-' skip
\r
64 until the Nth result from the end (showing a total of N results if within
\r
65 bounds of the total number of results and not limited with --maxitems).
\r
67 Note that --first with a negative N for thread or summary output requires
\r
68 counting the number of matching threads in advance, which is a heavy
\r
71 Signed-off-by: Jani Nikula <jani@nikula.org>
\r
73 notmuch-search.c | 67 ++++++++++++++++++++++++++++++++++++++++++++---------
\r
74 1 files changed, 55 insertions(+), 12 deletions(-)
\r
76 diff --git a/notmuch-search.c b/notmuch-search.c
\r
77 index 6f04d9a..ceef6ac 100644
\r
78 --- a/notmuch-search.c
\r
79 +++ b/notmuch-search.c
\r
80 @@ -194,13 +194,20 @@ static int
\r
81 do_search_threads (const search_format_t *format,
\r
82 notmuch_query_t *query,
\r
83 notmuch_sort_t sort,
\r
87 + unsigned int maxitems)
\r
89 notmuch_thread_t *thread;
\r
90 notmuch_threads_t *threads;
\r
91 notmuch_tags_t *tags;
\r
93 int first_thread = 1;
\r
94 + unsigned int i, start;
\r
97 + first += notmuch_query_count_threads (query);
\r
98 + start = first < 0 ? 0 : first;
\r
100 threads = notmuch_query_search_threads (query);
\r
101 if (threads == NULL)
\r
102 @@ -208,17 +215,23 @@ do_search_threads (const search_format_t *format,
\r
104 fputs (format->results_start, stdout);
\r
107 - notmuch_threads_valid (threads);
\r
108 - notmuch_threads_move_to_next (threads))
\r
110 + notmuch_threads_valid (threads) &&
\r
111 + (!maxitems || i < start + maxitems);
\r
112 + notmuch_threads_move_to_next (threads), i++)
\r
116 + thread = notmuch_threads_get (threads);
\r
119 + notmuch_thread_destroy (thread);
\r
123 if (! first_thread)
\r
124 fputs (format->item_sep, stdout);
\r
126 - thread = notmuch_threads_get (threads);
\r
128 if (output == OUTPUT_THREADS) {
\r
129 format->item_id (thread, "thread:",
\r
130 notmuch_thread_get_thread_id (thread));
\r
131 @@ -271,12 +284,20 @@ do_search_threads (const search_format_t *format,
\r
133 do_search_messages (const search_format_t *format,
\r
134 notmuch_query_t *query,
\r
138 + unsigned int maxitems)
\r
140 notmuch_message_t *message;
\r
141 notmuch_messages_t *messages;
\r
142 notmuch_filenames_t *filenames;
\r
143 int first_message = 1;
\r
144 + unsigned int i, start;
\r
147 + first += notmuch_query_count_messages(query);
\r
149 + start = first < 0 ? 0 : first;
\r
151 messages = notmuch_query_search_messages (query);
\r
152 if (messages == NULL)
\r
153 @@ -284,10 +305,14 @@ do_search_messages (const search_format_t *format,
\r
155 fputs (format->results_start, stdout);
\r
158 - notmuch_messages_valid (messages);
\r
159 - notmuch_messages_move_to_next (messages))
\r
161 + notmuch_messages_valid (messages) &&
\r
162 + (!maxitems || i < start + maxitems);
\r
163 + notmuch_messages_move_to_next (messages), i++)
\r
168 message = notmuch_messages_get (messages);
\r
170 if (output == OUTPUT_FILES) {
\r
171 @@ -394,6 +419,8 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
\r
172 const search_format_t *format = &format_text;
\r
174 output_t output = OUTPUT_SUMMARY;
\r
175 + unsigned int maxitems = 0;
\r
178 argc--; argv++; /* skip subcommand argument */
\r
180 @@ -412,6 +439,22 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
\r
181 fprintf (stderr, "Invalid value for --sort: %s\n", opt);
\r
184 + } else if (STRNCMP_LITERAL (argv[i], "--first=") == 0) {
\r
186 + opt = argv[i] + sizeof ("--first=") - 1;
\r
187 + first = strtol(opt, &p, 10);
\r
188 + if (*opt == '\0' || p == opt || *p != '\0') {
\r
189 + fprintf (stderr, "Invalid value for --first: %s\n", opt);
\r
192 + } else if (STRNCMP_LITERAL (argv[i], "--maxitems=") == 0) {
\r
194 + opt = argv[i] + sizeof ("--maxitems=") - 1;
\r
195 + maxitems = strtoul(opt, &p, 10);
\r
196 + if (*opt == '\0' || p == opt || *p != '\0') {
\r
197 + fprintf (stderr, "Invalid value for --maxitems: %s\n", opt);
\r
200 } else if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
\r
201 opt = argv[i] + sizeof ("--format=") - 1;
\r
202 if (strcmp (opt, "text") == 0) {
\r
203 @@ -478,11 +521,11 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
\r
205 case OUTPUT_SUMMARY:
\r
206 case OUTPUT_THREADS:
\r
207 - ret = do_search_threads (format, query, sort, output);
\r
208 + ret = do_search_threads (format, query, sort, output, first, maxitems);
\r
210 case OUTPUT_MESSAGES:
\r
212 - ret = do_search_messages (format, query, output);
\r
213 + ret = do_search_messages (format, query, output, first, maxitems);
\r
216 ret = do_search_tags (notmuch, format, query);
\r