[RFC PATCH v2 2/3] cli: add options --first and --maxitems to notmuch search
authorJani Nikula <jani@nikula.org>
Mon, 31 Oct 2011 21:18:09 +0000 (23:18 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:39:56 +0000 (09:39 -0800)
f9/2007f9a5e0bcb0ebf57d9946f3566f406326b1 [new file with mode: 0644]

diff --git a/f9/2007f9a5e0bcb0ebf57d9946f3566f406326b1 b/f9/2007f9a5e0bcb0ebf57d9946f3566f406326b1
new file mode 100644 (file)
index 0000000..27786e1
--- /dev/null
@@ -0,0 +1,219 @@
+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 5A86F431FB6\r
+       for <notmuch@notmuchmail.org>; Mon, 31 Oct 2011 14:18:27 -0700 (PDT)\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 BJm5avjV6kbW for <notmuch@notmuchmail.org>;\r
+       Mon, 31 Oct 2011 14:18:26 -0700 (PDT)\r
+Received: from mail-bw0-f53.google.com (mail-bw0-f53.google.com\r
+       [209.85.214.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 4E9A1429E29\r
+       for <notmuch@notmuchmail.org>; Mon, 31 Oct 2011 14:18:24 -0700 (PDT)\r
+Received: by bkbzs8 with SMTP id zs8so4069839bkb.26\r
+       for <notmuch@notmuchmail.org>; Mon, 31 Oct 2011 14:18:21 -0700 (PDT)\r
+Received: by 10.204.9.211 with SMTP id m19mr2654817bkm.92.1320095901236;\r
+       Mon, 31 Oct 2011 14:18:21 -0700 (PDT)\r
+Received: from localhost (dsl-hkibrasgw4-fe5cdc00-23.dhcp.inet.fi.\r
+       [80.220.92.23])\r
+       by mx.google.com with ESMTPS id k13sm16686618fah.0.2011.10.31.14.18.19\r
+       (version=SSLv3 cipher=OTHER); Mon, 31 Oct 2011 14:18:20 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [RFC PATCH v2 2/3] cli: add options --first and --maxitems to notmuch\r
+       search\r
+Date: Mon, 31 Oct 2011 23:18:09 +0200\r
+Message-Id:\r
+ <e3440dc1ad040137074b1db5ff3882c987234b0e.1320093940.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.5.4\r
+In-Reply-To: <cover.1320093940.git.jani@nikula.org>\r
+References: <cover.1320093940.git.jani@nikula.org>\r
+In-Reply-To: <cover.1320093940.git.jani@nikula.org>\r
+References: <cover.1320093940.git.jani@nikula.org>\r
+Cc: amdragon@mit.edu\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: Mon, 31 Oct 2011 21:18:27 -0000\r
+\r
+Add options --first=[-]N and --maxitems=M to notmuch search to determine\r
+the first result and maximum number of results to display.\r
+\r
+Option --maxitems=M limits the maximum number of results to display to M.\r
+\r
+Option --first=[-]N skips the first N results; with the leading '-' skip\r
+until the Nth result from the end (showing a total of N results if within\r
+bounds of the total number of results and not limited with --maxitems).\r
+\r
+Note that --first with a negative N for thread or summary output requires\r
+counting the number of matching threads in advance, which is a heavy\r
+operation.\r
+\r
+Signed-off-by: Jani Nikula <jani@nikula.org>\r
+---\r
+ notmuch-search.c |   67 ++++++++++++++++++++++++++++++++++++++++++++---------\r
+ 1 files changed, 55 insertions(+), 12 deletions(-)\r
+\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index 6f04d9a..ceef6ac 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -194,13 +194,20 @@ 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 first,\r
++                 unsigned int maxitems)\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
++    unsigned int i, start;\r
++\r
++    if (first < 0)\r
++      first += notmuch_query_count_threads (query);\r
++    start = first < 0 ? 0 : first;\r
\r
+     threads = notmuch_query_search_threads (query);\r
+     if (threads == NULL)\r
+@@ -208,17 +215,23 @@ 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) &&\r
++           (!maxitems || i < start + maxitems);\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 < start) {\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 +284,20 @@ 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 first,\r
++                  unsigned int maxitems)\r
+ {\r
+     notmuch_message_t *message;\r
+     notmuch_messages_t *messages;\r
+     notmuch_filenames_t *filenames;\r
+     int first_message = 1;\r
++    unsigned int i, start;\r
++\r
++    if (first < 0)\r
++      first += notmuch_query_count_messages(query);\r
++\r
++    start = first < 0 ? 0 : first;\r
\r
+     messages = notmuch_query_search_messages (query);\r
+     if (messages == NULL)\r
+@@ -284,10 +305,14 @@ 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) &&\r
++           (!maxitems || i < start + maxitems);\r
++       notmuch_messages_move_to_next (messages), i++)\r
+     {\r
++      if (i < start)\r
++          continue;\r
++\r
+       message = notmuch_messages_get (messages);\r
\r
+       if (output == OUTPUT_FILES) {\r
+@@ -394,6 +419,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
++    unsigned int maxitems = 0;\r
++    int first = 0;\r
\r
+     argc--; argv++; /* skip subcommand argument */\r
\r
+@@ -412,6 +439,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], "--first=") == 0) {\r
++          char *p;\r
++          opt = argv[i] + sizeof ("--first=") - 1;\r
++          first = strtol(opt, &p, 10);\r
++          if (*opt == '\0' || p == opt || *p != '\0') {\r
++              fprintf (stderr, "Invalid value for --first: %s\n", opt);\r
++              return 1;\r
++          }\r
++      } else if (STRNCMP_LITERAL (argv[i], "--maxitems=") == 0) {\r
++          char *p;\r
++          opt = argv[i] + sizeof ("--maxitems=") - 1;\r
++          maxitems = strtoul(opt, &p, 10);\r
++          if (*opt == '\0' || p == opt || *p != '\0') {\r
++              fprintf (stderr, "Invalid value for --maxitems: %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 +521,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, first, maxitems);\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, first, maxitems);\r
+       break;\r
+     case OUTPUT_TAGS:\r
+       ret = do_search_tags (notmuch, format, query);\r
+-- \r
+1.7.5.4\r
+\r