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