Re: [PATCH 9/9] add has: query prefix to search for specific properties
[notmuch-archives.git] / 53 / 80107f8a49f938c1694b0bb9e19abf09fdc18d
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 B592B429E21\r
6         for <notmuch@notmuchmail.org>; Tue, 15 Nov 2011 12:09:10 -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 wxJB-b0UN4TZ for <notmuch@notmuchmail.org>;\r
17         Tue, 15 Nov 2011 12:09:10 -0800 (PST)\r
18 Received: from mail-ey0-f181.google.com (mail-ey0-f181.google.com\r
19         [209.85.215.181]) (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 B1C4A431FB6\r
22         for <notmuch@notmuchmail.org>; Tue, 15 Nov 2011 12:09:09 -0800 (PST)\r
23 Received: by eyx24 with SMTP id 24so7941857eyx.26\r
24         for <notmuch@notmuchmail.org>; Tue, 15 Nov 2011 12:09:07 -0800 (PST)\r
25 Received: by 10.213.8.148 with SMTP id h20mr2393ebh.113.1321387746453;\r
26         Tue, 15 Nov 2011 12:09:06 -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 k13sm2095156fah.0.2011.11.15.12.09.04\r
30         (version=SSLv3 cipher=OTHER); Tue, 15 Nov 2011 12:09:05 -0800 (PST)\r
31 From: Jani Nikula <jani@nikula.org>\r
32 To: notmuch@notmuchmail.org\r
33 Subject: [PATCH v7 2/6] cli: add options --offset and --limit to notmuch\r
34  search\r
35 Date: Tue, 15 Nov 2011 22:08:49 +0200\r
36 Message-Id:\r
37  <760c5c952c6de12bc138d991c0f146eef7413c37.1321386762.git.jani@nikula.org>\r
38 X-Mailer: git-send-email 1.7.5.4\r
39 In-Reply-To: <cover.1321386762.git.jani@nikula.org>\r
40 References: <cover.1321386762.git.jani@nikula.org>\r
41 In-Reply-To: <cover.1321386762.git.jani@nikula.org>\r
42 References: <cover.1321386762.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: Tue, 15 Nov 2011 20:09:10 -0000\r
56 \r
57 Add options --offset=[-]N and --limit=M to notmuch search to determine the\r
58 first result and maximum number of results to display.\r
59 \r
60 Option --limit=M limits the maximum number of results to display to M.\r
61 \r
62 Option --offset=[-]N skips the first N results; with the leading '-' skip\r
63 until the Nth result from the end.\r
64 \r
65 Note that --offset with a negative N for thread or summary output requires\r
66 counting the number of matching threads in advance.\r
67 \r
68 Signed-off-by: Jani Nikula <jani@nikula.org>\r
69 ---\r
70  NEWS             |    5 ++++\r
71  notmuch-search.c |   68 ++++++++++++++++++++++++++++++++++++++++++++---------\r
72  notmuch.1        |   19 +++++++++++++-\r
73  notmuch.c        |    9 +++++++\r
74  4 files changed, 87 insertions(+), 14 deletions(-)\r
75 \r
76 diff --git a/NEWS b/NEWS\r
77 index 88f7b20..f224d02 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" --offset and --limit options\r
85 +\r
86 +  The search command now takes options --offset=[-]N and --limit=N to limit\r
87 +  the number of results shown.\r
88 +\r
89  Optimizations\r
90  -------------\r
91  \r
92 diff --git a/notmuch-search.c b/notmuch-search.c\r
93 index 6f04d9a..36686d1 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 offset,\r
103 +                  int limit)\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 (offset < 0) {\r
113 +       offset += notmuch_query_count_threads (query);\r
114 +       if (offset < 0)\r
115 +           offset = 0;\r
116 +    }\r
117  \r
118      threads = notmuch_query_search_threads (query);\r
119      if (threads == NULL)\r
120 @@ -208,17 +217,22 @@ 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) && (limit < 0 || i < offset + limit);\r
129 +        notmuch_threads_move_to_next (threads), i++)\r
130      {\r
131         int first_tag = 1;\r
132  \r
133 +       thread = notmuch_threads_get (threads);\r
134 +\r
135 +       if (i < offset) {\r
136 +           notmuch_thread_destroy (thread);\r
137 +           continue;\r
138 +       }\r
139 +\r
140         if (! first_thread)\r
141             fputs (format->item_sep, stdout);\r
142  \r
143 -       thread = notmuch_threads_get (threads);\r
144 -\r
145         if (output == OUTPUT_THREADS) {\r
146             format->item_id (thread, "thread:",\r
147                              notmuch_thread_get_thread_id (thread));\r
148 @@ -271,12 +285,21 @@ do_search_threads (const search_format_t *format,\r
149  static int\r
150  do_search_messages (const search_format_t *format,\r
151                     notmuch_query_t *query,\r
152 -                   output_t output)\r
153 +                   output_t output,\r
154 +                   int offset,\r
155 +                   int limit)\r
156  {\r
157      notmuch_message_t *message;\r
158      notmuch_messages_t *messages;\r
159      notmuch_filenames_t *filenames;\r
160      int first_message = 1;\r
161 +    int i;\r
162 +\r
163 +    if (offset < 0) {\r
164 +       offset += notmuch_query_count_messages (query);\r
165 +       if (offset < 0)\r
166 +           offset = 0;\r
167 +    }\r
168  \r
169      messages = notmuch_query_search_messages (query);\r
170      if (messages == NULL)\r
171 @@ -284,10 +307,13 @@ do_search_messages (const search_format_t *format,\r
172  \r
173      fputs (format->results_start, stdout);\r
174  \r
175 -    for (;\r
176 -        notmuch_messages_valid (messages);\r
177 -        notmuch_messages_move_to_next (messages))\r
178 +    for (i = 0;\r
179 +        notmuch_messages_valid (messages) && (limit < 0 || i < offset + limit);\r
180 +        notmuch_messages_move_to_next (messages), i++)\r
181      {\r
182 +       if (i < offset)\r
183 +           continue;\r
184 +\r
185         message = notmuch_messages_get (messages);\r
186  \r
187         if (output == OUTPUT_FILES) {\r
188 @@ -394,6 +420,8 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
189      const search_format_t *format = &format_text;\r
190      int i, ret;\r
191      output_t output = OUTPUT_SUMMARY;\r
192 +    int offset = 0;\r
193 +    int limit = -1; /* unlimited */\r
194  \r
195      argc--; argv++; /* skip subcommand argument */\r
196  \r
197 @@ -412,6 +440,22 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
198                 fprintf (stderr, "Invalid value for --sort: %s\n", opt);\r
199                 return 1;\r
200             }\r
201 +       } else if (STRNCMP_LITERAL (argv[i], "--offset=") == 0) {\r
202 +           char *p;\r
203 +           opt = argv[i] + sizeof ("--offset=") - 1;\r
204 +           offset = strtol (opt, &p, 10);\r
205 +           if (*opt == '\0' || p == opt || *p != '\0') {\r
206 +               fprintf (stderr, "Invalid value for --offset: %s\n", opt);\r
207 +               return 1;\r
208 +           }\r
209 +       } else if (STRNCMP_LITERAL (argv[i], "--limit=") == 0) {\r
210 +           char *p;\r
211 +           opt = argv[i] + sizeof ("--limit=") - 1;\r
212 +           limit = strtoul (opt, &p, 10);\r
213 +           if (*opt == '\0' || p == opt || *p != '\0') {\r
214 +               fprintf (stderr, "Invalid value for --limit: %s\n", opt);\r
215 +               return 1;\r
216 +           }\r
217         } else if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {\r
218             opt = argv[i] + sizeof ("--format=") - 1;\r
219             if (strcmp (opt, "text") == 0) {\r
220 @@ -478,11 +522,11 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
221      default:\r
222      case OUTPUT_SUMMARY:\r
223      case OUTPUT_THREADS:\r
224 -       ret = do_search_threads (format, query, sort, output);\r
225 +       ret = do_search_threads (format, query, sort, output, offset, limit);\r
226         break;\r
227      case OUTPUT_MESSAGES:\r
228      case OUTPUT_FILES:\r
229 -       ret = do_search_messages (format, query, output);\r
230 +       ret = do_search_messages (format, query, output, offset, limit);\r
231         break;\r
232      case OUTPUT_TAGS:\r
233         ret = do_search_tags (notmuch, format, query);\r
234 diff --git a/notmuch.1 b/notmuch.1\r
235 index bba479e..cda777b 100644\r
236 --- a/notmuch.1\r
237 +++ b/notmuch.1\r
238 @@ -214,11 +214,26 @@ when sorting by\r
239  .B newest\-first\r
240  the threads will be sorted by the newest message in each thread.\r
241  \r
242 -.RE\r
243 -.RS 4\r
244  By default, results will be displayed in reverse chronological order,\r
245  (that is, the newest results will be displayed first).\r
246 +.RE\r
247 +\r
248 +.RS 4\r
249 +.TP 4\r
250 +.BR \-\-offset=[\-]N\r
251 +\r
252 +Skip displaying the first N results. With the leading '\-', start at the Nth\r
253 +result from the end.\r
254 +.RE\r
255 +\r
256 +.RS 4\r
257 +.TP 4\r
258 +.BR \-\-limit=N\r
259  \r
260 +Limit the number of displayed results to N.\r
261 +.RE\r
262 +\r
263 +.RS 4\r
264  See the\r
265  .B "SEARCH SYNTAX"\r
266  section below for details of the supported syntax for <search-terms>.\r
267 diff --git a/notmuch.c b/notmuch.c\r
268 index e004c6c..394371d 100644\r
269 --- a/notmuch.c\r
270 +++ b/notmuch.c\r
271 @@ -222,6 +222,15 @@ static command_t commands[] = {\r
272        "\t\t(oldest-first) or reverse chronological order\n"\r
273        "\t\t(newest-first), which is the default.\n"\r
274        "\n"\r
275 +      "\t--offset=[-]N\n"\r
276 +      "\n"\r
277 +      "\t\tSkip displaying the first N results. With the leading '-',\n"\r
278 +      "\t\tstart at the Nth result from the end.\n"\r
279 +      "\n"\r
280 +      "\t--limit=N\n"\r
281 +      "\n"\r
282 +      "\t\tLimit the number of displayed results to N.\n"\r
283 +      "\n"\r
284        "\tSee \"notmuch help search-terms\" for details of the search\n"\r
285        "\tterms syntax." },\r
286      { "show", notmuch_show_command,\r
287 -- \r
288 1.7.5.4\r
289 \r