[PATCH v4 2/6] cli: add options --first and --maxitems to notmuch search
[notmuch-archives.git] / 2b / a79e6f2d78ff644ad6ba4d6e4eab846a538d17
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 67D03429E34\r
6         for <notmuch@notmuchmail.org>; Sun,  6 Nov 2011 13:47:30 -0800 (PST)\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
9 X-Spam-Flag: NO\r
10 X-Spam-Score: -0.7\r
11 X-Spam-Level: \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 uhMB0n2mzLW9 for <notmuch@notmuchmail.org>;\r
17         Sun,  6 Nov 2011 13:47:28 -0800 (PST)\r
18 Received: from mail-fx0-f53.google.com (mail-fx0-f53.google.com\r
19         [209.85.161.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 AAC2D429E32\r
22         for <notmuch@notmuchmail.org>; Sun,  6 Nov 2011 13:47:26 -0800 (PST)\r
23 Received: by mail-fx0-f53.google.com with SMTP id n15so1762114faa.26\r
24         for <notmuch@notmuchmail.org>; Sun, 06 Nov 2011 13:47:26 -0800 (PST)\r
25 Received: by 10.223.75.129 with SMTP id y1mr231801faj.1.1320616046297;\r
26         Sun, 06 Nov 2011 13:47:26 -0800 (PST)\r
27 Received: from localhost (dsl-hkibrasgw4-fe5cdc00-23.dhcp.inet.fi.\r
28         [80.220.92.23])\r
29         by mx.google.com with ESMTPS id p14sm28458939faf.20.2011.11.06.13.47.24\r
30         (version=SSLv3 cipher=OTHER); Sun, 06 Nov 2011 13:47:25 -0800 (PST)\r
31 From: Jani Nikula <jani@nikula.org>\r
32 To: notmuch@notmuchmail.org\r
33 Subject: [PATCH v4 2/6] cli: add options --first and --maxitems to notmuch\r
34         search\r
35 Date: Sun,  6 Nov 2011 23:47:11 +0200\r
36 Message-Id:\r
37  <45e0f0f47250b20b4c3372910fea4d2fd5144e20.1320615322.git.jani@nikula.org>\r
38 X-Mailer: git-send-email 1.7.5.4\r
39 In-Reply-To: <cover.1320615322.git.jani@nikula.org>\r
40 References: <cover.1320615322.git.jani@nikula.org>\r
41 In-Reply-To: <cover.1320615322.git.jani@nikula.org>\r
42 References: <cover.1320615322.git.jani@nikula.org>\r
43 X-BeenThere: notmuch@notmuchmail.org\r
44 X-Mailman-Version: 2.1.13\r
45 Precedence: list\r
46 List-Id: "Use and development of the notmuch mail system."\r
47         <notmuch.notmuchmail.org>\r
48 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
49         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
50 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
51 List-Post: <mailto:notmuch@notmuchmail.org>\r
52 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
53 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
54         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
55 X-List-Received-Date: Sun, 06 Nov 2011 21:47:30 -0000\r
56 \r
57 Add options --first=[-]N and --maxitems=M to notmuch search to determine\r
58 the first result and maximum number of results to display.\r
59 \r
60 Option --maxitems=M limits the maximum number of results to display to M.\r
61 \r
62 Option --first=[-]N skips the first N results; with the leading '-' skip\r
63 until the Nth result from the end (showing a total of N results if within\r
64 bounds of the total number of results and not limited with --maxitems).\r
65 \r
66 Note that --first with a negative N for thread or summary output requires\r
67 counting the number of matching threads in advance.\r
68 \r
69 Signed-off-by: Jani Nikula <jani@nikula.org>\r
70 ---\r
71  NEWS             |    5 ++++\r
72  notmuch-search.c |   70 ++++++++++++++++++++++++++++++++++++++++++++---------\r
73  notmuch.1        |   21 ++++++++++++++-\r
74  3 files changed, 82 insertions(+), 14 deletions(-)\r
75 \r
76 diff --git a/NEWS b/NEWS\r
77 index b44b11e..bfdba7b 100644\r
78 --- a/NEWS\r
79 +++ b/NEWS\r
80 @@ -23,6 +23,11 @@ Add search terms to  "notmuch dump"\r
81    search/show/tag. The output file argument of dump is deprecated in\r
82    favour of using stdout.\r
83  \r
84 +Add "notmuch search" --first and --maxitems options\r
85 +\r
86 +  The search command now takes options --first=[-]N and --maxitems=N to limit\r
87 +  the number of results shown.\r
88 +\r
89  Notmuch 0.9 (2011-10-01)\r
90  ========================\r
91  \r
92 diff --git a/notmuch-search.c b/notmuch-search.c\r
93 index 6f04d9a..c62a594 100644\r
94 --- a/notmuch-search.c\r
95 +++ b/notmuch-search.c\r
96 @@ -194,13 +194,22 @@ static int\r
97  do_search_threads (const search_format_t *format,\r
98                    notmuch_query_t *query,\r
99                    notmuch_sort_t sort,\r
100 -                  output_t output)\r
101 +                  output_t output,\r
102 +                  int first,\r
103 +                  int maxitems)\r
104  {\r
105      notmuch_thread_t *thread;\r
106      notmuch_threads_t *threads;\r
107      notmuch_tags_t *tags;\r
108      time_t date;\r
109      int first_thread = 1;\r
110 +    int i;\r
111 +\r
112 +    if (first < 0) {\r
113 +       first += notmuch_query_count_threads (query);\r
114 +       if (first < 0)\r
115 +           first = 0;\r
116 +    }\r
117  \r
118      threads = notmuch_query_search_threads (query);\r
119      if (threads == NULL)\r
120 @@ -208,17 +217,23 @@ do_search_threads (const search_format_t *format,\r
121  \r
122      fputs (format->results_start, stdout);\r
123  \r
124 -    for (;\r
125 -        notmuch_threads_valid (threads);\r
126 -        notmuch_threads_move_to_next (threads))\r
127 +    for (i = 0;\r
128 +        notmuch_threads_valid (threads) &&\r
129 +            (maxitems < 0 || i < first + maxitems);\r
130 +        notmuch_threads_move_to_next (threads), i++)\r
131      {\r
132         int first_tag = 1;\r
133  \r
134 +       thread = notmuch_threads_get (threads);\r
135 +\r
136 +       if (i < first) {\r
137 +           notmuch_thread_destroy (thread);\r
138 +           continue;\r
139 +       }\r
140 +\r
141         if (! first_thread)\r
142             fputs (format->item_sep, stdout);\r
143  \r
144 -       thread = notmuch_threads_get (threads);\r
145 -\r
146         if (output == OUTPUT_THREADS) {\r
147             format->item_id (thread, "thread:",\r
148                              notmuch_thread_get_thread_id (thread));\r
149 @@ -271,12 +286,21 @@ do_search_threads (const search_format_t *format,\r
150  static int\r
151  do_search_messages (const search_format_t *format,\r
152                     notmuch_query_t *query,\r
153 -                   output_t output)\r
154 +                   output_t output,\r
155 +                   int first,\r
156 +                   int maxitems)\r
157  {\r
158      notmuch_message_t *message;\r
159      notmuch_messages_t *messages;\r
160      notmuch_filenames_t *filenames;\r
161      int first_message = 1;\r
162 +    int i;\r
163 +\r
164 +    if (first < 0) {\r
165 +       first += notmuch_query_count_messages (query);\r
166 +       if (first < 0)\r
167 +           first = 0;\r
168 +    }\r
169  \r
170      messages = notmuch_query_search_messages (query);\r
171      if (messages == NULL)\r
172 @@ -284,10 +308,14 @@ do_search_messages (const search_format_t *format,\r
173  \r
174      fputs (format->results_start, stdout);\r
175  \r
176 -    for (;\r
177 -        notmuch_messages_valid (messages);\r
178 -        notmuch_messages_move_to_next (messages))\r
179 +    for (i = 0;\r
180 +        notmuch_messages_valid (messages) &&\r
181 +            (maxitems < 0 || i < first + maxitems);\r
182 +        notmuch_messages_move_to_next (messages), i++)\r
183      {\r
184 +       if (i < first)\r
185 +           continue;\r
186 +\r
187         message = notmuch_messages_get (messages);\r
188  \r
189         if (output == OUTPUT_FILES) {\r
190 @@ -394,6 +422,8 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
191      const search_format_t *format = &format_text;\r
192      int i, ret;\r
193      output_t output = OUTPUT_SUMMARY;\r
194 +    int maxitems = -1; /* unlimited */\r
195 +    int first = 0;\r
196  \r
197      argc--; argv++; /* skip subcommand argument */\r
198  \r
199 @@ -412,6 +442,22 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
200                 fprintf (stderr, "Invalid value for --sort: %s\n", opt);\r
201                 return 1;\r
202             }\r
203 +       } else if (STRNCMP_LITERAL (argv[i], "--first=") == 0) {\r
204 +           char *p;\r
205 +           opt = argv[i] + sizeof ("--first=") - 1;\r
206 +           first = strtol (opt, &p, 10);\r
207 +           if (*opt == '\0' || p == opt || *p != '\0') {\r
208 +               fprintf (stderr, "Invalid value for --first: %s\n", opt);\r
209 +               return 1;\r
210 +           }\r
211 +       } else if (STRNCMP_LITERAL (argv[i], "--maxitems=") == 0) {\r
212 +           char *p;\r
213 +           opt = argv[i] + sizeof ("--maxitems=") - 1;\r
214 +           maxitems = strtoul (opt, &p, 10);\r
215 +           if (*opt == '\0' || p == opt || *p != '\0') {\r
216 +               fprintf (stderr, "Invalid value for --maxitems: %s\n", opt);\r
217 +               return 1;\r
218 +           }\r
219         } else if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {\r
220             opt = argv[i] + sizeof ("--format=") - 1;\r
221             if (strcmp (opt, "text") == 0) {\r
222 @@ -478,11 +524,11 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
223      default:\r
224      case OUTPUT_SUMMARY:\r
225      case OUTPUT_THREADS:\r
226 -       ret = do_search_threads (format, query, sort, output);\r
227 +       ret = do_search_threads (format, query, sort, output, first, maxitems);\r
228         break;\r
229      case OUTPUT_MESSAGES:\r
230      case OUTPUT_FILES:\r
231 -       ret = do_search_messages (format, query, output);\r
232 +       ret = do_search_messages (format, query, output, first, maxitems);\r
233         break;\r
234      case OUTPUT_TAGS:\r
235         ret = do_search_tags (notmuch, format, query);\r
236 diff --git a/notmuch.1 b/notmuch.1\r
237 index bba479e..c97334c 100644\r
238 --- a/notmuch.1\r
239 +++ b/notmuch.1\r
240 @@ -214,11 +214,28 @@ when sorting by\r
241  .B newest\-first\r
242  the threads will be sorted by the newest message in each thread.\r
243  \r
244 -.RE\r
245 -.RS 4\r
246  By default, results will be displayed in reverse chronological order,\r
247  (that is, the newest results will be displayed first).\r
248 +.RE\r
249 +\r
250 +.RS 4\r
251 +.TP 4\r
252 +.BR \-\-first=[\-]N\r
253 +\r
254 +This option can be used to skip the display of first N results. With the\r
255 +leading '\-' skip until the Nth result from the end, showing a total of N\r
256 +results if not otherwise bounded by total number of matching results or\r
257 +\-\-maxitems.\r
258 +.RE\r
259 +\r
260 +.RS 4\r
261 +.TP 4\r
262 +.BR \-\-maxitems=N\r
263  \r
264 +This option can be used to limit the number of results to display to N.\r
265 +.RE\r
266 +\r
267 +.RS 4\r
268  See the\r
269  .B "SEARCH SYNTAX"\r
270  section below for details of the supported syntax for <search-terms>.\r
271 -- \r
272 1.7.5.4\r
273 \r