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 1A44E431FBF for ; Mon, 26 Mar 2012 14:04:31 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" 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 mh+LsAt6J0Ju for ; Mon, 26 Mar 2012 14:04:29 -0700 (PDT) Received: from mail-lb0-f181.google.com (mail-lb0-f181.google.com [209.85.217.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 9236A431FAF for ; Mon, 26 Mar 2012 14:04:29 -0700 (PDT) Received: by lbok6 with SMTP id k6so4671094lbo.26 for ; Mon, 26 Mar 2012 14:04:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references :in-reply-to:references:x-gm-message-state; bh=D56dZfuAcdnIg2Ra6n886KLjYTaJlZGih4CRIc3PEgo=; b=UYN/18Rdyi5AJcDXnWDB7yY7U/D0TpGIG+Dz2jiux+8ozydFmzeaERdnuughK8QO09 ivCQglPqtZ8qiGvqv9IOCXDiPsFOWBumKh3A+Uy2xliv2Z+SmTABvic60WFoHlli0Svy TvmtxMPckRbcWjLcXrnWqF4PvyZyOCezSKpMAGyDLDZsidyro6zSq9NzdbY0R8B/E3Cm WGkXa2Q+n68OjD1LeuQaJXamg9yFBXJgkkXlfrmWY5dluWd9Vy1yWxHVK26GBrl9mRkq btWHPPWQvWZRIjai0y2K+2EnNTdqBvaMTR1DSI6ByhAurN5wLJIhTzsF7QxBHZLTnZ/a 0UBQ== Received: by 10.152.145.135 with SMTP id su7mr17135117lab.5.1332795866454; Mon, 26 Mar 2012 14:04:26 -0700 (PDT) Received: from localhost (dsl-hkibrasgw4-fe50f800-253.dhcp.inet.fi. [84.248.80.253]) by mx.google.com with ESMTPS id oy17sm18177700lab.7.2012.03.26.14.04.24 (version=SSLv3 cipher=OTHER); Mon, 26 Mar 2012 14:04:25 -0700 (PDT) From: Jani Nikula To: notmuch@notmuchmail.org Subject: [PATCH v3 3/4] cli: refactor "notmuch tag" query tagging into a separate function Date: Tue, 27 Mar 2012 00:04:10 +0300 Message-Id: <9c547e08d098cf75e52e6ecb7f2af2ca75730274.1332795415.git.jani@nikula.org> X-Mailer: git-send-email 1.7.5.4 In-Reply-To: References: In-Reply-To: References: X-Gm-Message-State: ALoCoQkeZlT38jroLnKl94fvz8v/POdErNgBl+HjVRSu/Bt1BgoB7/MtdSSSL6kz59jb0GYTNBts Cc: tomi.ollila@iki.fi 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: Mon, 26 Mar 2012 21:04:31 -0000 Refactor to make tagging code easier to reuse in the future. No functional changes. Signed-off-by: Jani Nikula --- notmuch-tag.c | 104 +++++++++++++++++++++++++++++++++------------------------ 1 files changed, 60 insertions(+), 44 deletions(-) diff --git a/notmuch-tag.c b/notmuch-tag.c index 0a6b140..05feed3 100644 --- a/notmuch-tag.c +++ b/notmuch-tag.c @@ -110,6 +110,63 @@ _optimize_tag_query (void *ctx, const char *orig_query_string, return query_string; } +/* Tag messages matching 'query_string' according to 'tag_ops', which + * must be an array of tagging operations terminated with an empty + * element. */ +static int +tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string, + tag_operation_t *tag_ops, notmuch_bool_t synchronize_flags) +{ + notmuch_query_t *query; + notmuch_messages_t *messages; + notmuch_message_t *message; + int i; + + /* Optimize the query so it excludes messages that already have + * the specified set of tags. */ + query_string = _optimize_tag_query (ctx, query_string, tag_ops); + if (query_string == NULL) { + fprintf (stderr, "Out of memory.\n"); + return 1; + } + + query = notmuch_query_create (notmuch, query_string); + if (query == NULL) { + fprintf (stderr, "Out of memory.\n"); + return 1; + } + + /* tagging is not interested in any special sort order */ + notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED); + + for (messages = notmuch_query_search_messages (query); + notmuch_messages_valid (messages) && !interrupted; + notmuch_messages_move_to_next (messages)) + { + message = notmuch_messages_get (messages); + + notmuch_message_freeze (message); + + for (i = 0; tag_ops[i].tag; i++) { + if (tag_ops[i].remove) + notmuch_message_remove_tag (message, tag_ops[i].tag); + else + notmuch_message_add_tag (message, tag_ops[i].tag); + } + + notmuch_message_thaw (message); + + if (synchronize_flags) + notmuch_message_tags_to_maildir_flags (message); + + notmuch_message_destroy (message); + } + + notmuch_query_destroy (query); + + return interrupted; +} + int notmuch_tag_command (void *ctx, int argc, char *argv[]) { @@ -118,12 +175,10 @@ notmuch_tag_command (void *ctx, int argc, char *argv[]) char *query_string; notmuch_config_t *config; notmuch_database_t *notmuch; - notmuch_query_t *query; - notmuch_messages_t *messages; - notmuch_message_t *message; struct sigaction action; notmuch_bool_t synchronize_flags; int i; + int ret; /* Setup our handler for SIGINT */ memset (&action, 0, sizeof (struct sigaction)); @@ -170,14 +225,6 @@ notmuch_tag_command (void *ctx, int argc, char *argv[]) return 1; } - /* Optimize the query so it excludes messages that already have - * the specified set of tags. */ - query_string = _optimize_tag_query (ctx, query_string, tag_ops); - if (query_string == NULL) { - fprintf (stderr, "Out of memory.\n"); - return 1; - } - config = notmuch_config_open (ctx, NULL, NULL); if (config == NULL) return 1; @@ -189,40 +236,9 @@ notmuch_tag_command (void *ctx, int argc, char *argv[]) synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config); - query = notmuch_query_create (notmuch, query_string); - if (query == NULL) { - fprintf (stderr, "Out of memory.\n"); - return 1; - } - - /* tagging is not interested in any special sort order */ - notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED); + ret = tag_query (ctx, notmuch, query_string, tag_ops, synchronize_flags); - for (messages = notmuch_query_search_messages (query); - notmuch_messages_valid (messages) && !interrupted; - notmuch_messages_move_to_next (messages)) - { - message = notmuch_messages_get (messages); - - notmuch_message_freeze (message); - - for (i = 0; tag_ops[i].tag; i++) { - if (tag_ops[i].remove) - notmuch_message_remove_tag (message, tag_ops[i].tag); - else - notmuch_message_add_tag (message, tag_ops[i].tag); - } - - notmuch_message_thaw (message); - - if (synchronize_flags) - notmuch_message_tags_to_maildir_flags (message); - - notmuch_message_destroy (message); - } - - notmuch_query_destroy (query); notmuch_database_close (notmuch); - return interrupted; + return ret; } -- 1.7.5.4