[PATCH v7 2/6] cli: add options --offset and --limit to notmuch search
authorJani Nikula <jani@nikula.org>
Tue, 15 Nov 2011 20:08:49 +0000 (22:08 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:40:11 +0000 (09:40 -0800)
53/80107f8a49f938c1694b0bb9e19abf09fdc18d [new file with mode: 0644]

diff --git a/53/80107f8a49f938c1694b0bb9e19abf09fdc18d b/53/80107f8a49f938c1694b0bb9e19abf09fdc18d
new file mode 100644 (file)
index 0000000..facc977
--- /dev/null
@@ -0,0 +1,289 @@
+Return-Path: <jani@nikula.org>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id B592B429E21\r
+       for <notmuch@notmuchmail.org>; Tue, 15 Nov 2011 12:09:10 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id wxJB-b0UN4TZ for <notmuch@notmuchmail.org>;\r
+       Tue, 15 Nov 2011 12:09:10 -0800 (PST)\r
+Received: from mail-ey0-f181.google.com (mail-ey0-f181.google.com\r
+       [209.85.215.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id B1C4A431FB6\r
+       for <notmuch@notmuchmail.org>; Tue, 15 Nov 2011 12:09:09 -0800 (PST)\r
+Received: by eyx24 with SMTP id 24so7941857eyx.26\r
+       for <notmuch@notmuchmail.org>; Tue, 15 Nov 2011 12:09:07 -0800 (PST)\r
+Received: by 10.213.8.148 with SMTP id h20mr2393ebh.113.1321387746453;\r
+       Tue, 15 Nov 2011 12:09:06 -0800 (PST)\r
+Received: from localhost (dsl-hkibrasgw4-fe5cdc00-23.dhcp.inet.fi.\r
+       [80.220.92.23])\r
+       by mx.google.com with ESMTPS id k13sm2095156fah.0.2011.11.15.12.09.04\r
+       (version=SSLv3 cipher=OTHER); Tue, 15 Nov 2011 12:09:05 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v7 2/6] cli: add options --offset and --limit to notmuch\r
+ search\r
+Date: Tue, 15 Nov 2011 22:08:49 +0200\r
+Message-Id:\r
+ <760c5c952c6de12bc138d991c0f146eef7413c37.1321386762.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.5.4\r
+In-Reply-To: <cover.1321386762.git.jani@nikula.org>\r
+References: <cover.1321386762.git.jani@nikula.org>\r
+In-Reply-To: <cover.1321386762.git.jani@nikula.org>\r
+References: <cover.1321386762.git.jani@nikula.org>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Tue, 15 Nov 2011 20:09:10 -0000\r
+\r
+Add options --offset=[-]N and --limit=M to notmuch search to determine the\r
+first result and maximum number of results to display.\r
+\r
+Option --limit=M limits the maximum number of results to display to M.\r
+\r
+Option --offset=[-]N skips the first N results; with the leading '-' skip\r
+until the Nth result from the end.\r
+\r
+Note that --offset with a negative N for thread or summary output requires\r
+counting the number of matching threads in advance.\r
+\r
+Signed-off-by: Jani Nikula <jani@nikula.org>\r
+---\r
+ NEWS             |    5 ++++\r
+ notmuch-search.c |   68 ++++++++++++++++++++++++++++++++++++++++++++---------\r
+ notmuch.1        |   19 +++++++++++++-\r
+ notmuch.c        |    9 +++++++\r
+ 4 files changed, 87 insertions(+), 14 deletions(-)\r
+\r
+diff --git a/NEWS b/NEWS\r
+index 88f7b20..f224d02 100644\r
+--- a/NEWS\r
++++ b/NEWS\r
+@@ -23,6 +23,11 @@ Add search terms to  "notmuch dump"\r
+   search/show/tag. The output file argument of dump is deprecated in\r
+   favour of using stdout.\r
\r
++Add "notmuch search" --offset and --limit options\r
++\r
++  The search command now takes options --offset=[-]N and --limit=N to limit\r
++  the number of results shown.\r
++\r
+ Optimizations\r
+ -------------\r
\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index 6f04d9a..36686d1 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -194,13 +194,22 @@ static int\r
+ do_search_threads (const search_format_t *format,\r
+                  notmuch_query_t *query,\r
+                  notmuch_sort_t sort,\r
+-                 output_t output)\r
++                 output_t output,\r
++                 int offset,\r
++                 int limit)\r
+ {\r
+     notmuch_thread_t *thread;\r
+     notmuch_threads_t *threads;\r
+     notmuch_tags_t *tags;\r
+     time_t date;\r
+     int first_thread = 1;\r
++    int i;\r
++\r
++    if (offset < 0) {\r
++      offset += notmuch_query_count_threads (query);\r
++      if (offset < 0)\r
++          offset = 0;\r
++    }\r
\r
+     threads = notmuch_query_search_threads (query);\r
+     if (threads == NULL)\r
+@@ -208,17 +217,22 @@ do_search_threads (const search_format_t *format,\r
\r
+     fputs (format->results_start, stdout);\r
\r
+-    for (;\r
+-       notmuch_threads_valid (threads);\r
+-       notmuch_threads_move_to_next (threads))\r
++    for (i = 0;\r
++       notmuch_threads_valid (threads) && (limit < 0 || i < offset + limit);\r
++       notmuch_threads_move_to_next (threads), i++)\r
+     {\r
+       int first_tag = 1;\r
\r
++      thread = notmuch_threads_get (threads);\r
++\r
++      if (i < offset) {\r
++          notmuch_thread_destroy (thread);\r
++          continue;\r
++      }\r
++\r
+       if (! first_thread)\r
+           fputs (format->item_sep, stdout);\r
\r
+-      thread = notmuch_threads_get (threads);\r
+-\r
+       if (output == OUTPUT_THREADS) {\r
+           format->item_id (thread, "thread:",\r
+                            notmuch_thread_get_thread_id (thread));\r
+@@ -271,12 +285,21 @@ do_search_threads (const search_format_t *format,\r
+ static int\r
+ do_search_messages (const search_format_t *format,\r
+                   notmuch_query_t *query,\r
+-                  output_t output)\r
++                  output_t output,\r
++                  int offset,\r
++                  int limit)\r
+ {\r
+     notmuch_message_t *message;\r
+     notmuch_messages_t *messages;\r
+     notmuch_filenames_t *filenames;\r
+     int first_message = 1;\r
++    int i;\r
++\r
++    if (offset < 0) {\r
++      offset += notmuch_query_count_messages (query);\r
++      if (offset < 0)\r
++          offset = 0;\r
++    }\r
\r
+     messages = notmuch_query_search_messages (query);\r
+     if (messages == NULL)\r
+@@ -284,10 +307,13 @@ do_search_messages (const search_format_t *format,\r
\r
+     fputs (format->results_start, stdout);\r
\r
+-    for (;\r
+-       notmuch_messages_valid (messages);\r
+-       notmuch_messages_move_to_next (messages))\r
++    for (i = 0;\r
++       notmuch_messages_valid (messages) && (limit < 0 || i < offset + limit);\r
++       notmuch_messages_move_to_next (messages), i++)\r
+     {\r
++      if (i < offset)\r
++          continue;\r
++\r
+       message = notmuch_messages_get (messages);\r
\r
+       if (output == OUTPUT_FILES) {\r
+@@ -394,6 +420,8 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
+     const search_format_t *format = &format_text;\r
+     int i, ret;\r
+     output_t output = OUTPUT_SUMMARY;\r
++    int offset = 0;\r
++    int limit = -1; /* unlimited */\r
\r
+     argc--; argv++; /* skip subcommand argument */\r
\r
+@@ -412,6 +440,22 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
+               fprintf (stderr, "Invalid value for --sort: %s\n", opt);\r
+               return 1;\r
+           }\r
++      } else if (STRNCMP_LITERAL (argv[i], "--offset=") == 0) {\r
++          char *p;\r
++          opt = argv[i] + sizeof ("--offset=") - 1;\r
++          offset = strtol (opt, &p, 10);\r
++          if (*opt == '\0' || p == opt || *p != '\0') {\r
++              fprintf (stderr, "Invalid value for --offset: %s\n", opt);\r
++              return 1;\r
++          }\r
++      } else if (STRNCMP_LITERAL (argv[i], "--limit=") == 0) {\r
++          char *p;\r
++          opt = argv[i] + sizeof ("--limit=") - 1;\r
++          limit = strtoul (opt, &p, 10);\r
++          if (*opt == '\0' || p == opt || *p != '\0') {\r
++              fprintf (stderr, "Invalid value for --limit: %s\n", opt);\r
++              return 1;\r
++          }\r
+       } else if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {\r
+           opt = argv[i] + sizeof ("--format=") - 1;\r
+           if (strcmp (opt, "text") == 0) {\r
+@@ -478,11 +522,11 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
+     default:\r
+     case OUTPUT_SUMMARY:\r
+     case OUTPUT_THREADS:\r
+-      ret = do_search_threads (format, query, sort, output);\r
++      ret = do_search_threads (format, query, sort, output, offset, limit);\r
+       break;\r
+     case OUTPUT_MESSAGES:\r
+     case OUTPUT_FILES:\r
+-      ret = do_search_messages (format, query, output);\r
++      ret = do_search_messages (format, query, output, offset, limit);\r
+       break;\r
+     case OUTPUT_TAGS:\r
+       ret = do_search_tags (notmuch, format, query);\r
+diff --git a/notmuch.1 b/notmuch.1\r
+index bba479e..cda777b 100644\r
+--- a/notmuch.1\r
++++ b/notmuch.1\r
+@@ -214,11 +214,26 @@ when sorting by\r
+ .B newest\-first\r
+ the threads will be sorted by the newest message in each thread.\r
\r
+-.RE\r
+-.RS 4\r
+ By default, results will be displayed in reverse chronological order,\r
+ (that is, the newest results will be displayed first).\r
++.RE\r
++\r
++.RS 4\r
++.TP 4\r
++.BR \-\-offset=[\-]N\r
++\r
++Skip displaying the first N results. With the leading '\-', start at the Nth\r
++result from the end.\r
++.RE\r
++\r
++.RS 4\r
++.TP 4\r
++.BR \-\-limit=N\r
\r
++Limit the number of displayed results to N.\r
++.RE\r
++\r
++.RS 4\r
+ See the\r
+ .B "SEARCH SYNTAX"\r
+ section below for details of the supported syntax for <search-terms>.\r
+diff --git a/notmuch.c b/notmuch.c\r
+index e004c6c..394371d 100644\r
+--- a/notmuch.c\r
++++ b/notmuch.c\r
+@@ -222,6 +222,15 @@ static command_t commands[] = {\r
+       "\t\t(oldest-first) or reverse chronological order\n"\r
+       "\t\t(newest-first), which is the default.\n"\r
+       "\n"\r
++      "\t--offset=[-]N\n"\r
++      "\n"\r
++      "\t\tSkip displaying the first N results. With the leading '-',\n"\r
++      "\t\tstart at the Nth result from the end.\n"\r
++      "\n"\r
++      "\t--limit=N\n"\r
++      "\n"\r
++      "\t\tLimit the number of displayed results to N.\n"\r
++      "\n"\r
+       "\tSee \"notmuch help search-terms\" for details of the search\n"\r
+       "\tterms syntax." },\r
+     { "show", notmuch_show_command,\r
+-- \r
+1.7.5.4\r
+\r