Re: [PATCH 4/4] setup: prompt user for search.exclude_tags value
authorAustin Clements <amdragon@MIT.EDU>
Fri, 20 Jan 2012 04:19:02 +0000 (23:19 +1900)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:42:47 +0000 (09:42 -0800)
2d/35594ffb327021f87e4c3855d67ea2bda2ea89 [new file with mode: 0644]

diff --git a/2d/35594ffb327021f87e4c3855d67ea2bda2ea89 b/2d/35594ffb327021f87e4c3855d67ea2bda2ea89
new file mode 100644 (file)
index 0000000..d01faf5
--- /dev/null
@@ -0,0 +1,230 @@
+Return-Path: <amdragon@mit.edu>\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 5F470431FAF\r
+       for <notmuch@notmuchmail.org>; Thu, 19 Jan 2012 20:19:23 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\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 JL3u0az9MHWG for <notmuch@notmuchmail.org>;\r
+       Thu, 19 Jan 2012 20:19:22 -0800 (PST)\r
+Received: from dmz-mailsec-scanner-8.mit.edu (DMZ-MAILSEC-SCANNER-8.MIT.EDU\r
+       [18.7.68.37])\r
+       by olra.theworths.org (Postfix) with ESMTP id 9B8FF431FAE\r
+       for <notmuch@notmuchmail.org>; Thu, 19 Jan 2012 20:19:22 -0800 (PST)\r
+X-AuditID: 12074425-b7f4a6d0000008e0-55-4f18eb4a6a50\r
+Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
+       by dmz-mailsec-scanner-8.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id 6D.6B.02272.A4BE81F4; Thu, 19 Jan 2012 23:19:22 -0500 (EST)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+       by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id q0K4JLRJ018735; \r
+       Thu, 19 Jan 2012 23:19:21 -0500\r
+Received: from awakening.csail.mit.edu (awakening.csail.mit.edu [18.26.4.91])\r
+       (authenticated bits=0)\r
+       (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+       by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id q0K4JK4K011605\r
+       (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+       Thu, 19 Jan 2012 23:19:21 -0500 (EST)\r
+Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.77)\r
+       (envelope-from <amdragon@mit.edu>)\r
+       id 1Ro5wM-0004cf-6U; Thu, 19 Jan 2012 23:19:02 -0500\r
+Date: Thu, 19 Jan 2012 23:19:02 -0500\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: Pieter Praet <pieter@praet.org>\r
+Subject: Re: [PATCH 4/4] setup: prompt user for search.exclude_tags value\r
+Message-ID: <20120120041902.GV16740@mit.edu>\r
+References: <1326586654-16840-3-git-send-email-amdragon@mit.edu>\r
+       <1327000744-25463-1-git-send-email-pieter@praet.org>\r
+       <1327000744-25463-5-git-send-email-pieter@praet.org>\r
+       <20120119194437.GP16740@mit.edu> <8762g7o0k4.fsf@praet.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+Content-Disposition: inline\r
+In-Reply-To: <8762g7o0k4.fsf@praet.org>\r
+User-Agent: Mutt/1.5.21 (2010-09-15)\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFmphleLIzCtJLcpLzFFi42IRYrdT1/V6LeFv8HY6h8X1mzOZLX6/vsHs\r
+       wOTxbNUtZo+OfZdZA5iiuGxSUnMyy1KL9O0SuDJOnz/FVLBcpWLhqilMDYyPpboYOTkkBEwk\r
+       2p7cZ4OwxSQu3FsPZgsJ7GOUOPsqpouRC8jewCgx9dtNFgjnJJNEy7lDjBDOEkaJH7vPg7Ww\r
+       CKhKXJp5hQnEZhPQkNi2fzkjiC0ioCxx+slPdhCbWUBLYuvGD2BxYQFPiZfz28DivAI6Ems6\r
+       LrJBDH3HKLHh8SQmiISgxMmZT1hgmm/8ewkU5wCypSWW/+MACXMKqEvsPnWCGcQWFVCRmHJy\r
+       G9sERqFZSLpnIemehdC9gJF5FaNsSm6Vbm5iZk5xarJucXJiXl5qka6FXm5miV5qSukmRnBg\r
+       u6juYJxwSOkQowAHoxIPL7erhL8Qa2JZcWXuIUZJDiYlUV6Vx0AhvqT8lMqMxOKM+KLSnNTi\r
+       Q4wSHMxKIrwOT4ByvCmJlVWpRfkwKWkOFiVxXk2td35CAumJJanZqakFqUUwWRkODiUJ3rWv\r
+       gBoFi1LTUyvSMnNKENJMHJwgw3mAhi8BqeEtLkjMLc5Mh8ifYlSUEudtBUkIgCQySvPgemGJ\r
+       5xWjONArwryrQKp4gEkLrvsV0GAmoMEeTWIgg0sSEVJSDYwdt5fm3TTwmtSVfvmlV9oWrszm\r
+       fccjdjyq7upYIllxg3+ro92s/CXttyOUC2zvTOWtMJ9mu0ZTuZT70WTz28y3gpZ8nbto89ar\r
+       29QLDO8GXnTk+qDzenmUanPwsaMnFjx6ueGT5IPmd89v/7cuuvp+2ta4SKMJCa0/p3jXbhHc\r
+       vfX9zEmVJzSXKbEUZyQaajEXFScCAOWOm2cXAwAA\r
+Cc: Notmuch Mail <notmuch@notmuchmail.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: Fri, 20 Jan 2012 04:19:23 -0000\r
+\r
+Quoth Pieter Praet on Jan 19 at 10:16 pm:\r
+> On Thu, 19 Jan 2012 14:44:37 -0500, Austin Clements <amdragon@MIT.EDU> wrote:\r
+> > Quoth Pieter Praet on Jan 19 at  8:19 pm:\r
+> > > Allow users to customize the search.exclude_tags option during setup.\r
+> > > \r
+> > > ---\r
+> > >  notmuch-setup.c |   36 ++++++++++++++++++++++++++++++++++++\r
+> > >  1 files changed, 36 insertions(+), 0 deletions(-)\r
+> > > \r
+> > > diff --git a/notmuch-setup.c b/notmuch-setup.c\r
+> > > index c3ea937..44d4aaa 100644\r
+> > > --- a/notmuch-setup.c\r
+> > > +++ b/notmuch-setup.c\r
+> > > @@ -101,6 +101,8 @@ notmuch_setup_command (unused (void *ctx),\r
+> > >      int is_new;\r
+> > >      const char **new_tags;\r
+> > >      size_t new_tags_len;\r
+> > > +    const char **search_exclude_tags;\r
+> > > +    size_t search_exclude_tags_len;\r
+> > >  \r
+> > >  #define prompt(format, ...)                                     \\r
+> > >      do {                                                        \\r
+> > > @@ -195,6 +197,40 @@ notmuch_setup_command (unused (void *ctx),\r
+> > >          g_ptr_array_free (tags, TRUE);\r
+> > >      }\r
+> > >  \r
+> > > +    search_exclude_tags = notmuch_config_get_search_exclude_tags (config, &search_exclude_tags_len);\r
+> > > +\r
+> > > +    printf ("Tags to exclude when searching messages (separated by spaces) [");\r
+> > > +\r
+> > > +    for (i = 0; i < search_exclude_tags_len; i++) {\r
+> > > +        if (i != 0)\r
+> > > +            printf (" ");\r
+> > > +        printf ("%s", search_exclude_tags[i]);\r
+> > > +    }\r
+> > > +\r
+> > > +    prompt ("]: ");\r
+> > > +\r
+> > > +    if (strlen (response)) {\r
+> > > +        GPtrArray *tags = g_ptr_array_new ();\r
+> > > +        char *tag = response;\r
+> > > +        char *space;\r
+> > > +\r
+> > > +        while (tag && *tag) {\r
+> > > +            space = strchr (tag, ' ');\r
+> > > +            if (space)\r
+> > > +                g_ptr_array_add (tags, talloc_strndup (ctx, tag, space - tag));\r
+> > > +            else\r
+> > > +                g_ptr_array_add (tags, talloc_strdup (ctx, tag));\r
+> > > +            tag = space;\r
+> > > +            while (tag && *tag == ' ')\r
+> > > +                tag++;\r
+> > > +        }\r
+> > > +\r
+> > > +        notmuch_config_set_search_exclude_tags (config, (const char **) tags->pdata,\r
+> > > +                                     tags->len);\r
+> > > +\r
+> > > +        g_ptr_array_free (tags, TRUE);\r
+> > > +    }\r
+> > > +\r
+> > \r
+> > Holy code duplication.  Can we move most of this (at least the\r
+> > response parsing part and maybe the prompt printing) into a function\r
+> > and use it for both new tags and exclude tags?\r
+> > \r
+> \r
+> Depends on who "we" is... :)  I would gladly do it if I could, but I\r
+> think this uber1337 copy-paste coding job serves as *very* convincing\r
+> proof that I'm pretty much in the dark (with a single wet match) when it\r
+> comes to C :)\r
+\r
+Hah, okay.  How about this as an initial minor refactoring of the code\r
+for new.tags?\r
+\r
+diff --git a/notmuch-setup.c b/notmuch-setup.c\r
+index c3ea937..dcfa607 100644\r
+--- a/notmuch-setup.c\r
++++ b/notmuch-setup.c\r
+@@ -87,6 +87,38 @@ welcome_message_post_setup (void)\r
+ "have sufficient storage space available now.\n\n");\r
+ }\r
\r
++static void\r
++print_tag_list (const char **tags, size_t tags_len)\r
++{\r
++    unsigned int i;\r
++    for (i = 0; i < tags_len; i++) {\r
++      if (i != 0)\r
++          printf (" ");\r
++      printf ("%s", tags[i]);\r
++    }\r
++}\r
++\r
++static GPtrArray *\r
++parse_tag_list (void *ctx, char *response)\r
++{\r
++    GPtrArray *tags = g_ptr_array_new ();\r
++    char *tag = response;\r
++    char *space;\r
++\r
++    while (tag && *tag) {\r
++      space = strchr (tag, ' ');\r
++      if (space)\r
++          g_ptr_array_add (tags, talloc_strndup (ctx, tag, space - tag));\r
++      else\r
++          g_ptr_array_add (tags, talloc_strdup (ctx, tag));\r
++      tag = space;\r
++      while (tag && *tag == ' ')\r
++          tag++;\r
++    }\r
++\r
++    return tags;\r
++}\r
++\r
+ int\r
+ notmuch_setup_command (unused (void *ctx),\r
+                      unused (int argc), unused (char *argv[]))\r
+@@ -164,30 +196,11 @@ notmuch_setup_command (unused (void *ctx),\r
+     new_tags = notmuch_config_get_new_tags (config, &new_tags_len);\r
\r
+     printf ("Tags to apply to all new messages (separated by spaces) [");\r
+-\r
+-    for (i = 0; i < new_tags_len; i++) {\r
+-      if (i != 0)\r
+-          printf (" ");\r
+-      printf ("%s", new_tags[i]);\r
+-    }\r
+-\r
++    print_tag_list(new_tags, new_tags_len);\r
+     prompt ("]: ");\r
\r
+     if (strlen (response)) {\r
+-      GPtrArray *tags = g_ptr_array_new ();\r
+-      char *tag = response;\r
+-      char *space;\r
+-\r
+-      while (tag && *tag) {\r
+-          space = strchr (tag, ' ');\r
+-          if (space)\r
+-              g_ptr_array_add (tags, talloc_strndup (ctx, tag, space - tag));\r
+-          else\r
+-              g_ptr_array_add (tags, talloc_strdup (ctx, tag));\r
+-          tag = space;\r
+-          while (tag && *tag == ' ')\r
+-              tag++;\r
+-      }\r
++      GPtrArray *tags = parse_tag_list (ctx, response);\r
\r
+       notmuch_config_set_new_tags (config, (const char **) tags->pdata,\r
+                                    tags->len);\r
+\r