Re: [PATCH 4/4] emacs: Use the new JSON reply format.
[notmuch-archives.git] / 18 / 06384e3539cef97b0d1da9c5e008cfc50f6c66
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 BDB38429E2E\r
6         for <notmuch@notmuchmail.org>; Sun, 13 Nov 2011 13:15:50 -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 OX6DTrKdWBzt for <notmuch@notmuchmail.org>;\r
17         Sun, 13 Nov 2011 13:15:48 -0800 (PST)\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 60443429E2F\r
22         for <notmuch@notmuchmail.org>; Sun, 13 Nov 2011 13:15:47 -0800 (PST)\r
23 Received: by mail-bw0-f53.google.com with SMTP id q10so6289712bka.26\r
24         for <notmuch@notmuchmail.org>; Sun, 13 Nov 2011 13:15:47 -0800 (PST)\r
25 Received: by 10.204.156.219 with SMTP id y27mr1975872bkw.125.1321218946871;\r
26         Sun, 13 Nov 2011 13:15:46 -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 j9sm27632176bkd.2.2011.11.13.13.15.44\r
30         (version=SSLv3 cipher=OTHER); Sun, 13 Nov 2011 13:15:45 -0800 (PST)\r
31 From: Jani Nikula <jani@nikula.org>\r
32 To: notmuch@notmuchmail.org\r
33 Subject: [PATCH v6 2/6] cli: add options --first and --maxitems to notmuch\r
34         search\r
35 Date: Sun, 13 Nov 2011 23:15:31 +0200\r
36 Message-Id:\r
37  <394a87893c97e19911041abd7a12e6b97bdb8846.1321217854.git.jani@nikula.org>\r
38 X-Mailer: git-send-email 1.7.5.4\r
39 In-Reply-To: <cover.1321217854.git.jani@nikula.org>\r
40 References: <cover.1321217854.git.jani@nikula.org>\r
41 In-Reply-To: <cover.1321217854.git.jani@nikula.org>\r
42 References: <cover.1321217854.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, 13 Nov 2011 21:15:51 -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        |   19 +++++++++++++-\r
74  notmuch.c        |    9 +++++++\r
75  4 files changed, 89 insertions(+), 14 deletions(-)\r
76 \r
77 diff --git a/NEWS b/NEWS\r
78 index 71c7c9a..abc749a 100644\r
79 --- a/NEWS\r
80 +++ b/NEWS\r
81 @@ -23,6 +23,11 @@ Add search terms to  "notmuch dump"\r
82    search/show/tag. The output file argument of dump is deprecated in\r
83    favour of using stdout.\r
84  \r
85 +Add "notmuch search" --first and --maxitems options\r
86 +\r
87 +  The search command now takes options --first=[-]N and --maxitems=N to limit\r
88 +  the number of results shown.\r
89 +\r
90  Notmuch 0.9 (2011-10-01)\r
91  ========================\r
92  \r
93 diff --git a/notmuch-search.c b/notmuch-search.c\r
94 index 6f04d9a..c62a594 100644\r
95 --- a/notmuch-search.c\r
96 +++ b/notmuch-search.c\r
97 @@ -194,13 +194,22 @@ static int\r
98  do_search_threads (const search_format_t *format,\r
99                    notmuch_query_t *query,\r
100                    notmuch_sort_t sort,\r
101 -                  output_t output)\r
102 +                  output_t output,\r
103 +                  int first,\r
104 +                  int maxitems)\r
105  {\r
106      notmuch_thread_t *thread;\r
107      notmuch_threads_t *threads;\r
108      notmuch_tags_t *tags;\r
109      time_t date;\r
110      int first_thread = 1;\r
111 +    int i;\r
112 +\r
113 +    if (first < 0) {\r
114 +       first += notmuch_query_count_threads (query);\r
115 +       if (first < 0)\r
116 +           first = 0;\r
117 +    }\r
118  \r
119      threads = notmuch_query_search_threads (query);\r
120      if (threads == NULL)\r
121 @@ -208,17 +217,23 @@ do_search_threads (const search_format_t *format,\r
122  \r
123      fputs (format->results_start, stdout);\r
124  \r
125 -    for (;\r
126 -        notmuch_threads_valid (threads);\r
127 -        notmuch_threads_move_to_next (threads))\r
128 +    for (i = 0;\r
129 +        notmuch_threads_valid (threads) &&\r
130 +            (maxitems < 0 || i < first + maxitems);\r
131 +        notmuch_threads_move_to_next (threads), i++)\r
132      {\r
133         int first_tag = 1;\r
134  \r
135 +       thread = notmuch_threads_get (threads);\r
136 +\r
137 +       if (i < first) {\r
138 +           notmuch_thread_destroy (thread);\r
139 +           continue;\r
140 +       }\r
141 +\r
142         if (! first_thread)\r
143             fputs (format->item_sep, stdout);\r
144  \r
145 -       thread = notmuch_threads_get (threads);\r
146 -\r
147         if (output == OUTPUT_THREADS) {\r
148             format->item_id (thread, "thread:",\r
149                              notmuch_thread_get_thread_id (thread));\r
150 @@ -271,12 +286,21 @@ do_search_threads (const search_format_t *format,\r
151  static int\r
152  do_search_messages (const search_format_t *format,\r
153                     notmuch_query_t *query,\r
154 -                   output_t output)\r
155 +                   output_t output,\r
156 +                   int first,\r
157 +                   int maxitems)\r
158  {\r
159      notmuch_message_t *message;\r
160      notmuch_messages_t *messages;\r
161      notmuch_filenames_t *filenames;\r
162      int first_message = 1;\r
163 +    int i;\r
164 +\r
165 +    if (first < 0) {\r
166 +       first += notmuch_query_count_messages (query);\r
167 +       if (first < 0)\r
168 +           first = 0;\r
169 +    }\r
170  \r
171      messages = notmuch_query_search_messages (query);\r
172      if (messages == NULL)\r
173 @@ -284,10 +308,14 @@ do_search_messages (const search_format_t *format,\r
174  \r
175      fputs (format->results_start, stdout);\r
176  \r
177 -    for (;\r
178 -        notmuch_messages_valid (messages);\r
179 -        notmuch_messages_move_to_next (messages))\r
180 +    for (i = 0;\r
181 +        notmuch_messages_valid (messages) &&\r
182 +            (maxitems < 0 || i < first + maxitems);\r
183 +        notmuch_messages_move_to_next (messages), i++)\r
184      {\r
185 +       if (i < first)\r
186 +           continue;\r
187 +\r
188         message = notmuch_messages_get (messages);\r
189  \r
190         if (output == OUTPUT_FILES) {\r
191 @@ -394,6 +422,8 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
192      const search_format_t *format = &format_text;\r
193      int i, ret;\r
194      output_t output = OUTPUT_SUMMARY;\r
195 +    int maxitems = -1; /* unlimited */\r
196 +    int first = 0;\r
197  \r
198      argc--; argv++; /* skip subcommand argument */\r
199  \r
200 @@ -412,6 +442,22 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
201                 fprintf (stderr, "Invalid value for --sort: %s\n", opt);\r
202                 return 1;\r
203             }\r
204 +       } else if (STRNCMP_LITERAL (argv[i], "--first=") == 0) {\r
205 +           char *p;\r
206 +           opt = argv[i] + sizeof ("--first=") - 1;\r
207 +           first = strtol (opt, &p, 10);\r
208 +           if (*opt == '\0' || p == opt || *p != '\0') {\r
209 +               fprintf (stderr, "Invalid value for --first: %s\n", opt);\r
210 +               return 1;\r
211 +           }\r
212 +       } else if (STRNCMP_LITERAL (argv[i], "--maxitems=") == 0) {\r
213 +           char *p;\r
214 +           opt = argv[i] + sizeof ("--maxitems=") - 1;\r
215 +           maxitems = strtoul (opt, &p, 10);\r
216 +           if (*opt == '\0' || p == opt || *p != '\0') {\r
217 +               fprintf (stderr, "Invalid value for --maxitems: %s\n", opt);\r
218 +               return 1;\r
219 +           }\r
220         } else if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {\r
221             opt = argv[i] + sizeof ("--format=") - 1;\r
222             if (strcmp (opt, "text") == 0) {\r
223 @@ -478,11 +524,11 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
224      default:\r
225      case OUTPUT_SUMMARY:\r
226      case OUTPUT_THREADS:\r
227 -       ret = do_search_threads (format, query, sort, output);\r
228 +       ret = do_search_threads (format, query, sort, output, first, maxitems);\r
229         break;\r
230      case OUTPUT_MESSAGES:\r
231      case OUTPUT_FILES:\r
232 -       ret = do_search_messages (format, query, output);\r
233 +       ret = do_search_messages (format, query, output, first, maxitems);\r
234         break;\r
235      case OUTPUT_TAGS:\r
236         ret = do_search_tags (notmuch, format, query);\r
237 diff --git a/notmuch.1 b/notmuch.1\r
238 index bba479e..15c3147 100644\r
239 --- a/notmuch.1\r
240 +++ b/notmuch.1\r
241 @@ -214,11 +214,26 @@ when sorting by\r
242  .B newest\-first\r
243  the threads will be sorted by the newest message in each thread.\r
244  \r
245 -.RE\r
246 -.RS 4\r
247  By default, results will be displayed in reverse chronological order,\r
248  (that is, the newest results will be displayed first).\r
249 +.RE\r
250 +\r
251 +.RS 4\r
252 +.TP 4\r
253 +.BR \-\-first=[\-]N\r
254 +\r
255 +Skip displaying the first N results. With the leading '\-', start at the Nth\r
256 +result from the end.\r
257 +.RE\r
258 +\r
259 +.RS 4\r
260 +.TP 4\r
261 +.BR \-\-maxitems=N\r
262  \r
263 +Limit the number of displayed results to N.\r
264 +.RE\r
265 +\r
266 +.RS 4\r
267  See the\r
268  .B "SEARCH SYNTAX"\r
269  section below for details of the supported syntax for <search-terms>.\r
270 diff --git a/notmuch.c b/notmuch.c\r
271 index e004c6c..601ffad 100644\r
272 --- a/notmuch.c\r
273 +++ b/notmuch.c\r
274 @@ -222,6 +222,15 @@ static command_t commands[] = {\r
275        "\t\t(oldest-first) or reverse chronological order\n"\r
276        "\t\t(newest-first), which is the default.\n"\r
277        "\n"\r
278 +      "\t--first=[-]N\n"\r
279 +      "\n"\r
280 +      "\t\tSkip displaying the first N results. With the leading '-',\n"\r
281 +      "\t\tstart at the Nth result from the end.\n"\r
282 +      "\n"\r
283 +      "\t--maxitems=N\n"\r
284 +      "\n"\r
285 +      "\t\tLimit the number of displayed results to N.\n"\r
286 +      "\n"\r
287        "\tSee \"notmuch help search-terms\" for details of the search\n"\r
288        "\tterms syntax." },\r
289      { "show", notmuch_show_command,\r
290 -- \r
291 1.7.5.4\r
292 \r